Skip to content Skip to sidebar Skip to footer

How To Iterate Through All Nodes Of A Tree?

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 m

Solution 1:

You can do something like that:

for subtree in tree.subtrees():
    first = subtree.label().split('-')[0]
    subtree.set_label(first)

Solution 2:

I came up with this:

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

Post a Comment for "How To Iterate Through All Nodes Of A Tree?"