-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
29 lines (24 loc) · 840 Bytes
/
stats.py
File metadata and controls
29 lines (24 loc) · 840 Bytes
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
def get_book_text(book_path):
with open (book_path) as f:
book_text = f.read()
return book_text
def book_word_counter(book_path):
words = get_book_text(book_path).split()
return len(words)
def character_counter(book_path):
char_counts = {}
book = get_book_text(book_path).lower()
for character in book:
lowercase_char = character.lower()
if character in char_counts:
char_counts[lowercase_char] += 1
else:
char_counts[lowercase_char] = 1
return char_counts
def sort_num(chars):
return chars["num"]
def sorting_function(book):
dictionary = character_counter(book)
sorting_list = [{"char": char, "num": num} for char, num in dictionary.items() if char.isalpha()]
sorting_list.sort(reverse=True, key=sort_num)
return sorting_list