From efeee444775ee16739d31ff3feaa3cf63ffc711d Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Sat, 23 Mar 2024 15:13:49 +1100 Subject: [PATCH 1/2] Lowercase function names --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 8f90e32..5e6aa82 100644 --- a/main.py +++ b/main.py @@ -23,7 +23,7 @@ def get_book_text(path): def word_count(document): return len(document.split()) -def Letter_frequency(document): +def letter_frequency(document): letter_count = {} for d in document.lower(): if d in letter_count: @@ -37,7 +37,7 @@ def sort_on(d): def sort_letter_frequency(document): - letter_count = Letter_frequency(document) + letter_count = letter_frequency(document) only_alphabets = {} sorted_list = [] for item in letter_count.items(): From d6909ebcb05d94f6b2508a4cae1a2bdba36bc3ef Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Sat, 23 Mar 2024 15:29:29 +1100 Subject: [PATCH 2/2] Linter changes --- main.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 5e6aa82..095902b 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,14 @@ +"""The Bookbot program""" + def main(): + """Generates the book report""" history = "/home/chrisstirrup/workspace/github.com/ChrisStirrup/bookbot/books/History.txt" shelly = "/home/chrisstirrup/workspace/github.com/ChrisStirrup/bookbot/books/frankenstein.txt" book_path = input('Please enter a path to your .txt document: ') - if book_path == 'shelly' or book_path == '': + if book_path in ('shelly', ''): book_path = shelly if book_path == 'history': book_path = history - - text = get_book_text(book_path) print("Book Report") print(f"There are {word_count(text)} words found in this document") @@ -15,38 +16,40 @@ def main(): print(f"The '{thing['letter']}' letter was found '{thing['frequency']}' times") print("Thus endeth the report.") - def get_book_text(path): - with open(path) as f: + """Returns all the text from the given path""" + with open(path, encoding="utf-8") as f: return f.read() - + def word_count(document): + """Returns the count of words in the given string""" return len(document.split()) def letter_frequency(document): + """Returns a set containing the count of each word in the given string""" letter_count = {} for d in document.lower(): - if d in letter_count: + if d in letter_count: letter_count[d] += 1 - else: + else: letter_count[d] = 1 - return letter_count + return letter_count def sort_on(d): + """Sorting function on the key 'frequency'""" return d["frequency"] - def sort_letter_frequency(document): + """Returns a sorted list containing the frequency of each letter in the given string""" letter_count = letter_frequency(document) only_alphabets = {} sorted_list = [] for item in letter_count.items(): if item[0].isalpha(): only_alphabets[item[0]] = item[1] - for letter in only_alphabets: - sorted_list.append({'letter': letter, "frequency":only_alphabets[letter]}) + for letter, frequency in only_alphabets.items(): + sorted_list.append({'letter': letter, "frequency": frequency}) sorted_list.sort(reverse=True, key=sort_on) return sorted_list - main()