テキスト内のセンチメントの分析
この例では、センチメント分析のために Valence Aware Dictionary and sEntiment Reasoner (VADER) アルゴリズムを使用する方法を示します。
VADER アルゴリズムは、アノテーション付きの単語のリスト (センチメント辞書) を使用します。各単語は、対応するセンチメント スコアをもちます。VADER アルゴリズムでは、テキスト内の後続の単語のスコアを変更する単語リストも利用します。
増幅因子 – 後続のトークンのセンチメントを高める単語または n-gram。たとえば、"absolutely" (絶対に) や "amazingly" (驚くほど) などの単語。
減衰因子 – 後続のトークンのセンチメントを弱める単語または n-gram。たとえば、"hardly" (ほとんど) や "somewhat" (やや) などの単語。
否定 – 後続のトークンのセンチメントを否定する単語。たとえば、"not" や "isn't" などの単語です。
テキストのセンチメントを評価するには、関数 vaderSentimentScores
を使用します。
データの読み込み
readtable
を使用して、ファイル weekendUpdates.xlsx
内のテキスト データを抽出します。ファイル weekendUpdates.xlsx
には、ハッシュタグ "#weekend"
と "#vacation"
を含むステータス更新が含まれています。
filename = "weekendUpdates.xlsx"; tbl = readtable(filename,'TextType','string'); head(tbl)
ID TextData __ _________________________________________________________________________________ 1 "Happy anniversary! ❤ Next stop: Paris! ✈ #vacation" 2 "Haha, BBQ on the beach, engage smug mode! 😍 😎 ❤ 🎉 #vacation" 3 "getting ready for Saturday night 🍕 #yum #weekend 😎" 4 "Say it with me - I NEED A #VACATION!!! ☹" 5 "😎 Chilling 😎 at home for the first time in ages…This is the life! 👍 #weekend" 6 "My last #weekend before the exam 😢 👎." 7 "can’t believe my #vacation is over 😢 so unfair" 8 "Can’t wait for tennis this #weekend 🎾🍓🥂 😀"
テキスト データから、トークン化された文書の配列を作成し、最初のいくつかの文書を表示します。
str = tbl.TextData; documents = tokenizedDocument(str); documents(1:5)
ans = 5×1 tokenizedDocument: 11 tokens: Happy anniversary ! ❤ Next stop : Paris ! ✈ #vacation 16 tokens: Haha , BBQ on the beach , engage smug mode ! 😍 😎 ❤ 🎉 #vacation 9 tokens: getting ready for Saturday night 🍕 #yum #weekend 😎 13 tokens: Say it with me - I NEED A #VACATION ! ! ! ☹ 19 tokens: 😎 Chilling 😎 at home for the first time in ages … This is the life ! 👍 #weekend
センチメントの評価
関数 vaderSentimentLexicon
を使用して、トークン化された文書のセンチメントを評価します。スコアが 1 に近いほどポジティブ センチメントを示し、スコアが -1 に近いほどネガティブ センチメントを示し、スコアが 0 に近いほどニュートラル センチメントを示します。
compoundScores = vaderSentimentScores(documents);
最初のいくつかの文書のスコアを表示します。
compoundScores(1:5)
ans = 5×1
0.4738
0.9348
0.6705
-0.5067
0.7345
ワード クラウドで、ポジティブ センチメントを含むテキストとネガティブ センチメントを含むテキストを可視化します。
idx = compoundScores > 0; strPositive = str(idx); strNegative = str(~idx); figure subplot(1,2,1) wordcloud(strPositive); title("Positive Sentiment") subplot(1,2,2) wordcloud(strNegative); title("Negative Sentiment")
参考
vaderSentimentScores
| ratioSentimentScores
| tokenizedDocument