Analytics Vidhya
Published in

Analytics Vidhya

Sentiment Analysis using TextBlob

Photo by DARYL WEBER

What is Sentiment Analysis?

The algorithms of sentiment analysis principally specialize in process opinions, attitudes, and even emoticons in an exceedingly corpus of texts. The vary of established sentiments considerably varies from one technique to a different. whereas a customary analyzer defines up to a few basic polar emotions (positive, negative, neutral), the limit of additional advanced models is broader.

TextBlob:

TextBlob is an open-source python library for processing textual data. it offers a simple API to access its methods and perform basic NLP tasks.
TextBlob performs different operations on textual data such as noun phrase extraction, sentiment analysis, classification, translation, etc.

TextBlob is built on top of NLTK and Pattern also it is very easy to use and can process the text in a few lines of code. TextBlob can help you start with the NLP tasks.

Let’s see a very simple example to determine sentiment Analysis in Python using TextBlob.

Before starting lets install TextBlob.

Step#1: Execute pip install TextBlob on Anaconda/command prompt.

Step#2: Once install you can provide the data and analyze the sentiments over it.

Observation: We can see 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.

Let’s take another example to check Polarity and Subjectivity.

We can see this statement is neutral and sentence is objective.

We can work with other functions and analyze the output depends on requirements.

Word Tokenization:

Word tokenization is the process of splitting a large sample of text into words. 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 etc. The Natural Language Tool kit(NLTK) is a library used to achieve this.

We have TextBlob which is based on NLTK so, we are using methods of it directly.

Word count:

We can calculate the word count in given message and work on those according to a requirement.

Noun Phrase Extraction:

Since we extracted the words in the previous section, instead of that we can just extract out the noun phrases from the textblob.

Noun Phrase extraction is particularly important when you want to analyze the “who” in a sentence. Lets see an example below.

Translation and Language Detection:

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

Let’s take an example to understand this.

Are you able to guess the text..? We can do it using textblob.

So, it’s French, We can convert it to the any language which we can understand easily. We will convert this into English for better understanding of meaning.

Yeahh!!!! Have a beautiful day :)

Spelling Correction:

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

We can also check the list of suggested word and its confidence using the spellcheck function. In our sentence “Morning” was wrongly mentioned, Let’s check the suggestions for that and maximum confidence will get the preference in correction.

We can see spellcheck gave us 100% confidence for Morning and it got replaced as well.

Advantages and Disadvantages:

Advantages:
1. TextBlob is built on the shoulders of NLTK and Pattern, therefore it is simple for beginners by providing an intuitive interface to NLTK.
2. TextBlob provides functions such as language translation and detection which is powered by Google Translate.

Disadvantages:
1. TextBlob is little slower in the comparison to spacy but faster than NLTK. (Spacy > TextBlob > NLTK)
2. TextBlob does not provide features like dependency parsing, word vectors etc. which is provided by spacy.

Reference: Stackabuse

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store