-
Notifications
You must be signed in to change notification settings - Fork 5
/
twitter_term_frequency.py
47 lines (37 loc) · 1.29 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
import sys
import string
import json
from collections import Counter
from nltk.tokenize import TweetTokenizer
from nltk.corpus import stopwords
import matplotlib.pyplot as plt
def process(text, tokenizer=TweetTokenizer(), stopwords=[]):
""" Process tweet text:
- Lowercase
- tokenize
- stopword removal
- digits removal
Return: list of strings
"""
text = text.lower()
tokens = tokenizer.tokenize(text)
return [tok for tok in tokens if tok not in stopwords and not tok.isdigit()]
if __name__ == '__main__':
fname = sys.argv[1]
tweet_tokenizer = TweetTokenizer()
punct = list(string.punctuation)
stopword_list = stopwords.words('english') + punct + ['rt', 'via', '...']
tf = Counter()
with open(fname, 'r') as f:
for line in f:
tweet = json.loads(line)
tokens = process(text=tweet['text'], tokenizer=tweet_tokenizer, stopwords=stopword_list)
tf.update(tokens)
for tag, count in tf.most_common(20):
print("{}: {}".format(tag, count))
y = [count for tag, count in tf.most_common(20)]
x = range(1, len(y)+1)
plt.bar(x, y)
plt.title("Term frequencies used in US-Iran Stream Data")
plt.ylabel("Frequency")
plt.savefig('us-iran-term-distn.png')