avp26/u4/word_count.py

16 lines
328 B
Python

import string
def count_words(sentence):
counts = {}
text = sentence.lower()
for mark in string.punctuation.replace("'", ""):
text = text.replace(mark, " ")
for word in text.split():
word = word.strip("'")
if word:
counts[word] = counts.get(word, 0) + 1
return counts