Skip to content

Instantly share code, notes, and snippets.

@amar061996
Last active February 21, 2016 20:17
Show Gist options
  • Select an option

  • Save amar061996/d75f58aa06af6afea5be to your computer and use it in GitHub Desktop.

Select an option

Save amar061996/d75f58aa06af6afea5be to your computer and use it in GitHub Desktop.
Sentiment Analysis of tweets streaming on twitter related to the users Entry.
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
from textblob import TextBlob
import io
consumer_key='Your Consumer Key'
consumer_secret='Your Consumer Secret'
access_token='Your Access Token'
access_secret='Your Access Secret'
auth=OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)
api=tweepy.API(auth)
fw=open("./sentiment.txt","w")
class Listener(StreamListener):
def on_data(self, raw_data):
tweet= raw_data.split('text":')[1].split('","source"')[0]
pol= str(TextBlob(tweet).polarity)
full_tweet=tweet+" : "+pol
fw.write(full_tweet+"\n") #write the data to a file called sentiment.txt
print full_tweet
def on_error(self, status_code):
print "Error :",status_code
choice=raw_input("Enter the choice: ") #Enter the term which is to be searched in the streaming tweets
tweets=Stream(auth,Listener())
tweets.filter(track=[choice])
fw.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment