-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.

  • When you show some effort of your own, i.e. share code, you are much more likely to get answers. – MERose Jun 06 '17 at 09:25
  • Check this, https://stackoverflow.com/questions/36831354/absolute-position-of-leaves-in-nltk-tree – Vimos May 24 '18 at 12:06

1 Answers1

0

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
alexis
  • 48,685
  • 16
  • 101
  • 161