|
/∗ |
|
To change this license header, choose License Headers in Project Properties. |
To change this template file, choose Tools | Templates |
and open the template in the editor. |
/ |
package servlet1; |
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; |
/ |
|
@author jayanthi |
/ |
public class SentimentAnalyzer |
{ |
static Properties props; |
static StanfordCoreNLP pipeline; |
public void initialize(String path) |
{ |
// creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and sentiment |
props = new Properties(); |
props.setProperty(“parse.model”, path+”edu\\stanford\\nlp\\models\\lexparser\\englishPCFG.ser.gz”); |
props.setProperty(“sentiment.model”, path+”edu\\stanford\\nlp\\models\\sentiment\\sentiment.ser.gz”); |
props.setProperty(“annotators”, “tokenize, ssplit, parse, sentiment”); |
pipeline = new StanfordCoreNLP(props); |
//LexicalizedParser lp = LexicalizedParser.loadModel(“edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz”); |
} |
public SentimentResult getSentimentResult(String text) { |
SentimentResult sentimentResult = new SentimentResult(); |
SentimentClassification sentimentClass = new SentimentClassification(); |
if (text ! = null && text.length() > 0) { |
// run all Annotators on the text |
Annotation annotation = pipeline.process(text); |
for (CoreMap sentence: annotation.get(CoreAnnotations.SentencesAnnotation.class)) { |
// this is the parse tree of the current sentence |
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); |
SimpleMatrix sm = RNNCoreAnnotations.getPredictions(tree); |
String sentimentType = sentence.get(SentimentCoreAnnotations.SentimentClass.class); |
sentimentClass.setVeryPositive((double)Math.round(sm.get(4) 100d)); |
sentimentClass.setPositive((double)Math.round(sm.get(3) 100d)); |
sentimentClass.setNeutral((double)Math.round(sm.get(2) 100d)); |
sentimentClass.setNegative((double)Math.round(sm.get(1) 100d)); |
sentimentClass.setVeryNegative((double)Math.round(sm.get(0) 100d)); |
sentimentResult.setSentimentScore(RNNCoreAnnotations.getPredictedClass(tree)); |
sentimentResult.setSentimentType(sentimentType); |
sentimentResult.setSentimentClass(sentimentClass); |
} |
} |
Return sentimentResult; |
} |
} |
|