-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency.py
More file actions
58 lines (42 loc) · 1.15 KB
/
frequency.py
File metadata and controls
58 lines (42 loc) · 1.15 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
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[1])
#hw()
#lines(sent_file)
#lines(tweet_file)
content=[]
json_dict={}
tweet_file=open(sys.argv[1])
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)
frequency={}
#print content[2]
total_count=0
for tweet in content:
tweet=tweet.lower()
newtweet=tweet.replace('\n',"")
terms=newtweet.split(" ")
for term in terms:
total_count+=1
term=term.strip()
if term in frequency:
frequency[term]+=1
elif term not in frequency:
frequency[term]=1
#print frequency.values()
#print total_count
for k in sorted(frequency, key=frequency.get, reverse=True):
print('%s %.6f' %(k, frequency[k]/total_count))
if __name__ == '__main__':
main()