main content

analyze sentiment in text -凯发k8网页登录

this example shows how to use the valence aware dictionary and sentiment reasoner (vader) algorithm for sentiment analysis.

the vader algorithm uses a list of annotated words (the sentiment lexicon), where each word has a corresponding sentiment score. the vader algorithm also utilizes word lists that modify the scores of proceeding words in the text:

  • boosters – words or n-grams that boost the sentiment of proceeding tokens. for example, words like "absolutely" and "amazingly".

  • dampeners – words or n-grams that dampen the sentiment of proceeding tokens. for example, words like "hardly" and "somewhat".

  • negations – words that negate the sentiment of proceeding tokens. for example, words like "not" and "isn't".

to evaluate sentiment in text, use the vadersentimentscores function.

load data

extract the text data in the file weekendupdates.xlsx using readtable. the file weekendupdates.xlsx contains status updates containing the hashtags "#weekend" and "#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 🎾🍓🥂 😀"                                  

create an array of tokenized documents from the text data and view the first few documents.

str = tbl.textdata;
documents = tokenizeddocument(str);
documents(1:5)
ans = 
  5x1 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

evaluate sentiment

evaluate the sentiment of the tokenized documents using the vadersentimentlexicon function. scores close to 1 indicate positive sentiment, scores close to -1 indicate negative sentiment, and scores close to 0 indicate neutral sentiment.

compoundscores = vadersentimentscores(documents);

view the scores of the first few documents.

compoundscores(1:5)
ans = 5×1
    0.4738
    0.9348
    0.6705
   -0.5067
    0.7345

visualize the text with positive and negative sentiment in word clouds.

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")

figure contains objects of type wordcloud. the chart of type wordcloud has title positive sentiment. the chart of type wordcloud has title negative sentiment.

see also

| |

related topics

网站地图