Search in sources :

Example 1 with SentimentCostAndGradient

use of edu.stanford.nlp.sentiment.SentimentCostAndGradient in project CoreNLP by stanfordnlp.

the class SentimentAnnotator method annotate.

@Override
public void annotate(Annotation annotation) {
    if (annotation.containsKey(CoreAnnotations.SentencesAnnotation.class)) {
        // TODO: parallelize
        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            Tree binarized = sentence.get(TreeCoreAnnotations.BinarizedTreeAnnotation.class);
            if (binarized == null) {
                throw new AssertionError("Binarized sentences not built by parser");
            }
            Tree collapsedUnary = transformer.transformTree(binarized);
            SentimentCostAndGradient scorer = new SentimentCostAndGradient(model, null);
            scorer.forwardPropagateTree(collapsedUnary);
            sentence.set(SentimentCoreAnnotations.SentimentAnnotatedTree.class, collapsedUnary);
            int sentiment = RNNCoreAnnotations.getPredictedClass(collapsedUnary);
            sentence.set(SentimentCoreAnnotations.SentimentClass.class, SentimentUtils.sentimentString(model, sentiment));
            Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
            if (tree != null) {
                collapsedUnary.setSpans();
                // map the sentiment annotations onto the tree
                Map<IntPair, String> spanSentiment = Generics.newHashMap();
                for (Tree bt : collapsedUnary) {
                    IntPair p = bt.getSpan();
                    int sen = RNNCoreAnnotations.getPredictedClass(bt);
                    String sentStr = SentimentUtils.sentimentString(model, sen);
                    if (!spanSentiment.containsKey(p)) {
                        // we'll take the first = highest one discovered
                        spanSentiment.put(p, sentStr);
                    }
                }
                if (((CoreLabel) tree.label()).containsKey(CoreAnnotations.SpanAnnotation.class)) {
                    throw new IllegalStateException("This code assumes you don't have SpanAnnotation");
                }
                tree.setSpans();
                for (Tree t : tree) {
                    IntPair p = t.getSpan();
                    String str = spanSentiment.get(p);
                    if (str != null) {
                        CoreLabel cl = (CoreLabel) t.label();
                        cl.set(SentimentCoreAnnotations.SentimentClass.class, str);
                        cl.remove(CoreAnnotations.SpanAnnotation.class);
                    }
                }
            }
        }
    } else {
        throw new RuntimeException("unable to find sentences in: " + annotation);
    }
}
Also used : SentimentCostAndGradient(edu.stanford.nlp.sentiment.SentimentCostAndGradient) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) IntPair(edu.stanford.nlp.util.IntPair) SentimentCoreAnnotations(edu.stanford.nlp.sentiment.SentimentCoreAnnotations) CoreLabel(edu.stanford.nlp.ling.CoreLabel) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SentimentCoreAnnotations(edu.stanford.nlp.sentiment.SentimentCoreAnnotations) RNNCoreAnnotations(edu.stanford.nlp.neural.rnn.RNNCoreAnnotations) Tree(edu.stanford.nlp.trees.Tree) CoreMap(edu.stanford.nlp.util.CoreMap)

Aggregations

CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1 CoreLabel (edu.stanford.nlp.ling.CoreLabel)1 RNNCoreAnnotations (edu.stanford.nlp.neural.rnn.RNNCoreAnnotations)1 SentimentCoreAnnotations (edu.stanford.nlp.sentiment.SentimentCoreAnnotations)1 SentimentCostAndGradient (edu.stanford.nlp.sentiment.SentimentCostAndGradient)1 Tree (edu.stanford.nlp.trees.Tree)1 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)1 CoreMap (edu.stanford.nlp.util.CoreMap)1 IntPair (edu.stanford.nlp.util.IntPair)1