TextBlob for Sentimental Analysis

Saaisri
featurepreneur
Published in
3 min readJul 23, 2022

What is Sentimental Analysis?

Sentiment analysis lets you analyze the sentiment behind a given piece of text. It is a technique through which you can analyze a piece of text to determine the sentiment behind it. It combines machine learning and natural language processing (NLP) to achieve this.

Using basic Sentiment analysis, a program can understand whether the sentiment behind a piece of text is positive, negative, or neutral.

It is a powerful technique in Artificial intelligence that has important business applications.

  • Social media sentiment analysis
  • Movie reviews analysis
  • News sentiment analysis

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.

First, let’s install Textblob by simply going to the terminal and running the code below.

pip install textblob

To import to the text editor:

from textblob
import TextBlob

Once install you can provide the data and analyze the sentiments over it.

The output is categorized between two — Polarity and Subjectivity.

Polarity is a float value within the range [-1.0 to 1.0] where 0 indicates neutral, +1 indicates a very positive sentiment and -1 represents a very negative sentiment.

Subjectivity is a float value within the range [0.0 to 1.0] where 0.0 is very objective and 1.0 is very subjective. Subjective sentence expresses some personal feelings, views, beliefs, opinions, allegations, desires, beliefs, suspicions, and speculations where as Objective sentences are factual.

>>> testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
testimonial.sentiment
Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)>>> testimonial.sentiment.polarity
0.39166666666666666

Word Tokenization:

You can break TextBlobs into words or sentences. This is a requirement in natural language processing tasks where each word needs to be captured and subjected to further analysis like classifying and counting them for a particular sentiment.

>>> zen = TextBlob("Beautiful is better than ugly. "
"Explicit is better than implicit. "
"Simple is better than complex.")
>>> zen.words
WordList(['Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is', 'better', 'than', 'complex'])
>>> zen.sentences
[Sentence("Beautiful is better than ugly."), Sentence("Explicit is better than implicit."), Sentence("Simple is better than complex.")]

Translation and Language Detection:

Using TextBlob we can translate any language to other language to understand the exact meaning of it.

french = TextBlob("belle journée")
french.detect.language()
fr

Spelling Correction:

Spelling correction is a awesome feature which TextBlob provides, we can be accessed using the correct function as shown below.

>>> b = TextBlob("I havv goood speling!")
>>> print(b.correct())
I have good spelling!

--

--