Files

48 lines
1.2 KiB
Python
Raw Permalink Normal View History

2026-05-20 13:45:10 -05:00
"""Entrypoint for bookbot"""
import sys
from stats import count_characters_in_book, count_words_in_book, sort_character_counts
def get_book_text(file_path: str) -> str:
"""Retrieves the contents of the given file"""
with open(file_path, mode="r", encoding="utf-8") as f:
return f.read()
2026-05-20 11:41:59 -05:00
def main():
"""Main method for bookbot"""
2026-05-20 13:45:10 -05:00
if len(sys.argv) != 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
book_path = sys.argv[1]
book_text = get_book_text(book_path)
word_count = count_words_in_book(book_text)
character_counts = count_characters_in_book(book_text)
sorted_counts = sort_character_counts(character_counts)
print("============ BOOKBOT ============")
print(f"Analyzing book found at {book_path}...")
print("----------- Word Count ----------")
print(f"Found {word_count} total words")
print("--------- Character Count -------")
for item in sorted_counts:
char = item["char"]
if not char.isalpha():
continue
print(f"{char}: {item["num"]}")
print("============= END ===============")
2026-05-20 11:41:59 -05:00
if __name__ == "__main__":
main()