删除父级树中的节点

时间:2016-04-05 16:37:29

标签: python nlp nltk

我有一个nltk父级树,我想在父级树中删除一些节点(比如说只是叶子节点开始)。我尝试在python中使用del函数,但它只删除节点的值,而不是节点和与之关联的边缘。如何删除节点以及进入它的边缘。

v1 = parser.parse('How to lose weight ?')
ptree = ParentedTree.convert(v1)
t = ptree
for leaf in leaf_values:
     leaf_index = leaf_values.index(leaf)
     tree_location = ptree.leaf_treeposition(leaf_index)
     print tree_location
     del ptree[tree_location]
     ptree.pretty_print()
     ptree = t
     leaf_values = ptree.leaves()

0 个答案:

没有答案