def ethos(file):
f = open(file)
raw = f.read()
tokens = nltk.word_tokenize(raw)
words_to_match = ['love' , 'good' , 'excellent' , 'perfect' , 'brilliant' , 'easy' , 'well' , 'made' , 'impressive' , 'great']
matching_tokens = []
for tokens in tokens:
if tokens in words_to_match:
matching_tokens.append(tokens)
return matching_tokens
我无法理解为什么这段代码无法返回列表,只是返回一个令牌/字,执行后
答案 0 :(得分:1)
你的return
语句在循环中,这意味着只要tokens in words_to_match
为真,函数就会立即返回。要纠正这个问题,只需将return
移出循环,如下所示:(为简单起见,我删除了打开文件的部分。这只是一个测试。你必须让你的方法读取该文件)
import nltk
def ethos():
raw = 'i love well made products'
tokens = nltk.word_tokenize(raw)
words_to_match = ['love' , 'good' , 'excellent' , 'perfect' , 'brilliant' , 'easy' , 'well' , 'made' , 'impressive' , 'great']
matching_tokens = []
for tokens in tokens:
if tokens in words_to_match:
matching_tokens.append(tokens)
return matching_tokens
print ethos()
结果是
['love', 'well', 'made']
似乎有效