【发布时间】:2014-05-06 11:23:04
【问题描述】:
我正在使用斯坦福 nlp 情绪分析。我已经从一个博客中尝试过这段代码,但我无法获得诸如“正面”或“负面”之类的陈述的情绪值或一些分数。
以下是代码。
public class SemanticAnalysis {
public static void main(String args[]) {
sentimentAnalysis sentiments = new sentimentAnalysis();
sentiments.findSentiment("Stanford University is located in California. " +
"It is a great university");
}
}
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 "";
}
}
【问题讨论】:
标签: java stanford-nlp sentiment-analysis