-
Notifications
You must be signed in to change notification settings - Fork 435
/
livesenti.py
90 lines (70 loc) · 2.07 KB
/
livesenti.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 22 19:04:47 2016
@author: Shreyans Shrimal
"""
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
from textblob import TextBlob
import matplotlib.pyplot as plt
import re
"# -- coding: utf-8 --"
def calctime(a):
return time.time()-a
positive=0
negative=0
compound=0
count=0
initime=time.time()
plt.ion()
import test
ckey=test.ckey
csecret=test.csecret
atoken=test.atoken
asecret=test.asecret
class listener(StreamListener):
def on_data(self,data):
global initime
t=int(calctime(initime))
all_data=json.loads(data)
tweet=all_data["text"].encode("utf-8")
#username=all_data["user"]["screen_name"]
tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
blob=TextBlob(tweet.strip())
global positive
global negative
global compound
global count
count=count+1
senti=0
for sen in blob.sentences:
senti=senti+sen.sentiment.polarity
if sen.sentiment.polarity >= 0:
positive=positive+sen.sentiment.polarity
else:
negative=negative+sen.sentiment.polarity
compound=compound+senti
print count
print tweet.strip()
print senti
print t
print str(positive) + ' ' + str(negative) + ' ' + str(compound)
plt.axis([ 0, 70, -20,20])
plt.xlabel('Time')
plt.ylabel('Sentiment')
plt.plot([t],[positive],'go',[t] ,[negative],'ro',[t],[compound],'bo')
plt.show()
plt.pause(0.0001)
if count==200:
return False
else:
return True
def on_error(self,status):
print status
auth=OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream= Stream(auth, listener(count))
twitterStream.filter(track=["Donald Trump"])