-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetSentiment.py
73 lines (47 loc) · 1.52 KB
/
tweetSentiment.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
from pattern.web import Twitter, plaintext
from pattern.web.locale import geocode
import csv
import indicoio
#enter your api key here
indicoio.config.api_key = ""
class tweetSentiment(object):
def __init__(self, topic, tweetCount):
self.topic = topic
self.tweetCount = tweetCount
self.t = Twitter(language='EN')
self.i = None
def fArray(self):
'''full array including tweet and sentiment'''
fullArray = []
for tweet in self.t.search(self.topic, start=self.i, count = self.tweetCount):
fullArray.append([tweet.text,indicoio.sentiment(tweet.text)])
self.i = tweet.id
return fullArray
def sArray(self):
'''calculate sentiment '''
sentimentArray = []
for tweet in self.t.search(self.topic, start=self.i, count = self.tweetCount):
sentimentArray.append(indicoio.sentiment(tweet.text))
self.i = tweet.id
return sentimentArray
def average(self,numArray):
'''average sentiment'''
return sum(numArray)/len(numArray)
def trending(self):
'''trending sentiment'''
trendArray = []
for trend in Twitter().trends(cached=False):
trendArray.append([trend,indicoio.sentiment(trend)])
return trendArray
def main():
sentiment = tweetSentiment('wall street', 10)
#array of tweets and sentiment
fArr = sentiment.fArray()
#array of only tweet sentiment
sArr = sentiment.sArray()
#array of trending and sentiment
tArr = sentiment.trending()
#print "Number of Tweets: " + str(len(sArr))
#print "Subject Sentiment: " + str(sentiment.average(tArr))
if __name__ == "__main__":
main()