title: Subtree Extraction NLTK Tree
body: <p>I need a little help with NLTK trees.</p>
  
  <p>I am trying to extract some subtrees from this french tree:</p>
  
  <p><img src="https://i.stack.imgur.com/T1Oac.png" alt="Original Tree"></p>
  
  <pre class="lang-none prettyprint-override"><code>(SENT (NP-SUJ↓ (PRO=H Personne)) (VN=H (ADV* ne) (V=H sait)) (ADV* exactement) (PONCT* .))
  </code></pre>
  
  <p>I only want to extract trees having '=H' at the end of the POS label and then add the parent node:</p>
  
  <p>Like this: <code>(NP-SUJ↓ (PRO=H Personne)) and this: (VN=H (V=H sait))</code></p>
  
  <p>And I wrote a function to do so:</p>
  
  <pre><code>def AddParent(tree):
      grammar = []
      for subtree in tree.subtrees():
          if subtree.height()==2 and subtree.label().endswith("=H"):
              PartialTree = ParentedTree(subtree.parent().label(), 
                                 [ParentedTree(subtree.label(), subtree)])
              grammar.append(PartialTree)
      return grammar
  
  #Test
  pt = ParentedTree.fromstring("(SENT (NP-SUJ↓ (PRO=H Personne)) (VN=H (ADV* ne) (V=H sait)) (ADV* exactement) (PONCT* .))")
  AddParent(pt)
  [ParentedTree('NP-SUJ↓', [ParentedTree('PRO=H', ['Personne'])]), 
  ParentedTree('VN=H', [ParentedTree('V=H', ['sait'])])]
  </code></pre>
  
  <p>I have two issues here: first, I want to keep adding information to those subtrees from the original tree. For instance, I want to keep adding ancestor nodes and then children, to do something like this :</p>
  
  <pre><code>(SENT (NP-SUJ↓ ) (VN=H (V=H sait)))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/o5XRn.png" alt="Subtree"></p>
  
  <p>But I lost track of the original tree...</p>
  
  <p>Second, the <code>parent()</code> function returns all the subtrees contained in it. And I just want to have specific nodes.</p>
  
  <p>What would be the good approach to extract this last subtree???</p>
  
  <p>Thank you very much for your help! I am new at this but I really like it!</p>