如何找到多个句子/一段/大段文本的聚合情感。
下面是我基于 github Stanford 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);
最佳答案
stanford corenlp 中的情绪分析工具包是在句子级数据集上训练的。如果你需要一个文档级的情感引擎,我认为在文档上训练一个新的模型是更好的选择。您也可以尝试一个一个地处理句子,并使用一些棘手的方法(例如平均值、最大值)作为您的基线来测试它是如何工作的。
关于java - 如何获得多个句子的整体情绪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999067/