Closed4

シンプルなテキスト処理ライブラリ「TextBlob」を試す

kun432kun432

GitHubレポジトリ

https://github.com/sloria/TextBlob

TextBlob: 簡単なテキスト処理

TextBlob はテキストデータを処理するための Python ライブラリです。
品詞タグ付け、名詞句抽出、感情分析、分類など、一般的な自然言語処理 (NLP) タスクに簡単に取り組めるシンプルな API を提供します。

(snip)

TextBlob は NLTKPattern という巨大なライブラリの上に構築されており、両者ともうまく連携します。

機能一覧

  • 名詞句抽出
  • 品詞タグ付け
  • 感情分析
  • 分類 (ナイーブベイズ、決定木)
  • トークン化 (テキストを単語や文に分割)
  • 単語およびフレーズの頻度集計
  • 構文解析
  • n-グラム
  • 単語の屈折変化 (複数形化および単数形化) と正規化
  • スペルチェック
  • 拡張機能を通じた新たなモデルや言語の追加
  • WordNet との統合

ドキュメント

https://textblob.readthedocs.io/en/dev/

軽量な感情分析を探してて、使えそうな雰囲気を感じているがどうだろうか?あと、日本語使えるんだろうか?と思ったんだけど、どうも使えるっぽい?

https://qiita.com/yasutaka_ono/items/6a565c333995d43918ff

kun432kun432

Quickstart

https://textblob.readthedocs.io/en/dev/quickstart.html

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を見ると、どうやら翻訳は削除されている。

https://github.com/sloria/TextBlob/blob/dev/CHANGELOG.rst#0160-2020-04-26

0.18.0 (2024-02-15)

(snip)

Removals:

  • TextBlob.translate() and TextBlob.detect_language, and textblob.translate are removed. Use the official Google Translate API instead (:issue:215).

0.16.0 (2020-04-26)

Deprecations:

  • TextBlob.translate() and TextBlob.detect_language are deprecated. Use the official Google Translate API instead (:issue:215).

うむ、日本語には対応していないということで。。。

kun432kun432

英語なら問題なさそう

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にクローズされました