LoginSignup
24
26

More than 5 years have passed since last update.

【python】テキスト処理に使えるTextBlobを使う

Last updated at Posted at 2018-11-30

はじめに

テキスト処理に使えるライブラリであるTextBlob

  • 翻訳
  • 名詞抽出
  • 感情分析
  • タグ付け

など、テキスト処理において幅広い用途で使用される。
面白そうなので遊ぶ。

翻訳

英語->日本語

translate.py
from textblob import TextBlob

analysis = TextBlob("TextBlob sure looks like it has some interesting features")
print(analysis)
print(analysis.translate(to='ja'))

結果

$ python translate.py 
TextBlob sure looks like it has some interesting features
TextBlobには興味深い機能があるようです

おぉ、きちんと翻訳されとる。。。
では逆に日本語対応しているのか??

日本語->英語

translate.py
from textblob import TextBlob

analysis = TextBlob("TextBlobには興味深い機能があるようです")
print(analysis)
print(analysis.translate(to='en'))

結果

$ python translate.py 
TextBlobには興味深い機能があるようです
TextBlob seems to have an interesting function

できてるっぽいぞ。。。

そのほか少し。

その他

test.py
# 言語抽出
analysis = TextBlob("TextBlob sure looks like it has some interesting features")
print(analysis.detect_language())
# => en

# 単語のリスト
print(analysis.words)
# => ['TextBlob', 'sure', 'looks', 'like', 'it', 'has', 'some', 'interesting', 'features']

# 単語の出現回数
print(analysis.words.count("looks"))
# => 1

# 単語のタグ付け
print(analysis.tags)
# => [('TextBlob', 'NNP'), ('sure', 'JJ'), ('looks', 'VBZ'), ('like', 'IN'), ('it', 'PRP'), ('has', 'VBZ'), ('some', 'DT'), ('interesting', 'JJ'), ('features', 'NNS')]

# 感情分析(極性分析)
print(analysis.sentiment)
# => Sentiment(polarity=0.5, subjectivity=0.6944444444444444)

結果

自然言語は表現が多様なのでいろいろ遊べそうだ。
翻訳と感情分類を深堀りしてみるのが面白そうだと感じる。

24
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
26