-

Monday 26 March 2018

Sentiment analysis based on Twitter data using tweepy and textblob

The following code is tested in Ubuntu 14.04 and installation steps also for Ubuntu 14.04
Tweepy helps to connect your python script to twitter and fetch data based on your arguments. Textblob helps in analyzing the sentiment of tweets.

Installation of Tweepy
pip install tweepy

Installation of Textblob
pip install -U textblob

After installing the above dependencies, one has to login to twitter developer account. If you have already had a twitter account, then verify your account with mobile number otherwise create a new account. After creating the twitter account, log in to https://apps.twitter.com/. After this create an app, which generates the consumer_key, consumer_secret, access_token, access_token_secret. These 4 keys helps in connecting twitter APIs to authenticate and fetch the data.

The following code helps in analyzing the sentiment.

import tweepy
from textblob import TextBlob

# Step 1 - Authenticate
consumer_key= 'XXXXXXXXXXXXXXXXXXXXX'
consumer_secret= 'XXXXXXXXXXXXXXXXXX'
access_token='XXXXXXXXXXXXXXXXXXXX'
access_token_secret='XXXXXXXXXXXXXXXXX'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

#Step 2 - Retrieve Tweets
public_tweets = api.search('makeinindia')

for tweet in public_tweets:
    #Step 3 Perform Sentiment Analysis on Tweets
    analysis = TextBlob(tweet.text)
    print(analysis.sentiment)
 

1 comment:

  1. Your blog is absolutely fantastic and great android apps development tutorial for beginners. Good work.
    Machine Learning Corporate Training
    https://www.analyticspath.com/machine-learning-corporate-trainin

    ReplyDelete