2016-04-23 8 views
1

Ich arbeite an einem Projekt, das Echtzeit-Tweets analysiert und die Stimmungen des Benutzers identifiziert. Also verwende ich twitter4j, um Echtzeit-Tweets zu erhalten und diese Tweets an Stanfords Core NLP zu senden. Ich empfange die Echtzeit-Tweets korrekt. Aber wenn ich diese Tweets zu Stanfords Core NLP füttere, bekomme ich einen Laufzeitfehler.Ausnahme im Thread "Twitter4J Async Dispatcher [0]" java.lang.NoClassDefFoundError

PrintSampleStream Klasse, die Echtzeit-Tweets mit Twitter4J bekommt:

import javax.swing.JDialog; 
import javax.swing.JOptionPane; 

import javax.swing.Timer; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import twitter4j.*; 
import twitter4j.conf.*; 

public class PrintSampleStream { 

    private String twitter_handle; 

    PrintSampleStream() 
    { 
     twitter_handle = null; 
    } 

    PrintSampleStream(String tw) 
    { 
     twitter_handle = tw; 
    } 

    public void twitterConnector() throws TwitterException { 
     ConfigurationBuilder cb = new ConfigurationBuilder(); 
      cb.setDebugEnabled(true).setOAuthConsumerKey("bbbb") 
        .setOAuthConsumerSecret("bbbb") 
        .setOAuthAccessToken("bbbb") 
        .setOAuthAccessTokenSecret("bbbb"); 
      TwitterStream twitterStream = new TwitterStreamFactory(cb.build()) 
        .getInstance(); 
     StatusListener listener = new StatusListener() { 
      @Override 
      public void onStatus(Status status) { 
       System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); 
       NLP.init(); 
       System.out.println(status.getText() + " : " + NLP.findSentiment(status.getText())); 
       //storeTweets(status.getText()); 
       //JOptionPane.showMessageDialog(null, status.getText()); 
      } 

      @Override 
      public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { 
       System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); 
      } 

      @Override 
      public void onTrackLimitationNotice(int numberOfLimitedStatuses) { 
       System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); 
      } 

      @Override 
      public void onScrubGeo(long userId, long upToStatusId) { 
       System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); 
      } 

      @Override 
      public void onStallWarning(StallWarning warning) { 
       System.out.println("Got stall warning:" + warning); 
      } 

      @Override 
      public void onException(Exception ex) { 
       ex.printStackTrace(); 
      } 
     }; 
     twitterStream.addListener(listener); 
     FilterQuery filtre = new FilterQuery(); 
     String[] keywordsArray = {twitter_handle}; 
     filtre.track(keywordsArray); 
     twitterStream.filter(filtre); 
    } 
} 

NLP-Klasse, die Echtzeit-Tweets von Twitter4J Stanford-Core NLP erhalten Feeds:

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

public class NLP { 
    static StanfordCoreNLP pipeline; 

    public static void init() { 
     pipeline = new StanfordCoreNLP("MyPropFile.properties"); 
    } 

    public static int findSentiment(String tweet) { 

     int mainSentiment = 0; 
     if (tweet != null && tweet.length() > 0) { 
      int longest = 0; 
      Annotation annotation = pipeline.process(tweet); 
      for (CoreMap sentence : annotation 
        .get(CoreAnnotations.SentencesAnnotation.class)) { 
       Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); 
       int sentiment = RNNCoreAnnotations.getPredictedClass(tree); 
       String partText = sentence.toString(); 
       if (partText.length() > longest) { 
        mainSentiment = sentiment; 
        longest = partText.length(); 
       } 

      } 
     } 
     return mainSentiment; 
    } 
} 

Mein Laufzeitfehler ist:

@laliyaD - Lalinda feels tired 
Exception in thread "Twitter4J Async Dispatcher[0]" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<clinit>(StanfordCoreNLP.java:99) 
    at NLP.init(NLP.java:13) 
    at PrintSampleStream$1.onStatus(PrintSampleStream.java:38) 
    at twitter4j.StatusStreamImpl.onStatus(StatusStreamImpl.java:75) 
    at twitter4j.StatusStreamBase$1.run(StatusStreamBase.java:105) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 8 more 

Eigentlich bekomme ich die Echtzeit-Tweets von twitter4 j. Irgendeine Hilfe?

Antwort

1

Sie benötigen SLF4J (Simple Protokollierung Fassade für Java) herunterladen und es in Ihrem Classpath.

Sie benötigen mindestens slf4j-api-1.7.21.jar und slf4j-simple-1.7.21.jar, um Protokollmeldungen aus der NLP-Bibliothek anzeigen zu können.

http://www.slf4j.org/download.html

+0

Es hat viele JAR-Dateien. Welche JAR-Datei/Dateien sollte ich aufnehmen? –

+0

@LalindaSampath: Sie brauchen die oben erwähnten '' api'' und '' einfachen '' Gläser. – ck1

1

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory bedeutet, dass die Bibliothek slf4j in Ihrem Klassenpfad benötigt wird.

Wenn Sie Maven verwenden, können Sie diese Abhängigkeit verwenden:

<dependency> 
<groupId>org.slf4j</groupId> 
<artifactId>slf4j-simple</artifactId> 
<version>1.7.21</version> 
</dependency> 
+0

Ich benutze nicht maven. Können Sie mir sagen, welche Dateien ich in meinen Java-Build-Pfad von der slf4j-Bibliothek aufnehmen soll? –

Verwandte Themen