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']
>>> from nltk import word_tokenize, pos_tag
>>> sentence = "At eight o'clock on Thursday film morning word line test best beautiful Ram Aaron design"
>>> nouns = [token for token, pos in pos_tag(word_tokenize(sentence)) if pos.startswith('N')]
>>> nouns
['Thursday', 'film', 'morning', 'word', 'line', 'test', 'Ram', 'Aaron', 'design']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With