-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (34 loc) · 1.11 KB
/
main.py
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
42
43
44
45
46
47
48
def text_stats_report(word_count, char_freq):
print("--- Begin report of books/frankenstein.txt ---")
print(f"{word_count} words found in the document")
print()
# sort the dictionary
char_freq_dict = {
char: count
for char, count in sorted(
char_freq.items(), key=lambda item: item[1], reverse=True
)
}
for char, freq in char_freq_dict.items():
print(f"The '{char}' was found {freq} times")
print("--- End report ---")
def count_characters(text):
char_dict = {}
for char in text:
if char.isalpha():
char = char.lower()
if char in char_dict.keys():
char_dict[char] += 1
else:
char_dict[char] = 1
return char_dict
def split_text_into_list(text):
return text.split()
def text_word_count(text):
return len(split_text_into_list(text))
def main():
with open("books/frankenstein.txt") as f:
file_contents = f.read()
text_stats_report(text_word_count(file_contents), count_characters(file_contents))
if __name__ == "__main__":
main()