感情分析

Python でのソーシャルメディアデータ分析

Alex Hanna

Computational Social Scientist

感情分析とは

  • 手法
    • 文書内のポジティブ/ネガティブな単語を数える
    • 文書全体のポジティブ度/ネガティブ度を評価する
  • 用途
    • 企業、製品、政治家、政策への反応を分析する
Python でのソーシャルメディアデータ分析

感情分析のツール

  • VADER SentimentIntensityAnalyzer()
    • Natural Language Toolkit(nltk)の一部
    • ツイートのような短いテキストに適している
    • 特定の単語(例:angry、happy)の感情を測定する
    • 絵文字(😀)や大文字表記(Nice と NICE の違い)の感情も考慮する
Python でのソーシャルメディアデータ分析

感情分析を実装する

from nltk.sentiment.vader import SentimentIntensityAnalyzer

sid = SentimentIntensityAnalyzer()
sentiment_scores = tweets['text'].apply(sid.polarity_scores)
Python でのソーシャルメディアデータ分析

感情スコアを解釈する

  • プロセスの一環としてツイートを読む
    • 妥当性はあるか(自分のポジティブ・ネガティブの感覚と一致しているか)
Python でのソーシャルメディアデータ分析

感情スコアを解釈する

tweet1 = 'RT @jeffrey_heer: Thanks for inviting me, and thanks 
for the lovely visualization of the talk! ...'
print(sid.polarity_scores(tweet1))
{'neg': 0.0, 'neu': 0.496, 'pos': 0.504, 'compound': 0.9041}
tweet2 = 'i am having problems with google play music'
print(sid.polarity_scores(tweet2)
{'neg': 0.267, 'neu': 0.495, 'pos': 0.238, 'compound': -0.0772}
Python でのソーシャルメディアデータ分析

感情スコアの平均を算出する

sentiment = sentiment_scores.apply(lambda x: x['compound'])

sentiment_fb = sentiment[check_word_in_tweet('facebook', tweets)] .resample('1 min').mean() sentiment_gg = sentiment[check_word_in_tweet('google', tweets)] .resample('1 min').mean()
Python でのソーシャルメディアデータ分析

感情スコアをプロットする

plt.plot(
    sentiment_fb.index.minute, 
    sentiment_fb, color = 'blue'
    )
plt.plot(
    sentiment_g.index.minute, 
    sentiment_gg, color = 'green'
    )   
plt.xlabel('Minute')
plt.ylabel('Sentiment')
plt.title('Sentiment of companies')
plt.legend(('Facebook', 'Google'))
plt.show()

Facebook と Google の感情スコア比較

Python でのソーシャルメディアデータ分析

練習しましょう!

Python でのソーシャルメディアデータ分析

Preparing Video For Download...