Closed4
シンプルなテキスト処理ライブラリ「TextBlob」を試す
GitHubレポジトリ
TextBlob: 簡単なテキスト処理
TextBlob はテキストデータを処理するための Python ライブラリです。
品詞タグ付け、名詞句抽出、感情分析、分類など、一般的な自然言語処理 (NLP) タスクに簡単に取り組めるシンプルな API を提供します。(snip)
TextBlob は NLTK と Pattern という巨大なライブラリの上に構築されており、両者ともうまく連携します。
機能一覧
- 名詞句抽出
- 品詞タグ付け
- 感情分析
- 分類 (ナイーブベイズ、決定木)
- トークン化 (テキストを単語や文に分割)
- 単語およびフレーズの頻度集計
- 構文解析
- n-グラム
- 単語の屈折変化 (複数形化および単数形化) と正規化
- スペルチェック
- 拡張機能を通じた新たなモデルや言語の追加
- WordNet との統合
ドキュメント
軽量な感情分析を探してて、使えそうな雰囲気を感じているがどうだろうか?あと、日本語使えるんだろうか?と思ったんだけど、どうも使えるっぽい?
インストール
Colaboratoryで。
パッケージインストール。
!pip install -U textblob
!pip freeze | grep -i textblob
出力
textblob==0.19.0
NLTKのコーパスをダウンロードする・・・日本語対応してたっけ???
!python -m textblob.download_corpora
Quickstart
from textblob import TextBlob
wiki = TextBlob("Pythonは、高レベルかつ汎用性の高いプログラミング言語です。")
wiki.tags
出力
[('Pythonは、高レベルかつ汎用性の高いプログラミング言語です。', 'NN')]
wiki.noun_phrases
出力
WordList(['pythonは、高レベルかつ汎用性の高いプログラミング言語です。'])
wiki.sentiment
出力
Sentiment(polarity=0.0, subjectivity=0.0)
うーん、これは・・・
Qiitaの記事だとtranslate
というメソッドがあるっぽい。
wiki.translate(to='en')
出力
AttributeError: 'TextBlob' object has no attribute 'translate'
おっとー・・・ChangeLogを見ると、どうやら翻訳は削除されている。
0.18.0 (2024-02-15)
(snip)
Removals:
TextBlob.translate()
andTextBlob.detect_language
, andtextblob.translate
are removed. Use the official Google Translate API instead (:issue:215
).
0.16.0 (2020-04-26)
Deprecations:
TextBlob.translate()
andTextBlob.detect_language
are deprecated. Use the official Google Translate API instead (:issue:215
).
うむ、日本語には対応していないということで。。。
英語なら問題なさそう
from textblob import TextBlob
wiki = TextBlob("Hello! It's a beautiful day today. I had no luck at all with my horse racing predictions yesterday, and I lost big... I'll do my best next week!")
wiki.tags
出力
[('Hello', 'NN'),
('It', 'PRP'),
("'s", 'VBZ'),
('a', 'DT'),
('beautiful', 'JJ'),
('day', 'NN'),
('today', 'NN'),
('I', 'PRP'),
('had', 'VBD'),
('no', 'DT'),
('luck', 'NN'),
('at', 'IN'),
('all', 'DT'),
('with', 'IN'),
('my', 'PRP$'),
('horse', 'NN'),
('racing', 'VBG'),
('predictions', 'NNS'),
('yesterday', 'NN'),
('and', 'CC'),
('I', 'PRP'),
('lost', 'VBD'),
('big', 'JJ'),
('I', 'PRP'),
("'ll", 'MD'),
('do', 'VB'),
('my', 'PRP$'),
('best', 'JJS'),
('next', 'JJ'),
('week', 'NN')]
wiki.noun_phrases
出力
WordList(['hello', 'beautiful day', 'big ...'])
display(wiki.sentiment)
display(wiki.sentiment.polarity)
display(wiki.sentiment.subjectivity)
出力
Sentiment(polarity=0.4625, subjectivity=0.35000000000000003)
0.4625
0.35000000000000003
このスクラップは2025/02/15にクローズされました