Sentiment analysis is an automated process of extraction and analyzing data considering customers’ expectations, preferences and aversions from natural language texts – reviews, blogs or social media records. Twitter sentiment analysis gives companies a better understanding of what people think about their brand, service, product features or any particular topic.

We have already discussed how to create a crawler and scrape Twitter data previously, now it’s time to talk about utilization and drawing conclusions from the data.

During Twitter sentiment analysis (opinion mining) all gathered tweets (opinions) are being classifyed as Positive, Negative or Neutral. It is called Polarity Detection. The rating scale can be supplemented with Very negative and Very positive or even transformed into numerical estimatiton from 1 to 5, from 1 to 10 etc., where the highest number corresponds to the highest positive emotion.

For example, Tesla Company is promoting Solar Roofs for houses now, and here is one of the comments relating to this topic on Twitter:

We can manually read, understand and interpret the mood of this text as Positive or we can use the tools of automated text analysis to make our computer do this job for us. Discarding special cases with emotionally complex or sarcastic tweets, machine can classify statements with a high degree of accuracy. Let’s make a test.

This result is achieved by the a method called Natural Language Processing (NLP), based on two main algorithms:

In the modern text processing software both algorithms are combined to achieve the higher accuracy.

Twitter sentiment analysis Python code

Examples of Python code for sentiment analysis Twitter are based on packages for web scraping, natural language processing, data-science and data visualization.

The minimum required functionality is covered by tweepy and textblob. You will need to receive API keys and access tokens before using tweepy, the most popular Twitter API wrapper for Python. Then get tweets with tweepy and clean text from unwanted characters with regular expressions.

Textbloob already has built-in methods for sentence polarity detection:

from textblob import TextBlob

blob = TextBlob(tweet_text_here)


This example will print out text polarity value. Based on the value we get the following conclusions about a tweet:

Full code with only tweepy and textblob supplemented with detailed explanations can be found here.

Extended functionality requires a bunch of Python packages:

In addition to polarity it would be nice to calculate subjectivity for every tweet. Subjectivity describes how strong the personal opinion, emotion or judgment is represented in a text. Subjective statements are significant for our research, because we rather want to deal with specific private opinions than some abstract tweets. Textblob has sentiment.subjectivity method that returns a float number between 0 and 1 representing a value of subjectivity.

An interesting example code with extended functionality and visualization for Twitter sentiment analysis Python can be found at this link. If you are searching for an expert-level approach to data analytics, read articles on Kaggle, e.g., this one by Paolo Ripamonti.

Tools for sentiment analysis of twitter data

Ready to use tools for sentiment analysis of Twitter data solve the whole range of tasks from data mining to a qualitative presentation of the results. Let’s take a quick look at some of them.

An informed choice of Twitter sentiment analysis tool is possible if you understand its methods and algorithms and can clearly articulate its purposes for your business.

3 Responses

  1. I have been checking out many of your stories and i can state pretty good stuff. I will make sure to bookmark your website. Olympie Berke Christy

Leave a Reply

Your email address will not be published. Required fields are marked *