Sentiment Analysis of Twitter Users using Python

This article shows you how to perform a sentiment analysis of Twitter users using Python.

Before we go to the program, first of all, let me tell you about sentiment analysis in brief.

Sentiment analysis (also known as opinion mining) is the process to determine whether a piece of text is positive, negative or neutral.

We can use sentiment analysis to find the feeling of people about a specific topic. Sentiment analysis is widely used in social media analysis, reviews, marketing, politics, etc.

  • Social media use it to tag hate speeches.
  • Companies producing products use it to find whether people are liking or disliking their product.
  • Political parties use it to find how people react to their political decisions etc.

There are two ways to do sentiment analysis.

  1. Machine Learning-based methods
  2. Lexicon-based methods.

Here we are going to use the lexicon-based method to do sentiment analysis of Twitter users with Python.

TextBlob is a famous text processing library in python that provides an API that can perform a variety of Natural Language Processing tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more. TextBlob is based on NTLK.

Let’s see how to do sentiment analysis with the help of this library.

Installing TextBlob Library

Run these commands in your system terminal.

pip install -U textblob
python -m textblob.download_corpora

For more info on how to install TextBlob click here.

Installing Tweepy

Tweepy is a great Python library that can easily access the Twitter API. This library can make our task easy.
Run these commands in your system terminal to install Tweepy.

pip install tweepy

Downloading Tweets from Twitter using Twitter API

First of all, we need to fetch tweets from twitter. This can be done using twitter’s streaming API which helps in getting users tweets about a particular topic(s).

Before moving forward, we need to make developers account on twitter. Then we have to register an app to get the various keys associated with the API.

Follow these steps to make the developers account and get the keys.

  • Go the link https://developer.twitter.com/
  • On this page, click on the Sign in at the top right corner.
  • Sign in with your Twitter account credentials or the account you want to associate with your app.
  • After you logged in, visit http://apps.twitter.com/.
  • Now click on the button “Create an app”.
  • Fill in the required details. Leave the Callback URL’s field empty.
  • After filling the details, click on the create button to create the app.
  • Finally, click on the “Create my access token” button and chose the access type. This will create the required keys.
  • Copy ‘Consumer Key’, ‘Consumer Secret’, ‘Access token’ and ‘Access Token Secret’.

Now let us begin the most awaited part, “How to do sentiment analysis of tweets”.

TextBlob object

A Textblob object is created in the following way:

obj = TextBlob("It is a wonderful book. I liked it very much.")

Textblob obj thus created contains many properties. One of which is the sentiment property.

Sentiment Property

The sentiment property returns a tuple of the form Sentiment (polarity, subjectivity). The polarity score is lies within the range [-1.0, 1.0] where higher value tells that the text is more positive and vice versa. The subjectivity score lies within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.

obj.sentiment

Output:

Sentiment(polarity=0.62, subjectivity=0.6866666666666666)

Python source code for Sentiment Analysis Of Twitter Users

Now it’s time to see the Python code that will able to perform our sentiment analysis task for Twitter. Below is our Python program to do our task.

#Importing the required libraries
import tweepy 
from textblob import TextBlob
import re 

#Setting the keys for twitter API
consumer_key= "xxxxxxxxxxxxxxxx" 
consumer_key_secret= "xxxxxxxxxxxxxxxx" 
access_token= "xxxxxxxxxxxxxxxx" 
access_token_secret= "xxxxxxxxxxxxxxxx" 

#Setting up the Twitter Client using Tweepy library to make the connection.
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_key_secret) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth,wait_on_rate_limit=True)

#Function to download the tweets on a particular topic and the required number of tweets using the api.
def download_tweets(topic,count):
    item=topic
    tweets=[]
    for tweet in tweepy.Cursor(api.search,q = item,lang = "en", tweet_mode='extended').items(count):
        tweets.append(tweet.full_text)
    return tweets

#Regular expressions are used to remove unwanted punctuation marks,url's,spaces etc. 
replace_1 = re.compile("(\.)|(\;)|(\:)|(\!)|(\')|(\?)|(\,)|(\")|(\()|(\))|(\[)|(\])")
replace_2 = re.compile("(<br\s*/><br\s*/>)|(\-)|(\/)")

#Funtion that returns the clean text.
def clean_text(text):
    text=re.sub(r"http\S+", "", text)
    text = replace_1.sub("", text)
    text = replace_2.sub(" ", text)
    return text

#Funtion that finds the sentiment of a text(tweet) using the textblob library and returns it.
def get_sentiment(tweet):
    tweet = clean_text(tweet)
    obj = TextBlob(clean_text(tweet)) 
    if obj.sentiment.polarity > 0:
        return 'positive'
    elif obj.sentiment.polarity == 0:
        return 'neutral'
    else:
        return 'negative'
    
#Main function calling.
if __name__ == "__main__":
    topic = input("Enter the topic:\n")  #Asking the topic on which the sentiment analysis is to be done.
    count = int(input("Enter the number of tweets to be analysed\n"))
    downloaded_tweets=download_tweets(topic,count) #This line fetch the corresponding tweets.
    tweets_with_sentiment=[]
    
    #Now the tweets fetched needs to be parsed and get the sentiment for each tweet.
    for tweet in downloaded_tweets:
        sentiment = get_sentiment(tweet) #
        tweets_with_sentiment.append({'text':tweet,'sentiment':sentiment})
    
    #These lines seperate the positve , negative and neutral tweets.
    positive_tweets=[tweet for tweet in tweets_with_sentiment if tweet['sentiment']=='positive'] 
    negative_tweets=[tweet for tweet in tweets_with_sentiment if tweet['sentiment']=='negative']
    neutral_tweets=[tweet for tweet in tweets_with_sentiment if tweet['sentiment']=='neutral']
    
    #These lines calculate the percentage of positive , negative and neutral tweets.
    pos_per = (len(positive_tweets)/len(tweets_with_sentiment))*100
    neg_per = (len(negative_tweets)/len(tweets_with_sentiment))*100
    neu_per = (len(neutral_tweets)/len(tweets_with_sentiment))*100
    
    #Displaying the results
    print("Percentage of positive tweets -> {}%".format(pos_per))
    print("Percentage of negative tweets -> {}%".format(neg_per))
    print("Percentage of neutral tweets -> {}%".format(neu_per)
    
    #Printing the overall sentiment
    if(pos_per>neg_per):
        print("Overall sentiment for this topic is: POSITIVE")
    elif(pos_per<neg_per):
        print("Overall sentiment for this topic is: NEGATIVE")
    else:
        print("Overall sentiment for this topic is: NEUTRAL")

In the above Python program, you can follow the comment to know what we did in each and every step. So I am not going to describe the code. You can follow the code and read comments.

Also, read:

I hope you liked the article.

 

One response to “Sentiment Analysis of Twitter Users using Python”

  1. ANJALI SINGH says:

    Well explained in detail with proper understandable codes.

Leave a Reply

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