Обновить u4/word_count.py
This commit is contained in:
parent
ab271ef12f
commit
c2bab639f4
@ -1,8 +1,13 @@
|
|||||||
|
import string
|
||||||
|
from collections import Counter
|
||||||
def count_words(sentence):
|
def count_words(sentence):
|
||||||
cleaned = "".join(c if c.isalnum() or c == "'" else " " for c in sentence.lower())
|
text = sentence.lower()
|
||||||
counts = {}
|
for mark in string.punctuation.replace("'", ""):
|
||||||
for word in cleaned.split():
|
text = text.replace(mark, " ")
|
||||||
|
words = []
|
||||||
|
for word in text.split():
|
||||||
word = word.strip("'")
|
word = word.strip("'")
|
||||||
if word:
|
if word:
|
||||||
counts[word] = counts.get(word, 0) + 1
|
words.append(word)
|
||||||
return counts
|
|
||||||
|
return dict(Counter(words))
|
||||||
Loading…
Reference in New Issue
Block a user