Skip to content

Commit

Permalink
little project
Browse files Browse the repository at this point in the history
  • Loading branch information
mychamli committed Oct 5, 2024
1 parent 381c455 commit e85ed60
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def main():
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
print(text)
print(get_word_count(text))
dic = get_char_frequency(text)
print("--- Begin report of books/frankenstein.txt ---")
print(f" {get_word_count(text)} words found in the document")
dic2 = [{
'char' : c , 'count': dic[c]
}
for c in dic
]
def sort_on(dict):
return dict['count']
dic2.sort(reverse=True,key=sort_on)
for d in dic2:
char = d['char']
count = d['count']
if char.isalpha():
print(f"The {char} character was found {count} times")
print("--- End report ---")
def get_word_count(text):
return len(text.split())

def get_char_frequency(text):
text = text.lower()
dic = {}
for c in text:
if c in dic:
dic[c]+=1
else:
dic[c] = 1
return dic

def get_book_text(path):
with open(path) as f:
return f.read()


main()

0 comments on commit e85ed60

Please sign in to comment.