Natural Language Processing Topics: Sentiment Analysis with NLTK
First let us import nltk and download the VADER lexicon.
import nltk nltk.download('vader_lexicon')
Let us import sentiment intensity analyzer from VADER. And, create an instance of the sentiment analyzer.from nltk.sentiment.vader import SentimentIntensityAnalyzer sid = SentimentIntensityAnalyzer()
What we can do is, use this sentiment analyzer and find the intensity of any string. The intensity values are scores on three metrics, pos, neg and neu . For example:sid.polarity_scores("The movie I watched was okay. I would not watch it second time.") >>> {'neg': 0.0, 'neu': 0.84, 'pos': 0.16, 'compound': 0.2263}
The compound value some combination of all the three values, positive, negative and neutral. As the sentence that I provided is pretty neutral, the scores also indicates that it is pretty neutral.Using a different sentence, we can see it is slightly more postive, but not that much.
sid.polarity_scores("The movie I watched was perfect to watch. It had the right kind of humor I was looking for.") >>> {'neg': 0.0, 'neu': 0.731, 'pos': 0.269, 'compound': 0.6712}
Now using a very negative sentence..
sid.polarity_scores("The food was very bland. It would not hurt the chef to put in more spices, and not sure why the chef decided to make the food so ghastly. Not eating here again !!!") >>> {'neg': 0.052, 'neu': 0.851, 'pos': 0.097, 'compound': 0.3999}