【问题标题】:Sentiment analysis with stanford nlp does not work斯坦福 nlp 的情绪分析不起作用
【发布时间】:2015-09-01 16:08:47
【问题描述】:

我正在尝试使用 stanford nlp 来获取文本的情绪: 这是我的代码:

import java.util.Properties;

import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;

public class SentimentAnalyzer {

    public static void main(String[] args) {
        findSentiment("");
    }

    public static void findSentiment(String line) {
        line = "I started taking the little pill about 6 years ago.";
        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) {
            System.out.println("Neutral " + line);
        }
        else{
        }
        /*
         * TweetWithSentiment tweetWithSentiment = new TweetWithSentiment(line,
         * toCss(mainSentiment)); return tweetWithSentiment;
         */

    }
}

我也使用此链接中的说明: https://blog.openshift.com/day-20-stanford-corenlp-performing-sentiment-analysis-of-twitter-using-java/

但我收到以下错误:

Exception in thread "main" java.lang.NullPointerException
at edu.stanford.nlp.rnn.RNNCoreAnnotations.getPredictedClass(RNNCoreAnnotations.java:58)
at SentimentAnalyzer.findSentiment(SentimentAnalyzer.java:27)
at SentimentAnalyzer.main(SentimentAnalyzer.java:14)

指向这一行的:

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

【问题讨论】:

    标签: java stanford-nlp sentiment-analysis


    【解决方案1】:

    改用这个:

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

    编辑: 要获得正面、负面和中立的 cmets,请使用这个 sn-p:

    switch (mainSentiment) {
            case 0:
                return "Very Negative";
            case 1:
                return "Negative";
            case 2:
                return "Neutral";
            case 3:
                return "Positive";
            case 4:
                return "Very Positive";
            default:
                return "";
            }
    

    【讨论】:

    • 非常感谢,它现在工作了我如何查看句子是中性还是 pos 或 neg?
    • 用代码 sn-p 更新了注释,以显示句子是神经的、正面的还是负面的。
    • 你太棒了伙计:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多