4
votes

I use Stanford core NLP library for sentiment analysis. The below code return the class of an example but how can I get the score? for example -0.3 for negative etc

private int getScore(String line) {
    boolean isrun = false;
    StanfordCoreNLP pipeline = null;
    if(!isrun){
        Properties props = getProperties();
        pipeline = new StanfordCoreNLP(props);
        isrun = true;
    }
    Annotation annotation;

    int sentiment = -1;
    if (line != null && line.length() > 0) {
        annotation = pipeline.process(line);
        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
            sentiment = RNNCoreAnnotations.getPredictedClass(tree);
        }
    }
    return sentiment;
}

EDIT

In online demo when mouse is on root at the graph we can see that the example is negative 72%. How can get this number?

2
What do you mean "return the class of an example"? It returns an int primitive (sentiment).GeorgeG
The class is an integer number 0:very negative, 1:negative 2:neutral 3:positive and 4:very positive. But this is the class. I want the scoreJimmysnn

2 Answers

5
votes

0.Download Stanford NLP Core Lib and import external lib stanford-corenlp-3.5.2-models.jar, stanford-corenlp-3.5.2.jar, stanford-corenlp-3.5.2-sources.jar and ejml-0.23.jar into this package.

1.Build this class NLP in Eclipse

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.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;

public class NLP {
static StanfordCoreNLP pipeline;

public static void init() {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
    pipeline = new StanfordCoreNLP(props);
}

public static int findSentiment(String tweet) {

    int mainSentiment = 0;
    if (tweet != null && tweet.length() > 0) {
        int longest = 0;
        Annotation annotation = pipeline.process(tweet);
        for (CoreMap sentence : annotation
                .get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree tree = sentence
                    .get(SentimentAnnotatedTree.class);
            int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
            SimpleMatrix sentiment_new = RNNCoreAnnotations.getPredictions(tree);             
            String partText = sentence.toString();
            if (partText.length() > longest) {
                mainSentiment = sentiment;
                longest = partText.length();
            }
        }
    }
    return mainSentiment;
    }
}

2.Build a new class to parse your sentence with NLP

import java.util.ArrayList;

public class What2Think {

    public static void main(String[] args) {
        ArrayList<String> tweets = new ArrayList<String>();
        tweets.add("In this country, \"democracy\" means pro-government. #irony");
        NLP.init();
        for(String tweet : tweets) {
            System.out.println(tweet + " : " + NLP.findSentiment(tweet));
        }
    }
}

Run it

1
votes

I had a similar requirement. You can get this information from SimpleMatrix

 SimpleMatrix sm = RNNCoreAnnotations.getPredictions(tree);

If you print the variable sm the output has something like this

Type = dense , numRows = 5 , numCols = 1
 0.111  
 0.758  
 0.087  
 0.035  
 0.009 

This gives the estimated probability. In the online demo, you can see these values in %.

You can find my implementation here.

Hope it helps!!!