-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_letter_counter.py
More file actions
35 lines (29 loc) · 1.21 KB
/
text_letter_counter.py
File metadata and controls
35 lines (29 loc) · 1.21 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
# Repeated words and letter counter and display top num letters and word their counts and remove punctuation
def frequent_count_text(text, num=3):
word_count = {}
text_count = {}
for i in text.split():
if i.lower() in word_count:
word_count[i.lower()] += 1
else:
word_count[i.lower()] = 1
for j in i:
if j.lower() in text_count:
text_count[j.lower()] += 1
else:
text_count[j.lower()] = 1
top_words = sorted(word_count.items(), key=lambda kv: (-kv[1], kv[0]))[:num]
top_letters = sorted(text_count.items(), key=lambda kv: (-kv[1], kv[0]))[:num]
# 4) Format outputs and return (letters first, words second)
letter_lines = [f"Top {num} letter:"]
letter_lines += [f"{ch}: {cnt}" for ch, cnt in top_letters]
word_lines = [f"Top {num} word:
"]
word_lines += [f"{w}: {cnt}" for w, cnt in top_words]
letter_output = "\n".join(letter_lines)
word_output = "\n".join(word_lines)
return f"{letter_output}\n\n{word_output}"
# Example usage:
input_text = "This is a sample text with several words this is a test text"
result = frequent_count_text(input_text, 3)
print(result)