-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_term_frequency.py
59 lines (54 loc) · 1.69 KB
/
twitter_term_frequency.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
49
50
51
52
53
54
55
56
57
58
59
import sys
import string
import json
from collections import Counter
from nltk.tokenize import TweetTokenizer
from nltk.corpus import stopwords
def process(text, tokenizer=TweetTokenizer(), stopwords=[]):
"""Process the text of a tweet:
- Lowercase
- Tokenize
- Stopword removal
- Digits removal
Return: list of strings
"""
text = text.lower()
tokens = tokenizer.tokenize(text)
# If we want to normalize contraction, uncomment this
# tokens = normalize_contractions(tokens)
return [tok for tok in tokens if tok not in stopwords and not tok.isdigit()]
def normalize_contractions(tokens):
"""Example of normalization for English contractions.
Return: generator
"""
token_map = {
"i'm": "i am",
"you're": "you are",
"it's": "it is",
"we're": "we are",
"we'll": "we will",
}
for tok in tokens:
if tok in token_map.keys():
for item in token_map[tok].split():
yield item
else:
yield tok
print ("=="*30)
print ("Term frequency")
print ("=="*30, "\n")
if __name__ == '__main__':
tweet_tokenizer = TweetTokenizer()
punct = list(string.punctuation)
stopword_list = stopwords.words('english') + punct + ['rt', 'via']
fname = sys.argv[1]
tf = Counter()
with open(fname, 'r') as f:
for line in f:
tweet = json.loads(line)
tokens = process(text=tweet.get('text', ''),
tokenizer=tweet_tokenizer,
stopwords=stopword_list)
tf.update(tokens)
for tag, count in tf.most_common(30):
print ("{},{}".format(tag.encode('utf-8'), count))