avp26/u4/word_count.py

13 lines
266 B
Python

import re
def count_words(text):
words = re.findall(r"[^\W_]+(?:'[^\W_]+)?", text.lower(), re.UNICODE)
result = {}
for word in words:
if word in result:
result[word] += 1
else:
result[word] = 1
return result