-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ab_hashtags.py
35 lines (32 loc) · 927 Bytes
/
Ab_hashtags.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
import json
import nltk
def get_hashtags(tweet):
'''
parameter: tweet json object
return: list of hashtags in that tweet
'''
if 'retweeted_status' in tweet:
tweet = tweet['retweeted_status']
if 'extended_tweet' in tweet:
hashtags = tweet['extended_tweet']['entities']['hashtags']
else:
hashtags = tweet['entities']['hashtags']
tags = []
for hashtag in hashtags:
tags.append(hashtag['text']) # get hashtag text only
return tags
def all_hashtags(filename):
'''
parameter: string filename
return: list of all hashtags
'''
with open(filename) as infile:
tweets = json.load(infile)
hashtags = []
for tweet in tweets:
hashtags += get_hashtags(tweet)
return hashtags
# return top 10 hashtags
def ten_hashtags(filename):
hashtags = all_hashtags(filename)
return nltk.FreqDist(hashtags).most_common(10)