【发布时间】:2022-01-09 16:22:43
【问题描述】:
我有以下定义以打印功能结尾的东西:
from nltk.corpus import words
nltk.download('words')
correct_spellings = words.words()
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from nltk.metrics.distance import edit_distance
def answer_nine(entries=['cormulent', 'incendenece', 'validrate']):
for entry in entries:
temp = [(jaccard_distance(set(ngrams(entry, 2)), set(ngrams(w, 2))),w) for w in correct_spellings if w[0]==entry[0]]
result = print(sorted(temp, key = lambda val:val[0])[0][1])
return result
answer_nine()
我已正确打印出三个结果,但我希望将它们列在一个列表中。我尝试以多种不同的方式将它们分配到一个列表中,但我总是收到以下错误消息:AttributeError: 'NoneType' object has no attribute 'append'。 我不明白为什么我的结果有如果 NoneType 有值,我在这里缺少什么?
ps.:如果我像这样删除打印功能:result = sorted(temp, key = lambda val:val[0])[0][1] 我只收到第三个单词,但至少它有字符串作为类型。
【问题讨论】:
标签: python nltk text-mining