简体   繁体   中英

In nltk tree how can I access a parent from a child?

Suppose I had a variable that holds a tree of nltk tree class. Is there any function like parent() or something that returns the parent of the node?

使用NLTK 3,您可以使用nltk.tree.ParentedTree它有一个parent()方法。

You need a different data structure: A tree whose nodes contain pointers to their parent. The NLTK now provides the type nltk.tree.ParentedTree (as @Gerhard already pointed out). But the NLTK corpora and tools generate plain Tree objects, so you need to convert. If you have a variable mytree that contains your tree, convert it like this:

from nltk.tree import ParentedTree
newtree = ParentedTree.convert(mytree)

The nodes of the new tree will have a parent() method that you can use to navigate up the tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM