List of usage examples for edu.stanford.nlp.ie.machinereading.structure AnnotationUtils sentenceToString
public static String sentenceToString(CoreMap sent)
From source file:bi.meteorite.sentiment.NLPStep.java
License:Apache License
private String processString(String document) { // shut off the annoying intialization messages Properties props = new Properties(); //specify the annotators that we want to use to annotate the text. We need a tokenized sentence with POS tags to extract sentiment. //this forms our pipeline props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation = pipeline.process(document); List<Sentence> sentences = new ArrayList<Sentence>(); /*/* w w w . j av a 2s.co m*/ * We're going to iterate over all of the sentences and extract the sentiment. We'll adopt a majority rule policy */ for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) { //for each sentence, we get the sentiment that CoreNLP thinks this sentence indicates. Tree sentimentTree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class); int sentimentClassIdx = RNNCoreAnnotations.getPredictedClass(sentimentTree); SentimentClass sentimentClass = SentimentClass.getSpecific(sentimentClassIdx); /* * Each possible sentiment has an associated probability, so let's pull the entire * set of probabilities across all sentiment classes. */ double[] probs = new double[SentimentClass.values().length]; { SimpleMatrix mat = RNNCoreAnnotations.getPredictions(sentimentTree); for (int i = 0; i < SentimentClass.values().length; ++i) { probs[i] = mat.get(i); } } /* * Add the sentence and the associated probabilities to our list. */ String sentenceStr = AnnotationUtils.sentenceToString(sentence).replace("\n", ""); sentences.add(new Sentence(probs, sentenceStr, sentimentClass)); } SentimentClass sentimentClass = null; if (meta.getAnalysisType().equals("Wilson Score")) { sentimentClass = SentimentRollup.WILSON_SCORE.apply(sentences); } else if (meta.getAnalysisType().equals("Simple Vote Rollup")) { sentimentClass = SentimentRollup.SIMPLE_VOTE.apply(sentences); } else if (meta.getAnalysisType().equals("Longest Sentence Wins")) { sentimentClass = SentimentRollup.LONGEST_SENTENCE_WINS.apply(sentences); } else if (meta.getAnalysisType().equals("Last Sentence Wins")) { sentimentClass = SentimentRollup.LAST_SENTENCE_WINS.apply(sentences); } else if (meta.getAnalysisType().equals("Average Probabilities Rollup")) { sentimentClass = SentimentRollup.AVERAGE_PROBABILITIES.apply(sentences); } if (sentimentClass != null) { return sentimentClass.toString(); } else return null; }