Files
bookbot/main.py

34 lines
894 B
Python
Raw Permalink Normal View History

2025-06-27 15:51:22 +02:00
from stats import count_words
from stats import count_characters
from stats import sort_by_count
2025-06-27 15:55:25 +02:00
import sys
2025-06-27 15:51:22 +02:00
def get_book_text(filepath):
with open(filepath) as f:
return f.read()
def main():
2025-06-27 15:55:25 +02:00
if len(sys.argv) < 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
read_file = get_book_text(sys.argv[1])
2025-06-27 15:51:22 +02:00
counted = count_words(read_file)
counted_chars = count_characters(read_file)
print("============ BOOKBOT ============")
print("Analyzing book found at books/frankenstein.txt...\n----------- Word Count ----------")
print(f"Found {counted} total words")
print("--------- Character Count -------")
sorted = sort_by_count(counted_chars)
for item in sorted:
if item["char"].isalpha():
print(f"{item["char"]}: {item["num"]}")
print("============= END ===============")
main()