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

时间:2014-05-06 11:23:05

标签: java stanford-nlp sentiment-analysis

我正在使用斯坦福大学的nlp情绪分析。我从一个博客尝试过这个代码,但是我无法获得像#34; positive"这样的语句的情感值。或者"否定"或者一些分数。

以下是代码。

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 "";
    }
}

3 个答案:

答案 0 :(得分:1)

你到底想要做什么?您在那里的sentimentAnalysis类仅处理情绪并返回null"",并且您没有对该返回值执行任何操作。此代码不向用户提供任何反馈。

也许你应该在调试器中运行它或在那里抛出几个print语句,这样你就可以弄清楚它在做什么并找到合理的返回值。

您可以阅读大量文档,了解您正在寻找的内容。如果API for the Stanford NLP library没有告诉你你需要知道的一切,我会感到惊讶。

答案 1 :(得分:1)

这是我写的两个函数,

用于初始化情绪管道和另一个

function processTextSentiment返回字符串文本中所有句子的情感类,并将参数传递给此函数

例如,以下句子将返回&#34;否定:否定:&#34;:

无论苹果报告第一季度业绩的iPhone售出数量多少,它可能主要代表公司在本季度能够提升供应的能力,因为需求仍超过供应量只剩下几个星期了。 2015年及以后购买1股优质股票 2015年正在成为股市的另一个伟大的一年。

public static void initializeSentiPipeline(){
    tokenizerProps = new Properties();
    tokenizerProps.setProperty("annotators", "tokenize, ssplit");
    tokenizer = new StanfordCoreNLP(tokenizerProps);
    pipelineProps = new Properties();
    pipelineProps.setProperty("annotators", "parse, sentiment");
    pipelineProps.setProperty("enforceRequirements", "false");
    sentipipeline = new StanfordCoreNLP(pipelineProps);
}
public static String processTextSentiment(String text){
    Annotation annotation = tokenizer.process(text);
    sentipipeline.annotate(annotation);
    StringBuilder sb = new StringBuilder(32);
    for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
        System.out.println("  " + sentence.get(SentimentCoreAnnotations.ClassName.class));
        sb.append(sentence.get(SentimentCoreAnnotations.ClassName.class));
        sb.append(':');
    }
    return sb.toString();
}

答案 2 :(得分:-1)

你的情绪很好

int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

是得分......它的范围在0-4之间。

我稍微修改了代码,为你提供了积极/消极的评分&#34;

    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)) {
        Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
        int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

        String partText = sentence.toString();
        System.out.println("Sentence: '" + partText + "' is rather " + sentimentText[sentiment]);

        if (partText.length() > longest) {
            mainSentiment = sentiment;
            longest = partText.length();
        }
    }

    if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
        System.out.println("Overall it was sort of neutral review");
    }
    else if (mainSentiment > 2) {
        System.out.println("Overall we are happy");
    }
    else {
        System.out.println("Bottom line. We are displeased");
    }