Aktualizovat u4/word_count.py

This commit is contained in:
Oleksandr Mateshchuk 2026-10-06 20:50:59 +00:00
parent 01ecb88aa5
commit a357fd6df5

View File

@ -1,13 +1,14 @@
import re
from collections import Counter
text = input()
words = re.findall(r"[^\W_]+(?:'[^\W_]+)?", text.lower(), re.UNICODE)
counts = {}
def count_words(text):
text = text.replace("_", " ")
text = text.replace("’", "'")
pattern = r"[^\W_]+(?:'[^\W_]+)?"
words = re.findall(pattern, text.lower())
return dict(Counter(words))
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
word_count = count_words
for word, count in sorted(counts.items()):
print(f"{word}: {count}")