-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (40 loc) · 1.66 KB
/
Copy pathmain.py
File metadata and controls
51 lines (40 loc) · 1.66 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
from stats import count_words
import sys
def book_to_string(path_to_file):
with open(path_to_file) as f:
file_contents = f.read()
return file_contents
def count_words(text):
words = text.split()
return len(words)
def count_characters(text):
lowered_text = text.lower()
character_count = {}
for char in lowered_text:
character_count[char] = character_count.get(char, 0) + 1
return character_count
def print_character_count_report(character_count_dictionary):
def sort_on(dict):
return dict["count"]
list_of_character_counts = []
for character in character_count_dictionary:
list_of_character_counts.append({"character": character, "count": character_count_dictionary[character]})
list_of_character_counts.sort(reverse=True, key=sort_on)
for dictionary in list_of_character_counts:
if (dictionary['character']).isalpha():
print(f"'{dictionary['character']}: {dictionary['count']}'")
def main():
try:
if len(sys.argv) < 2:
print("Did you enter the command correctly? Usage: python3 main.py <path_to_book>")
whole_book_string = book_to_string(sys.argv[1])
word_count = count_words(whole_book_string)
character_count_dictionary = count_characters(whole_book_string)
print(f"--- Begin report of {sys.argv[1]} ---")
print(f"{word_count} words found in the document")
print_character_count_report(character_count_dictionary)
print("--- End report ---")
except FileNotFoundError as error:
print(f"{error}\nIs your file name entered correctly?")
if __name__ == '__main__':
main()