gpt4 book ai didi

stanford-nlp - 如何获取 CoreNLP Sentiment 的分数分布值?

转载 作者:行者123 更新时间:2023-12-04 04:13:47 30 4
gpt4 key购买 nike

我在我的 ubuntu 实例上设置了 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 从注释句子中获取树对象。然后,您可以通过 RNNCoreAnnotations 类获取预测结果。我在下面编写了以下独立的演示代码,展示了如何获取 CoreNLP 情绪预测的每个标签的分数。

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.,您可以看到情绪是Positive,并且返回矩阵中的最大值是0.618,在第四个位置,当将矩阵解释为有序列表时。

第二句 This is a neutral sentence. 在中间的分数最高,为 0.952,因此是 Neutral 情绪。

最后一句相应地具有负面情绪,其最高分0.652位居第二。

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

关于stanford-nlp - 如何获取 CoreNLP Sentiment 的分数分布值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881641/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com