Sentiment Analysis of Twitter
[Back] Within the Twitter API we can mine for tweets. In this case we will take a key search word and then find the 10 most recent tweets, and then give a sentiment score. A value of 0.0 is neutral. A positive score is a positive sentiment, and a negative score is a negative sentiment [Twitter user search][Reddit Top Posts][Reddit with search for keyword].
Within the Twitter API was can search for keywords, and then mine for their sentiment. In the following we search for the last 10 tweets for the key word of "cybersecurity":
import tweepy import tweepy as tw from textblob import TextBlob import sys consumer_key = '' consumer_secret = '' access_key= '' access_secret = '' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api_obj = tweepy.API(auth) search_term = "cyber+security" if (len(sys.argv)>1): search_term=(sys.argv) print ("Search term: ",search_term) search_results = api_obj.search(q=search_term, count=10) res =  for tw in search_results: res.append(tw.text.encode('ascii', 'ignore')) sentiment_objects = [TextBlob(tweet.decode()) for tweet in res] sentiment_values = [[tweet.sentiment.polarity, str(tweet)] for tweet in sentiment_objects] score=0 for s in sentiment_values: score = score+float (s) print ("Overall score from 10 tweets: ", score) print i=0 for s in sentiment_values: i=i+1 print (i,end=' ') if float (s) > 0: print ('Positive ', end=' ') elif float(s) == 0: print ('Neutral ',end=' ') else: print ('Negative ',end=' ') print ("Score: ",s, "Text: ",s) print
A sample run is:
Search term: cryptography Overall score from 10 tweets: 0.983333333333 1 Neutral Score: 0.0 Text: @magomimmo @EttoreMenguzzo Questo dipende dalla definizione che dai. Ma a stretto rigore anche una permission-based https://t.co/Bd6vtEprPc 2 Positive Score: 0.45 Text: How MIT's Fiat Cryptography might make the web more secure https://t.co/KbGnb3e4J8 https://t.co/vyzrC8xLGY 3 Positive Score: 0.45 Text: How MIT's Fiat Cryptography might make the web more secure https://t.co/eb3zDACQcK https://t.co/8PQhlgbJYG 4 Negative Score: -0.1 Text: #cryptocurrency Bitcoin price is up compared to the last few days. #what_is_coming_next . . . @biditex_com https://t.co/GC7ZI3375c 5 Neutral Score: 0.0 Text: Cryptography ICE Cube experiment https://t.co/HGxU44j7HF via @esa 6 Neutral Score: 0.0 Text: RT @wearetemtum: temtum CTO Ginger Saltos on @hackernoon talking about the lessons learned while working with the Ecuadorian Government. # 7 Positive Score: 0.1 Text: RT @Gaamuk: With the launch of the worlds first intercontinental quantum satellite Micius in 2016, China has demonstrated long distance cr 8 Neutral Score: 0.0 Text: Pseudonymous tx fee cypherpunks Bitcoin non-fungible token Satoshi blocks node miner lambo difficulty target. Ether https://t.co/ervP4dInMr 9 Positive Score: 0.1 Text: With the launch of the worlds first intercontinental quantum satellite Micius in 2016, China has demonstrated long https://t.co/14Z8xyeexm 10 Negative Score: -0.0166666666667 Text: The book Cryptonomicon has finally come to me from England. Long story short, its about the importance of cryptogr https://t.co/rVHQ3jU9nE