title: Absolute position of leaves in NLTK tree
body: <p>I am trying to find the span (start index, end index) of a noun phrase in a given sentence. The following is the code for extracting noun phrases</p>
  
  <pre><code>sent=nltk.word_tokenize(a)
  sent_pos=nltk.pos_tag(sent)
  grammar = r"""
      NBAR:
          {&lt;NN.*|JJ&gt;*&lt;NN.*&gt;}  # Nouns and Adjectives, terminated with Nouns
  
      NP:
          {&lt;NBAR&gt;}
          {&lt;NBAR&gt;&lt;IN&gt;&lt;NBAR&gt;}  # Above, connected with in/of/etc...
      VP:
          {&lt;VBD&gt;&lt;PP&gt;?}
          {&lt;VBZ&gt;&lt;PP&gt;?}
          {&lt;VB&gt;&lt;PP&gt;?}
          {&lt;VBN&gt;&lt;PP&gt;?}
          {&lt;VBG&gt;&lt;PP&gt;?}
          {&lt;VBP&gt;&lt;PP&gt;?}
  """
  
  cp = nltk.RegexpParser(grammar)
  result = cp.parse(sent_pos)
  nounPhrases = []
  for subtree in result.subtrees(filter=lambda t: t.label() == 'NP'):
    np = ''
    for x in subtree.leaves():
      np = np + ' ' + x[0]
    nounPhrases.append(np.strip())
  </code></pre>
  
  <p>For <em>a = "The American Civil War, also known as the War between the States or simply the Civil War, was a civil war fought from 1861 to 1865 in the United States after several Southern slave states declared their secession and formed the Confederate States of America.</em>", the noun phrases extracted are</p>
  
  <p><em>['American Civil War', 'War', 'States', 'Civil War', 'civil war fought', 'United States', 'several Southern', 'states', 'secession', 'Confederate States', 'America'].</em></p>
  
  <p>Now I need to find the span (start position and end position of the phrase) of noun phrases. For example, the span of above noun phrases will be </p>
  
  <p><em>[(1,3), (9,9), (12, 12), (16, 17), (21, 23), ....]</em>.</p>
  
  <p>I'm fairly new to NLTK and I've looked into <a href="http://www.nltk.org/_modules/nltk/tree.html" rel="noreferrer">http://www.nltk.org/_modules/nltk/tree.html</a>. I tried to use <em>Tree.treepositions()</em> but I couldn't manage to extract absolute positions using these indices. Any help would be greatly appreciated. Thank You!</p>