-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
36 lines (30 loc) · 1.03 KB
/
main.py
File metadata and controls
36 lines (30 loc) · 1.03 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
from stats import word_count, char_count, sort_char_count, sort_on
import sys
def get_book_text(filepath):
with open(filepath) as f:
file_contents = f.read()
return file_contents
def print_report(sorted):
print("============ BOOKBOT ============")
print(f"Analyzing book found at books/frankenstein.txt...")
print("----------- Word Count ----------")
print("Found 75767 total words")
print("----------- Character Count -------")
for item in sorted:
if item['char'].isalpha():
print(f"{item['char']}: {item['num']}")
def main():
try:
path_to_file = sys.argv[1]
print(path_to_file)
text = get_book_text(path_to_file)
word_count(text)
to_sort = sort_char_count(char_count(text))
to_sort.sort(key=sort_on, reverse=True)
print_report(to_sort)
except:
print("Usage: python3 main.py <path_to_book>")
print('Example: python3 main.py "books/frankenstein.txt"')
sys.exit(1)
if __name__ == "__main__":
main()