在将stanford情绪分析与java集成时获取错误 [英] Getting the error while integrating stanford sentiment analysis with java

查看:253
本文介绍了在将stanford情绪分析与java集成时获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用stanford情感nlp库和java进行情绪分析。但是当我执行代码时,我收到错误。无法弄清楚。

I am working on sentiment analysis using stanford sentiment nlp library with java. But when I am executing the code I am getting the error. Not able to figure it out.

我的代码如下:

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 "";
     }
 }

但是当我运行代码时,我得到了跟随错误。

But when i am running the code i am getting the following error.

   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)  


推荐答案

您缺少对高效Java矩阵库的引用类路径中的(EJML)。

You are missing the reference to Efficient Java Matrix Library (EJML) in your classpath.

BTW。尝试将代码拆分为较小的单一任务方法,以获得更清晰的代码。

BTW. Try to split your code into smaller single task methods to get more clear code.

class SentimentAnalysis {

     public String findSentiment(String line) {

        if(line == null || line.isEmpty()) {
          throw new IllegalArgumentException("The line must not be null or empty.");
        }

        Annotation annotation = processLine(line);

        int mainSentiment = findMainSentiment(annotation);

        if(mainSentiment < 0 || mainSentiment == 2 || mainSentiment > 4) { //You should avoid magic numbers like 2 or 4 try to create a constant that will provide a description why 2
           return null; //You shold avoid null returns 
        }

        return "";

     }


     private int findMainSentiment(Annotation annotation) {

        int mainSentiment = Integer.MIN_VALUE;
        int longest = Integer.MIN_VALUE;


        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {

           int sentenceLength = String.valueOf(sentence).length();

           if(sentenceLength > longest) {

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

             mainSentiment = RNNCoreAnnotations.getPredictedClass(tree);

             longest = sentenceLength ;

            }
        }

        return mainSentiment;

     }


     private Annotation processLine(String line) {

        StanfordCoreNLP pipeline = createPieline();

        return pipeline.process(line);

     }

     private StanfordCoreNLP createPieline() {

        Properties props = createPipelineProperties();

        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        return pipeline;

     }

     private Properties createPipelieProperties() {

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

        return props;

     }


 }

这篇关于在将stanford情绪分析与java集成时获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆