1

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

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)
4

0 回答 0