-
Notifications
You must be signed in to change notification settings - Fork 4
/
ex05e-dd.py
executable file
·55 lines (43 loc) · 1.39 KB
/
ex05e-dd.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
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/env python
"""
Write a function `wordcount(filename)` that reads a text file and
returns a dictionary, mapping words into occurrences (disregarding
case) of that word in the text. For the purposes of this
exercise, a ``word'' is defined as a sequence of letters and the
character ``-'', i.e., ``e-mail'' and ``more-or-less'' should both
be counted as a single word.
"""
import string
def wordcount(filename):
"""
Read `filename` and return a dictionary mapping each word into the
corresponding count of occurrences within the contents of
`filename`.
This solution was contributed by Desislava Dimitrova
at the Python course on March 19--20, 2014.
"""
wc = {}
# read file contents
file_handle = open(filename,'r')
contents = file_handle.read()
contents = contents.lower()
# replace punctuation with spaces
for item in string.punctuation:
if item == '-':
continue
contents = contents.replace(item,' ')
# take unique words
words = contents.split()
words = set(words)
# word counting
for item in words:
count = contents.count(item)
wc[str(item)] = int(count)
file_handle.close()
return wc
if __name__ == '__main__':
wc = wordcount('lorem_ipsum.txt')
assert wc['and'] == 3
assert wc['more-or-less'] == 1
assert wc['their'] == 2
assert wc['infancy'] == 1