斯坦福NLP情绪评分总是在c#中返回-1

时间:2015-10-08 01:34:30

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

我正在尝试使用从本网站和斯坦福主要网站借来的代码在c#中实施Stanford NLP情绪分析代码。以下代码有效但Score始终为-1。分数应该在0到4之间。任何帮助?

    // We should change current directory, so StanfordCoreNLP could find all the model files automatically
    var curDir = Environment.CurrentDirectory;
    Directory.SetCurrentDirectory(jarRoot);
    var pipeline = new StanfordCoreNLP(props);
    Directory.SetCurrentDirectory(curDir);

    foreach(String text in texts) {   
        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);

        // run all Annotators on this text
        pipeline.annotate(document);

        // these are all the sentences in this document
        // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
        var sentences = document.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList;

        String[] sentimentText = { "Very Negative","Negative", "Neutral", "Positive", "Very Positive"};

        foreach(CoreMap sentence in sentences) {
            Tree tree = (Tree)sentence.get(typeof(edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation));
            int score = RNNCoreAnnotations.getPredictedClass(tree);
            Console.WriteLine(sentimentText[score]);  // prints sentiment for each sentence in the doc

        }

1 个答案:

答案 0 :(得分:1)

找到解决方案:

变化:

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

为:

Tree tree = (Tree)sentence.get(typeof(edu.stanford.nlp.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree));