-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
31 lines (24 loc) · 882 Bytes
/
main.py
File metadata and controls
31 lines (24 loc) · 882 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
import sys
from stats import get_num_words, get_char_counts, sort_char_counts
def get_book_text(filepath: str) -> str:
with open(filepath, "r", encoding="utf-8") as f:
return f.read()
def main():
if len(sys.argv) != 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
path = sys.argv[1]
print("============ BOOKBOT ============")
print(f"Analyzing book found at {path}...")
print("----------- Word Count ----------")
text = get_book_text(path)
num_words = get_num_words(text)
print(f"Found {num_words} total words")
print("--------- Character Count -------")
counts = get_char_counts(text)
sorted_chars = sort_char_counts(counts)
for item in sorted_chars:
print(f"{item['char']}: {item['num']}")
print("============= END ===============")
if __name__ == "__main__":
main()