-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (25 loc) · 1009 Bytes
/
Copy pathmain.py
File metadata and controls
41 lines (25 loc) · 1009 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
37
38
39
40
41
from stats import get_num_words, get_char_count, sorted_dicts
import sys
def get_book_text(filepath):
with open(filepath) as f:
return f.read()
def main():
if len(sys.argv) != 2: # ensure a single argument is provided
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
path_to_book = sys.argv[1]
contents = get_book_text(path_to_book)
num_words = get_num_words(contents)
char_counts = get_char_count(contents) # returns a dict
char_counts_lst = sorted_dicts(char_counts)
# Print results to console
print("============ BOOKBOT ============")
print(f"Analyzing book found at {path_to_book}...")
print("----------- Word Count ----------")
print(f"Found {num_words} total words")
print("--------- Character Count -------")
for item in char_counts_lst:
if item["character"].isalpha():
print(f"{item['character']}: {item['count']}")
print("============= END ===============")
main()