0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

日本語の感情分析をPythonとTransformersでやってみよう

Posted at

SNSの投稿やレビューを見て「これってポジティブ?ネガティブ?」と気になったこと、ありませんか?
今回は、無料で公開されているBERTベースの日本語感情分析モデルを使って、Pythonでサクッと感情分析を行う方法をご紹介します。


Step 1:ライブラリのインストール

最初に必要なライブラリをインストールします。以下のセルを実行してください(Google Colab推奨です)。

# 初回のみ実行(感情分析モデルと日本語処理に必要なライブラリ)
!pip install transformers fugashi ipadic unidic-lite

Step 2:日本語感情分析モデルを読み込む

transformers ライブラリの pipeline を使って、感情分類モデルを簡単に利用できます。
ここでは、llm-book/bert-base-japanese-v3-marc_ja というモデルを使います。

from transformers import pipeline

# 日本語の感情分析パイプラインを構築
classifier = pipeline(
    "text-classification",
    model="llm-book/bert-base-japanese-v3-marc_ja"
)

Step 3:SNS投稿の感情を判定してみよう!

実際に任意のSNS投稿を入れて、ポジティブ・ニュートラル・ネガティブのどれかを判定してみましょう。

# 任意の日本語テキスト
text = "この製品は本当に素晴らしいと思いました。"

# 感情分析を実行
result = classifier(text)[0]

# 結果の表示
print("入力文:", text)
print("感情ラベル:", result["label"])  # positive / neutral / negative
print("確信度:", round(result["score"], 3))

出力結果(例)

入力文: この製品は本当に素晴らしいと思いました。
感情ラベル: positive
確信度: 0.998
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?