In the machine learning/deep learning space, natural language processing (NLP) is one aspect used to solve a lot of problems, and sentiment analysis is a branch of NLP. In this article, we are going to take a deep dive into sentiment analysis using a tool called Textblob.
In simple terms, sentiment analysis is the act of using NLP to help us understand the opinion of the public in a particular context in natural language.
Popular applications of sentiment analysis
Social media sentiment analysis
Movie reviews analysis
News sentiment analysis
The evolution in NLP has been remarkable because we now have tools and packages that make solving problems with NLP very easy. One of those tools is Textblob.
Textblob is a Python NLP library that uses a natural language toolkit (NLTK). It uses NLTK because it is simple, easy to deploy, will use up fewer resources, gives dependency parsing, and can be used even for small applications.
Textblob can be used for complex analysis and working with textual data. When a sentence is passed into Textblob it gives two outputs, which are polarity and subjectivity. Polarity is the output that lies between [-1,1], where -1 refers to negative sentiment and +1 refers to positive sentiment. Subjectivity is the output that lies within [0,1] and refers to personal opinions and judgments.
There are different use cases for Textblob like noun phrase extraction, part of speech tagging, tokenization, word inflection, lemmatization, WordNet integration, and more. Textblob is mostly used to carry out the task of sentiment analysis using its pre-trained inbuilt classifier and can carry out several sentiment analyses. Now, let’s try it out.
First, let’s install Textblob by simply going to the terminal and running the code below.
1
pip install textblob
After that let’s go to our text editor and import Textblob
1 2
from textblob import TextBlob
Now to try it by carrying out a simple operation that will give us both the polarity and subjectivity sentiment of a sentence.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
text = TextBlob(‘she is a good cook’)
print(text.sentiment)
Now
let’ s write another chunk of code where we will define a
function that calculates the subjectivity and the polarity and gives an output based on the threshold that we’ ll choose.
``
`python
def sentiment_analysis(tweet):
def getSubjectivity(text):
return TextBlob(text).sentiment.subjectivity
#Create a function to get the polarity
def getPolarity(text):
return TextBlob(text).sentiment.polarity
#Create two new columns ‘Subjectivity’ & ‘Polarity’
tweet[‘TextBlob_Subjectivity’] = tweet[‘tweet’].apply(getSubjectivity)
tweet [‘TextBlob_Polarity’] = tweet[‘tweet’].apply(getPolarity)
def getAnalysis(score):
if score < 0:
return ‘Negative’
elif score == 0:
return ‘Neutral’
else:
return ‘Positive’
tweet [‘TextBlob_Analysis’] = tweet [‘TextBlob_Polarity’].apply(getAnalysis )
return tweet
Aside from sentiment analysis, there are other awesome things you can do with Textblob. Let’s use Textblob to create a summary of some random text.
1
2
3
4
5
6
7
8
9
10
11
12
13
import random
blob = TextBlob(‘I am a graduate of computer science from a good university and I am interested in machine learning and crypto, I am also the first daughter and I have five sisters and we are from London’)
nouns = list()
for word, tag in blob.tags:
if tag == 'NN':
nouns.append(word.lemmatize())
print("This text is about...")
for item in random.sample(nouns, 5):
word = Word(item)
print(word.pluralize())
Textblob can also be used for correction
1 2
blob = TextBlob('I have a car and big huse') blob.correct()
There are several other ways you can use Textblob for both sentiment analysis and for various reasons. It can easily get you started with NLP. Try out its various use cases by going through their documentation here: https://textblob.readthedocs.io/en/dev/.