【问题标题】:Stanford NLP sentiment score returns -1 always in c#斯坦福 NLP 情绪得分在 c# 中总是返回 -1
【发布时间】:2015-10-08 01:34:29
【问题描述】:

我正在尝试使用从本网站和斯坦福主网站上借来的代码在 c# 中实现斯坦福 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

        }

【问题讨论】:

  • 认真的吗?你认为这是一个可以回答的问题吗?唯一提到scoreint score = RNNCoreAnnotations.getPredictedClass(tree); 该方法显然返回-1 但没有该代码我们怎么能猜到为什么?
  • RNNCoreAnnotations类的分享码。
  • 如果您阅读 RNNCoreAnnotations.getPredictedClass() 的文档,它会告诉您为什么返回 -1:nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/neural/…
  • 这不应该被否决!这是完全可以回答的,对我很有帮助——你只需要了解 stanford-nlp 项目。 Ibexy 的回答是正确的。

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


【解决方案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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多