Three Python Libraries for Sentiment Analysis

Edna Figueira Fernandes
2 min readSep 27, 2020

Sentiment Analysis is a sub-field of Natural Language processing that focuses on interpreting and classifying emotions using text data. This week, I worked on a small project with the goal of classifying products’ reviews into positive, negative, and neutral. For this purpose, I used three python libraries: TextBlob, AFINN, and Vader, and compared their results to come out with a final sentiment. On this blog post, I am going to elaborate a little on these libraries and I will show a small code on how to implement them.

TextBlob

TextBlob is a python library for processing text data. It has an API for common NLPs processing tasks including sentiment analysis. The sentiment property of TextBlob returns the polarity and subjectivity scores of the text. The polarity score is a value in the range of -1 to 1, where -1 represents a negative sentiment and 1 represents a positive sentiment. A value of zero represents a neutral sentiment. Subjectivity indicates how opinionated the text is. It returns a value in the range of 0 to 1, where zero represents an objective text and 1 represents a subjective text. In order to use the library for text classification in a pandas DataFrame, import the library and create a function to calculate the text’s polarity score. Finally, create another function to translate these scores into positive, negative, or neutral. The code below shows an example of how to implement it.

from textblob import TextBlobdef textblob_polarity(text):
return TextBlob(text).sentiment.polarity
def getAnalysis(score):
if score < 0:
return 'Negative'
elif score == 0:
return 'Neutral'
else:
return 'Positive'
df['polarity'] = df[text].apply(textblob_polarity)
df['classification'] = df['polarity'].apply(getAnalysis)

AFINN Sentiment Lexicon

The AFINN lexicon “is a list of English terms manually rated for valence”. The values range between -5 and 5 in which -5 represents a negative sentiment and 5 represents a positive sentiment. Below, I am demonstrating how to implement AFINN for text classification into a pandas DataFrame.

from afinn import Afinndef afinn_polarity(text):
return Afinn().score(text)
def getAnalysis(score):
if score < 0:
return 'Negative'
elif score == 0:
return 'Neutral'
else:
return 'Positive'
df['polarity'] = df[text].apply(afinn_polarity)
df['classification'] = df['polarity'].apply(getAnalysis)

Vader Sentiment Lexicon

Vader (Valence Aware Dictionary and Sentiment Reasoner) is another lexicon used for sentiment analysis. It uses a combination of words labeled according to their semantic orientation. Besides classifying the texts into positive and negative, Vader also gives the compound score, which tells how positive or negative the text is. Below is a small piece of code demonstrating how to use the library on a pandas DataFrame.

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzerdef varder_polarity(text):
return SentimentIntensityAnalyzer().polarity_scores(text)
def varder_analysis(score):
if score['compound'] >= 0.05:
return 'Positive'
elif score['compound'] <= -0.5:
return 'Negative'
else:
return 'Neutral'
df['polarity'] = df[text].apply(varder_analysis)
df['classification'] = df['polarity'].apply(varder_analysis)

I hope you find this helpful!

References

--

--