-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (39 loc) · 1.37 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
50
51
52
53
def main():
book_path = "books/frankenstein.txt"
frankenstein = read_book(book_path)
# read and print frankenstein.txt to console
print("Main text: \n" + frankenstein)
# get word count for frankenstein.txt
print("Word count: \n" + str(count_words(frankenstein)) + "\n")
# get character counts for frankenstein.txt
chars = count_chars(frankenstein)
print(chars)
print("Character count (sorted): \n" + str(chars))
# get pretty character report
print(char_report(chars, book_path))
def read_book(path_to_file):
with open(path_to_file) as f:
file_contents = f.read()
return file_contents
def count_words(book_text):
return len(book_text.split())
def count_chars(book_text):
chars = {}
for char in book_text:
lchar = char.lower()
if lchar not in chars:
chars[lchar] = {"name": lchar, "num": 1}
else:
chars[lchar]["num"] += 1
return list(chars.values())
def sort_on(dict):
return dict["num"]
def char_report(char_dict, path):
char_dict.sort(reverse=True, key=sort_on)
report = "--- Begin report of " + path + " ---\n"
for char in char_dict:
if char["name"].isalpha():
report += "The \'" + char["name"] + "\' character was found " + str(char["num"]) + " times.\n"
report += "--- End report ---"
return report
main()