vendredi 17 juin 2016

Subtree Extraction NLTK Tree

I need a little help with NLTK trees.

I am trying to extract some subtrees from this french tree:

Original Tree

(SENT (NP-SUJ↓ (PRO=H Personne)) (VN=H (ADV* ne) (V=H sait)) (ADV* exactement) (PONCT* .))

I only want to extract trees having '=H' at the end of the POS label and then add the parent node:

Like this: (NP-SUJ↓ (PRO=H Personne)) and this: (VN=H (V=H sait))

And I wrote a function to do so:

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'])])]

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 :

(SENT (NP-SUJ↓ ) (VN=H (V=H sait)))

Subtree

But I lost track of the original tree...

Second, the parent() function returns all the subtrees contained in it. And I just want to have specific nodes.

What would be the good approach to extract this last subtree???

Thank you very much for your help! I am new at this but I really like it!

Aucun commentaire:

Enregistrer un commentaire