【发布时间】:2014-02-24 21:10:16
【问题描述】:
你如何找到多个句子/一个段落/大段文本的聚合情绪。
下面的代码基于 github 斯坦福 CoreNLP 测试和各种示例,但我发现的所有内容都已完成情感分析,仅计算单个句子的情感。但我想要整个推文的情绪,不管里面有多少句子。
我能想到的唯一其他方法是为SentimentPipeline.main(String[]) 创建一个单独的线程并将文本提供给stdin,并在sdout 中收集整体情绪。我更希望能够使用我的代码使其更简单/更高效,但我没有找到任何东西。
另外,我不想像大多数人那样对 jar 进行系统调用,因为我每天要发送数百万条推文。每次加载资源的开销太大了。
Annotation document = new Annotation(text);
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
String output;
for (CoreMap sentence : sentences) {
// traversing the words in the current sentence a CoreLabel is a CoreMap with additional token-specific methods
output = "";
for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
// this is the text of the token
String word = token.get(TextAnnotation.class);
// this is the Parts Of Speech tag of the token (noun, verb, adjective etc)
// String pos = token.get(PartOfSpeechAnnotation.class);
// this is the NER label of the token
String ne = token.get(NamedEntityTagAnnotation.class);
if (!ne.contentEquals("O")) {
output = output + (ne + " " + word + " ");
}
}
//**************Sentiment Analysis
Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
String sentiment = RNNCoreAnnotations.getPredictedClass(tree);
【问题讨论】:
-
如果你在这里找不到答案,也可以考虑发帖到官方java-nlp-user mailing list,如果你还没有。
标签: java jar stanford-nlp sentiment-analysis