我正在尝试使用Stanford Core NLP获得实时推文的感性分数。目前,我正在获取实时推文。但是,当我将其提供给Stanford Core NLP程序时,出现了错误。
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无法解析为类型
最佳答案
尝试改变
Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
至
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
关于java - SentimentCoreAnnotations.AnnotatedTree无法解析为一种类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36813383/