Perform Textual Sentiment Analysis in Java Using A Deep Learning Model
Perform Textual Sentiment Analysis in Java Using A Deep Learning Model
Java Magazine
Coding, Tools
Perform textual sentiment analysis in Java using a deep
learning model
Yuli Vasiliev | December 17, 2021
Positive? Negative? Neutral? Use the Stanford CoreNLP suite to analyze customer product
reviews.
Sentiment analysis is a text classification task focused on identifying whether a piece of text is positive, negative, or neutral. For example, you
might be interested in analyzing the sentiment of customer feedback on a certain product or in detecting the sentiment on a certain topic
trending in social media.
This article illustrates how such tasks can be implemented in Java using the sentiment tool integrated into Stanford CoreNLP, an open source
library for natural language processing.
After completing the steps above, you are ready to create a Java program that runs a Stanford CoreNLP pipeline to process text.
In the following example, you implement a simple Java program that runs a Stanford CoreNLP pipeline for sentiment analysis on text
containing several sentences.
To start, implement a class that provides a method to initialize the pipeline and a method that will use this pipeline to split a submitted text
into sentences and then to classify the sentiment of each sentence. Here is what the implementation of this class might look like.
//nlpPipeline.java
import java.util.Properties;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
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;
int sentimentInt;
String sentimentName;
sentimentInt = RNNCoreAnnotations.getPredictedClass(tree);
sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
In the following implementation, the sample text is hardcoded in the program for simplicity. The sample sentences were designed to cover the
entire spectrum of sentiment scores available with Stanford CoreNLP: very positive, positive, neutral, negative, and very negative.
//SentenceSentiment.java
String text = "This is an excellent book. I enjoy reading it. I can read on Sundays. Today
nlpPipeline.init();
nlpPipeline.estimatingSentiment(text);
$ javac nlpPipeline.java
$ javac SentenceSentiment.java
$ java SentenceSentiment
The first column in the output above contains the name of the sentiment class predicted for a sentence. The second column contains the
corresponding number code of the predicted class. The third column contains the sentence.
Then, add the following method to the nlpPipeline class created in the previous section:
//nlpPipeline.java
...
int sentimentInt = 2;
.get(CoreAnnotations.SentencesAnnotation.class).get(0);
.get(SentimentAnnotatedTree.class);
sentimentInt = RNNCoreAnnotations.getPredictedClass(tree);
sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
return sentimentName;
import com.opencsv.CSVReader;
import com.opencsv.CSVParser;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;
import java.io.FileReader;
import java.io.IOException;
nlpPipeline.init();
String[] row;
System.out.println("Review: " + row[1] + "\t" + " Amazon rating: " + row[4] + "\t" + " Sen
}
$ javac nlpPipeline.java
$ javac ReviewsSentiment.java
$ java ReviewsSentiment
Conclusion
This article provided a quick introduction to sentiment analysis in Java with Stanford CoreNLP, a free open source solution for natural
language processing, allowing you to analyze the sentiment of a sentence from very negative to very positive. You tested the Stanford
CoreNLP sentiment classifier on sample text containing sentences with different sentiment polarities. Then, you built a sentiment analyzer for
customer reviews and tested it on a set of actual reviews downloaded from Amazon.
Dig deeper
Java records: Serialization, marshaling, and bean state validation
How to program machine learning in Java with the Tribuo library
Containerizing apps with jlink
Designing and implementing a library
Yuli Vasiliev
Yuli Vasiliev is a programmer, freelance author, and consultant currently specializing in open source development;
Oracle database technologies; and, more recently, natural-language processing (NLP).
Previous Post
11 great Java tricks from dev.java
Nicolai Parlog | 7 min read
2021 © Oracle Site Map Privacy / Do Not Sell My Info Cookie Preferences Ad Choices Careers