Files
bookbot/stats.py
2025-02-22 19:49:06 +02:00

27 lines
562 B
Python

def get_num_words(text):
words = text.split()
return len(words)
def get_chars_dict(text):
chars = {}
for c in text:
lowered = c.lower()
if lowered in chars:
chars[lowered] += 1
else:
chars[lowered] = 1
return chars
def sort_on(d):
return d["num"]
def chars_dict_to_sorted_list(num_chars_dict):
sorted_list = []
for ch in num_chars_dict:
sorted_list.append({"char": ch, "num": num_chars_dict[ch]})
sorted_list.sort(reverse=True, key=sort_on)
return sorted_list