【问题标题】:Incorrect output while using Stanford CoreNLP Sentiment Analysis使用斯坦福 CoreNLP 情绪分析时输出不正确
【发布时间】:2016-12-29 11:10:52
【问题描述】:

当我输入句子时:

“很高兴能回来!我们在这里重新建立联系并结识新朋友 ghc16 的创新者”

那么返回的情绪是负面的。无法理解发生这种情况的原因。该语句是肯定的,但它仍然返回负值。

    class SentimentAnalyzer {

        public TweetWithSentiment findSentiment(String line) {

        if(line == null || line.isEmpty()) {
          throw new IllegalArgumentException("The line must not be null or empty.");
        }

        Annotation annotation = processLine(line);

        int mainSentiment = findMainSentiment(annotation);

        if(mainSentiment < 0 || mainSentiment > 4) { //You should avoid magic numbers like 2 or 4 try to create a constant that will provide a description why 2
           return null; //You should avoid null returns 
        }

        TweetWithSentiment tweetWithSentiment = new TweetWithSentiment(line, toCss(mainSentiment));
        return tweetWithSentiment;

    }

    private String toCss(int sentiment) {
        switch (sentiment) {
        case 0:
            return "very negative";
        case 1:
            return "negative";
        case 2:
            return "neutral";
        case 3:
            return "positive";
        case 4:
            return "very positive";
        default:
            return "default";
        }

     }


     private int findMainSentiment(Annotation annotation) {

        int mainSentiment = Integer.MIN_VALUE;
        int longest = Integer.MIN_VALUE;


        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {

            for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {

                String word = token.get(CoreAnnotations.TextAnnotation.class);
                String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
                String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
                String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);

                System.out.println("word: " + word);
                System.out.println("pos: " + pos);
                System.out.println("ne: " + ne);
                System.out.println("Lemmas: " + lemma);

            }      

           int sentenceLength = String.valueOf(sentence).length();

           if(sentenceLength > longest) {

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

             mainSentiment = RNNCoreAnnotations.getPredictedClass(tree);

             longest = sentenceLength ;

            }
        }

        return mainSentiment;

     }


     private Annotation processLine(String line) {

        StanfordCoreNLP pipeline = createPieline();

        return pipeline.process(line);

     }

     private StanfordCoreNLP createPieline() {

        Properties props = createPipelineProperties();

        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        return pipeline;

     }

     private Properties createPipelineProperties() {

        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment");

        return props;

     }


 }

【问题讨论】:

    标签: java stanford-nlp sentiment-analysis


    【解决方案1】:

    这又是一个技术限制regarding nlp lib itself的案例,主要针对一些具体点:

    1. 模棱两可的情感词 - “这个产品非常好用”与“这个产品非常好”

    2. 遗漏的否定 - “我绝不会在数百万年后说这个产品值得购买”

    3. 引用/间接文本 - “我爸爸说这个产品很糟糕,但我不同意”

    4. 比较 - “这个产品的用处和脑袋上的洞差不多”

    5. 任何微妙之处 - “这款产品丑陋、缓慢且没有吸引力,但它是市场上唯一能胜任这项工作的产品”

    在您的示例中,算法没有任何问题。让我们分别分析一下文本的某些部分:

    • So excited to be back! -> 正面
    • We're here to reconnect with -> 中性
    • meet new innovators at ghc16 -> 中性

    在一个简单的平均数中,我们会有介于 中性积极 之间的东西。但是,正如我们所见,该算法是不可预测的,这就是为什么如果您在文本中添加一个单词(& 也没有得到很好的解释):

    很高兴能回来!我们在这里与你和重新建立联系 ghc16 的新创新者

    ...它会返回 neutral 作为结果。


    建议:

    1. 不要将sentiment 1 视为消极的东西,一旦你将面临这样的情况;
    2. 如果可以控制,尽量让文字正确简洁,以获得更好的效果;
    3. 尽可能多地划分句子,并为每个句子单独运行算法。然后,根据您自己的测试用例进行自定义平均。

    如果没有一个适合,请考虑切换到另一个Machine-learning technique

    【讨论】:

    • 那么这是否意味着“&”被解释为否定,还有其他符号有类似问题吗?
    • 并不是他们消极,而是他们的存在让人困惑,软件可能不明白什么意思。这种混乱使得 nlp 趋向于消极的路径(没有 you,这使它更加混乱)。关键是:如果可以,请替换特殊字符。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多