首页 > 解决方案 > R:情绪分析包

问题描述

有谁知道为什么R中SentimentAnalysisaggregate包中的函数参数没有对情绪分数进行分组?这是一个简单的可重现示例:analyzeSentiment

> library(SentimentAnalysis)
> documents <- c("Wow, I really like the new light sabers!",
+                "That book was excellent.",
+                "R is a fantastic language.",
+                "The service in this restaurant was miserable.",
+                "This is neither positive or negative.",
+                "The waiter forget about my dessert -- what poor service!")
> Group=factor(c(1,1,2,2,3,3))
> Test_data=data.frame(documents=documents,Group=Group,stringsAsFactors = F)
> sentiment <- analyzeSentiment(x=Test_data$documents,aggregate=Test_data$Group)
> Test_data$SentimentQDAP=sentiment$SentimentQDAP
> Test_data
                                                 documents Group SentimentQDAP
1                 Wow, I really like the new light sabers!     1     0.3333333
2                                 That book was excellent.     1     0.5000000
3                               R is a fantastic language.     2     0.5000000
4            The service in this restaurant was miserable.     2    -0.3333333
5                    This is neither positive or negative.     3     0.0000000
6 The waiter forget about my dessert -- what poor service!     3    -0.4000000

这给了我以下信息:

在此处输入图像描述

函数analyzeSentiment 的帮助说“聚合:可以对文档进行分组的因素变量。这在加入例如同一天的新闻或移动同一作者的评论时很有帮助”。

我的问题是为什么每个组内的分数不一样?

标签: rgroupingsentiment-analysis

解决方案


我检查了函数的源代码analyzeSentiment以查看该参数会发生什么aggregate

有趣的是,它没有在你的代码中做任何事情的原因是因为这个参数本质上是多余的——它根本不会被使用!如果您从初始analyzeSentiment函数开始跟踪调用堆栈,则aggregate参数只会被传递,直到它到达情绪计算的主要中心 - analyzeSentiment.DocumentTermMatrix. 这是计算结果的数据帧然后返回的地方,传递给的值aggregate似乎在代码中的任何地方都没有出现。它被传入并且从未使用过。

为了向后兼容,必须是他们从未添加到包中的功能,或者是以前版本遗留下来的工件。


推荐阅读