-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
29 lines (23 loc) · 913 Bytes
/
main.py
File metadata and controls
29 lines (23 loc) · 913 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
import sys
from stats import get_word_count, get_char_count, sort_char_dict
def get_book_text(filepath: str) -> str:
with open(filepath) as f:
file_contents = f.read()
return file_contents
def main():
if len(sys.argv) < 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
filepath = sys.argv[1] # use the second argument passed as the filename
book_text = get_book_text(filepath)
num_words = get_word_count(book_text)
char_counts = get_char_count(book_text)
sorted_chars = sort_char_dict(char_counts)
print("============ BOOKBOT ============")
print(f"Analyzing book found at {filepath}")
print("----------- Word Count ----------")
print(f"Found {num_words} total words")
print("--------- Character Count -------")
for char_dict in sorted_chars:
print(f"{char_dict['char']}: {char_dict['count']}")
main()