-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweetSentiment.py
60 lines (49 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
import sys
import json
phrase = []
def getPhrases(afinn):
# analyze AFINN file and get all the phrases
for line in afinn:
temp = line.split("\t")[0]
if len(temp.split(" ")) >1 :
phrase.append(temp)
def calculateScore(text,sent_file):
# get the word sentimental score from AFINN-111.txt
sentiment = {}
sentiment = givenSentimentScore(sent_file)
score=0
for str in phrase:
if text.find(str) >= 0:
score+=(text.count(str)*sentiment[str])
text = text.replace(str,"")
tokens = text.split(" ")
for token in tokens:
if sentiment.has_key(token.lower()):
score+=sentiment[token.lower()]
print score
def givenSentimentScore(sent_file):
score = {} # dictionary for storing sentiment score
for line in sent_file:
term, scor = line.split("\t") # store word and respective score
score[term] = int(scor)
return score
def sentimentScore(output,sent_file):
# store tweets in data
data = []
for line in output:
data.append(json.loads(line))
#calculate sentiment score
for tweet in data:
if tweet.has_key("text"):
calculateScore(tweet["text"],sent_file)
sent_file.seek(0,0)
def lines(fp):
print str(len(fp.readlines()))
def main():
tweet_file = open(sys.argv[2])
getPhrases(open(sys.argv[1]))
sentimentScore(tweet_file,open(sys.argv[1]))
#lines(sent_file)
#lines(tweet_file)
if __name__ == '__main__':
main()