2016-04-23 77 views
0

我試圖使用斯坦福核心NLP獲取生動推文的感傷評分。目前我正在收到實時推文。但是當我將它提供給Stanford Core NLP程序時,我收到了一個錯誤。SentimentCoreAnnotations.AnnatedatedTree無法解析爲類型

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.AnnotatedTree.class); 
       int sentiment = RNNCoreAnnotations.getPredictedClass(tree); 
       String partText = sentence.toString(); 
       if (partText.length() > longest) { 
        mainSentiment = sentiment; 
        longest = partText.length(); 
       } 

      } 
     } 
     return mainSentiment; 
    } 
} 

我從類NLP調用此init()和findSentiment()函數。

NLP.init(); 
System.out.println(status.getText() + " : " + NLP.findSentiment(status.getText())); 

我的錯誤是:SentimentCoreAnnotations.AnnotatedTree不能被解析爲一個類型

回答

3

嘗試改變

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

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