-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
47 lines (35 loc) · 1.12 KB
/
main.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
def main():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
num_words = get_num_words(text)
chars_dict = get_chars_dict(text)
alphachar_dict = clean_alpha(chars_dict)
report(book_path, num_words, alphachar_dict)
def get_num_words(text):
words = text.split()
return len(words)
def get_chars_dict(text):
chars = {}
for c in text:
lowered = c.lower()
if lowered in chars:
chars[lowered] += 1
else:
chars[lowered] = 1
return chars
def clean_alpha(text):
keys_to_remove = [key for key in text.keys() if not key.isalpha()]
for key in keys_to_remove:
del text[key]
return text
def report(bookpath, num_words, text):
print(f"--- Begin report of {bookpath} ---")
print(f"{num_words} words found in the document")
sorted_dict = sorted(text.items(), key=lambda item: item[1], reverse=True)
for key, value in sorted_dict:
print(f"The '{key}' character was found {value} times")
print("--- End report ---")
def get_book_text(path):
with open(path) as f:
return f.read()
main()