Named Entity Recognition for NLTK in Python. Identifying the NE
Asked Answered
H

6

20

I need to classify words into their parts of speech. Like a verb, a noun, an adverb etc.. I used the

nltk.word_tokenize() #to identify word in a sentence 
nltk.pos_tag()       #to identify the parts of speech
nltk.ne_chunk()      #to identify Named entities. 

The out put of this is a tree. Eg

>>> sentence = "I am Jhon from America"
>>> sent1 = nltk.word_tokenize(sentence )
>>> sent2 = nltk.pos_tag(sent1)
>>> sent3 =  nltk.ne_chunk(sent2, binary=True)
>>> sent3
Tree('S', [('I', 'PRP'), ('am', 'VBP'), Tree('NE', [('Jhon', 'NNP')]), ('from', 'IN'), Tree('NE', [('America', 'NNP')])])

When accessing the element in this tree, i did it as follows:

>>> sent3[0]
('I', 'PRP')
>>> sent3[0][0]
'I'
>>> sent3[0][1]
'PRP'

But when accessing a Named Entity:

>>> sent3[2]
Tree('NE', [('Jhon', 'NNP')])
>>> sent3[2][0]
('Jhon', 'NNP')
>>> sent3[2][1]    
Traceback (most recent call last):
  File "<pyshell#121>", line 1, in <module>
    sent3[2][1]
  File "C:\Python26\lib\site-packages\nltk\tree.py", line 139, in __getitem__
    return list.__getitem__(self, index)
IndexError: list index out of range

I got the above error.

What i want is to get the output as 'NE' similar to the previous 'PRP' so i cant identify which word is a Named Entity. Is there any way of doing this with NLTK in python?? If so please post the command. Or is there a function in the tree library to do this? I need the node value 'NE'

Heeling answered 18/4, 2011 at 20:14 Comment(0)
H
14

This answer may be off base, and in which case I'll delete it, as I don't have NLTK installed here to try it, but I think you can just do:

   >>> sent3[2].node
   'NE'

sent3[2][0] returns the first child of the tree, not the node itself

Edit: I tried this when I got home, and it does indeed work.

Humphreys answered 18/4, 2011 at 20:58 Comment(3)
Before looking at the node attribute, you'll want to check if isinstance(sent3[2], Tree) (after doing from nltk.tree import Tree).Cattycornered
@Cattycornered Thanks mate, Really helpful. The next problem i faced was on how to know if an element is a tree or not. As i needed to iterate through the elements using a for loop. The if isinstance(sent3[2], Tree) is what i have been looking for all this while. Thanks again.Heeling
in current version (3.1) node is replaced by label()Encampment
D
4

Below is my code:

chunks = ne_chunk(postags, binary=True)
for c in chunks:
  if hasattr(c, 'node'):
    myNE.append(' '.join(i[0] for i in c.leaves()))
Dropsical answered 15/2, 2013 at 5:11 Comment(0)
E
2

This will work

for sent in chunked_sentences:
  for chunk in sent:
    if hasattr(chunk, "label"):
        print(chunk.label())
Eliseelisee answered 28/8, 2017 at 19:11 Comment(0)
C
1

I agree with bdk

sent3[2].node

O/P - 'NE'

I think there is no function in nltk to do it.Above solution will work but for reference you can check here

for looping problem you can do :-

 for i in range(len(sent3)):
     if "NE" in str(sent3[i]):
          print sent3[i].node

I have executed this in nltk and it works fine..

Cassandracassandre answered 9/10, 2013 at 11:18 Comment(0)
C
1

Now sent3[2].node is outdated.

use sent3[2].label() instead

Continuative answered 11/4, 2017 at 17:34 Comment(0)
K
0

You can treat the sentence as a tree and loop through it.

entities = nltk.ne_chunk(text)
for c in entities:
    # Is an entity
    if isinstance(elem, nltk.Tree):
        print('elem: ', elem.leaves(), elem.label())
    else:
       # Not an entity
Karlow answered 3/12, 2021 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.