Deepak Balmiki
4 min readJan 27, 2021

Twitter Sentiment Analysis with Tweepy and TextBlob using Jupyter Notebook

Sentiment analysis is a process that analyzes text data by classifying sentiments in three different groups positive, negative, and neutral. There are several use cases available on sentiment analysis. One of the most compelling use case of sentiment analysis today is brand awareness, and Twitter is home to lots of consumer data that can provide brand awareness insights.
If you can understand what people are saying about you in a natural context, you can work towards addressing key problems and improving your business processes. So how exactly can you get that up and running?
Here common use cases based on Sentiment Analysis
· Improving Your Customer Support Tracking
· Your Employees’ Feedback
· Providing Better Product Analytics
· Monitoring Market Research
· Keeping an Eye on Your Competition
In the following journal we will try to do sentiment analysis of Tweets with python using text blob and tweepy in Jupyter notebook, passing keyword as a parameter and get the sentiment on it based on the most recent 100 tweets that comprise the keyword.
We will be using two famous libraries Tweepy and TextBlob accompanied by other supporting libraries.

Tweepy: The API class provides access to the entire twitter RESTful API methods. Each method can accept various parameters and return responses. For more information about these methods please refer to API Reference.

TextBlob: TextBlob is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more. TextBlob full documentation

Sentiment Analysis: The sentiment property returns a named tuple of the form Sentiment (polarity, subjectivity). The polarity score is a float within the range [-1.0, 1.0]. The subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.

Load Libraries

Twitter Developer account
Create a Twitter account to access the twitter API for required tweets. Once your Twitter developer accounts created you will get following details .
1.consumer_key
2.consumer_secret
3.access_token
4.access_token_secret

Authentication
API required authentication for accessing Tweets from Twitter. Declared required authentication data under variables for further usage

Extracting Tweets Using Retrieve Tweets with Keyword
Providing keyword search and respective date range to pull the records.

Data Munging
Data Munging is the method by which data of any type is processed and prepared for analysis. It’s a critical first step in data analysis. Several times it required to spend up to 80 percent of time converting data into a form suitable for analysis
Stopwords Removal:
Stop Words: A stop word is a commonly used word (such as “the”, “a”, “an”, “on”, “it”, ”itself”, ”they”, ”them”, ”their”) that a search engine has been programmed to ignore, both when indexing entries for searching and when retrieving them as the result of a search query.
We would not want these words taking up space in our database or taking up valuable processing time. For this, we can remove them easily, by storing a list of words that you consider to be stop words. NLTK(Natural Language Toolkit) in python has a list of stopwords stored in 16 different languages. You can find them in the nltk_data directory.

Punctuation Removal: Punctuation refers to the tools used in writing to separate sentences, phrases, and clauses so that their intended meaning is clear. Here we have regex module to remove punctuation from tweets.

Analyzing Tweets with Sentiment Analysis
This tells us that the English phrase “sentiment analysis is a good technique” has a polarity of about 0.7, meaning it is positive, and a subjectivity of about 0.6, meaning it is subjective.

Plotting Sentiment Analysis
Twitter Data sentiment analysis has been done using different graphical representation. It provides multiple view on Tweets sentiment analysis
· Date wise Sentiment Analysis — Polarity
· Date wise Sentiment Analysis — Subjectivity
· Overall Tweets Sentiment Analysis

Overall Tweets Sentiment Analysis