java - 如何获得多个句子的整体情绪

标签 java jar stanford-nlp sentiment-analysis

如何找到多个句子/一段/大段文本的聚合情感。

下面是我基于 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/

相关文章:

java - 无法在 Jenkins 中配置 Maven

java - 在正在运行的 java jar 中打开文件

java - 强制 stanford 解析器接受未经解析器词典许可的 POS 标签

stanford-nlp - 如何基于 stanford-nlp 条件随机场模型训练法国 NER?

Java:当嵌套子对象处于 Activity 状态时锁定父对象(迭代器)

java - 何时重新编译 JNI 绑定(bind)和客户端代码?

java - 平台强制版本控制机制是 Java 最需要的特性吗?

xml - 我的xml文件未从root用户保存到cloudera中的hadoop

java - 线程 "main"java.lang.Error : unresolved variable 中出现异常

java - 如何从 java jar 创建 windows 服务?