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

时间:2015-08-05 12:21:48

标签: java c# nlp stanford-nlp sentiment-analysis

我正在尝试使用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为)(NP(DT a)(JJ好)(NN电影))))) }

在尝试确定情绪时仍然将返回值设为 -1

3 个答案:

答案 0 :(得分:0)

C#相当于&#34;类&#34; field是&#34; typeof&#34;操作者:

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

答案 1 :(得分:0)

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

 int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

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

答案 2 :(得分:0)

您忘记将情绪注释器添加到注释器列表中,这就是为什么您仍然得到-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");

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