feat: complete project
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+22314
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -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():
|
||||
"""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__":
|
||||
main()
|
||||
|
||||
@@ -5,3 +5,7 @@ description = "bookbot"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = []
|
||||
|
||||
[tool.pyright]
|
||||
include = ["."]
|
||||
extraPaths = ["."]
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user