-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_term_frequency_graph.py
43 lines (37 loc) · 1.23 KB
/
twitter_term_frequency_graph.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
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 the text of a tweet:
- 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__':
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)
y = [count for tag, count in tf.most_common(30)]
x = range(1, len(y)+1)
plt.bar(x, y)
plt.title("Term Frequencies")
plt.ylabel("Frequency")
plt.savefig('term_distribution.png')