如何遍历树的所有节点? [英] How to iterate through all nodes of a tree?

查看:781
本文介绍了如何遍历树的所有节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简化解析树的节点,即给定一个节点,我摆脱了第一个连字符以及该连字符之后的所有内容.例如,如果一个节点是NP-TMP-FG,我想使其成为NP,如果它是SBAR-SBJ,我希望使其成为SBAR,依此类推.这是我拥有的一个解析树的示例

I want to simplify my parse trees' nodes, i.e. given a node I get rid of the first hyphen and whatever that comes after that hyphen. For example if a node is NP-TMP-FG, I want to make it NP and if it is SBAR-SBJ, I want to make it SBAR and so on. This is an example of one parse tree that I have

( (S (S-TPC-2 (NP-SBJ (NP (DT The) (NN asbestos) (NN fiber) ) (, ,)
(NP (NN crocidolite) ) (, ,) ) (VP (VBZ is) (ADJP-PRD (RB unusually) (JJ resilient) )
(SBAR-TMP (IN once) (S (NP-SBJ (PRP it) ) (VP (VBZ enters) (NP (DT the) (NNS lungs) ))))
(, ,) (PP (IN with)(S-NOM (NP-SBJ (NP (RB even) (JJ brief) (NNS exposures) ) (PP (TO to)
(NP (PRP it) ))) (VP (VBG causing) (NP (NP (NNS symptoms) ) (SBAR (WHNP-1 (WDT that) )
(S (NP-SBJ (-NONE- *T*-1) ) (VP (VBP show) (PRT (RP up) ) (ADVP-TMP (NP (NNS decades) ) 
(JJ later) )))))))))) (, ,) (NP-SBJ (NNS researchers) ) (VP (VBD said)(SBAR (-NONE- 0) 
(S (-NONE- *T*-2) )))    (. .) )) 

这是我的代码,但是不起作用.

This is my code but it doesn't work.

import re
import nltk
from nltk.tree import *
tree = Tree.fromstring(line) // Each parse tree is stored in one single line
for subtree in tree.subtrees():
    re.sub('-.*', '', subtree.label())
print tree

我想问题是subtree.label()显示了节点,但是由于它是一个函数,因此无法更改. print subtree.label()的输出是:

I guess the problem is that subtree.label() shows the nodes but it cannot be changed since it is a function. The output of print subtree.label() is :

S
S-TPC-2
NP-SBJ
NP
DT
NN
,

以此类推...

推荐答案

我想到了这一点:

for subtree in tree.subtrees():
    s = subtree.label()
    subtree.set_label(re.sub('-.*', "", s))

这篇关于如何遍历树的所有节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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