我有以下字符串:
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
之后的数字是树的高度
我试图找到一个内置的,但最终我只是建立了以下算法:
代码:
用法:
相关问题 更多 >
编程相关推荐