-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
37 lines (30 loc) · 1.1 KB
/
main.py
File metadata and controls
37 lines (30 loc) · 1.1 KB
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
def main():
def get_text(file):
with open(file, "r") as f:
file_contents = f.read()
return file_contents
def count_words(book_text):
length = len(book_text.split())
return length
def count_chars(book_text):
words = book_text.split()
characters = {}
for word in words:
for char in word.lower():
characters[char] = characters.get(char, 0) + 1
return characters
def create_report(book_file):
start = f"--- Begin report of {book_file} ---"
text = get_text(book_file)
words = count_words(text)
chars = count_chars(text)
alphas = {k: v for k, v in chars.items() if k.isalpha()}
sorted_by_count = sorted(alphas.items(), key=lambda kv: kv[1], reverse=True)
print(start)
print(f"{words} words found in the document \n")
for pair in sorted_by_count:
print(f"The '{pair[0]}' character was found {pair[1]} times")
print("--- End report ---")
book_path = "./books/frankenstein.txt"
create_report(book_path)
main()