Twitter Sentiment Analysis

Rahul Yadav
3 min readOct 2, 2017

--

we know that millions of peoples uses twitter and they tweet on almost every topic in the world.They give reaction on different topics ,give opinion ,suggestions,express their feelings etc .So twitter is a Big psychological data base which update every second of every day.So we are gonna this data base to analysis million of text snippets in a second.

>>>work Flow of Twitter Sentiment analysis using Machine Learning:

step1:Tokenization which means we will take a tweet and split it into single words.

step2:Make a bag of words which means each word occurs how many times.

step3:Now will check the sentiment value of each word from sentiment lexicon which have already pre recorded.

Let’s start:

First we have to register our account for the twitter API so for that login into your twitter account and open https://apps.twitter.com/app/new .So fill up the required information and company name doesn’t matter so fill any URL but in proper format https://www.<anything>.com.

So after that you got registered for using Twitter API and you are now on your detail page .Click on the Keys and Access Token ….so these are very important for us to use twitter api .

So now its time to install Dependency in our machine .For sentiment analysis we require only two dependency :

  1. tweepy: it will help us to access twitter api in our code.To install this run the following command.
sudo pip install tweepy
  1. textblob: this helps us for real sentiment analysis.To install this run the following command.
sudo pip install textblob

some example code of textblob:

so in python terminal import TextBlob from textblob

>>>from textblob import TextBlob

now assign our sentence in a variable lets say var

>>>var=TextBlob(“sun rise from east”)

so now if we want to split this whole sentence into words we can make use of

>>>var.words

output will be :

WordList( [ ‘sun’ , ’rise’ , ’from’ , ’east’ ] )

now to check sentiment analysis of the above we can make use of

>>>var.sentiment.polarity

this will give output of a value between -1 to +1.

Now move on the real actual code in python :

#import tweepy library which helps us to access twitter apiimport tweepy#import TextBlob from textblobfrom textblob import TextBlob

#make four variables which stores your keys as in string
#you can get these from your twitter api registered dashboard
consumer_key='<your consumer key>'
consumer_secret='<your consumer secret>'

access_token='<your access token>'
access_token_secret='<your access token secret>'
#create a varable which helps as to authicate with twitter apiauth=tweepy.OAuthHandler(consumer_key,consumer_secret)#set access foryour access_tokenauth.set_access_token(access_token,access_token_secret)#make main variable which auth with twitter aiapi=tweepy.API(auth)#make a variable which stores all the tweets which contains the word you are searching for
#here i search all the tweets which contain the word cricket
public_tweets=api.search('cricket')
#make a for loop and access all the tweets one by onefor tweet in public_tweets:
#print the tweets if you want
print(tweet.text) #make a variable which stores a list which contains the split words using TextBlob. analysis=TextBlob(tweet.text)
#print sentiment of each using sentiment
print(analysis.sentiment)

so from output of above code you can see the sentiment value of each tweet between -1 to +1. means positive or negative sentiments :D.

--

--

No responses yet