CS50 AI 项目 6b 问题:代码无法正确处理所有标记化

问题描述 投票:0回答:0

到目前为止,我已提交该项目 4 次,但总是被拒绝并显示相同的消息:

您的代码无法正确处理所有标记化,这可能会导致解析和解释错误。请务必查看规范的要求以进行适当的标记化。

我尝试过使用和不使用 strip,并对列表进行排序并反转它,并且不排序,这也是 tokenize 函数的要求:

tokenize 函数应接受文档(字符串)作为输入,并返回该文档中所有单词的列表(按顺序且小写)。
您应该使用 nltk 的

word_tokenize
函数来执行标记化。
返回列表中的所有单词都应小写。
过滤掉标点符号和停用词(不太可能对查询有用的常用词)。标点符号定义为 string.punctuation 中的任何字符(导入字符串后)。停用词定义为
nltk.corpus.stopwords.words("english")
中的任何单词。
如果一个单词在文档中出现多次,它也应该在返回的列表中出现多次(除非它被过滤掉)。

这是我的代码:

def tokenize(document):
    wt = nltk.word_tokenize(document.lower())
    s = nltk.corpus.stopwords.words("english")
    l = []
    for i in wt:
        i = i.strip(string.punctuation)
        if i not in string.punctuation and i not in s and i:
            l.append(i)
    return sorted(l,reverse=True)
python artificial-intelligence cs50 tokenize
© www.soinside.com 2019 - 2024. All rights reserved.