2014-04-01 26 views
3

我想將qdappolarity函數應用於文檔向量,每個文檔可以包含多個句子,並獲取每個文檔的相應極性。例如:使用R的qdap包進行文件極性估計,無需發送SplitSplit

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,但不適用於其他文本集(我不確定除期間以外的全套休息時間)。

所以,我懷疑處理這個問題的最好方法是讓文檔向量的每個元素看起來像一個長句子。我該怎麼做,或者有另一種方式?

+0

刪除結束符是額外的工作,如果你只是想忽略警告。你的結果是一樣的,所以你似乎不需要警告。首先我會說如果它的互動性,那麼你可以忽略一個警告,因爲它只是一個標誌,說這可能是壞的。如果你真的想壓制這個警告,那麼使用'suppressWarnings'並放棄這個訣竅,因爲剝離標點符號只需要額外的時間。還要注意''極性'的算法不再侷限於-1和1. –

+0

@TylerRinker如果算法真的工作相同(即將字符串視爲單個長句),那麼忽略警告沒有問題,但結果不同(見答案)。 –

+1

最大這讓我對他的代碼bugglet。它與單詞數量和逗號處理有關。感謝您的發現。我用find發信給你:https://github.com/trinker/qdap/blob/master/NEWS.md –

回答

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 
2

馬克斯發現了一個bug在這個版本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 

查看更多文檔。