-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.py
More file actions
60 lines (42 loc) · 1.41 KB
/
histogram.py
File metadata and controls
60 lines (42 loc) · 1.41 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
import string
import random
def ProcessFile(filename):
histogram = dict()
file = open(filename)
for line in file:
ProcessLine(line, histogram)
return histogram
def ProcessLine(line, histogram):
line = line.replace('-', ' ')
for word in line.split():
word = word.strip(string.punctuation + string.whitespace)
word = word.lower()
histogram[word] = histogram.get(word, 0) + 1
def MostCommonWords(histogram):
myTupla = []
for key, value in histogram.items():
myTupla.append((value, key))
myTupla.sort(reverse = True)
return myTupla
def TotalWords(histogram):
return sum(histogram.values())
def TotalDifferentWords(histogram):
return len(histogram)
def RandomWord(histogram):
myTupla = []
for word, frequency in histogram.items():
myTupla.extend([word] * frequence)
return random.choice(myTupla)
if __name__ == '__main__':
histogram = ProcessFile('emma.txt')
print "Total words: %s" %str(TotalWords(histogram))
print "Total different words: %s" %str(TotalDifferentWords(histogram))
#Most Common Words
myTupla = MostCommonWords(histogram)
print "The most common words are: "
for frequence, word in myTupla[:10]:
print "%s - %s" %(word, frequence)
#Random Words
print "\n\nHere are some random words from the book"
for i in range(5):
print RandomWord(histogram)