-
Notifications
You must be signed in to change notification settings - Fork 9
/
freq.py
36 lines (28 loc) · 837 Bytes
/
freq.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
# freq.py
import string
def compareItems((w1,c1), (w2,c2)):
if c1 > c2:
return - 1
elif c1 == c2:
return cmp(w1, w2)
else:
return 1
def main():
# get the sequence of words from the file
fname = raw_input("File to analyze: ")
text = open(fname,'r').read()
text = string.lower(text)
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
text = string.replace(text, ch, ' ')
words = string.split(text)
# construct a dictionary of word counts
counts = {}
for w in words:
counts[w] = counts.get(w,0) + 1
# output analysis of n most frequent words.
n = input("Output analysis of how many words? ")
items = counts.items()
items.sort(compareItems)
for i in range(n):
print "%-10s%5d" % items[i]
if __name__ == '__main__': main()