fix: update project

Changes:

- Renamed main.py to bookbot.py
- Added module docstring
- Updated function docstrings
- Some code refactoring
- Get the path to the filename from the first OS argument.
- Updated the documentation.
This commit is contained in:
Dan Anglin 2024-02-03 06:33:08 +00:00
parent 8efb073cc1
commit f179081e82
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 193 additions and 67 deletions

View file

@ -1 +1,68 @@
# bookbot
## Bookbot
### Summary
Bookbot reads a book from a text file and prints a simple book report to the standard output.
### Requirements
Python version 3.0 or higher.
### Quick guide
Clone this repository.
```sh
git clone https://github.com/dananglin/bookbot.git
cd bookbot
```
You can print the program's help message with the `-h` flag.
```sh
python bookbot.py -h
```
Download a book in text format (e.g. txt, markdown, etc).
As an example you can download the free ebook called `Frankenstein or The Modern Prometheus` from [here](https://raw.githubusercontent.com/asweigart/codebreaker/master/frankenstein.txt).
```sh
mkdir books
curl -L https://raw.githubusercontent.com/asweigart/codebreaker/master/frankenstein.txt -o books/frankenstein.txt
```
Run Bookbot to produce the book report.
```sh
$ python bookbot.py books/frankenstein.txt
--- Begin report of books/frankenstein.txt ---
77986 words found in the document
The character 'a' was found 26743 times
The character 'b' was found 5026 times
The character 'c' was found 9243 times
The character 'd' was found 16863 times
The character 'e' was found 46043 times
The character 'f' was found 8731 times
The character 'g' was found 5974 times
The character 'h' was found 19725 times
The character 'i' was found 24613 times
The character 'j' was found 504 times
The character 'k' was found 1755 times
The character 'l' was found 12739 times
The character 'm' was found 10604 times
The character 'n' was found 24367 times
The character 'o' was found 25225 times
The character 'p' was found 6121 times
The character 'q' was found 324 times
The character 'r' was found 20818 times
The character 's' was found 21155 times
The character 't' was found 30365 times
The character 'u' was found 10407 times
The character 'v' was found 3833 times
The character 'w' was found 7638 times
The character 'x' was found 677 times
The character 'y' was found 7914 times
The character 'z' was found 243 times
--- End report ---
```

125
bookbot.py Normal file
View file

@ -0,0 +1,125 @@
'''
Bookbot reads a book from a text file and prints out a simple report about it
to the standard output.
'''
import argparse
def main() -> None:
'''The entrypoint to the bookbot code.'''
parser = argparse.ArgumentParser(
prog='Bookbot',
description='Bookbot produces a simple book report.',
)
parser.add_argument(
'filename',
type=str,
help='the path to the text file.'
)
args = parser.parse_args()
text = read_file(args.filename)
report(args.filename, number_of_words(text), character_map(text))
def read_file(path: str) -> str:
'''
Opens a file from a given file path and returns the contents of the file.
Parameters:
path (str): The path to the text file.
Returns:
text (str): The contents of the text file.
'''
with open(path, mode='r', encoding='utf-8') as file:
text = file.read()
return text
def number_of_words(text: str) -> int:
'''
Returns the number of words in a given text string.
Parameters:
text (str): The string of text.
Returns:
num_words (int): The number of words in the text.
'''
num_words = len(text.split())
return num_words
def character_map(text: str) -> dict[str, int]:
'''
Returns a dictionary with the amount of times
a character appears in a given text.
Non-letter characters are ignored.
Parameters:
text (str): The string of text.
Returns:
output (dict[str, int]): A dictionary with the amount of times each
character appears in the text.
'''
output = {
'a': 0,
'b': 0,
'c': 0,
'd': 0,
'e': 0,
'f': 0,
'g': 0,
'h': 0,
'i': 0,
'j': 0,
'k': 0,
'l': 0,
'm': 0,
'n': 0,
'o': 0,
'p': 0,
'q': 0,
'r': 0,
's': 0,
't': 0,
'u': 0,
'v': 0,
'w': 0,
'x': 0,
'y': 0,
'z': 0,
}
for character in text:
if character.lower() in output:
output[character.lower()] += 1
return output
def report(path: str, total_words: int, characters: dict[str, int]) -> None:
'''
Prints a report about the given text.
Parameters:
path (str): The path to the text file.
total_words (int): The total amount of words found in the
text file.
characters: The dictionary with the amount of times
each character appears in the text.
Returns:
None
'''
print(f"--- Begin report of {path} ---")
print(f"{total_words} words found in the document\n")
for key, value in characters.items():
print(f"The character '{key}' was found {value} times")
print("--- End report ---")
if __name__ == "__main__":
main()

66
main.py
View file

@ -1,66 +0,0 @@
def main():
path = "books/frankenstein.txt"
with open(path) as file:
file_contents = file.read()
report(
path,
get_num_words(file_contents),
character_dict(file_contents)
)
def get_num_words(text):
'''get_num_words returns the number of words in a given text string'''
words = text.split()
return len(words)
def character_dict(text):
'''character_dict returns a dictionary with the amount of times
a character appears in a given text'''
output = {
'a': 0,
'b': 0,
'c': 0,
'd': 0,
'e': 0,
'f': 0,
'g': 0,
'h': 0,
'i': 0,
'j': 0,
'k': 0,
'l': 0,
'm': 0,
'n': 0,
'o': 0,
'p': 0,
'q': 0,
'r': 0,
's': 0,
't': 0,
'u': 0,
'v': 0,
'w': 0,
'x': 0,
'y': 0,
'z': 0,
}
for character in text:
if character.lower() in output:
output[character.lower()] += 1
return output
def report(path, total_words, characters):
'''report prints a report about the specified book'''
print(f"--- Begin report of {path} ---")
print(f"{total_words} words found in the document\n")
for key, value in characters.items():
print(f"The character '{key}' was found {value} times")
print("--- End report ---")
main()