NLTK tree data structure, finding a node, it's parent or children

强颜欢笑 提交于 2019-11-27 21:38:41

For NLTK 3.0, you want to use the ParentedTree subclass.

http://www.nltk.org/api/nltk.html#nltk.tree.ParentedTree

Using the sample tree you've given, create a ParentedTree and search for the node you want:

from nltk.tree import ParentedTree
ptree = ParentedTree.fromstring('(ROOT (S (NP (PRP It)) \
        (VP (VBZ is) (ADJP (RB so) (JJ nice))) (. .)))')

leaf_values = ptree.leaves()

if 'nice' in leaf_values:
    leaf_index = leaf_values.index('nice')
    tree_location = ptree.leaf_treeposition(leaf_index)
    print tree_location
    print ptree[tree_location]

You can iterate through the tree directly to get the child subtrees. The parent() method is used to find the parent tree for the given subtree.

Here's an example using a deeper tree for child and parent:

from nltk.tree import ParentedTree
ptree = ParentedTree.fromstring('(ROOT (S (NP (JJ Congressional) \
    (NNS representatives)) (VP (VBP are) (VP (VBN motivated) \
    (PP (IN by) (NP (NP (ADJ shiny) (NNS money))))))) (. .))')

def traverse(t):
    try:
        t.label()
    except AttributeError:
        return
    else:

        if t.height() == 2:   #child nodes
            print t.parent()
            return

        for child in t:
            traverse(child)

traverse(ptree)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!