what is wrong with this code of nltk python -


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 = []     tokens in tokens:         if tokens in words_to_match:             matching_tokens.append(tokens)             return matching_tokens 

i not able understand why code not able return list, returning 1 token/word, after execution

your return statement in loop, means function returns tokens in words_to_match true. correct problem, move return out of loop, this: (for simplicity removed part of opening file. it's test. you'll have let method read file)

import nltk  def ethos():     raw = 'i love made products'     tokens = nltk.word_tokenize(raw)     words_to_match = ['love' , 'good' , 'excellent' , 'perfect' , 'brilliant' , 'easy' , 'well' , 'made' , 'impressive' , 'great']     matching_tokens = []     tokens in tokens:         if tokens in words_to_match:             matching_tokens.append(tokens)     return matching_tokens  print ethos() 

the result is

['love', 'well', 'made'] 

it seems work


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -