gpt4 book ai didi

java - 在 C# 中使用斯坦福 NLP 库,在尝试获取情绪(正面/负面)时,它总是返回 -1!知道为什么吗?

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:06 26 4
gpt4 key购买 nike

我正在尝试使用 Stanford Core NLP 检查一个陈述是肯定的还是否定的。

我在网上找到了一些 Java 引用资料,并且能够将缺失的部分转换/编码为 C#。

在尝试获取情绪分数时 - 我总是得到 -1 作为返回值。

我想这可能是因为我无法转换

 Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);

与其对应的 .NET。

java.lang.Class treeClass = new edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation().getClass();

Tree tree = (Tree)sentence.get(treeClass);

完整代码如下:

var jarRoot = @"D:\Core NLP Files\stanford-corenlp-full-2015-04-20\stanford-corenlp-full-2015-04-20\stanford-corenlp-3.5.2-models";

// Text for processing
var text = txtInp.Text;

// Annotation pipeline configuration
var props = new java.util.Properties();

props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
props.setProperty("sutime.binders", "0");
props.setProperty("ner.useSUTime", "false");

// We should change current directory, so D:\Core NLP Files\stanford-corenlp-full-2015-04-20\stanford-corenlp-full-2015-04-20 could find all the model files automatically
var curDir = Environment.CurrentDirectory;
Directory.SetCurrentDirectory(jarRoot);
var pipeline = new StanfordCoreNLP(props);
Directory.SetCurrentDirectory(curDir);

// Annotation
var annotation = new edu.stanford.nlp.pipeline.Annotation(text);
pipeline.annotate(annotation);

// Result - Pretty Print
using (var stream = new ByteArrayOutputStream())
{
pipeline.prettyPrint(annotation, new PrintWriter(stream));

//Analyze the statement as positive or negative


int mainSentiment = 0;
int longest = 0;
String[] sentimentText = { "Very Negative","Negative", "Neutral", "Positive", "Very Positive"};

NumberFormat NF = new DecimalFormat("0.0000");

//for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class))

var sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList;

foreach(CoreMap sentence in sentences )
{
java.lang.Class treeClass = new edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation().getClass();

Tree tree = (Tree)sentence.get(treeClass);


**int sentiment = RNNCoreAnnotations.getPredictedClass(tree);**

String partText = sentence.ToString();
label1.Text = "Sentence: '" + partText + "' is rather " + sentimentText[sentiment];

if (partText.Length > longest)
{
mainSentiment = sentiment;
longest = partText.Length;
}
}

if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
label1.Text = ("Overall it was sort of neutral review");
}
else if (mainSentiment > 2) {
label1.Text = ("Overall we are happy");
}
else {
label1.Text = ("Bottom line. We are displeased");
}


stream.close();
}
}

为什么我可能会得到 -1 作为情绪的返回值?

这是更新后的代码:-

Tree tree = (Tree)sentence.get(typeof(edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation));

int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

树的值(value) - {(ROOT (S (NP (NN matrix)) (VP (VBZ is) (NP (DT a) (JJ good) (NN movie)))))

在尝试确定情绪时,返回值仍为 -1

最佳答案

 Tree tree =(Tree)sentence.get(typeof(SentimentCoreAnnotations.SentimentAnnotatedTree));

int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

使用这个作为代码,我测试过。它有效。

关于java - 在 C# 中使用斯坦福 NLP 库,在尝试获取情绪(正面/负面)时,它总是返回 -1!知道为什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31832387/

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