-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokens_histogram.py
41 lines (31 loc) · 1.1 KB
/
tokens_histogram.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
"""
Plots an histogram of the tokens labelled as ['NOUN', 'ADJ', 'NUM', 'PROPN'], using Pyplot.
Very memory consuming!
"""
import pickle
from collections import Counter
from os import path
import matplotlib.pyplot as plt
from tqdm import tqdm
from data.co_occurrence import pos_filename, data_folder, interesting_tags, num_words
def main():
counter_file = path.join(data_folder, 'train_word_count.pickle')
if path.isfile(counter_file):
with open(counter_file, 'rb') as f:
return pickle.load(f)
cnt = Counter()
with open(pos_filename) as f_refs:
for tagged_word in tqdm(f_refs, desc='Counting word frequencies in references', total=num_words):
if tagged_word != '\n':
word, tag = tagged_word.split()
if tag in interesting_tags:
cnt.update([word])
with open(counter_file, 'wb') as f:
pickle.dump(cnt, f)
# Plot common words histogram
cnt = dict(cnt.most_common(100))
plt.bar(cnt.keys(), cnt.values())
plt.xticks(rotation='vertical')
plt.show()
if __name__ == '__main__':
main()