Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Last active April 13, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruanbekker/9f29a97d61d4b92c09ccca78c5d27857 to your computer and use it in GitHub Desktop.
Save ruanbekker/9f29a97d61d4b92c09ccca78c5d27857 to your computer and use it in GitHub Desktop.
Sentiment Analysis with TextBlob

Sentiment Analysis

Understanding and Extracting Feelings from Data

Sentiment Analysis Flow:

  • Split the words up (Tokenization)
  • Read each time the words are tokenized or shows up (Bag of Words)
  • Lookup the sentiment value for each word from a lexicon that has everything pre recorded to classify the sentiment value

Polarity: How Positive or Negative Subjectivity: Opinion vs Factual

from textblob import TextBlob
import nltk

nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
wiki = TextBlob("how in the hell do you want me to stay calm?!")
wiki.tags
\# [('how', 'WRB'), ('in', 'IN'), ('the', 'DT'), ('hell', 'NN'), ('do', 'VBP'), ('you', 'PRP'), ('want', 'VB'), ('me', 'PRP'), ('to', 'TO'), ('stay', 'VB'), ('calm', 'NNS')]

wiki.words
\# WordList(['i', 'am', 'so', 'unhappy', 'and', 'mad', 'this', 'is', 'totally', 'unacceptable'])

wiki.sentiment.polarity
\# 0.37500000000000006

wiki.sentiment
\# Sentiment(polarity=-0.4083333333333334, subjectivity=0.8833333333333333)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment