Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract nouns using NLTK pos_tag()?

Tags:

python

nlp

nltk

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']
like image 725
user3710832 Avatar asked Dec 01 '22 19:12

user3710832


1 Answers

>>> 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']
like image 86
alvas Avatar answered Dec 09 '22 20:12

alvas