AR_HZ
AR_HZ

Reputation: 365

How do I get the score distribution value for CoreNLP Sentiment?

I have setup CoreNLP server on my ubuntu instance and it works ok. I more interested in Sentiment module and currently I get is

{
sentimentValue: "2",
sentiment: "Neutral"
}

What I need is score distribution value, as you see here: http://nlp.stanford.edu:8080/sentiment/rntnDemo.html

 "scoreDistr": [0.1685, 0.7187, 0.0903, 0.0157, 0.0068]

What am I missing or How do I get such data ?

Thanks

Upvotes: 3

Views: 1096

Answers (1)

tkja
tkja

Reputation: 2030

You need to get the a tree object via SentimentCoreAnnotations.SentimentAnnotatedTree.class from your annotated sentence. Then, you can get the predictions through the RNNCoreAnnotations class. I wrote the following self-contained demo code below that shows how to get the scores for each label of a CoreNLP Sentiment prediction.

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.ejml.simple.SimpleMatrix;

import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;

public class DemoSentiment {
    public static void main(String[] args) {
        final List<String> texts = Arrays.asList("I am happy.", "This is a neutral sentence.", "I am very angry.");
        final Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
        final StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        for (String text : texts) {
            final Annotation doc = new Annotation(text);
            pipeline.annotate(doc);
            for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
                final Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
                final SimpleMatrix sm = RNNCoreAnnotations.getPredictions(tree);
                final String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
                System.out.println("sentence:  "+sentence);
                System.out.println("sentiment: "+sentiment);
                System.out.println("matrix:    "+sm);
            }
        }
    }
}

The output will be similar (some floating point rounding errors or an updated models might change the scores) to what is below.

For the first sentence I am happy., you can see that the sentiment is Positive, and, the highest value in the returned matrix is 0.618, at the fourth position, when interpreting the matrix as an ordered listing.

The second sentence This is a neutral sentence. has its highest score in the middle, at 0.952, hence the Neutral sentiment.

The last sentence has correspondingly a Negative sentiment, with its highest score of 0.652 at the second position.

sentence:  I am happy.
sentiment: Positive
matrix:    Type = dense , numRows = 5 , numCols = 1
0.016  
0.037  
0.132  
0.618  
0.196  

sentence:  This is a neutral sentence.
sentiment: Neutral
matrix:    Type = dense , numRows = 5 , numCols = 1
0.001  
0.007  
0.952  
0.039  
0.001  

sentence:  I am very angry.
sentiment: Negative
matrix:    Type = dense , numRows = 5 , numCols = 1
0.166  
0.652  
0.142  
0.028  
0.012

Upvotes: 3

Related Questions