NLTK树数据结构,找到一个节点,它是父节点还是子节点 [英] NLTK tree data structure, finding a node, it's parent or children

查看:348
本文介绍了NLTK树数据结构,找到一个节点,它是父节点还是子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nltk的Tree数据结构来处理parsetree字符串.

I am using nltk's Tree data structure to work with parsetree strings.

from nltk.tree import Tree
parsed = Tree('(ROOT (S (NP (PRP It)) (VP (VBZ is) (ADJP (RB so) (JJ nice))) (. .)))')

但是,数据结构似乎受到限制. 是否可以通过其字符串值获取节点,然后导航至顶部或底部?

The data structure, however, seems to be limited. Is it possible to get a node by it's string value and then navigate to top or bottom?

例如,假设您要获取字符串值为'nice'的节点,然后查看其父级,子级等,那么可以通过nltk的Tree来实现吗?

For example suppose you want to get the node with string value 'nice' and then see what's its parent, children, etc. Is it achievable via nltk's Tree?

推荐答案

对于NLTK 3.0,您想使用ParentedTree子类.

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

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

使用您提供的示例树,创建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]

您可以直接遍历树以获取子子树. parent()方法用于查找给定子树的父树.

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)

这篇关于NLTK树数据结构,找到一个节点,它是父节点还是子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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