Обновить u4/word_count.py

This commit is contained in:
Arina Dohadailo 2026-10-06 12:21:21 +00:00
parent c2bab639f4
commit dab28d5075

View File

@ -1,13 +1,16 @@
import string import string
from collections import Counter
def count_words(sentence): def count_words(sentence):
counts = {}
text = sentence.lower() text = sentence.lower()
for mark in string.punctuation.replace("'", ""): for mark in string.punctuation.replace("'", ""):
text = text.replace(mark, " ") text = text.replace(mark, " ")
words = []
for word in text.split(): for word in text.split():
word = word.strip("'") word = word.strip("'")
if word: if word:
words.append(word) counts[word] = counts.get(word, 0) + 1
return dict(Counter(words)) return counts