Example usage for edu.stanford.nlp.trees Trees rightEdge

List of usage examples for edu.stanford.nlp.trees Trees rightEdge

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees Trees rightEdge.

Prototype

public static int rightEdge(Tree t, Tree root) 

Source Link

Document

Returns the positional index of the right edge of a tree t within a given root, as defined by the size of the yield of all material preceding t plus all the material contained in t.

Usage

From source file:gate.stanford.Parser.java

License:Open Source License

/**
 * Generate a SyntaxTreeNode Annotation corresponding to this Tree.  Work 
 * recursively so that the annotations are actually generated from the 
 * bottom up, in order to build the consists list of annotation IDs.
 * //from   w  w  w.j  av a2s. co m
 * @param tree  the current subtree
 * @param rootTree  the whole sentence, used to find the span of the current subtree
 * @return a GATE Annotation of type "SyntaxTreeNode"
 */
protected Annotation annotatePhraseStructureRecursively(AnnotationSet annotationSet,
        StanfordSentence stanfordSentence, Tree tree, Tree rootTree) {
    Annotation annotation = null;
    Annotation child;
    String label = tree.value();

    List<Tree> children = tree.getChildrenAsList();

    if (children.size() == 0) {
        return null;
    }
    /* implied else */

    /* following line generates ClassCastException
     *       IntPair span = tree.getSpan();
     * edu.stanford.nlp.ling.CategoryWordTag
     * at edu.stanford.nlp.trees.Tree.getSpan(Tree.java:393)
     * but I think it's a bug in the parser, so I'm hacking 
     * around it as follows. */
    int startPos = Trees.leftEdge(tree, rootTree);
    int endPos = Trees.rightEdge(tree, rootTree);

    Long startNode = stanfordSentence.startPos2offset(startPos);
    Long endNode = stanfordSentence.endPos2offset(endPos);

    List<Integer> consists = new ArrayList<Integer>();

    Iterator<Tree> childIter = children.iterator();
    while (childIter.hasNext()) {
        child = annotatePhraseStructureRecursively(annotationSet, stanfordSentence, childIter.next(), rootTree);
        if ((child != null) && (!child.getType().equals(inputTokenType))) {
            consists.add(child.getId());
        }
    }
    annotation = annotatePhraseStructureConstituent(annotationSet, startNode, endNode, label, consists,
            tree.depth());

    return annotation;
}