-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
43 lines (36 loc) · 1.24 KB
/
Copy pathmain.py
File metadata and controls
43 lines (36 loc) · 1.24 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
38
39
40
41
42
43
from stats import character_counter, word_counter
import sys
def main():
if len(sys.argv) != 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
else:
book = sys.argv[1]
text = set_file(book)
number_of_characters = character_counter(text)
make_report(number_of_characters, text, book)
def set_file(path_to_file):
with open(path_to_file) as f:
file = f.read()
return file
def print_book(text):
print(text)
def make_report(number_of_characters, text, book):
list_of_characters = []
for character in number_of_characters:
if character.isalpha():
list_of_characters.append({
"character": character,
"num": number_of_characters[character]})
list_of_characters.sort(reverse=True, key=sort_on)
print_report(text, book, list_of_characters)
def sort_on(dict):
return dict["num"]
def print_report(text, book, list_of_characters):
print(f"--- Begin report of {book} ---")
print(f"{word_counter(text)} words found in the document")
print()
for character in list_of_characters:
print(f"'{character['character']}: {character['num']}'")
print("--- End report ---")
main()