[英]Estimating document polarity using R's qdap package without sentSplit
我想將qdap
的polarity
函數應用於文檔向量,每個文檔可以包含多個句子,並獲得每個文檔的相應極性。 例如:
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
但不適用於其他文本集(我不確定除句點之外的完整中斷集)。
所以,我懷疑接近這個的最好方法是使文檔向量的每個元素看起來像一個長句子。 我該怎么做,還是有另一種方式?
Max在這個版本的qdap(1.3.4)中發現了一個錯誤,它將占位符計為一個影響等式的單詞,因為分母是sqrt(n)
,其中n
是單詞count。 從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
請參閱文檔了解更多信息。
看起來像刪除標點和其他額外的技巧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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.