1

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)
wwii
  • 23,232
  • 7
  • 37
  • 77
Frank Moses
  • 127
  • 1
  • 5
  • 1
    Please fix the indentation for your functions. – wwii Nov 12 '18 at 01:05
  • @wwii thank you for pointing out the mistake. I think there was return statement which was misplaced so I have fixed it (I think there were a few others too which I have fixed) But even after that my code does not produce the desired output. – Frank Moses Nov 12 '18 at 01:08
  • 2
    I get your expected result, `{'want': [0, 2], 'your': [1]}`, with the code you posted. In your posted code, `keywords` contains `'your'` - BUT in your explanation of the problem you stated `keywords1` contains `'you'`, which *kewords* are you using? – wwii Nov 12 '18 at 01:14
  • @wwii i used 'your' while running my program. It was a typo in the post. Even with you it should show me ${''want": [0, 2], "you": [0, 2]}$. Why is it not working in my computer – Frank Moses Nov 12 '18 at 01:20
  • @wwii Do I need to import some liberaries? – Frank Moses Nov 12 '18 at 01:22
  • 1
    your code works fine. there are improvements you can make, but you will get your expected output – aydow Nov 12 '18 at 01:25
  • 1
    If you were trying to use libraries without importing them it would be getting `NameError`s – wwii Nov 12 '18 at 01:25
  • 1
    If you are curious, you can *watch* your code execute at http://pythontutor.com/ – wwii Nov 12 '18 at 01:26
  • 1
    Maybe save your file, reopen it, then execute it. How are you executing it? – wwii Nov 12 '18 at 01:28
  • I have installed python 3.5 version in my computer. After writing the code I am running it with "F5". So I think it should work. Thank you for introducing me to pythontutor.com. – Frank Moses Nov 12 '18 at 01:33
  • 1
    That is the way I am running it and it works. – wwii Nov 12 '18 at 01:34
  • 1
    I copy and pasted your posted code and it produces your desired result. – wwii Nov 12 '18 at 01:39
  • Ok I also got the desired result. – Frank Moses Nov 12 '18 at 01:40

0 Answers0