Twitterの感情分析

Rで学ぶソーシャルメディアデータ分析

Vivek Vijayaraghavan

Data Science Coach

レッスン概要

  • 感情分析とは
  • ツイートで感情分析を実施
  • 結果を解釈し、感情や意見を把握
Rで学ぶソーシャルメディアデータ分析

感情分析

  • 製品・ブランドの認知を把握
  • ポジ・ネガ・中立の意見を抽出・定量化
  • テキストからtrust、joy、angerなどの感情

感情分析の感情

Rで学ぶソーシャルメディアデータ分析

感情分析の重要性

  • 顧客の認知は購買に影響
  • 顧客が何を感じるかの把握に有用
  • 顧客の声を能動的に収集し直接関与
Rで学ぶソーシャルメディアデータ分析

感情分析の仕組み

  • 事前定義の感情辞書でスコア化
  • 語の意味・意図に基づき学習・採点
  • 各語をポジ/ネガ語との近さで評価
  • 特定感情を表す語にも同様に拡張
Rで学ぶソーシャルメディアデータ分析

感情分析の手順

感情分析のステップ1

Rで学ぶソーシャルメディアデータ分析

感情分析の手順

感情分析のステップ2

Rで学ぶソーシャルメディアデータ分析

感情分析の手順

感情分析のステップ3

Rで学ぶソーシャルメディアデータ分析

感情分析の手順

感情分析のステップ4

Rで学ぶソーシャルメディアデータ分析

感情分析用のツイート抽出

# Extract tweets on galaxy fold
twts_galxy  <-  search_tweets("galaxy fold", n = 5000, 
                                lang = "en", include_rts = FALSE)
Rで学ぶソーシャルメディアデータ分析

感情分析の実行

# Perform sentiment analysis for tweets on galaxy fold
library(syuzhet)
sa.value <- get_nrc_sentiment(twts_galxy$text)
Rで学ぶソーシャルメディアデータ分析

感情スコアの確認

# View the sentiment scores
sa.value[1:5,1:7]
anger    anticipation    disgust    fear    joy    sadness    surprise
<dbl>       <dbl>         <dbl>     <dbl>  <dbl>    <dbl>       <dbl> 
0             0             0         0      0        0           0
1             0             0         0      0        0           0
1             1             0         2      1        1           1
0             0             0         1      0        0           0
0             0             0         0      0        0           0
Rで学ぶソーシャルメディアデータ分析

感情スコアの合計

# Calculate sum of sentiment scores
score <- colSums(sa.value[,])
Rで学ぶソーシャルメディアデータ分析

感情スコアのデータフレーム

# Convert to data frame
score_df <- data.frame(score)
# View the data frame
score_df
               score
               <dbl>
anger           211            
anticipation    825            
disgust         214            
fear            253            
joy             412            
sadness         197            
surprise        315            
trust           641            
negative        487            
positive       1351
Rで学ぶソーシャルメディアデータ分析

感情スコアのデータフレーム

# Convert row names into 'sentiment' column
# Combine with sentiment scores
sa.score <- cbind(sentiment = row.names(score_df), 
                  score_df, row.names=NULL)
Rで学ぶソーシャルメディアデータ分析

感情スコアのデータフレーム

# View data frame with sentiment scores
print(sa.score)
sentiment      score
<fct>         <dbl>
anger           211            
anticipation    825            
disgust         214            
fear            253            
joy             412            
sadness         197            
surprise        315            
trust           641            
negative        487            
positive        1351    
Rで学ぶソーシャルメディアデータ分析

感情の可視化

  • ggplot()で感情を可視化
# Plot the sentiment scores
ggplot(data = sa.score2, aes(x = sentiment, y = score, 
       fill = sentiment)) +
       geom_bar(stat = "identity") +
       theme(axis.text.x = element_text(angle = 45, hjust = 1))
Rで学ぶソーシャルメディアデータ分析

感情を可視化

感情を可視化

Rで学ぶソーシャルメディアデータ分析

Passons à la pratique !

Rで学ぶソーシャルメディアデータ分析

Preparing Video For Download...