import re
from collections import defaultdict
def wordcount(text):
# 去掉标点符号
text = re.sub(r'[^\w\s]', '', text)
# 转换成小写
text = text.lower()
# 拆分成单词
words = text.split()
# 使用defaultdict统计单词出现次数
word_count = defaultdict(int)
for word in words:
word_count[word] += 1
return dict(word_count)
# 示例输入
text = """
Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!
"""
# 调用函数并输出结果
result = wordcount(text)
print(result)