-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (42 loc) · 1.36 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
48
49
def main():
path = "/Users/jpbowen/workspace/github.com/jpbowen/bookbot/books/frankenstein.txt"
text = get_book_text(path)
num_words = count_words(text)
lowered_words = get_lowercase(text)
count_letters = get_char_dict(lowered_words)
sorted_char = get_dict_sort(char_dict)
print ("--- Begin report of books/frankenstein.txt ---")
print(f"{num_words} words found in the document\n")
for items in sorted_char:
for char, value in items.items():
print(f"The {char} character was found {value} times")
print(f"--- End report ---")
def count_words(text):
words = text.split()
return len(words)
def get_book_text(path):
with open(path) as f:
return f.read()
def get_lowercase(text):
lowered_string = text.lower()
return lowered_string
char_dict = {}
def get_char_dict(lowered_words):
for char in lowered_words:
if char.isalpha() != True:
continue
elif char in char_dict:
char_dict[char] += 1
else:
char_dict[char] = 1
return char_dict
def sort_on(char_dict):
count_list = list(char_dict.values())
return count_list[0]
char_list = []
def get_dict_sort(char_dict):
for char, count in char_dict.items():
char_list.append({char:count})
char_list.sort(reverse=True, key=sort_on)
return char_list
main()