Sentiment analysis

Analyzing Social Media Data in Python

Alex Hanna

Computational Social Scientist

Understanding sentiment analysis

  • Method
    • Counting positive/negative words in the document
    • Assessing positivity/negativity of the whole document
  • Uses
    • Analyzing reactions to a company, product, politician, or policy
Analyzing Social Media Data in Python

Sentiment analysis tools

  • VADER SentimentIntensityAnalyzer()
    • Part of Natural Language Toolkit (nltk)
    • Good for short texts like tweets
    • Measures sentiment of particular words (e.g. angry, happy)
    • Also considers sentiment of emoji (😀) and capitalization (Nice vs NICE)
Analyzing Social Media Data in Python

Implementing sentiment analysis

from nltk.sentiment.vader import SentimentIntensityAnalyzer

sid = SentimentIntensityAnalyzer()
sentiment_scores = tweets['text'].apply(sid.polarity_scores)
Analyzing Social Media Data in Python

Interpreting sentiment scores

  • Reading tweets as part of the process
    • Does it have face validity? (i.e. does this match my idea of what it means to be positive or negative?)
Analyzing Social Media Data in Python

Interpreting sentiment scores

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}
Analyzing Social Media Data in Python

Generating sentiment averages

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()
Analyzing Social Media Data in Python

Plotting sentiment scores

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 vs. Google sentiment

Analyzing Social Media Data in Python

Let's practice!

Analyzing Social Media Data in Python

Preparing Video For Download...