情绪评分斯坦福核心NLP

Tan*_*eer 2 stanford-nlp sentiment-analysis

我们如何使用斯坦福核心NLP获得完整句子的情绪评分?

它将完整的句子分为正面和负面的情绪,但我们可以得到斯坦福NLP工具的总情绪分数吗?

小智 5

我所做的是根据句子长度平均每个句子的分数.其背后的逻辑是,较长的句子应该比较短的句子更重要.

代码如下所示:

String line = "Great item! HDMI and decent wifi required as with all streaming devices.\n" +
            "The flow on the homepage is very good and responsive. Watching a series is a doddle, flow is great, no action required.\n" +
            "The remote and controller app both work a treat.\n" +
            "I really like this device.\n" +
            "I'd like to see an Amazon-written mirroring app available for non-Amazon products but no-one likes talking to each other in this field!";

    Long textLength = 0L;
    int sumOfValues = 0;

    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) {
                textLength += partText.length();
                sumOfValues = sumOfValues + sentiment * partText.length();

                System.out.println(sentiment + " " + partText);
            }
        }
    }

    System.out.println("Overall: " + (double)sumOfValues/textLength);
Run Code Online (Sandbox Code Playgroud)

这里下载整个项目