I am taking Sentiment Analysis code from my blog:
Code is straightforward go through that:
- Use SentimentCoreAnnotations.SentimentClass.class to get sentiment
type.
- Use RNNCoreAnnotations.getPredictedClass to get sentiment
score
Sentiment Analyzer
package com.interviewBubble.sentimentanalysis;
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 SentimentAnalyzer {
StanfordCoreNLP pipeline;
public void initialize() {
Properties properties = new Properties();
properties.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
pipeline = new StanfordCoreNLP(properties);
}
public SentimentResult getSentimentResult(String text) {
SentimentClassification classification = new SentimentClassification();
SentimentResult sentimentResult = new SentimentResult();
if (text != null && text.length() > 0) {
Annotation annotation = pipeline.process(text);
for(CoreMap sentence: annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
// System.out.println(sentence);
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
//System.out.println(tree);
SimpleMatrix simpleMatrix = RNNCoreAnnotations.getPredictions(tree);
//System.out.println(simpleMatrix);
classification.setVeryNegative((int)Math.round(simpleMatrix.get(0)*100d));
classification.setNegative((int)Math.round(simpleMatrix.get(1)*100d));
classification.setNeutral((int)Math.round(simpleMatrix.get(2)*100d));
classification.setPositive((int)Math.round(simpleMatrix.get(3)*100d));
classification.setVeryPositive((int)Math.round(simpleMatrix.get(4)*100d));
String setimentType = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
sentimentResult.setSentimentType(setimentType);
sentimentResult.setSentimentClass(classification);
sentimentResult.setSentimentScore(RNNCoreAnnotations.getPredictedClass(tree));
}
}
return sentimentResult;
}
}
Sentiment Analysis:
package com.interviewBubble.sentimentanalysis;
public class SentimentAnalysis {
public static void main(String[] args) {
// I have taken iPhone X (Silver) User Review from Amazon
String text = "Just love the X. Feel so Premium and a Head turner too. Face ID working fine but still miss "
+ "the fingerprint scanner very much. I jump from 5S to X so it’s a huge skip. I’m very very happy"
+ " with it. Specially battery backup is great after using with 4g cellular network and no heating "
+ "issue at all, though I’m not a mobile gamer, Oftentimes I play Boom Beach and I watch YouTube "
+ "videos and I surf a lot. It makes a deep hole in pocket at the Hefty price tag. So it’s all "
+ "upto your Consideration.\n";
SentimentAnalyzer sentimentAnalyzer = new SentimentAnalyzer();
sentimentAnalyzer.initialize();
SentimentResult sentimentResult = sentimentAnalyzer.getSentimentResult(text);
System.out.println("Sentiments Classification:");
System.out.println("Very positive: " + sentimentResult.getSentimentClass().getVeryPositive()+"%");
System.out.println("Positive: " + sentimentResult.getSentimentClass().getPositive()+"%");
System.out.println("Neutral: " + sentimentResult.getSentimentClass().getNeutral()+"%");
System.out.println("Negative: " + sentimentResult.getSentimentClass().getNegative()+"%");
System.out.println("Very negative: " + sentimentResult.getSentimentClass().getVeryNegative()+"%");
System.out.println("\nSentiments result:");
System.out.println("Sentiment Score: " + sentimentResult.getSentimentScore());
System.out.println("Sentiment Type: " + sentimentResult.getSentimentType());
}
}
OUTPUT:
0 [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
8 [main] INFO edu.stanford.nlp.pipeline.TokenizerAnnotator - No tokenizer type provided. Defaulting to PTBTokenizer.
12 [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
17 [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator parse
512 [main] INFO edu.stanford.nlp.parser.common.ParserGrammar - Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [0.5 sec].
517 [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator sentiment
Sentiments Classification:
Very positive: 4%
Positive: 23%
Neutral: 24%
Negative: 39%
Very negative: 8%
Sentiments result:
Sentiment Score: 1
Sentiment Type: Negative
Find whole project here