Sep 30, 2018

Example 8: Sentiment Analysis of Tweets from Twitter API

Saving Tweets in CSV file with Tweet Sentiments


Sentiment Analysis is the process of understanding and extracting human feelings from text data. To define technically, Sentiment Analysis is the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer's attitude towards a particular topic, product, etc. is positive, negative, or neutral.

Companies harness the power of social media and sentiment analysis through contextual mining of text which identifies and extracts subjective information in source material, and helping their business to understand the social sentiment for their brand, product or service while monitoring online conversations.

The easiest way to find sentiments is in tweets that users type in after they self understand, analyse, or make perception on a news, a situation, a product, a movie, or anything like that under the sun. The Twitter API lets us access the functionality of tweets reading from our code.

First of all I created a Twitter API with my login ID at https://developer.twitter.com and obtained the consumer_key, consumer_secret, access_token, and access_token_secret, that I have used to access the Twitter API.

We then use the tweepy package for accessing Twitter APIs in Python. For Python versions prior to 3.7, install tweepy simply with general pip command: pip install tweepy
For Python version 3.7, install tweepy updates directly from git as: pip install git+https://github.com/tweepy/tweepy.git

The TextBlob python library helps us in processing textual data to perform sentiment analysis with just few lines of code. 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. Install the TextBlob python library with pip as:
pip install textblob
python -m textblob.download_corpora

Note: Microsoft Visual C++ Compiler Package for Python 2.7 will be required to encounter error while installing textblob.

The TextBlob API provides sentiment analysis as Sentiment(polarity=0.0, subjectivity=0.0), where Polarity is the measures how positive or negative some text is, and Subjectivity is the measure of how much of an opinion is it vs how factual, where 0 is the neutral value with positive and negative values on the either sides.

The sample code I tested is shown below. Also, before running the code I created a blank csv file named as twitter_dataset.csv and put it in a directory as mentioned in the code.

Note: Please note that the tweepy package throws an error with Python 3.7. From Google search I found that apparently "async" cannot be used as an argument name in Python 3.7. To fix this, open streaming.py and replace async with async_. It fixed the error for me.

import tweepy
from textblob import TextBlob
import csv

#API keys and token for using the Twitter API
consumer_key = "#####E5UbboNCf3O9Z3N
#####"
consumer_secret = "
#####VbADAblJus2tF60JNX#####TQBV99kgHKpCvnC2b#####"
access_token = "
#####8578-#####dqm2p2UtIp8Uu#####d4cQCO4RmqHK#####"
access_token_secret = "
#####m0UYXecfEm6eDPEh#####LLCZmorNEQ8rcM#####"

#Authenticating with Twitter for the above keys
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

'''
public_tweets = api.home_timeline()
for tweet in public_tweets:
    print(tweet.text)
'''


tweet_search_topic = input("Enter the topic in twitter to analyse? : ")
public_tweets = api.search(tweet_search_topic)

#Writing the tweets in a csv file
with open('./MyData/twitter_dataset.csv', mode='w', encoding="utf-8") as tweets_file:
    tweet_writer = csv.writer(tweets_file, delimiter = ',', quotechar = '"', quoting = csv.QUOTE_MINIMAL)
    #Labelling the columns for the tweet dataset
    tweet_writer.writerow(['Tweet', 'Author', 'Date', 'Sentiment', 'Polarity'])
    #Analyzing each tweet from the tweets and writing it in the csv file
    for tweet in public_tweets:
        tweet_text = tweet.text
        tweet_user = tweet.user.name
        tweet_created_at = tweet.created_at
        tweet_sentiment = TextBlob(tweet_text).sentiment
        tweet_polarity = TextBlob(tweet_text).sentiment.polarity
        tweet_subjectivity = TextBlob(tweet_text).sentiment.subjectivity
        print(tweet_text, tweet_user, tweet_created_at)
        print(tweet_sentiment)
        tweet_writer.writerow([tweet_text, tweet_user, tweet_created_at, tweet_polarity, tweet_subjectivity])


The Results:

On running the code it asks to enter the topic in twitter to analyse. I enter say, "Putin".

It finds and prints tweets on Putin like this, with user's sentiment values:

@BigTinyBird @FoxNews @WhiteHouse @POTUS So, the mission was to get Putin to love America?
And it worked?
Wow.... h… https://t.co/ipLahQfYnE Max 2018-10-08 17:55:48
Sentiment(polarity=0.3, subjectivity=0.8)

... ... ...

Also it write the csv file twitter_dataset.csv as following, which we can used for further detailed analysis: