2014-05-06 10 views
5

Pracuję nad analizą nastrojów za pomocą biblioteki nlp sentencji stanforda z java. Ale kiedy wykonuję kod, otrzymuję błąd. Nie jestem w stanie tego rozgryźć.Uzyskiwanie błędu podczas integracji analizy nastrojów Stanforda z java

Mój kod wygląda następująco:

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

Ale kiedy używam kodu otrzymuję następujący błąd.

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) 
+2

Czy dodać wszystkie zawarte bibliotek w ścieżce klas? – markusw

Odpowiedz

4

Ty brakuje ejml-0.23.jar, dodaj go do swojej ścieżce klasy i powinno działać.

+0

Najważniejsza jest tutaj wersja. Jeśli spróbujesz użyć zbyt nowej wersji (od napisania 0.29 jest najnowsza), kończy się nowa runda komunikatów ClassNotFoundException. – demongolem

+0

dzięki bardzo mi pomógł – user1

4

Brakuje odniesienia do Efficient Java Matrix Library (EJML) w swoim classpath.

BTW. Spróbuj podzielić swój kod na mniejsze metody pojedynczego zadania, aby uzyskać bardziej przejrzysty kod.

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; 

    } 


} 
-1

Otrzymałem ten sam błąd i dodałem wszystkie pliki jar i kod został wykonany. Potem dowiedziałem się, usuwając jeden plik jar i uruchamiając kod, a odpowiedzią było dodanie następujących plików.

Obraz enter image description here

Powiązane problemy