Search code examples
javanlpstanford-nlp

Stanford nlp java


How can I extract these labels from a Tree? http://s9.postimg.org/uvbjudgi7/Immagine.png

Should I extract the Syntactic Categories for each token, could you help me?

I tried with:

Tree tree = sentence.get(TreeAnnotation.class);
tree.pennPrint();

for(int i = 0; i < tree.children().length; i++) {
   for(Tree r : tree.children()[i].localTrees()){
       System.out.println(r.nodeString());
    }
}

but I do not know how to extract the Syntactic Categories of a token!


Solution

  • I think you need a recursive function.

    public void output(Tree tree) {
        System.out.println(tree.nodeString());
        for(int i = 0; i < tree.numChildren(); i++) {
            output(tree.children()[i]);
        }
    }
    

    And you can extract the tag of a token by judge whether the child node is leaf, for exam:

    if(tree.numChildren() == 1 && tree.children()[0].isLeaf()) {
        System.out.println(tree.nodeString()+" "+tree.children()[0].nodeString());
    }