2014-05-06 26 views
5

Tôi đang làm việc trên phân tích tình cảm bằng cách sử dụng thư viện tình cảm stanford nlp với java. Nhưng khi tôi thực thi mã tôi nhận được lỗi. Không thể hình dung ra.Nhận được lỗi trong khi tích hợp phân tích tình cảm stanford với java

Mã của tôi là như sau:

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

Nhưng khi tôi đang chạy mã tôi đang nhận được lỗi sau.

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

Bạn có thêm tất cả các thư viện vào classpath của bạn chứa? – markusw

Trả lời

4

Bạn đang thiếu ejml-0.23.jar, thêm nó vào đường dẫn lớp và nó sẽ hoạt động.

+0

Phiên bản là chìa khóa ở đây. Nếu bạn cố gắng sử dụng quá mới của một phiên bản (như viết 0.29 là mới nhất), bạn kết thúc với một vòng mới của tin nhắn ClassNotFoundException. – demongolem

+0

cảm ơn nó đã giúp tôi rất nhiều – user1

4

Bạn thiếu tham chiếu đến Efficient Java Matrix Library (EJML) trong số classpath của mình.

BTW. Hãy thử chia mã của bạn thành các phương thức tác vụ đơn nhỏ hơn để có được mã rõ ràng hơn.

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

Tôi đã gặp lỗi tương tự và tôi đã thêm tất cả các tệp jar và mã đã được thực hiện. Sau đó, tôi phát hiện ra bằng cách loại bỏ một tệp jar và chạy mã và câu trả lời là thêm các tệp sau đây.

ảnh enter image description here

Các vấn đề liên quan