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

This commit is contained in:
Arina Dohadailo 2026-10-06 12:19:50 +00:00
parent ab271ef12f
commit c2bab639f4

View File

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