如何正确导航NLTK解析树?

时间:2015-05-05 15:39:59

标签: python tree nlp nltk

NLTK再次让我疯了。

如何正确浏览NLTK树(或ParentedTree)? 我想用父节点“VBZ”识别某个叶子,然后我想从那里向上移动到树的左边以识别NP节点。

我该怎么做?似乎没有想到NLTK树类......或者我太傻了......

感谢您的帮助!

Tree

1 个答案:

答案 0 :(得分:2)

根据你想做的事情,这应该有效。它将首先提供最近的左NP节点,然后是最接近的第二个节点等。因此,如果您有(S (NP1) (VP (NP2) (VBZ)))的树,则您的np_trees列表将具有[ParentedTree(NP2), ParentedTree(NP1)]

from nltk.tree import *

np_trees = []

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

    if t.label() == "VBZ":
        current = t
        while current.parent() is not None:

            while current.left_sibling() is not None:

                if current.left_sibling().label() == "NP":
                    np_trees.append(current.left_sibling())

                current = current.left_sibling()

            current = current.parent()

    for child in t:
        traverse(child)

tree = ParentedTree.fromstring("(S (NP (NNP)) (VP (VBZ) (NP (NNP))))")
traverse(tree)
print np_trees # [ParentedTree('NP', [ParentedTree('NNP', [])])]