This project is a Java-based implementation of Sentiment Analysis using the Stanford CoreNLP library. Sentiment Analysis is the process of determining the emotional tone behind text, whether it's positive, negative, neutral, or falls into other sentiment categories.
The Stanford CoreNLP provides statistical NLP, deep learning NLP, and rule-based NLP tools for major computational linguistics problems, which can be incorporated into applications with human language technology needs..
The underlying technology of this is based on a new type of Recursive Neural Network that builds on top of grammatical structures.
The Java documentation for stanford nlp can be found here.
The live Demo of Stanfod sentiment analysis.
-
Download the project and import into Eclipse
-
Set the build path which must have the following libraries
- stanford-corenlp-3.8.0
- ejml-0.23
- stanford-corenlp-3.8.0-models
Note: The stanford-corenlp-3.8.0-models has all the trained models. It is not set in the build path of this project due to size constraint.
Download the jar file from Stanford NLP site
- Run the MainApp.java file
All the dependencies can be downloaded from Stanford NLP site .
// creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and sentiment
props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
StanfordCoreNLP pipeline; = new StanfordCoreNLP(props);
Run the Annotators on the text and then get the SentimentAnnotatedTree
// this is the parse tree of the current sentence
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
//print the sentiment score using RNNCoreAnnotations
System.out.println("Sentiment Score: " + RNNCoreAnnotations.getPredictedClass(tree));
How to find 5 classes of sentiment classification: very negative, negative, neutral, positive, and very positive.
// this matrix contains the confidences
SimpleMatrix sm = RNNCoreAnnotations.getPredictedClass(tree);
The estimated probability/confidence looks something like this from 'sm' object
Type = dense , numRows = 5 , numCols = 1
0.111
0.758
0.087
0.035
0.009
This project has the following output
Sentence: I had very wonderful day today.
Sentiment Score: 4.0
Sentiment Type: Very positive
Very positive: 64.0%
Positive: 31.0%
Neutral: 3.0%
Negative: 1.0%
Very negative: 1.0%
Expected Result Sentiment: Very positive
Expected Result Found: true