diff --git a/u4/word_count.py b/u4/word_count.py index db43b07..d173eee 100644 --- a/u4/word_count.py +++ b/u4/word_count.py @@ -1,15 +1,11 @@ -import string +import re +from collections import Counter + def word_count(text): - words = [] - for word in text.lower().split(): - cleaned_word = word.strip(string.punctuation) - if cleaned_word: - words.append(cleaned_word) + pattern = r"\w+(?:'\w+)?" + words = re.findall(pattern, text.lower()) - result = {} - for word in words: - result[word] = result.get(word,0) +1 - - return result + return dict(Counter(words)) + \ No newline at end of file