-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (29 loc) · 997 Bytes
/
Copy pathmain.py
File metadata and controls
44 lines (29 loc) · 997 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
42
43
44
from stats import count_word_in_document, count_word_occurences, sort_dict
import sys
def get_book_text(filepath: str) -> str:
text_content = ""
with open(filepath) as f:
text_content = f.read()
return text_content
def main():
if len(sys.argv) != 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
path_to_book = sys.argv[1]
print("============ BOOKBOT ============")
# return the text content
text = get_book_text(path_to_book)
print(f"Analysing book found at {path_to_book}...")
# return number of word in document
num_words = count_word_in_document(text)
print("----------- Word Count ----------")
print(f"Found {num_words} total words")
# get the number of occurence for each charactere
chars_dict = count_word_occurences(text)
# sort them by occurences
sorted_chars = sort_dict(chars_dict)
print("--------- Character Count -------")
for item in sorted_chars:
print(f"'{item['char']}: {item['num']}'")
if __name__ == "__main__":
main()