2016-04-23 46 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);