我正在使用斯坦福情感nlp库和Java进行情感分析。但是,当我执行代码时,我得到了错误。无法弄清楚。

我的代码如下:

package com.nlp;

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

 public class SemanticAnalysis {
    public static void main(String args[]) {
        sentimentAnalysis sentiment = new sentimentAnalysis();
        sentiment.findSentiment("france is a good city");
    }
 }

 class sentimentAnalysis {
     public String findSentiment(String line) {

        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) {
                    mainSentiment = sentiment;
                    longest = partText.length();
                }
            }
        }
        if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
            return null;
        }
        return "";
     }
 }

但是,当我运行代码时,出现以下错误。
   Exception in thread "main" java.lang.NoClassDefFoundError: org/ejml/simple/SimpleBase
at edu.stanford.nlp.pipeline.SentimentAnnotator.<init>(SentimentAnnotator.java:45)
at edu.stanford.nlp.pipeline.StanfordCoreNLP$14.create(StanfordCoreNLP.java:845)
at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:81)

最佳答案

您缺少ejml-0.23.jar,将其添加到您的类路径中,它应该可以工作。

关于java - 在将斯坦福情感分析与Java集成时获取错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23492268/

10-11 22:45
查看更多