简体   繁体   English

在C#中使用Stanford NLP lib,在尝试获取情绪(正/负)时,它始终返回-1! 知道为什么吗?

[英]Using Stanford NLP lib in C# , While trying to get sentiment (positive/negative) it always returns -1! Any idea why?

I am trying to check if a statement is positive or negative using Stanford Core NLP. 我正在尝试使用Stanford Core NLP检查语句是肯定还是否定。

I found a few references online in Java and was able to convert/code in the missing pieces to C#. 我在Java网上找到了一些参考,并且能够将缺少的部分转换/编码为C#。

While trying to get the sentiment score - I always get -1 as the return value. 在尝试获得情感得分时-我总是得到-1作为返回值。

I think it could be because I was not able to convert 我认为可能是因为我无法转换

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

To its .NET equivalent. 与其.NET等效。

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

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

Here is the complete code: 这是完整的代码:

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();
        }
    }

Any ideas why I maybe getting -1 as the return value for the sentiment? 有什么想法为什么我可能将-1作为情感的返回值?

Here's the updated code:- 这是更新的代码:

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

                int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

Value of tree - {(ROOT (S (NP (NN matrix)) (VP (VBZ is) (NP (DT a) (JJ good) (NN movie)))))} 树的值-{(ROOT(S(NP(NN矩阵))(VP(VBZ是)(NP(DT a)(JJ好)(NN电影)))))}

Still getting the return value as -1 while trying to determine the sentiment. 在尝试确定情感时,仍然获得返回值为-1

与“ class”字段等效的C#是“ typeof”运算符:

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

 int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

use this as the code, i tested. 我用它作为代码。 It works. 有用。

You forgot to add the sentiment annotator to the list of annotators, that's why you still getting -1 as result. 您忘记将情感注释器添加到注释器列表中,这就是为什么结果仍然为-1的原因。

First update the code : 首先更新代码:

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

and then you also need to change this part of code : 然后您还需要更改这部分代码:

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

I've tested it and it works! 我已经对其进行了测试,并且可以正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM