我的要求是阅读输入文本并进行情绪分析。如果情绪分析是负面的,我应该只得到上下文中使用的否定关键字。
以下是实现情绪分析的代码片段:
字符串推文="电影最糟糕";
属性props = new Properties();
// props.put(" pos.model"," D:\ Stanford_API' s \ english-left3words-distsim.tagger");
props.setProperty(" annotators"," tokenize,ssplit,pos,lemma,parse,sentiment");
pipeline = new StanfordCoreNLP(props);
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);
SimpleMatrix sd= RNNCoreAnnotations.getNodeVector(tree);
System.out.println("---sentiment----"+sd.negative().eig());
//System.out.println("---sd.get(0)----"+sd.get(0));
String partText = sentence.toString();
if (partText.length() > longest)
{
mainSentiment = sentiment;
longest = partText.length();
}
}
}
如果情绪分析是消极的,那么请建议我如何从句子中获取关键词?