2016-08-10 7 views
3

私のubuntuインスタンスにCoreNLPサーバーをセットアップしても問題ありません。 Iセンチメントモジュールで、より興味を持っており、現在私が得るあなたがここに見るように、私は必要なものCoreNLP Sentimentのスコア分布値を取得するにはどうすればよいですか?

{ 
sentimentValue: "2", 
sentiment: "Neutral" 
} 

であることは、分布値をスコアです:http://nlp.stanford.edu:8080/sentiment/rntnDemo.html

"scoreDistr": [0.1685, 0.7187, 0.0903, 0.0157, 0.0068] 

私は何をしないのですかどのように私は、このようなデータを得るのですか?あなたの注釈付きの文からSentimentCoreAnnotations.SentimentAnnotatedTree.class経由ツリーオブジェクトを取得する必要があります

おかげ

答えて

2

。その後、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 
関連する問題