【问题标题】:How to get the sentimental statement like positive or negative using stanford nlp sentiment library?如何使用 stanford nlp 情感库获得正面或负面的情感陈述?
【发布时间】:2014-05-06 11:23:04
【问题描述】:

我正在使用斯坦福 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 "";
    }
}

【问题讨论】:

    标签: java stanford-nlp sentiment-analysis


    【解决方案1】:

    您究竟希望这会发生什么?你在那里的sentimentAnalysis 类只处理情绪并返回null"",你没有对该返回值做任何事情。此代码不会向用户提供任何反馈。

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

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

    【讨论】:

    • 是的,我添加了打印语句,有时我得到的分数是 1、2、3 或 4。问题是当我将形容词从好到坏或好时,分数几乎没有任何变化。它仍然给我分数为 2。当我打印“主要情绪”变量时
    • 我认为斯坦福 NLP 库需要经过培训才能以任何有意义的方式使用。你有没有事先给它任何训练数据?
    【解决方案2】:

    这是我写的两个函数,

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

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

    例如,下面这句话将返回“Negative:Negative:”:

    无论 Apple 公布第一季度业绩时销售的 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();
    }
    

    【讨论】:

      【解决方案3】:

      你得到的感觉很好

      int 情绪 = RNNCoreAnnotations.getPredictedClass(tree);

      是分数...范围在 0-4 之间。

      我稍微修改了代码,为您提供正/负“分数”

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 2015-02-19
        • 1970-01-01
        相关资源
        最近更新 更多