-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordAnalysis.py
34 lines (25 loc) · 939 Bytes
/
wordAnalysis.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
f = open('alice.txt', 'r')
count = {}
for line in f:
for word in line.split():
# remove punctuation
word = word.replace('_', '').replace('"', '').replace(',', '').replace('.', '')
word = word.replace('-', '').replace('?', '').replace('!', '').replace("'", "")
word = word.replace('(', '').replace(')', '').replace(':', '').replace('[', '')
word = word.replace(']', '').replace(';', '')
# ignore case
word = word.lower()
# ignore numbers
if word.isalpha():
if word in count:
count[word] = count[word] + 1
else:
count[word] = 1
keys = count.keys()
keys.sort()
# save the word count analysis to a file
out = open('alice_words.txt', 'w')
for word in keys:
out.write(word + " " + str(count[word]))
out.write('\n')
print("The word 'alice' appears " + str(count['alice']) + " times in the book.")