-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (37 loc) · 1002 Bytes
/
main.py
File metadata and controls
41 lines (37 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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()