-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (37 loc) · 1.21 KB
/
Copy pathmain.py
File metadata and controls
47 lines (37 loc) · 1.21 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
44
45
46
47
import sys
from stats import (
get_num_words,
chars_dict_to_sorted_list,
get_chars_dict,
)
def main():
if len(sys.argv) < 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
else:
try:
book_path = str(sys.argv[1]) # book_path is list which starts from 0
except IndexError:
print("here")
sys.exit(1)
text = get_book_text(book_path)
num_words = get_num_words(text)
chars_dict = get_chars_dict(text)
chars_sorted_list = chars_dict_to_sorted_list(chars_dict)
print_report(book_path, num_words, chars_sorted_list)
def get_book_text(path):
with open(path) as f:
return f.read()
def print_report(book_path, num_words, chars_sorted_list):
print("============ BOOKBOT ============")
print(f"Analyzing book found at {book_path}...")
print("----------- Word Count ----------")
print(f"Found {num_words} total words")
print("--------- Character Count -------")
for item in chars_sorted_list:
if not item["char"].isalpha():
continue
print(f"{item['char']}: {item['num']}")
print("============= END ===============")
if __name__ == "__main__":
main()