Example usage for edu.stanford.nlp.trees Tree deepCopy

List of usage examples for edu.stanford.nlp.trees Tree deepCopy

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees Tree deepCopy.

Prototype

public Tree deepCopy() 

Source Link

Document

Makes a deep copy of not only the Tree structure but of the labels as well.

Usage

From source file:opennlp.tools.parse_thicket.opinion_processor.DefaultSentimentProcessor.java

License:Apache License

/**
 * Outputs a tree using the output style requested
 *///from   w w w  .  j av a 2s.  c  o m
static void outputTree(PrintStream out, CoreMap sentence, List<Output> outputFormats) {
    Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
    for (Output output : outputFormats) {
        switch (output) {
        case PENNTREES: {
            Tree copy = tree.deepCopy();
            setSentimentLabels(copy);
            out.println(copy);
            break;
        }
        case VECTORS: {
            Tree copy = tree.deepCopy();
            setIndexLabels(copy, 0);
            out.println(copy);
            outputTreeVectors(out, tree, 0);
            break;
        }
        case ROOT: {
            out.println("  " + sentence.get(SentimentCoreAnnotations.SentimentClass.class));
            break;
        }
        case PROBABILITIES: {
            Tree copy = tree.deepCopy();
            setIndexLabels(copy, 0);
            out.println(copy);
            outputTreeScores(out, tree, 0);
            break;
        }
        default:
            throw new IllegalArgumentException("Unknown output format " + output);
        }
    }
}

From source file:pltag.util.Utils.java

License:Open Source License

public static Tree getMinimalConnectedStructure(Tree tree, Tree firstLeaf, Tree lastLeaf, int lastLeafIndex) {
    // find common ancestor node by traversing the tree bottom-up from the last leaf and up
    Tree commonAncestorNode = lastLeaf.parent(tree);
    while (!commonAncestorNode.getLeaves().get(0).equals(firstLeaf)) {
        commonAncestorNode = commonAncestorNode.parent(tree);
    }/* www. j  a  v a2  s.co m*/
    // found the common ancestor, now we need to clone the tree and chop the children non-terminals the span of which is outwith the last leaf
    Tree result = commonAncestorNode.deepCopy();
    List<Tree> leaves = result.getLeaves();
    lastLeaf = leaves.get(lastLeafIndex);
    Tree p = lastLeaf.parent(result);
    Tree d = lastLeaf;
    while (p != null) {
        if (p.numChildren() > 1) {
            // remove siblings to the right of d
            int index = indexOfChild(p, d.nodeNumber(result), result);
            pruneChildrenAfter(p, index);
        }
        d = p;
        p = p.parent(result);
    }
    return result;
}

From source file:reactivetechnologies.sentigrade.engine.nlp.SentimentAnalyzer.java

License:Apache License

private static void print(Tree tree) {
    if (LOG.isDebugEnabled()) {
        Tree t = tree.deepCopy();
        //setSentimentLabels(t);
        LOG.debug("leaf?" + t.isLeaf() + " phrrasal?" + t.isPhrasal() + " preterminal?" + t.isPreTerminal()
                + " class:" + RNNCoreAnnotations.getPredictedClass(t));
        LOG.debug("" + t + "");

    }/*from w  w  w . j av a 2 s. c o m*/

}

From source file:reactivetechnologies.sentigrade.engine.nlp.SentimentAnalyzer.java

License:Apache License

private SentimentVector calculatePOSScores(Tree parse) {
    SentimentVector vector = new SentimentVector();
    if (parse.isPhrasal()) {
        // TregexPattern pattern = TregexPattern.compile("@NP");//noun
        // TregexPattern pattern = TregexPattern.compile("@VP");
        // TregexPattern pattern = TregexPattern.compile("@JJS");//adjective
        // superlative
        // TregexPattern pattern = TregexPattern.compile("@CD");//numeric
        TregexPattern pattern = TregexPattern.compile("@ADJP");
        TregexMatcher matcher = pattern.matcher(parse);

        while (matcher.find()) {
            Tree match = matcher.getMatch();
            vector.adjScore += calcPOSAdj(match.deepCopy());
            vector.advScore += calcPOSAdv(match.deepCopy());
            vector.nounScore += calcPOSNoun(match.deepCopy());
            vector.verbScore += calcPOSVerb(match.deepCopy());
        }/*from  ww w.  j  a v  a2s . co  m*/

        pattern = TregexPattern.compile("@VP");
        matcher = pattern.matcher(parse);
        while (matcher.find()) {
            Tree match = matcher.getMatch();
            vector.adjScore += calcPOSAdj(match.deepCopy());
            vector.nounScore += calcPOSNoun(match.deepCopy());
            vector.verbScore += calcPOSVerb(match.deepCopy());
            vector.advScore += calcPOSAdv(match.deepCopy());
        }
    }
    return vector;
}