Skip to content Skip to sidebar Skip to footer

How To Get A Node In A Tree By Its Label In Nltk Python?

I have a tree: (S (WH-QUERY Which) (FLIGHT-NP (FLIGHT-CNP (FLIGHT-CNP (FLIGHT-N flight)) (FLIGHT-DEST to (CITY-NP (CITY-NAME Hu

Solution 1:

One way to do it is to walk the tree searching for nodes that match:

for subtree in tree.subtrees():
     if subtree.label() == 'CITY-NAME':
          print subtree.leaves()

Solution 2:

Look at the _get_node method in the function.

http://www.nltk.org/_modules/nltk/tree.html

Post a Comment for "How To Get A Node In A Tree By Its Label In Nltk Python?"