-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (42 loc) · 1.73 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
def main():
file_path = "books/frankenstein.txt"
text = get_text(file_path)
# Count the number of words and print the result
word_count = count_words(text)
# print(f"\n{word_count} words found in the document.")
# Count character frequencies and print the result
char_freq = character_frequencies(text)
# print("\nCharacter frequencies:")
# print(char_freq)
# Generate the report
generate_report(file_path, word_count, char_freq)
def generate_report(file_path, word_count, char_freq):
# Generates a formatted report
print(f"--- Begin report of {file_path} ---")
print(f"{word_count} words found in the document\n")
# Convert the dictionary to a list of tuples and sort by frequency
sorted_char_freq = sorted(char_freq.items(), key=lambda item: item[1], reverse=True)
# Print character frequencies in descending order
for char, freq in sorted_char_freq:
print(f"The '{char}' character was found {freq} times")
print("--- End report ---")
def character_frequencies(text):
# Counts the frequency of each character in the text
text = text.lower() # Convert the text to lowercase
frequency = {} # Initialize an empty dictionary
for char in text:
if char.isalpha(): # Count only alphabetic characters
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
return frequency
def count_words(text):
# Counts the number of words in the given text
words = text.split()
return len(words)
def get_text(file_path):
## Open the file and read its contents
with open(file_path) as f:
return f.read()
main()