avp26/u4/word_count.py
2026-10-06 12:34:11 +00:00

13 lines
279 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from collections import Counter
def count_words(text):
text = text.replace("_", " ")
text = text.replace("’", "'")
pattern = r"[^\W_]+(?:'[^\W_]+)?"
words = re.findall(pattern, text.lower())
return dict(Counter(words))
word_count = count_words