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
Post a Comment