Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (47 loc) · 1.67 KB
/
Copy pathmain.py
File metadata and controls
65 lines (47 loc) · 1.67 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def main():
print("--- Begin report of books/frankenstein.txt ---")
book_path = "books/frankenstein.txt"
text = get_book_text(book_path)
# Get number of words.
num_words = get_num_words(text)
print(f"{num_words} words found in the document.")
print("")
# Get the total number of each single chars.
num_chars = get_count_chars(text)
list_of_dict = convert_dict_to_a_list_of_dict(num_chars)
sorted_list = statistics_about_each_word(list_of_dict)
for item in sorted_list:
# Check if the key is a character.
if item['key'].isalpha() is True:
print(f"The '{item['key']}' character was found {item['value']} times.")
def statistics_about_each_word(list):
return sorted(list, key=lambda x: x.get('value'), reverse=True)
def convert_dict_to_a_list_of_dict(dict_of_chars):
# return [{key: value} for key, value in dict_of_chars.items()]
new_list = []
for k, v in dict_of_chars.items():
new_list.append({'key': k, 'value': v})
return new_list
def get_count_chars(text):
# Create an empty dict.
chars_dict = {}
# Iterate each char in the string.
for char in text:
# Convert the char in lowercase.
char = char.lower()
# Check if char already exists.
if char in chars_dict:
# If it does increment by 1.
chars_dict[char] += 1
else:
# If it does not initialize it to 1.
chars_dict[char] = 1
return chars_dict
def get_num_words(text):
words = text.split()
return len(words)
def get_book_text(path):
with open(path) as f:
return f.read()
if __name__ == "__main__":
main()