5

我想将qdap'spolarity函数应用于文档向量,每个文档向量可以包含多个句子,并为每个文档获取相应的极性。例如:

library(qdap)
polarity(DATA$state)$all$polarity
# Results:
 [1] -0.8165 -0.4082  0.0000 -0.8944  0.0000  0.0000  0.0000 -0.5774  0.0000
[10]  0.4082  0.0000
Warning message:
In polarity(DATA$state) :
  Some rows contain double punctuation.  Suggested use of `sentSplit` function.

这个警告不容忽视,因为它似乎在文档中添加了每个句子的极性分数。这可能导致文档级别的极性分数超出 [-1, 1] 范围。

我知道先运行sentSplit然后在句子中平均的选项,也许是按字数加权极性,但这是(1)效率低下(大约是在带有警告的完整文档上运行的时间的 4 倍),并且( 2)不清楚如何加重句子。这个选项看起来像这样:

DATA$id <- seq(nrow(DATA)) # For identifying and aggregating documents 
sentences <- sentSplit(DATA, "state")
library(data.table) # For aggregation
pol.dt <- data.table(polarity(sentences$state)$all)
pol.dt[, id := sentences$id]
document.polarity <- pol.dt[, sum(polarity * wc) / sum(wc), "id"]

我希望我可以polarity在删除了句点的向量版本上运行,但似乎sentSplit不止于此。这适用于DATA其他文本集,但不适用于其他文本集(我不确定除了句点之外的完整中断集)。

所以,我怀疑解决这个问题的最好方法是让文档向量的每个元素看起来像一个长句子。我该怎么做,或者有其他方法吗?

4

2 回答 2

2

Max 在此版本的 qdap (1.3.4) 中发现了一个错误,该错误将占位符计为影响等式的单词,因为分母是单词计数的sqrt(n)位置。n从 1.3.5 开始,这已得到纠正,因此两个不同的输出不匹配。

这是输出:

library(qdap)
counts(polarity(DATA$state))[, "polarity"]

## > counts(polarity(DATA$state))[, "polarity"]
##  [1] -0.8164966 -0.4472136  0.0000000 -1.0000000  0.0000000  0.0000000  0.0000000
##  [8] -0.5773503  0.0000000  0.4082483  0.0000000
## Warning message:
## In polarity(DATA$state) : 
##   Some rows contain double punctuation.  Suggested use of `sentSplit` function.

在这种情况下,使用strip无关紧要。它可能在涉及放大器、否定符、否定符和逗号的更复杂的情况下。这是一个例子:

## > counts(polarity("Really, I hate it"))[, "polarity"]
## [1] -0.5
## > counts(polarity(strip("Really, I hate it")))[, "polarity"]
## [1] -0.9

有关更多信息,请参阅文档。

于 2014-04-08T15:57:23.020 回答
0

看起来像删除标点符号和其他额外技巧polarity以认为向量是一个句子:

SimplifyText <- function(x) {
  return(removePunctuation(removeNumbers(stripWhitespace(tolower(x))))) 
}
polarity(SimplifyText(DATA$state))$all$polarity
# Result (no warning)
 [1] -0.8165 -0.4472  0.0000 -1.0000  0.0000  0.0000  0.0000 -0.5774  0.0000
[10]  0.4082  0.0000 
于 2014-04-01T01:59:18.167 回答