-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (91 loc) · 2.43 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
def main():
path_to_file = "books/frankenstein.txt"
text = get_book_text(path_to_file)
words = word_counter(text)
character_dict = character_count(text)
characters_sorted_list = sort_characters(character_dict)
# print(f"The text of to book located at {
# path_to_file} is below: \n \n \n {text}")
print(f"--- Begin report of {path_to_file} ---")
print(f"{words} found in this book.")
# print(f"The count of all characters can be seen below \n {character_dict}")
for item in characters_sorted_list:
if not item["character"].isalpha():
continue
print(f"The '{item['character']}' character was found {
item['number']} times")
def get_book_text(path):
with open(path) as f:
return f.read()
def word_counter(text):
words = text.split()
return len(words)
def character_count(text):
charcater_dict = {}
for i in text:
lowercase = i.lower()
if lowercase in charcater_dict:
charcater_dict[lowercase] += 1
else:
charcater_dict[lowercase] = 1
return charcater_dict
def sort_dict(dic):
return dic["number"]
def sort_characters(character_dict):
sorted_list = []
for character in character_dict:
sorted_list.append(
{"character": character, "number": character_dict[character]})
sorted_list.sort(reverse=True, key=sort_dict)
return sorted_list
main()
# print("hello world")
#
#
# def main():
# with open("./books/frankenstein.txt") as f:
# file_contents = f.read()
# print(file_contents)
#
#
# # main()
#
#
# def word_count():
# with open("./books/frankenstein.txt") as f:
# file_contents = f.read()
# words = file_contents.split()
# print(f"{len(words)} words found in the document")
# return len(words)
#
#
# word_count()
#
#
# character_count = {}
#
#
# def word_counter():
# with open("./books/frankenstein.txt") as f:
# file_contents = f.read()
# for letter in file_contents.lower():
# if letter in character_count:
# character_count[letter] += 1
# else:
# character_count[letter] = 1
#
# # print(character_count)
#
#
# word_counter()
#
# value_list = []
#
#
# def report():
# for k in character_count:
# if k.isalpha():
# print(f"The '{k}' charaster was found {character_count[k]} times")
#
#
# report()