Sentiment Analysis — Let TextBlob do all the Work!
Sometimes Sentiment Analysis becomes one of the major tasks while working in the Natural Language Processing(NLP) domain. There are many ways to compute the sentiment of a text and in this article, we will be discussing a technique to decipher the mood and emotions of the general public through a python library called TextBlob.
What is Sentiment Analysis?
The process of identifying users’ opinions expressed in text and categorizing them as positive, negative, or neutral is called sentiment analysis.
Computing sentiment analysis can also be manual work or automated work. Manually extracting sentiment from the text includes reading through every message, comment, or review which can be extremely time-consuming and can also be biased. While automating this process can help speed things up and can provide an unbiased sentiment.
What is TextBlob?
TextBlob is a library built for Natural Language Processing(NLP) and it is built upon Natural Language ToolKit(NLTK) to achieve most of its tasks. Many other tasks can be accomplished using TextBlob i.e. Text Classification and Language Translation etc., but we will be only focusing on sentiment analysis in this article.
How does TextBlob do all the work?
When we calculate the sentiment of a text through TextBlob, it provides us numeric values for polarity and subjectivity. The numeric value for polarity describes how much a text is negative or positive. Similarly, subjectivity describes how much a text is objective or subjective.
But how are these numeric values computed or where do these come from?
The answer to the above question is that TextBlob uses a process defined in _text.py for the sentiment calculation and each word in the lexicon is scored as follows:
These lexicons are referred to in “en-sentiment.xml”. Looking at the lexicons file, we can see that it does not contain any stopwords i.e., the, he, have, etc. because they do not have any sentiment. Other than that, each word is defined in the lexicon file with their part of speech (POS), polarity, subjectivity, intensity, and confidence.
When calculating a sentiment for a single word, TextBlob uses the “averaging” technique that is applied on values of polarity to compute a polarity score for a single word and hence similar operation applies to every single word and we get a combined polarity for longer texts.
TextBlob also handles negations and polarity is multiplied by -0.5.
Now there is a very interesting thing about TextBlob that it handles the modifiers also known as intensifiers which intensifies the meaning of text according to its pattern. Whenever a modifier word is used, TextBlob will ignore polarity and subjectivity and just use intensity to compute the sentiment of the text.
Another interesting fact about TextBlob is that when a negation combines with modifiers, the inverse intensity of the modifier enters for subjectivity and polarity.
Heading over to the code and implementing it ourselves
For using TextBlob, we will be installing it on our machines through the following command:
pip install -U textblob
And to install the corpora:
python -m textblob.download_corpora
The setup is completed. Now, we’ll import textblob into our code and see how does it actually work
from textblob import TextBlob
In this example, we will be computing sentiment for a customer review
review = 'WordPress.com works really well with Google for a great SEO ranking.'blob = TextBlob(review)for sentence in blob.sentences:
print(sentence.sentiment)
Executing the above code will provide you a sentiment score as follows
Sentiment(polarity=0.5, subjectivity=0.475)
See? TextBlob does all the work and provides you a sentiment of a text in an easy and simple way.
Now, we’ll see some interesting facts about TextBlob
As discussed above, when a modifier word is used, TextBlob ignores the subjectivity and polarity and will just use the intensity of the word
TextBlob("very good").sentimentSentiment(polarity=0.9099999999999999, subjectivity=0.7800000000000001)
Similarly, negation multiples the polarity by -0.5
TextBlob("not good").sentimentSentiment(polarity=-0.35, subjectivity=0.6000000000000001)
Similarly, combining the above two cases i.e. negation with modifiers, the inverse intensity of the modifier is used for polarity and subjectivity
TextBlob(“not very good”).sentimentSentiment(polarity=-0.26923076923076916, subjectivity=0.46153846153846156)
I hope this post has helped you. Feel free to put a comment below if you have any suggestions or questions.