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