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);
}
}
Aggregations