我正在研究斯坦福大学的 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 "";
}
}
最佳答案
您究竟希望这样做做什么?您拥有的 sentimentAnalysis
类仅处理情绪并返回 null
或 ""
,并且您不会对该返回值执行任何操作。此代码不会向用户提供任何反馈。
也许您应该在调试器中运行它,或者在其中抛出几个打印语句,以便您可以弄清楚它在做什么并找到合理的返回值。
您可以阅读大量文档来查找所需内容。如果 API for the Stanford NLP library 我会感到惊讶并没有告诉您需要知道的一切。
关于java - 如何使用 stanford nlp 情感库获得正面或负面的情感陈述?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23493343/