。その後、RNNCoreAnnotations
クラスを通じて予測を得ることができます。 CoreNLP Sentiment予測の各ラベルのスコアを取得する方法を示す以下の自己完結型デモコードを書きました。
import java.util.Arrays;
import java.util.List;
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 DemoSentiment {
public static void main(String[] args) {
final List<String> texts = Arrays.asList("I am happy.", "This is a neutral sentence.", "I am very angry.");
final Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
final StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
for (String text : texts) {
final Annotation doc = new Annotation(text);
pipeline.annotate(doc);
for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
final Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
final SimpleMatrix sm = RNNCoreAnnotations.getPredictions(tree);
final String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
System.out.println("sentence: "+sentence);
System.out.println("sentiment: "+sentiment);
System.out.println("matrix: "+sm);
}
}
}
}
出力は以下の通りです何に(一部の浮動小数点丸め誤差または更新されたモデルは、スコアを変更する場合があります)似ています。
最初の文I am happy.
について、あなたは4位で、返されたマトリックス中の最高値は0.618
で、感情がPositive
であることがわかり、とすることができ、順序付きリストとして行列を解釈するとき。
第2文のThis is a neutral sentence.
は、中位のスコアが0.952
であるため、Neutral
のセンチメントです。
最後の文は、対応してNegative
センチメントを持ち、最高スコアは0.652
で、2番目の位置にあります。
sentence: I am happy.
sentiment: Positive
matrix: Type = dense , numRows = 5 , numCols = 1
0.016
0.037
0.132
0.618
0.196
sentence: This is a neutral sentence.
sentiment: Neutral
matrix: Type = dense , numRows = 5 , numCols = 1
0.001
0.007
0.952
0.039
0.001
sentence: I am very angry.
sentiment: Negative
matrix: Type = dense , numRows = 5 , numCols = 1
0.166
0.652
0.142
0.028
0.012