avp26/u4/word_count.py

13 lines
341 B
Python

import string
from collections import Counter
def count_words(sentence):
text = sentence.lower()
for mark in string.punctuation.replace("'", ""):
text = text.replace(mark, " ")
words = []
for word in text.split():
word = word.strip("'")
if word:
words.append(word)
return dict(Counter(words))