-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_sentiment.py
57 lines (42 loc) · 1.22 KB
/
tweet_sentiment.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
import sys
import json
def lines(fp):
return str(len(fp.readlines()))
def tweetParse(tweet_file):
ss = []
for t in tweet_file.readlines():
y= json.loads(t)
#print str(y.encode("utf-8"))
if y.has_key("text"):
ss.append(str(y["text"].encode("utf-8")))
#print str(len(ss))
#for r in ss:
#print r #str(r.encode("utf-8")),"\n\t------\n"
# for w in r.split(' '):
# print str(w.encode("utf-8"))
return ss
def Calculate(tweet,dic):
for t in tweet:
val = 0.0
for w in t.split(' '):
if w not in dic.keys():
val = val +float(dic[w])
print float(val)
def main():
sent_file = open(sys.argv[1])
tweet_file = open(sys.argv[2])
dic = {}
for d in sent_file.readlines():
word, score = d.split("\t")
#dic.append((y[0], y[1]))
dic[word] = float(score)
#tweetParse(tweet_file)
Calculate(tweetParse(tweet_file),dic)
#lines(sent_file)
#lines(tweet_file)
#print str(tweet_file.readlines(1,2))
#print tweet_file[0]["text"].split()
#for (x,y) in dic:
# print y
if __name__ == '__main__':
main()