-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfreq.py
More file actions
28 lines (24 loc) · 678 Bytes
/
freq.py
File metadata and controls
28 lines (24 loc) · 678 Bytes
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
import sys
vocab = {} # dict to store frequency list
f = open(sys.stdin, 'r', encoding='utf-8')
# for each of the lines of input
for line in f.readlines():
# if there is no tab character, skip the line
if '\t' not in line:
continue
# make a list of the cells in the row
row = line.split('\t')
# the form is the value of the second cell
form = row[1]
# if we haven't seen it yet, set the frequency count to 0
if form not in vocab:
vocab[form] = 0
vocab[form] = vocab[form] + 1
freq = []
for w in vocab:
freq.append((vocab[w], w))
freq.sort(reverse=True)
fd = open('freq.txt', 'w+', encoding='utf-8')
for w in freq:
fd.write('%d\t%s' % (w[0], w[1]))
fd.close()