feat: complete project

This commit is contained in:
Stevan Freeborn
2026-05-20 13:45:10 -05:00
parent a59e2f96aa
commit cc9ace6c23
6 changed files with 44941 additions and 2 deletions
File diff suppressed because it is too large Load Diff
+22314
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+41 -2
View File
@@ -1,8 +1,47 @@
"""Main entrypoint for bookbot""" """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()
def main(): def main():
"""Main method for bookbot""" """Main method for bookbot"""
print("Hello from bookbot!")
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 ===============")
if __name__ == "__main__": if __name__ == "__main__":
main() main()
+4
View File
@@ -5,3 +5,7 @@ description = "bookbot"
readme = "README.md" readme = "README.md"
requires-python = ">=3.12" requires-python = ">=3.12"
dependencies = [] dependencies = []
[tool.pyright]
include = ["."]
extraPaths = ["."]
+44
View File
@@ -0,0 +1,44 @@
"""Provides functions to report statistics on books"""
from typing import TypedDict
class CharacterCount(TypedDict):
"""Represents number of times a character is found in a book"""
char: str
num: int
def count_words_in_book(book_content: str) -> int:
"""Returns the count of words in the book"""
return len(book_content.split())
def count_characters_in_book(book_content: str) -> dict[str, int]:
"""Returns a dictionary of the number of times a character appears in the book"""
result = {}
for character in book_content:
normalized_character = character.lower()
if normalized_character in result:
result[normalized_character] += 1
continue
result[normalized_character] = 1
return result
def sort_character_counts(counts: dict[str, int]) -> list[CharacterCount]:
"""Returns a list of character counts in descending order"""
result: list[CharacterCount] = []
for entry in counts:
result.append({ "char": entry, "num": counts[entry] })
result.sort(reverse=True, key=lambda x: x["num"])
return result