-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentimentanalysis.py
55 lines (43 loc) · 1.75 KB
/
sentimentanalysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from textblob import TextBlob
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import sys
import argparse
#Variables that contains the user credentials to access Twitter API
consumer_key='xxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_key='xxxxxxxx-xxxxxxxxxxxxxxxxx'
access_token_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
class StdOutListener(StreamListener):
def on_data(self, data):
jsondata = json.loads(data)
# print(jsondata) # will print whole json data with very detailed informations
if "text" in jsondata:
self.print_sentiment(jsondata['text'])
return True
def on_error(self, status):
print(status)
def print_sentiment(self,text):
sentimentData = TextBlob(text)
print(text.encode("utf-8"))
sentimentVal="";
if sentimentData.sentiment.polarity > 0 :
sentimentVal = "positive"
elif sentimentData.sentiment.polarity < 0 :
sentimentVal = "negative"
else :
sentimentVal = "neutral"
print("Sentiment : {} , Polarity Value :{}".format(sentimentVal, sentimentData.sentiment.polarity));
sys.stdout.flush()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='twitter sentiment analysis')
parser.add_argument("keyword", help="Please provide the keyword for which tweets will be received")
args = parser.parse_args()
# print(args.keyword)
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=[args.keyword])