Analyzing Twitter Sentiment with Python
The bot's Twitter account
is was @spxfeelslike and, before I continue to explain what I did there, here's one of its actual tweets:
The two main libraries used here are:
I won't go into too much details on the Twitter querying part of the code as it is pretty self-explanatory.
Rather, I'll focus on the sentiment part, which is basically 1 line of code (not counting the import section):
from textblob import TextBlob sentiment = TextBlob("Gotta love Python").sentiment
sentiment will have 2 properties:
subjectivity- indicates whether a sentiment was even detected a sentiment in the provided text
polarity- ranks the positive sentiment of the text, and ranges from -1 (extremely netagive) to 1 (extremely positive)
As you can see, once we have a list of tweets, all that's left to do is to count the total number of tweets with determined sentiment and the total number of positive tweets, and then simply calculate the ratio of positive tweets to total valid tweets.
Here's a full working code:
#!/usr/bin/env python # -*- coding: utf-8 -*- import tweepy from textblob import TextBlob # your Twitter app credentials _CONSUMER_KEY = "..." _CONSUMER_SECRET = "..." _ACCESS_TOKEN = "..." _ACCESS_TOKEN_SECRET = "..." # keyword to query keyword = "$SPX" # ----------------------------------- if __name__ == "__main__": # twitter authentication oauth = tweepy.OAuthHandler(_CONSUMER_KEY, _CONSUMER_SECRET) oauth.set_access_token(_ACCESS_TOKEN, _ACCESS_TOKEN_SECRET) twitter_api = tweepy.API(oauth) # get latest 100 tweets containing the phrase $SPX tweets = twitter_api.search(keyword, count=1000) # parse sentiment valid = 0 # counter for tweets with a determined sentiment positive = 0 # counter for positive tweets for tweet in tweets: text = TextBlob(tweet.text).sentiment if text.subjectivity != 0: valid += 1 positive += (text.polarity > 0) # sentiment ratio sentiment = positive / valid # construct tweet tweet = "Sentiment for %s is %.2f%%" % (keyword, (sentiment * 100)) # to actually tweet, uncomment this line: # twitter_api.update_status(status=tweet) # for now, just print out the tweet print(tweet)
Output would look something like this:
Sentiment for $SPX is 52.12%
* Use the above code with caution and enjoy...
Updated on 18 February 2017. For the latest version, please see: