I am fairly new to python. I am not able to figure out the bug. I want to extract nouns using NLTK.
I have written the following code:
import nltk
sentence = "At eight o'clock on Thursday film morning word line test best beautiful Ram Aaron design"
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
length = len(tagged) - 1
a = list()
for i in (0,length):
log = (tagged[i][1][0] == 'N')
if log == True:
a.append(tagged[i][0])
When I run this, 'a' only has one element
a
['detail']
I do not understand why?
When I do it without for loop, that is running
log = (tagged[i][1][0] == 'N')
if log == True:
a.append(tagged[i][0])
by change value of 'i' manually from 0 to 'length', i get the output perfectly, but with for loop it only returns the end element. Can someone tell me what is wrong happening with for loop.
'a' should be as follows after the code
['Thursday', 'film', 'morning', 'word', 'line', 'test', 'Ram' 'Aaron', 'design']
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…