NLP总是将情绪作为-1返回

时间:2017-08-16 09:37:04

标签: java nlp stanford-nlp

NLP库始终将情绪整数返回为-1

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

public class NLP {
    static StanfordCoreNLP pipeline;

    public static void init() {
        pipeline = new StanfordCoreNLP("MyPropFile.properties");
    }

    public static int findSentiment(String tweet) {

        int mainSentiment = 0;
        if (tweet != null && tweet.length() > 0) {
            int longest = 0;
            Annotation annotation = pipeline.process(tweet);
            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();
                }

            }
        }
        return mainSentiment;
    }
}

无论我传递什么句子,它总是返回-1。 示例:“Google is good”返回-1           “Google很糟糕”返回-1

1 个答案:

答案 0 :(得分:1)

只需更改此行

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

到这个

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

现在应该可以了。