如何正确导航NLTK解析树? [英] How to properly navigate an NLTK parse tree?

查看:114
本文介绍了如何正确导航NLTK解析树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NLTK再次让我发疯.

NLTK is driving me nuts again.

如何正确浏览NLTK树(或ParentedTree)? 我想用父节点"VBZ"标识某个叶子,然后我想从那里进一步向上移动到树的左侧,以标识NP节点.

How do I properly navigate through an NLTK tree (or ParentedTree)? I would like to identify a certain leaf with the parent node "VBZ", then I would like to move from there further up the tree and to the left to identify the NP node.

我该怎么做?似乎没有考虑过NLTK树类...或者我太愚蠢了...

How do I do this? The NLTK tree class does not seem to be thought through... Or I am too stupid...

感谢您的帮助!

推荐答案

根据您要执行的操作,这应该可以工作.它将首先为您提供最接近的左NP节点,然后为您提供第二最接近的NP节点,依此类推.因此,如果您有一棵树(S (NP1) (VP (NP2) (VBZ))),则您的np_trees列表将具有[ParentedTree(NP2), ParentedTree(NP1)].

Based on what you want to do, this should work. It will give you the closest left NP node first, then the second closest, etc. So, if you had a tree of (S (NP1) (VP (NP2) (VBZ))), your np_trees list would have [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', [])])]

这篇关于如何正确导航NLTK解析树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆