-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterm_sentiment.py
48 lines (41 loc) · 1.11 KB
/
term_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
import urllib
import json
import sys
from pprint import pprint,pformat
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)
# line(tweet_file)
afinnfile = sent_file.readlines()
scores = {} # initialize an empty dictionary
for line in afinnfile:
term, score = line.split("\t") # The file is tab-delimited. "\t" means "tab character"
scores[term] = int(score) # Convert the score to an integer.
tweet_lines = tweet_file.readlines()
newdict={}
for t in tweet_lines:
tweet = json.loads(t)
sentiment=0.0
if(tweet.has_key('text')):
tweet_text=tweet['text']
tweet_words = tweet_text.split(' ')
for word in tweet_words:
if word in scores:
sentiment+=scores[word]
for newword in tweet_words:
if newword not in newdict:
newdict[newword]=scores[word]*1.0
else:
newdict[newword]+=scores[word]*1.0
for key in sorted(newdict.keys()):
print key, newdict[key]
tweet_file.close()
sent_file.close()
if __name__ == '__main__':
main()