Building Twitter Sentiment Analyzer Tool


Twitter is the ultimate dataset for peoples emotions. In this article, I will show you how to build twitter sentiment analyzer tool that can analyze people’s sentiment (positive, negative or neutral) based on there tweets regarding any keyword.

So before we start, we need to install two packages.

pip install tweepy

pip install textblob

Next, we would need to create an app in Twitter in order to get Consumer and Access keys/tokens. These tokens are required for the library to access Twitter api.

import tweepy
from textblob import TextBlob

# Enter you keys by signing up at
consumer_key = "XYZ"
consumer_secret = "XYZ"
access_token = "XYZ"
access_secret = "XYZ"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth)


public_tweets ='Modi',show_user=[True],count=100)

for tweet in public_tweets:
    # if (not tweet.retweeted) and ('RT @' not in tweet.text):
    print ( + ':' + tweet.text)
    analysis = TextBlob(tweet.text)
    print (analysis.sentiment.polarity)
    polarity_value = analysis.sentiment.polarity
    if polarity_value > 0:
    elif polarity_value < 0:
 print (pos,neg,neutral)

In the above code, the tweepy accesses your twitter api to fetch latest 100 tweets on Indian Prime Minister – Modi. The code will then analyze the tweets and its keyword and provide the polarity of the tweet. If the polarity value is above 0 then it indicates a positive sentiment, if it less than 0 then it is a negative sentiment and if it is 0 then it is a neutral sentiment.

The code can be found in my github page as well.

I went further and built a django app to display the same code in action. You can have a look at it at


The django app code can also be found in my github page.

Leave a Reply

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