-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (42 loc) · 1.29 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
import re
import string
from pathlib import Path
def read_text_file(f: str | Path) -> str:
file = Path(f)
with file.open("r", encoding="utf-8") as stream:
return stream.read()
def split_words(w: str) -> list[str]:
words = re.split(r"\s+", w.strip())
return [word for word in words if word]
def total_words(w: str) -> int:
return len(split_words(w))
def count_words(w: str) -> dict[str, int]:
data = {}
for word in split_words(w.lower()):
if word not in data.keys():
data[word] = 0
elif word in data.keys():
data[word] += 1
return data
def count_letters(w: str) -> dict[str, int]:
data = {}
text = w.lower()
for letter in string.ascii_lowercase:
if letter not in data:
data[letter] = 0
for t in text:
if t == letter:
data[letter] += 1
return data
def main() -> str:
filepath = "books/frankenstein.txt"
text = read_text_file(filepath)
twords = total_words(text)
cwords = count_words(text)
cletters = count_letters(text)
print("Total words: ", twords)
print("Number of times 'monster' mentioned: ", cwords["monster"])
print("Frequency of letter occurence: ", cletters)
return text
if __name__ == "__main__":
main()