How to convert nltk.tree.Tree to other type of Tree?

I didn’t found any example on this problem, so I want to provide sample code for someone who is not that familiar with reading API.

# Construct nltk.tree.Tree
s = '(ROOT (S (NP (NP (DT The) (JJ old) (NN oak) (NN tree)) (PP (IN from) (NP (NNP India)))) (VP (VBD fell) (PRT (RP down)))))'
my_tree = Tree.fromstring(s)

# Convert my_tree to ParentedTree
my_parented_tree = ParentedTree(0, [])
my_parented_tree = my_parented_tree.convert(my_tree)

# Check result



  1. What is the ‘cls’ variable used for in Python classes?
  2. NLTK Tree convert API

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s