【问题标题】:Stanford CoreNLP sentiment斯坦福 CoreNLP 情绪
【发布时间】:2014-04-14 15:24:51
【问题描述】:

我正在尝试在 Eclipse 中实现 coreNLP 情感分析器。得到错误:

Unable to resolve "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"

作为类路径、文件名或 URL。我使用 maven 安装了所有 NLP 文件,所以我不确定它为什么要寻找其他东西。这是我遇到错误的代码。

import java.util.Properties;


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 StanfordSentiment {


StanfordCoreNLP pipeline; 



public StanfordSentiment(){
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");

    pipeline = new StanfordCoreNLP(props);


}

public float calculateSentiment (String text) {


        float mainSentiment = 0;

        int longest = 0;
        Annotation annotation = pipeline.process(text);
        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
            int sentiment = RNNCoreAnnotations.getPredictedClass(tree) - 2;
            String partText = sentence.toString();
            if (partText.length() > longest) {
                mainSentiment = sentiment;
                longest = partText.length();
            }

        }

       return mainSentiment;



}
}

【问题讨论】:

  • 原来我需要将 stanford-corenlp-3.3.1-models.jar 添加到构建路径中并且它有效。

标签: java dependencies nlp stanford-nlp


【解决方案1】:
public class SentimentAnalysis {

    public static void main(String[] args) throws IOException {
        String text = "I am very happy";
        Properties props = new Properties();
        props.setProperty("annotators",
                "tokenize, ssplit, pos, lemma, parse, sentiment");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        Annotation annotation = pipeline.process(text);
        List<CoreMap> sentences = annotation
                .get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            String sentiment = sentence
                    .get(SentimentCoreAnnotations.ClassName.class);
            System.out.println(sentiment + "\t" + sentence);
        }
    }
}

希望它会有所帮助..:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多