书生大模型实训营 入门岛 关卡任务2
编写一个wordcount 函数
import re
from collections import defaultdict
def word_count(text):
# 移除标点符号并转换为小写
cleaned_text = re.sub(r'[^\w\s]', '', text).lower()
# 分割文本到单词列表
words = cleaned_text.split()
# 使用defaultdict初始化计数字典
word_counts = defaultdict(int)
# 遍历单词列表并计数
for word in words:
word_counts[word] += 1
return dict(word_counts)
# 测试函数
text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""
result = word_count(text)
print(result)
远程服务器debug
安装扩展
进行调试