2011-10-01 16 views
11

Używam NLTK RegexpParser do wyodrębniania grup rzeczowników i grup verbgroups z tagowanych tokenów.NLTK Chunking i chodzenie w drzewku wyników

Jak przejść drzewo wynikowe, aby znaleźć tylko porcje należące do grup NP lub V?

from nltk.chunk import RegexpParser 

grammar = ''' 
NP: {<DT>?<JJ>*<NN>*} 
V: {<V.*>}''' 
chunker = RegexpParser(grammar) 
token = [] ## Some tokens from my POS tagger 
chunked = chunker.parse(tokens) 
print chunked 

#How do I walk the tree? 
#for chunk in chunked: 
# if chunk.??? == 'NP': 
#   print chunk 

(S (NP nośnika/NN) w/IN tkankowo/JJ i/CC hodowli komórkowej/JJ w/IN (NP/dt preparatem/NN) o/w (np implantów/NNS) i/CC (NP implantu/NN) (V zawierający/VBG) (NP/dt nośnika/NN) ./).

Odpowiedz

11

to powinno pracować :

for n in chunked: 
    if isinstance(n, nltk.tree.Tree):    
     if n.label() == 'NP': 
      do_something_with_subtree(n) 
     else: 
      do_something_with_leaf(n) 
+0

Daje mi AttributeError: 'krotki' obiekt ma atrybut 'węzeł' n jest

+0

edytowany odpowiedź ... –

+1

działa jak czar - dzięki! –

0

Mały błąd w token

from nltk.chunk import RegexpParser 
grammar = ''' 
NP: {<DT>?<JJ>*<NN>*} 
V: {<V.*>}''' 
chunker = RegexpParser(grammar) 
token = [] ## Some tokens from my POS tagger 
//chunked = chunker.parse(tokens) // token defined in the previous line but used tokens in chunker.parse(tokens) 
chunked = chunker.parse(token) // Change in this line 
print chunked 
0

odpowiedź Savino jest wielki, ale warto też zauważyć, że poddrzewa mogą być dostępne przez indeks, jak również, na przykład

for n in range(len(chunked)): 
    do_something_with_subtree(chunked[n])