java - 如何使用 stanford nlp 情感库获得正面或负面的情感陈述?

标签 java stanford-nlp sentiment-analysis

我正在研究斯坦福大学的 NLP 情感分析。我已经在一个博客中尝试过这段代码,但我无法获取诸如“积极”或“消极”或某些分数之类的陈述的情绪值。

以下是代码。

public class SemanticAnalysis {

    public static void main(String args[]) {
        sentimentAnalysis sentiments = new sentimentAnalysis();
        sentiments.findSentiment("Stanford University is located in California. " +
                "It is a great university");
    }

}


class sentimentAnalysis {
    public String findSentiment(String line) {
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        int mainSentiment = 0;

        if (line != null && line.length() > 0) {
            int longest = 0;
            Annotation annotation = pipeline.process(line);

            for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
                Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
                int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
                String partText = sentence.toString();
                if (partText.length() > longest) {
                    mainSentiment = sentiment;
                    longest = partText.length();
                }
            }
        }

        if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
            return null;
        }

        return "";
    }
}

最佳答案

您究竟希望这样做做什么?您拥有的 sentimentAnalysis 类仅处理情绪并返回 null"",并且您不会对该返回值执行任何操作。此代码不会向用户提供任何反馈。

也许您应该在调试器中运行它,或者在其中抛出几个打印语句,以便您可以弄清楚它在做什么并找到合理的返回值。

您可以阅读大量文档来查找所需内容。如果 API for the Stanford NLP library 我会感到惊讶并没有告诉您需要知道的一切。

关于java - 如何使用 stanford nlp 情感库获得正面或负面的情感陈述?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23493343/

相关文章:

java - 已编译的 JSP Java (*__jsp.java) 文件在哪里?

java - 斯坦福分类器与 Weka 分类器

r - R 中的情感分析(不使用 tm.plugin.tags)

python-3.x - 如何使用存储在 csv 中的情绪分析数据的 Pandas 绘制多折线图

python - NLTK 包和其他依赖项出现错误

java - 斯坦福自然语言处理 - 中文情感分析

java - 为什么这个数字要向下舍入?

java - 如何使用位于本地驱动器中的图像文件,将其打印在jsp 页面或servlet 页面中?

java - 通过在标记末尾添加 "/"来格式化 XMl 文档

java - 运行斯坦福大学 NER 和斯坦福大学 POS 标记器时出现不兼容的类更改错误