Getting the error while integrating stanford sentiment analysis with java

别来无恙 提交于 2020-01-02 04:33:05

问题


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.

My code is as follows:

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)  

回答1:


You are missing the ejml-0.23.jar, add it to your class path and it should work.




回答2:


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

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;

     }


 }



回答3:


I was getting same error and I added all the jar files and the code got executed. Then I found out by removing one-one jar file and running the code and answer was add the following files.

Image



来源:https://stackoverflow.com/questions/23492268/getting-the-error-while-integrating-stanford-sentiment-analysis-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!