project working for hard coded book
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
books
|
||||
0
__init.py__
Normal file
0
__init.py__
Normal file
BIN
__pycache__/stats.cpython-313.pyc
Normal file
BIN
__pycache__/stats.cpython-313.pyc
Normal file
Binary file not shown.
27
main.py
Normal file
27
main.py
Normal file
@@ -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()
|
||||
26
stats.py
Normal file
26
stats.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user