原文链接Tutorial/docs/L0/Python at camp3 · InternLM/Tutorial (github.com)
在完成此关卡前请完成ssh连接
可以参考我之前发的链接书生大模型第三期Linux+InternStudio 关卡-CSDN博客
首先创建python文件
然后完成任务所需的代码
import re
def wordcount(text):
# 将文本中的标点符号替换为空格
text = re.sub(r'[^\w\s]', ' ', text)
# 将文本转换为小写
text = text.lower()
# 分割文本为单词列表
words = text.split()
# 创建字典来存储单词计数
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
# 测试文本
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.
"""
# 调用函数并打印结果
print(wordcount(text))