在C#中使用Stanford NLP库,当试图获得情绪(积极/消极)时,它总是返回-1!知道为什么吗

本文关键字:消极 返回 为什么 积极 Stanford NLP | 更新日期: 2023-09-27 18:06:25

我正在尝试使用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);

树的值-{(ROOT(S(NP(NN矩阵(((VP(VBZ is((NP(DT a((JJ good((NN movie((((}

在试图确定情绪时,仍获得-1的返回值。

在C#中使用Stanford NLP库,当试图获得情绪(积极/消极)时,它总是返回-1!知道为什么吗

 Tree tree =(Tree)sentence.get(typeof(SentimentCoreAnnotations.SentimentAnnotatedTree));
 int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

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

您忘记将情感注释器添加到注释器列表中,这就是为什么结果仍然是-1。

首先更新代码:

Tree tree =(Tree)sentence.get(typeof(SentimentCoreAnnotations.SentimentAnnotatedTree));
int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

然后你还需要更改这部分代码:

props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, sentiment");

我已经测试过了,它有效!

等价于"class"字段的C#是"typeof"运算符:

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