为什么此代码对于关键字搜索无法正常工作?

时间:2018-11-12 00:43:51

标签: python function keyword-search wordsearch

我已经从kaggle练习中阅读了以下代码。函数multi_word_search(documents1, keywords1)的代码目的是显示document1中包含关键字1中某些单词的字符串索引。例如,如果

documents1 = ['what do you want to do', 'what is your research goal', 'what do you want to accomplish in life']
keywords1 = ['want', 'your']

然后该函数的输出应为{'want': [0, 2], 'your': [1]},但不幸的是,运行代码后我得到的输出为{'want': [0, 2]}。代码中的问题在哪里。在这方面的任何帮助将不胜感激。预先感谢。

def word_search(documents, keyword):
    indices=[]
    for i, doc in enumerate(documents):
        tokens=doc.split()
        normalized=[token.rstrip('.,').lower() for token in tokens]
        if keyword.lower() in normalized:
            indices.append(i)
    return indices
def multi_word_search(documents1, keywords1):
    keyword_to_indices={}
    for keyword2 in keywords1:
        keyword_to_indices[keyword2]=word_search(documents1, keyword2)
    return keyword_to_indices
s=['what do you want to do', 'what is your research goal', 'what do you want to accomplish in life']
keywords=['want', 'your']
r=multi_word_search(s,keywords)
print(r)

0 个答案:

没有答案