使用Stanford NLP进行的情绪分析不起作用 [英] Sentiment analysis with stanford nlp does not work

查看:266
本文介绍了使用Stanford NLP进行的情绪分析不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use stanford nlp to get the sentiment of a text: Here is my code:

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/

Also I use the instruction from this link: 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);

推荐答案

请改用此:

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

修改: 要获得正面,负面和中立的评论,请使用以下代码段:

To get positive, negative, and neutral comments, use this snippet:

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

这篇关于使用Stanford NLP进行的情绪分析不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆