-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweet_sentiment.py
More file actions
62 lines (46 loc) · 1.13 KB
/
tweet_sentiment.py
File metadata and controls
62 lines (46 loc) · 1.13 KB
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
import sys
import json
def hw():
print 'Hello, world!'
def lines(fp):
print str(len(fp.readlines()))
def main():
sent_file = open(sys.argv[1])
tweet_file = open(sys.argv[2])
#hw()
#lines(sent_file)
#lines(tweet_file)
content=[]
json_dict={}
tweet_file=open(sys.argv[2])
for line in tweet_file:
json_dict=json.loads(line)
if 'text' in json_dict.keys():
#print json_dict["text"]
unicode_str=json_dict['text']
encoded=unicode_str.encode('utf-8')
content.append(encoded)
#print len(content)
#print content[6]
affinfile=open(sys.argv[1])
scores={}
for line in affinfile:
term,score=line.split("\t")
scores[term]=int(score)
#print scores.items()
score_list=[]
#print content[2]
for tweet in content:
tweet=tweet.lower()
terms=tweet.split(" ")
count=0
for term in terms:
term=term.strip()
if term in scores:
count+=scores[term]
score_list.append(count)
#print score_list
for elem in score_list:
print elem
if __name__ == '__main__':
main()