A Beginner’s Guide to Sentiment Analysis with Python(textblob)

Mahima Gupta
3 min readNov 9, 2020

Sentiment Analysis:

The process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer’s attitude towards a particular topic, product, etc. is positive, negative, or neutral.

ex-Great-Positive, Not great-Negative

Why sentiment analysis?

  • In Business: In marketing field companies use it to develop their strategies, to understand customers’ feelings towards products or brand, how people respond to their campaigns or product launches and why consumers don’t buy some
    products.
  • In Politics: In political field, it is used to keep track of political view, to detect consistency and inconsistency between statements and actions at the government level. It can be used to predict election results as well!
  • In Public Actions: Sentiment analysis also is used to monitor and analyse social phenomena, for the spotting of potentially dangerous situations and determining the general mood of the blogosphere.

What is TextBlob?

textblob is the python library for processing textual data. build on the top of nltk.it is easier to use and it provides additional functionality, such as rules based sentiment scores.
Install it using following pip command:

pip install textblob

Sentiment Labels:

Each word in a corpus is labeled in terms of polarity and subjectivity (there are more labels as well, but we’re going to ignore them for now). A corpus sentiment is the average of these.

  • Polarity: How positive or negative a word is. -1 is very negative. +1 is very positive.
  • Subjectivity: How subjective, or opinionated a word is. 0 is fact. +1 is very much an opinion.

For more info on how TextBlob coded up its sentiment function.

code:

1. TextBlob(“great”).sentiment

Sentiment(polarity=0.8, subjectivity=0.75)

~TextBlob doesn’t not handle negation

2.TextBlob(“not great”).sentiment

Sentiment(polarity=-0.4, subjectivity=0.75)

Negation multiplies the polarity by -0.5, and doesn’t affect subjectivity.

polarity of “great”=.8,so polarity of “not great”=-0.4 (.8*-0.5=-0.4)

3.TextBlob(“very great”).sentiment

Sentiment(polarity=1.0, subjectivity=0.9750000000000001)

when very take before any word then The polarity gets maxed out at 1.0, but you can see that subjectivity is also modified by “very” to become 0.75⋅1.3=0.9750.75⋅1.3=0.975.

4.TextBlob(“i am great”).sentiment

Sentiment(polarity=0.8, subjectivity=0.75)

##sentiment not change when added stopwords##

How to find Polarity and Subjectivity of document?

textblob finds all of the words and phrase, than it can assign a polarity and subjectivity to all words ,average all of them together and return final polarity, subjectivity.

Conclusion:

You can use .sentiment method to find sentiment of any document and transcript .while this is not sophisticated technique.

Let’s take a look at the sentiment of the various transcripts, both overall and throughout the comedy routine.

notebook-link:

https://github.com/mahima-c/Text-Mining/blob/master/3-Sentiment-Analysis.ipynb

--

--