Why this code is not working properly for keyword search?

239 views Asked by At

I have read the following code from kaggle exercises. The aim of the code for the function multi_word_search(documents1, keywords1) is to show the string indices in documents1 that contain certain words in keywords1. For example if

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

then the output of the function should be {'want': [0, 2], 'your': [1]} but unfortunately the output that I get after running the code is {'want': [0, 2]}. Where is the problem in the code. Any help in this regard will be much appreciated. Thanks in advance.

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

There are 0 answers