forked from SaltikData/CourseraDataScience
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.6-top_ten.py
34 lines (26 loc) · 972 Bytes
/
hw1.6-top_ten.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
#!/usr/bin/env python
# @dpmehta02
# Coursera Data Science HW1 - prints top ten hashtags of Twitter livestream data to stdout: <hashtag:string> <count:float>
# USAGE: $ python top_ten.py <tweet_file>
import sys
import json
from collections import Counter
def main():
hashtags = []
# load each tweet as json
for line in open(sys.argv[1]):
tweet_json = json.loads(line)
# if a tweet has a hashtag ...
if "entities" in tweet_json.keys() and "hashtags" in tweet_json["entities"]:
# and isn't blank ...
if tweet_json['entities']['hashtags'] != []:
# append each hashtag (in unicode)
for hashtag in tweet_json["entities"]["hashtags"]:
unicode_hashtag = hashtag["text"].encode('utf-8')
hashtags.append(unicode_hashtag)
# print top ten hashtag counts to stdout
top_ten = Counter(hashtags).most_common(10)
for key, value in top_ten:
print key, float(value)
if __name__ == '__main__':
main()