Twitter Sentiment Analyzer
There are two sources for this project:
- Twitter Sentiment Analysis - Learn Python for Data Science #2 by Siraj Raval
- How to build a twitter sentiment Analyzer using TextBlob
In this case we are going to use TextBlob
Now, what is sentiment analysis, it is the process of determining whether a piece of writing is positive, negative or neutral, there are several ways to do this sentiment analysis, the two more common approaches will be:
- Lexicon-base model
- Machine Learning-based Method
In a really shallow description/definition, Lexicon is a method that will list the worlds as positive and negative, e.g 'nice',+2, 'good',+1,'terrible',-1.5), The algorithm will find all the words and combine individual results and provide a result base in that value.
Twitter API¶
First, we will need to register the app in twitter in order to get the various keys associated with the API, there will be 4 keys:
consumer_key
consumer_key_secret
access_token
access_token_secret
In order to continue we will need to install two packages, tweepy and textblob, we can do this using pip,
and with this will make the sentiment analysis. Once we have the packages we can start the code, first we need to import the packages
now, we assigned the keys to variable to pass it later in the code
Tweepy¶
Tweepy support OAuth authentication, this is handle by the class tweepy.OAuthHandler
, An instance of OAuthHandler
must be created passing the consumer token and the secret.
Next, on this instance, we will call a function set_access_token
by passing the access_token
and access_token_secret
.
Finally we create and instance of the api with the tweepy function API()
search
of the API.
Sentiment Analysis¶
TextBlob¶
TextBlob is a library Natural Language Processing (NLP).
Sentiment analysis¶
As a result of the sentiment analysis we will receive a tuple (polarity, subjectivity). The polarity score is a float within a range of [-1.0,1.0]. The subjectivity is a float within the range [0.0,1.0] where 0.0 is very objective and 1.0 is very subjective.
so now we are going to check each tweeter in our public_tweets
variable
here the complite script: