NLTK Chunking and walking the results tree
I'm using NLTK RegexpParser to extract noungroups and verbgroups from tagged tokens.

How do I walk the resulting tree to find only the chunks that are NP or V groups?


    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 Carrier/NN)
  for/IN
  tissue-/JJ
  and/CC
  cell-culture/JJ
  for/IN
  (NP the/DT preparation/NN)
  of/IN
  (NP implants/NNS)
  and/CC
  (NP implant/NN)
  (V containing/VBG)
  (NP the/DT carrier/NN)
  ./.)

NLTK Chunking and walking the results tree
===

I'm using NLTK RegexpParser to extract noungroups and verbgroups from tagged tokens.

How do I walk the resulting tree to find only the chunks that are NP or V groups?


    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 Carrier/NN)
  for/IN
  tissue-/JJ
  and/CC
  cell-culture/JJ
  for/IN
  (NP the/DT preparation/NN)
  of/IN
  (NP implants/NNS)
  and/CC
  (NP implant/NN)
  (V containing/VBG)
  (NP the/DT carrier/NN)
  ./.)
