From 16c1c1f5b1533b40658fa325e1f5e679c8d6bc3a Mon Sep 17 00:00:00 2001 From: htom Date: Fri, 27 Jun 2025 15:51:22 +0200 Subject: [PATCH] project working for hard coded book --- .gitignore | 2 ++ __init.py__ | 0 __pycache__/stats.cpython-313.pyc | Bin 0 -> 1166 bytes main.py | 27 +++++++++++++++++++++++++++ stats.py | 26 ++++++++++++++++++++++++++ 5 files changed, 55 insertions(+) create mode 100644 .gitignore create mode 100644 __init.py__ create mode 100644 __pycache__/stats.cpython-313.pyc create mode 100644 main.py create mode 100644 stats.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5ac87a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +books diff --git a/__init.py__ b/__init.py__ new file mode 100644 index 0000000..e69de29 diff --git a/__pycache__/stats.cpython-313.pyc b/__pycache__/stats.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d5fc77952562a1bdd3b94b8bdcc3718e80667023 GIT binary patch literal 1166 zcmaJ=&rcIU6n?Y2-7d6?6#RjTaZOON8mR|}0l|c%A=Ly-SQ|-5Bul#yLc3dMw;;qr zJxS9jHKvh+2RwTBr2hoO#5AJ^6HeZM{sX?5wg#&4CG&RX&6_v#ec#)@xaT)6V7UU(er{#INdKiAgMmj-b) zJphm*vm8U4r6h~oA}#I=m>va)fPvY_qcro2LpBwrWR}wb3uV1EhcacwS@)S*$Y<=p zF=fi{Vg7abvUKlN;%8LfN|urpdz(~JuOA$CKv&N}6ry@DNi{5Y zf_n)Ag8L{=s>q{7_$vh_CcA~)N9+<7S`M6?-{^|Qlf%A8g9Xo}XED1QhtCTXp%&?` zoGFd(D(C*bi(?gc<#AGlIB;E2xAR;#WSvvkW^+cWsnU>ijQfi%?*G;2mNd_)6K2Vy zQd1*^_qxJ4ro1yB;wns?Rb<;?G74qNOttfQ$IUQ>gA>RwsGxW@BUvg?p3qGfBrL8k z2>s|~EXzy#yvvXZt+P0}FLcs6wzQJA9ou>OVQ?>Y{hM~=Gy_l*DzkgJm z+EcEgqD_@%Ohx=kq7}qjM16W1YYS={I%wlLFH^`mcd3CJ;=e;W;Xo#Y97%*+I#N4` Pe$+=uUtJ9oT^#)l`~ua; literal 0 HcmV?d00001 diff --git a/main.py b/main.py new file mode 100644 index 0000000..fc3f328 --- /dev/null +++ b/main.py @@ -0,0 +1,27 @@ +from stats import count_words +from stats import count_characters +from stats import sort_by_count + +def get_book_text(filepath): + with open(filepath) as f: + return f.read() + +def main(): + read_file = get_book_text("books/frankenstein.txt") + counted = count_words(read_file) + counted_chars = count_characters(read_file) + + + print("============ BOOKBOT ============") + print("Analyzing book found at books/frankenstein.txt...\n----------- Word Count ----------") + print(f"Found {counted} total words") + print("--------- Character Count -------") + + sorted = sort_by_count(counted_chars) + for item in sorted: + if item["char"].isalpha(): + print(f"{item["char"]}: {item["num"]}") + + print("============= END ===============") + +main() diff --git a/stats.py b/stats.py new file mode 100644 index 0000000..8230a1b --- /dev/null +++ b/stats.py @@ -0,0 +1,26 @@ +def count_words(text): + return len(text.split()) + +def count_characters(text): + t = dict() + text = text.lower() + for c in text: + if c in t.keys(): + t[c] += 1 + else: + t.update({c:1}) + + return t + + +def sort_on(items): + return items["num"] + +def sort_by_count(data): + l = [] + + for c in data: + l.append({"char":c, "num":data[c]}) + + l.sort(reverse=True, key=sort_on) + return l