-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
36 lines (28 loc) · 990 Bytes
/
Copy pathmain.py
File metadata and controls
36 lines (28 loc) · 990 Bytes
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
import sys
from stats import get_num_words, get_chars_dict, sort_dict
def main():
try:
book_path = sys.argv[1]
except IndexError:
if len(sys.argv) != 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
text = get_book_text(book_path)
num_words = get_num_words(text)
chars_dict = get_chars_dict(text)
chars_list = sort_dict(chars_dict)
print_report(book_path, num_words, chars_list)
def get_book_text(path):
with open(path) as f:
return f.read()
def print_report(book_path, num_words, chars_list):
print(f"============ BOOKBOT ============")
print(f"Analyzing book found at {book_path}...")
print(f"----------- Word Count ----------")
print(f"Found {num_words} total words")
print(f"--------- Character Count -------")
for char in chars_list:
if char[0].isalpha():
print(f"{char[0]}: {char[1]}")
print("============= END ===============")
main()