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