Reputation: 1
I am new to nltk and finding it hard to deal with nltk tree. Given an nltk parsed tree from Penn treebank, I want to be able to count the span of each node recursively from bottom to up. Span of leaf nodes is 1. And the span of non terminal nodes is the sum of the span of its children. Could someone please show me how to do this?
Thank you.
Upvotes: -1
Views: 771
Reputation: 50190
If t
is any tree or subtree in an nltk.Tree
, its number of leaves is given by len(t.leaves())
.
>>> t = Tree.fromstring('(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))')
>>> t[1,1]
Tree('NP', [Tree('D', ['the']), Tree('N', ['cat'])])
>>> len(t[1,1].leaves())
2
Upvotes: 1