java - 斯坦福情感分析评分java

标签 java sentiment-analysis stanford-nlp

我使用 Stanford core NLP情感分析库。下面的代码返回了一个例子的类,但我怎样才能得到分数呢?例如 -0.3 表示负等

private int getScore(String line) {
    boolean isrun = false;
    StanfordCoreNLP pipeline = null;
    if(!isrun){
        Properties props = getProperties();
        pipeline = new StanfordCoreNLP(props);
        isrun = true;
    }
    Annotation annotation;

    int sentiment = -1;
    if (line != null && line.length() > 0) {
        annotation = pipeline.process(line);
        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
            sentiment = RNNCoreAnnotations.getPredictedClass(tree);
        }
    }
    return sentiment;
}

编辑

在线demo当鼠标位于图表的根部时,我们可以看到该示例为负 72%。如何获得这个数字?

最佳答案

0.下载斯坦福NLP核心库并导入外部库stanford-corenlp-3.5.2-models.jar、stanford-corenlp-3.5.2.jar、stanford-corenlp-3.5.2-sources.jar和ejml- 0.23.jar 到这个包中。

1.在Eclipse中构建此类NLP

import java.util.Properties;
import org.ejml.simple.SimpleMatrix;
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.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;

public class NLP {
static StanfordCoreNLP pipeline;

public static void init() {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
    pipeline = new StanfordCoreNLP(props);
}

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(SentimentAnnotatedTree.class);
            int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
            SimpleMatrix sentiment_new = RNNCoreAnnotations.getPredictions(tree);             
            String partText = sentence.toString();
            if (partText.length() > longest) {
                mainSentiment = sentiment;
                longest = partText.length();
            }
        }
    }
    return mainSentiment;
    }
}

2.建立一个新类来用NLP解析你的句子

import java.util.ArrayList;

public class What2Think {

    public static void main(String[] args) {
        ArrayList<String> tweets = new ArrayList<String>();
        tweets.add("In this country, \"democracy\" means pro-government. #irony");
        NLP.init();
        for(String tweet : tweets) {
            System.out.println(tweet + " : " + NLP.findSentiment(tweet));
        }
    }
}

运行它

关于java - 斯坦福情感分析评分java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28014779/

相关文章:

java - 从语法结构到树

machine-learning - 为什么斯坦福主题建模工具包识别出的 30 个主题如此相似?

java - 将字符串转换为时间格式

java - 无法从命令行运行已编译的 .classes

java - 创建抽象 Activity 类

python - 情感分析/线性回归(Django)

java - JAXB - 控制包装类中列表的元素名称

python - 在 PySpark 中使用字典进行情感分析

python - Lime 解释器显示与分类器预测不同的预测概率 - 情感分析

java - 如何修改StanfordNLP中的TokenRegex规则?