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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With