NLTK话语树到边缘lis

2024-09-28 03:16:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下字符串:

dt = ' ( NS-elaboration ( EDU 1 )  ( NS-elaboration ( EDU 2 )  ( NS-elaboration ( EDU 3 )  ( EDU 4 )  )  )  ) '

我可以将其转换为NLTK树,如下所示:

from nltk import Tree
t = Tree.fromstring(dt)

这棵树如图link所示

我想要的是这棵树的边缘列表。类似于以下内容:

NS-elaboration0    EDU1
NS-elaboration0    NS-elaboration1
NS-elaboration1    EDU2
NS-elaboration1    NS-elaboration2
NS-elaboration2    EDU3
NS-elaboration2    EDU4

其中NS-elaboration之后的数字是树的高度


Tags: 字符串fromimporttreedtlinknsedu
1条回答
网友
1楼 · 发布于 2024-09-28 03:16:51

我试图找到一个内置的,但最终我只是建立了以下算法:

代码:

from nltk import Tree

def get_edges(tree, i):
    from_str = f"{tree.label()}{i}"
    children = [f"{child.label()}{child.leaves()[0]}" for child in tree if isinstance(child, Tree) and child.height() == 2]
    children.extend([f"{child.label()}{i+1}" for child in tree if isinstance(child, Tree) and child.height() > 2])
    return [(from_str, child) for child in children]

def tree_to_edges(tree):
    height = 0
    rv = []
    to_check = [tree]
    while to_check:
        tree_to_check = to_check.pop(0)
        rv.extend(get_edges(tree_to_check, height))
        height += 1
        to_check.extend([child for child in tree_to_check if isinstance(child, Tree) and child.height() > 2])
    return rv

用法:

>>> dt = ' ( NS-elaboration ( EDU 1 )  ( NS-elaboration ( EDU 2 )  ( NS-elaboration ( EDU 3 )  ( EDU 4 )  )  )  ) '
>>> t = Tree.fromstring(dt)
>>> tree_to_edges(t)
[('NS-elaboration0', 'EDU1'),
 ('NS-elaboration0', 'NS-elaboration1'),
 ('NS-elaboration1', 'EDU2'),
 ('NS-elaboration1', 'NS-elaboration2'),
 ('NS-elaboration2', 'EDU3'),
 ('NS-elaboration2', 'EDU4')]

相关问题 更多 >

    热门问题