拓端tecdat|R語(yǔ)言自然語(yǔ)言處理(NLP):情感分析新聞文本數(shù)據(jù)
原文鏈接:http://tecdat.cn/?p=19095
原文出處:拓端數(shù)據(jù)部落公眾號(hào)
本文對(duì)R中的文本內(nèi)容進(jìn)行情感分析。此實(shí)現(xiàn)利用了各種現(xiàn)有的字典,此外,還可以創(chuàng)建自定義詞典。自定義詞典使用LASSO正則化作為一種統(tǒng)計(jì)方法來(lái)選擇相關(guān)詞語(yǔ)。最后,評(píng)估比較所有方法。
介紹
情感分析是自然語(yǔ)言處理(NLP),計(jì)算語(yǔ)言學(xué)和文本挖掘的核心研究分支。它是指從文本文檔中提取主觀信息的方法。換句話說(shuō),它提取表達(dá)意見(jiàn)的積極負(fù)面極性。人們也可能將情感分析稱(chēng)為?觀點(diǎn)挖掘?(Pang and Lee 2008)。
研究中的應(yīng)用
最近,情感分析受到了廣泛的關(guān)注(K. Ravi和Ravi 2015; Pang和Lee 2008),我們將在下面進(jìn)行探討。當(dāng)前在金融和社會(huì)科學(xué)領(lǐng)域的研究利用情感分析來(lái)理解人類(lèi)根據(jù)文本材料做出的決策。這立即揭示了對(duì)從業(yè)者以及金融研究和社會(huì)科學(xué)領(lǐng)域的從業(yè)者的多種含義:研究人員可以使用R提取與讀者相關(guān)的文本成分,并在此基礎(chǔ)上檢驗(yàn)其假設(shè)。同樣,從業(yè)人員可以衡量哪種措辭對(duì)他們的讀者而言實(shí)際上很重要,并相應(yīng)地提高他們的寫(xiě)作水平(Pr?llochs,F(xiàn)euerriegel和Neumann 2015)。在下面的兩個(gè)案例研究中,我們從金融和社會(huì)科學(xué)中論證了增加的收益。
應(yīng)用
幾個(gè)應(yīng)用程序演示了情感分析在組織和企業(yè)中的用途:
金融:?金融市場(chǎng)的投資者在行使股票所有權(quán)之前,會(huì)以金融新聞披露的形式參考文本信息。有趣的是,它們不僅依賴(lài)數(shù)據(jù),而且還依賴(lài)信息,例如語(yǔ)氣和情感(Henry 2008; Loughran和McDonald 2011; Tetlock 2007),從而極大地影響了股價(jià)。通過(guò)利用情感分析,自動(dòng)化交易者可以分析財(cái)務(wù)披露中傳達(dá)的情感,以便進(jìn)行投資決策。
市場(chǎng)營(yíng)銷(xiāo):?市場(chǎng)營(yíng)銷(xiāo)部門(mén)通常對(duì)跟蹤品牌形象感興趣。為此,他們從社交媒體上收集了大量用戶(hù)意見(jiàn),并評(píng)估個(gè)人對(duì)品牌,產(chǎn)品和服務(wù)的感受。
評(píng)級(jí)和評(píng)論平臺(tái):?評(píng)級(jí)和評(píng)論平臺(tái)通過(guò)收集用戶(hù)對(duì)某些產(chǎn)品和服務(wù)的評(píng)級(jí)或偏好來(lái)實(shí)現(xiàn)有價(jià)值的功能。在這里,人們可以自動(dòng)處理大量用戶(hù)生成的內(nèi)容(UGC)并利用由此獲得的知識(shí)。例如,人們可以確定哪些提示傳達(dá)了積極或者負(fù)面的意見(jiàn),甚至可以自動(dòng)驗(yàn)證其可信度。
情感分析方法
隨著情感分析被應(yīng)用于廣泛的領(lǐng)域和文本來(lái)源,研究已經(jīng)設(shè)計(jì)出各種測(cè)量情感的方法。最近的文獻(xiàn)綜述(Pang and Lee 2008)提供了一個(gè)全面的,與領(lǐng)域無(wú)關(guān)的調(diào)查。
一方面,當(dāng)機(jī)器學(xué)習(xí)方法追求高預(yù)測(cè)性能時(shí),它是首選。但是,機(jī)器學(xué)習(xí)通常充當(dāng)黑匣子,從而使解釋變得困難。另一方面,基于字典的方法會(huì)生成肯定和否定單詞的列表。然后,將這些單詞的相應(yīng)出現(xiàn)組合為單個(gè)情感評(píng)分。因此,基本的決定變得可追溯,研究人員可以理解導(dǎo)致特定情感的因素。
另外,?SentimentAnalysis
?允許生成定制的字典。它們針對(duì)特定領(lǐng)域進(jìn)行了定制,與純字典相比,提高了預(yù)測(cè)性能,并具有完全的可解釋性。可以在(Pr?llochs,F(xiàn)euerriegel和Neumann 2018)中找到此方法的詳細(xì)信息。
在執(zhí)行情感分析的過(guò)程中,必須將正在運(yùn)行的文本轉(zhuǎn)換為一種機(jī)器可讀的格式。這是通過(guò)執(zhí)行一系列預(yù)處理操作來(lái)實(shí)現(xiàn)的。首先,將文本標(biāo)記為單個(gè)單詞,然后執(zhí)行常見(jiàn)的預(yù)處理步驟:停用詞的刪除,詞干,標(biāo)點(diǎn)符號(hào)的刪除以及小寫(xiě)的轉(zhuǎn)換。這些操作也默認(rèn)在中進(jìn)行?SentimentAnalysis
,但可以根據(jù)個(gè)人需要進(jìn)行調(diào)整。
簡(jiǎn)短示范
# 分析單個(gè)字符極性(正/負(fù))
anaSen("是的,這對(duì)德國(guó)隊(duì)來(lái)說(shuō)是一場(chǎng)很棒的足球比賽!")
## [1] positive
## Levels: negative positive
# 創(chuàng)建字符串向量
documents <- c("哇,我真的很喜歡新的輕型軍刀!",
"那本書(shū)很棒。",
"R是一種很棒的語(yǔ)言。",
"這家餐廳的服務(wù)很糟糕。"
"這既不是正面也不是負(fù)面。",
"服務(wù)員忘了我的甜點(diǎn)-多么糟糕的服務(wù)!")
# 分析情感
anaSen(documents)
# 根據(jù)QDAP詞典提取基于詞典的情感
sentiment$SentimentQDAP
## [1] ?0.3333333 ?0.5000000 ?0.5000000 -0.3333333 ?0.0000000 -0.4000000
#查看情感方向(即正面,中性和負(fù)面)
ToDirection(sentiment$SentimentQDAP)
## [1] positive positive positive negative neutral ?negative
## Levels: negative neutral positive
response <- c(+1, +1, +1, -1, 0, -1)
comToRne(sentiment, response)
## ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?WordCount ?SentimentGI ?NegativityGI
## cor ? ? ? ? ? ? ? ? ? ? ? ?-0.18569534 ?0.990011498 -9.974890e-01
## cor.t.statistic ? ? ? ? ? ?-0.37796447 14.044046450 -2.816913e+01
## cor.p.value ? ? ? ? ? ? ? ? 0.72465864 ?0.000149157 ?9.449687e-06
## lm.t.value ? ? ? ? ? ? ? ? -0.37796447 14.044046450 -2.816913e+01
## r.squared ? ? ? ? ? ? ? ? ? 0.03448276 ?0.980122766 ?9.949843e-01
## RMSE ? ? ? ? ? ? ? ? ? ? ? ?3.82970843 ?0.450102869 ?1.186654e+00
## MAE ? ? ? ? ? ? ? ? ? ? ? ? 3.33333333 ?0.400000000 ?1.100000e+00
## Accuracy ? ? ? ? ? ? ? ? ? ?0.66666667 ?1.000000000 ?6.666667e-01
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ?NaN ?1.000000000 ? ? ? ? ? NaN
## Sensitivity ? ? ? ? ? ? ? ? 0.00000000 ?1.000000000 ?0.000000e+00
## Specificity ? ? ? ? ? ? ? ? 1.00000000 ?1.000000000 ?1.000000e+00
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ?0.00000000 ?0.500000000 ?0.000000e+00
## BalancedAccuracy ? ? ? ? ? ?0.50000000 ?1.000000000 ?5.000000e-01
## avg.sentiment.pos.response ?3.25000000 ?0.333333333 ?8.333333e-02
## avg.sentiment.neg.response ?4.00000000 -0.633333333 ?6.333333e-01
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?PositivityGI SentimentHE NegativityHE
## cor ? ? ? ? ? ? ? ? ? ? ? ? 0.942954167 ? 0.4152274 -0.083045480
## cor.t.statistic ? ? ? ? ? ? 5.664705543 ? 0.9128709 -0.166666667
## cor.p.value ? ? ? ? ? ? ? ? 0.004788521 ? 0.4129544 ?0.875718144
## lm.t.value ? ? ? ? ? ? ? ? ?5.664705543 ? 0.9128709 -0.166666667
## r.squared ? ? ? ? ? ? ? ? ? 0.889162562 ? 0.1724138 ?0.006896552
## RMSE ? ? ? ? ? ? ? ? ? ? ? ?0.713624032 ? 0.8416254 ?0.922958207
## MAE ? ? ? ? ? ? ? ? ? ? ? ? 0.666666667 ? 0.7500000 ?0.888888889
## Accuracy ? ? ? ? ? ? ? ? ? ?0.666666667 ? 0.6666667 ?0.666666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? NaN ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? 0.000000000 ? 0.0000000 ?0.000000000
## Specificity ? ? ? ? ? ? ? ? 1.000000000 ? 1.0000000 ?1.000000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ?0.000000000 ? 0.0000000 ?0.000000000
## BalancedAccuracy ? ? ? ? ? ?0.500000000 ? 0.5000000 ?0.500000000
## avg.sentiment.pos.response ?0.416666667 ? 0.1250000 ?0.083333333
## avg.sentiment.neg.response ?0.000000000 ? 0.0000000 ?0.000000000
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?PositivityHE SentimentLM NegativityLM
## cor ? ? ? ? ? ? ? ? ? ? ? ? ? 0.3315938 ? 0.7370455 ?-0.40804713
## cor.t.statistic ? ? ? ? ? ? ? 0.7029595 ? 2.1811142 ?-0.89389841
## cor.p.value ? ? ? ? ? ? ? ? ? 0.5208394 ? 0.0946266 ? 0.42189973
## lm.t.value ? ? ? ? ? ? ? ? ? ?0.7029595 ? 2.1811142 ?-0.89389841
## r.squared ? ? ? ? ? ? ? ? ? ? 0.1099545 ? 0.5432361 ? 0.16650246
## RMSE ? ? ? ? ? ? ? ? ? ? ? ? ?0.8525561 ? 0.7234178 ? 0.96186547
## MAE ? ? ? ? ? ? ? ? ? ? ? ? ? 0.8055556 ? 0.6333333 ? 0.92222222
## Accuracy ? ? ? ? ? ? ? ? ? ? ?0.6666667 ? 0.8333333 ? 0.66666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? 1.0000000 ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ? 0.0000000 ? 0.5000000 ? 0.00000000
## Specificity ? ? ? ? ? ? ? ? ? 1.0000000 ? 1.0000000 ? 1.00000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.0000000 ? 0.3333333 ? 0.00000000
## BalancedAccuracy ? ? ? ? ? ? ?0.5000000 ? 0.7500000 ? 0.50000000
## avg.sentiment.pos.response ? ?0.2083333 ? 0.2500000 ? 0.08333333
## avg.sentiment.neg.response ? ?0.0000000 ?-0.1000000 ? 0.10000000
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?PositivityLM RatioUncertaintyLM SentimentQDAP
## cor ? ? ? ? ? ? ? ? ? ? ? ? ? 0.6305283 ? ? ? ? ? ? ? ? NA ?0.9865356369
## cor.t.statistic ? ? ? ? ? ? ? 1.6247248 ? ? ? ? ? ? ? ? NA 12.0642877257
## cor.p.value ? ? ? ? ? ? ? ? ? 0.1795458 ? ? ? ? ? ? ? ? NA ?0.0002707131
## lm.t.value ? ? ? ? ? ? ? ? ? ?1.6247248 ? ? ? ? ? ? ? ? NA 12.0642877257
## r.squared ? ? ? ? ? ? ? ? ? ? 0.3975659 ? ? ? ? ? ? ? ? NA ?0.9732525629
## RMSE ? ? ? ? ? ? ? ? ? ? ? ? ?0.7757911 ? ? ? ? ?0.9128709 ?0.5398902495
## MAE ? ? ? ? ? ? ? ? ? ? ? ? ? 0.7222222 ? ? ? ? ?0.8333333 ?0.4888888889
## Accuracy ? ? ? ? ? ? ? ? ? ? ?0.6666667 ? ? ? ? ?0.6666667 ?1.0000000000
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? ? ? ? ?NaN ?1.0000000000
## Sensitivity ? ? ? ? ? ? ? ? ? 0.0000000 ? ? ? ? ?0.0000000 ?1.0000000000
## Specificity ? ? ? ? ? ? ? ? ? 1.0000000 ? ? ? ? ?1.0000000 ?1.0000000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.0000000 ? ? ? ? ?0.0000000 ?0.5000000000
## BalancedAccuracy ? ? ? ? ? ? ?0.5000000 ? ? ? ? ?0.5000000 ?1.0000000000
## avg.sentiment.pos.response ? ?0.3333333 ? ? ? ? ?0.0000000 ?0.3333333333
## avg.sentiment.neg.response ? ?0.0000000 ? ? ? ? ?0.0000000 -0.3666666667
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?NegativityQDAP PositivityQDAP
## cor ? ? ? ? ? ? ? ? ? ? ? ? ?-0.944339551 ? ?0.942954167
## cor.t.statistic ? ? ? ? ? ? ?-5.741148345 ? ?5.664705543
## cor.p.value ? ? ? ? ? ? ? ? ? 0.004560908 ? ?0.004788521
## lm.t.value ? ? ? ? ? ? ? ? ? -5.741148345 ? ?5.664705543
## r.squared ? ? ? ? ? ? ? ? ? ? 0.891777188 ? ?0.889162562
## RMSE ? ? ? ? ? ? ? ? ? ? ? ? ?1.068401367 ? ?0.713624032
## MAE ? ? ? ? ? ? ? ? ? ? ? ? ? 1.011111111 ? ?0.666666667
## Accuracy ? ? ? ? ? ? ? ? ? ? ?0.666666667 ? ?0.666666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ? 0.000000000 ? ?0.000000000
## Specificity ? ? ? ? ? ? ? ? ? 1.000000000 ? ?1.000000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.000000000 ? ?0.000000000
## BalancedAccuracy ? ? ? ? ? ? ?0.500000000 ? ?0.500000000
## avg.sentiment.pos.response ? ?0.083333333 ? ?0.416666667
## avg.sentiment.neg.response ? ?0.366666667 ? ?0.000000000
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?WordCount SentimentGI NegativityGI PositivityGI
## Accuracy ? ? ? ? ? ? ? ? ? 0.6666667 ? 1.0000000 ? 0.66666667 ? ?0.6666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ?NaN ? 1.0000000 ? ? ? ? ?NaN ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ?0.0000000 ? 1.0000000 ? 0.00000000 ? ?0.0000000
## Specificity ? ? ? ? ? ? ? ?1.0000000 ? 1.0000000 ? 1.00000000 ? ?1.0000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? 0.0000000 ? 0.5000000 ? 0.00000000 ? ?0.0000000
## BalancedAccuracy ? ? ? ? ? 0.5000000 ? 1.0000000 ? 0.50000000 ? ?0.5000000
## avg.sentiment.pos.response 3.2500000 ? 0.3333333 ? 0.08333333 ? ?0.4166667
## avg.sentiment.neg.response 4.0000000 ?-0.6333333 ? 0.63333333 ? ?0.0000000
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?SentimentHE NegativityHE PositivityHE
## Accuracy ? ? ? ? ? ? ? ? ? ? 0.6666667 ? 0.66666667 ? ?0.6666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ?NaN ? ? ? ? ?NaN ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ?0.0000000 ? 0.00000000 ? ?0.0000000
## Specificity ? ? ? ? ? ? ? ? ?1.0000000 ? 1.00000000 ? ?1.0000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? 0.0000000 ? 0.00000000 ? ?0.0000000
## BalancedAccuracy ? ? ? ? ? ? 0.5000000 ? 0.50000000 ? ?0.5000000
## avg.sentiment.pos.response ? 0.1250000 ? 0.08333333 ? ?0.2083333
## avg.sentiment.neg.response ? 0.0000000 ? 0.00000000 ? ?0.0000000
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?SentimentLM NegativityLM PositivityLM
## Accuracy ? ? ? ? ? ? ? ? ? ? 0.8333333 ? 0.66666667 ? ?0.6666667
## Precision ? ? ? ? ? ? ? ? ? ?1.0000000 ? ? ? ? ?NaN ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ?0.5000000 ? 0.00000000 ? ?0.0000000
## Specificity ? ? ? ? ? ? ? ? ?1.0000000 ? 1.00000000 ? ?1.0000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? 0.3333333 ? 0.00000000 ? ?0.0000000
## BalancedAccuracy ? ? ? ? ? ? 0.7500000 ? 0.50000000 ? ?0.5000000
## avg.sentiment.pos.response ? 0.2500000 ? 0.08333333 ? ?0.3333333
## avg.sentiment.neg.response ?-0.1000000 ? 0.10000000 ? ?0.0000000
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?RatioUncertaintyLM SentimentQDAP NegativityQDAP
## Accuracy ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.6666667 ? ? 1.0000000 ? ? 0.66666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? 1.0000000 ? ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ? ? ? ? 0.0000000 ? ? 1.0000000 ? ? 0.00000000
## Specificity ? ? ? ? ? ? ? ? ? ? ? ? 1.0000000 ? ? 1.0000000 ? ? 1.00000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.0000000 ? ? 0.5000000 ? ? 0.00000000
## BalancedAccuracy ? ? ? ? ? ? ? ? ? ?0.5000000 ? ? 1.0000000 ? ? 0.50000000
## avg.sentiment.pos.response ? ? ? ? ?0.0000000 ? ? 0.3333333 ? ? 0.08333333
## avg.sentiment.neg.response ? ? ? ? ?0.0000000 ? ?-0.3666667 ? ? 0.36666667
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?PositivityQDAP
## Accuracy ? ? ? ? ? ? ? ? ? ? ? ?0.6666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? ? NaN
## Sensitivity ? ? ? ? ? ? ? ? ? ? 0.0000000
## Specificity ? ? ? ? ? ? ? ? ? ? 1.0000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.0000000
## BalancedAccuracy ? ? ? ? ? ? ? ?0.5000000
## avg.sentiment.pos.response ? ? ?0.4166667
## avg.sentiment.neg.response ? ? ?0.0000000

從文本挖掘中執(zhí)行了一組預(yù)處理操作。將標(biāo)記每個(gè)文檔,最后將輸入轉(zhuǎn)換為文檔項(xiàng)矩陣。
輸入
提供了具有其他幾種輸入格式的接口,其中包括
字符串向量。
在
tm
?軟件包中實(shí)現(xiàn)的DocumentTermMatrix和?TermDocumentMatrix(Feinerer,Hornik和Meyer 2008)。tm
?軟件包實(shí)現(xiàn)的語(yǔ)料庫(kù)對(duì)象?(Feinerer,Hornik和Meyer 2008)。
我們?cè)谙旅嫣峁┦纠?/p>
向量的字符串
documents <- c("這很好",
"這不好",
"這介于兩者之間")
convertToDirection(analyzeSentiment(documents)$SentimentQDAP)
## [1] positive negative neutral
## Levels: negative neutral positive
文檔詞語(yǔ)矩陣
corpus <- VCorpus(VectorSource(documents))
convertToDirection(analyzeSentiment(corpus)$SentimentQDAP)
## [1] positive negative neutral
## Levels: negative neutral positive
語(yǔ)料庫(kù)對(duì)象
## [1] positive negative neutral
## Levels: negative neutral positive
可以直接與文檔術(shù)語(yǔ)矩陣一起使用,因此一開(kāi)始就可以使用自定義的預(yù)處理操作。之后,可以計(jì)算情感分?jǐn)?shù)。例如,可以使用其他列表中的停用詞替換停用詞。
字典
可區(qū)分三種不同類(lèi)型的詞典。它們所存儲(chǔ)的數(shù)據(jù)各不相同,這些數(shù)據(jù)最終還控制著可以應(yīng)用哪種情感分析方法。字典如下:
SentimentDictionaryWordlist
?包含屬于一個(gè)類(lèi)別的單詞列表。SentimentDictionaryBinary
?存儲(chǔ)兩個(gè)單詞列表,一個(gè)用于肯定條目,一個(gè)用于否定條目。SentimentDictionaryWeighted
?允許單詞的情感評(píng)分。
情感詞典詞表
# 替代
d <- Dictionary(c(“不確定”,“可能”,“有可能”))
summary(d)
## Dictionary type: ?word list (single set)
## Total entries: ? ?3
情感詞典
d <- DictionaryBin(c(“增加”,“上升”,“更多”),
c(“下降”))
summary(d)
## Dictionary type: ?binary (positive / negative)
## Total entries: ? ?5
## Positive entries: 3 (60%)
## Negative entries: 2 (40%)
情感詞典加權(quán)
d <- SentimentDictionaryWeighted(c(“增加”,“減少”,“退出”),
c(+1, -1, -10),
rep(NA, 3))
summary(d)
## Dictionary type: ?weighted (words with individual scores)
## Total entries: ? ?3
## Positive entries: 1 (33.33%)
## Negative entries: 2 (66.67%)
## Neutral entries: ?0 (0%)
##
## Details
## Average score: ? ? ?-3.333333
## Median: ? ? ? ? ? ? -1
## Min: ? ? ? ? ? ? ? ?-10
## Max: ? ? ? ? ? ? ? ?1
## Standard deviation: 5.859465
## Skewness: ? ? ? ? ? -0.6155602
d <- SentimentDictionary(c(“增加”,“減少”,“退出”),
c(+1, -1, -10),
rep(NA, 3))
summary(d)
## Dictionary type: ?weighted (words with individual scores)
## Total entries: ? ?3
## Positive entries: 1 (33.33%)
## Negative entries: 2 (66.67%)
## Neutral entries: ?0 (0%)
##
## Details
## Average score: ? ? ?-3.333333
## Median: ? ? ? ? ? ? -1
## Min: ? ? ? ? ? ? ? ?-10
## Max: ? ? ? ? ? ? ? ?1
## Standard deviation: 5.859465
## Skewness: ? ? ? ? ? -0.6155602
字典生成
用向量的形式表示因變量。此外,變量給出了單詞在文檔中出現(xiàn)的次數(shù)。然后,該方法估計(jì)具有截距和系數(shù)的線性模型。估計(jì)基于LASSO正則化,它執(zhí)行變量選擇。這樣,它將某些系數(shù)設(shè)置為正好為零。然后可以根據(jù)剩余單詞的系數(shù)按極性對(duì)它們進(jìn)行排序。
# 創(chuàng)建字符串向量
documents <- c(“這是一件好事!”,
“這是一件非常好的事!”,
“沒(méi)關(guān)系?!?/code>
“這是一件壞事?!保?/code>
“這是一件非常不好的事情。”
)
response <- c(1, 0.5, 0, -0.5, -1)
# 使用LASSO正則化生成字典
dict
## Type: weighted (words with individual scores)
## Intercept: 5.55333e-05
## -0.51 bad
## ?0.51 good
summary(dict)
## Dictionary type: ?weighted (words with individual scores)
## Total entries: ? ?2
## Positive entries: 1 (50%)
## Negative entries: 1 (50%)
## Neutral entries: ?0 (0%)
##
## Details
## Average score: ? ? ?-5.251165e-05
## Median: ? ? ? ? ? ? -5.251165e-05
## Min: ? ? ? ? ? ? ? ?-0.5119851
## Max: ? ? ? ? ? ? ? ?0.5118801
## Standard deviation: 0.7239821
## Skewness: ? ? ? ? ? 0
有幾種微調(diào)選項(xiàng)。只需更改參數(shù),就可以用彈性網(wǎng)絡(luò)模型替換LASSO? 。
最后,可以使用read()
?和?保存和重新加載字典?write()
評(píng)估
最終,例程允許人們進(jìn)一步挖掘生成的字典。一方面,可以通過(guò)summary()
?例程顯示簡(jiǎn)單的概述?。另一方面,核密度估計(jì)也可以可視化正詞和負(fù)詞的分布。
## Comparing: wordlist vs weighted
##
## Total unique words: 4213
## Matching entries: 2 (0.0004747211%)
## Entries with same classification: 0 (0%)
## Entries with different classification: 2 (0.0004747211%)
## Correlation between scores of matching entries: 1
## $totalUniqueWords
## [1] 4213
##
## $totalSameWords
## [1] 2
##
## $ratioSameWords
## [1] 0.0004747211
##
## $numWordsEqualClass
## [1] 0
##
## $numWordsDifferentClass
## [1] 2
##
## $ratioWordsEqualClass
## [1] 0
##
## $ratioWordsDifferentClass
## [1] 0.0004747211
##
## $correlation
## [1] 1
## ? ? ? ? ? ? ? ? ? ? ? ? ? ? Dictionary
## cor ? ? ? ? ? ? ? ? ? ? ? ? 0.94868330
## cor.t.statistic ? ? ? ? ? ? 5.19615237
## cor.p.value ? ? ? ? ? ? ? ? 0.01384683
## lm.t.value ? ? ? ? ? ? ? ? ?5.19615237
## r.squared ? ? ? ? ? ? ? ? ? 0.90000000
## RMSE ? ? ? ? ? ? ? ? ? ? ? ?0.23301039
## MAE ? ? ? ? ? ? ? ? ? ? ? ? 0.20001111
## Accuracy ? ? ? ? ? ? ? ? ? ?1.00000000
## Precision ? ? ? ? ? ? ? ? ? 1.00000000
## Sensitivity ? ? ? ? ? ? ? ? 1.00000000
## Specificity ? ? ? ? ? ? ? ? 1.00000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ?0.57142857
## BalancedAccuracy ? ? ? ? ? ?1.00000000
## avg.sentiment.pos.response ?0.45116801
## avg.sentiment.neg.response -0.67675202

下面的示例演示如何將計(jì)算出的字典用于預(yù)測(cè)樣本外數(shù)據(jù)的情感。然后通過(guò)將其與內(nèi)置詞典進(jìn)行比較來(lái)評(píng)估預(yù)測(cè)性能。
test_documents <- c(“這既不是好事也不是壞事”,
“真是好主意!”,
“不錯(cuò)”
)
pred <- predict(dict, test_documents)
## ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Dictionary
## cor ? ? ? ? ? ? ? ? ? ? ? ?5.922189e-05
## cor.t.statistic ? ? ? ? ? ?5.922189e-05
## cor.p.value ? ? ? ? ? ? ? ?9.999623e-01
## lm.t.value ? ? ? ? ? ? ? ? 5.922189e-05
## r.squared ? ? ? ? ? ? ? ? ?3.507232e-09
## RMSE ? ? ? ? ? ? ? ? ? ? ? 8.523018e-01
## MAE ? ? ? ? ? ? ? ? ? ? ? ?6.666521e-01
## Accuracy ? ? ? ? ? ? ? ? ? 3.333333e-01
## Precision ? ? ? ? ? ? ? ? ?0.000000e+00
## Sensitivity ? ? ? ? ? ? ? ? ? ? ? ? NaN
## Specificity ? ? ? ? ? ? ? ?3.333333e-01
## F1 ? ? ? ? ? ? ? ? ? ? ? ? 0.000000e+00
## BalancedAccuracy ? ? ? ? ? ? ? ? ? ?NaN
## avg.sentiment.pos.response 1.457684e-05
## avg.sentiment.neg.response ? ? ? ? ?NaN

## ? ? ? ? ? ? ? ? ? ? ? ? ? ? WordCount SentimentGI NegativityGI
## cor ? ? ? ? ? ? ? ? ? ? ? ?-0.8660254 -0.18898224 ? 0.18898224
## cor.t.statistic ? ? ? ? ? ?-1.7320508 -0.19245009 ? 0.19245009
## cor.p.value ? ? ? ? ? ? ? ? 0.3333333 ?0.87896228 ? 0.87896228
## lm.t.value ? ? ? ? ? ? ? ? -1.7320508 -0.19245009 ? 0.19245009
## r.squared ? ? ? ? ? ? ? ? ? 0.7500000 ?0.03571429 ? 0.03571429
## RMSE ? ? ? ? ? ? ? ? ? ? ? ?1.8257419 ?1.19023807 ? 0.60858062
## MAE ? ? ? ? ? ? ? ? ? ? ? ? 1.3333333 ?0.83333333 ? 0.44444444
## Accuracy ? ? ? ? ? ? ? ? ? ?1.0000000 ?0.66666667 ? 1.00000000
## Precision ? ? ? ? ? ? ? ? ? ? ? ? NaN ?0.00000000 ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? NaN ? ? ? ? ?NaN
## Specificity ? ? ? ? ? ? ? ? 1.0000000 ?0.66666667 ? 1.00000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ?0.0000000 ?0.00000000 ? 0.00000000
## BalancedAccuracy ? ? ? ? ? ? ? ? ?NaN ? ? ? ? NaN ? ? ? ? ?NaN
## avg.sentiment.pos.response ?2.0000000 -0.16666667 ? 0.44444444
## avg.sentiment.neg.response ? ? ? ?NaN ? ? ? ? NaN ? ? ? ? ?NaN
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?PositivityGI SentimentHE NegativityHE
## cor ? ? ? ? ? ? ? ? ? ? ? ? -0.18898224 -0.18898224 ? ? ? ? ? NA
## cor.t.statistic ? ? ? ? ? ? -0.19245009 -0.19245009 ? ? ? ? ? NA
## cor.p.value ? ? ? ? ? ? ? ? ?0.87896228 ?0.87896228 ? ? ? ? ? NA
## lm.t.value ? ? ? ? ? ? ? ? ?-0.19245009 -0.19245009 ? ? ? ? ? NA
## r.squared ? ? ? ? ? ? ? ? ? ?0.03571429 ?0.03571429 ? ? ? ? ? NA
## RMSE ? ? ? ? ? ? ? ? ? ? ? ? 0.67357531 ?0.67357531 ? ?0.8164966
## MAE ? ? ? ? ? ? ? ? ? ? ? ? ?0.61111111 ?0.61111111 ? ?0.6666667
## Accuracy ? ? ? ? ? ? ? ? ? ? 1.00000000 ?1.00000000 ? ?1.0000000
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? NaN ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? NaN ? ? ? ? ?NaN
## Specificity ? ? ? ? ? ? ? ? ?1.00000000 ?1.00000000 ? ?1.0000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? 0.00000000 ?0.00000000 ? ?0.0000000
## BalancedAccuracy ? ? ? ? ? ? ? ? ? ?NaN ? ? ? ? NaN ? ? ? ? ?NaN
## avg.sentiment.pos.response ? 0.27777778 ?0.27777778 ? ?0.0000000
## avg.sentiment.neg.response ? ? ? ? ?NaN ? ? ? ? NaN ? ? ? ? ?NaN
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?PositivityHE SentimentLM NegativityLM
## cor ? ? ? ? ? ? ? ? ? ? ? ? -0.18898224 -0.18898224 ? 0.18898224
## cor.t.statistic ? ? ? ? ? ? -0.19245009 -0.19245009 ? 0.19245009
## cor.p.value ? ? ? ? ? ? ? ? ?0.87896228 ?0.87896228 ? 0.87896228
## lm.t.value ? ? ? ? ? ? ? ? ?-0.19245009 -0.19245009 ? 0.19245009
## r.squared ? ? ? ? ? ? ? ? ? ?0.03571429 ?0.03571429 ? 0.03571429
## RMSE ? ? ? ? ? ? ? ? ? ? ? ? 0.67357531 ?1.19023807 ? 0.60858062
## MAE ? ? ? ? ? ? ? ? ? ? ? ? ?0.61111111 ?0.83333333 ? 0.44444444
## Accuracy ? ? ? ? ? ? ? ? ? ? 1.00000000 ?0.66666667 ? 1.00000000
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ?0.00000000 ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? NaN ? ? ? ? ?NaN
## Specificity ? ? ? ? ? ? ? ? ?1.00000000 ?0.66666667 ? 1.00000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? 0.00000000 ?0.00000000 ? 0.00000000
## BalancedAccuracy ? ? ? ? ? ? ? ? ? ?NaN ? ? ? ? NaN ? ? ? ? ?NaN
## avg.sentiment.pos.response ? 0.27777778 -0.16666667 ? 0.44444444
## avg.sentiment.neg.response ? ? ? ? ?NaN ? ? ? ? NaN ? ? ? ? ?NaN
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?PositivityLM RatioUncertaintyLM SentimentQDAP
## cor ? ? ? ? ? ? ? ? ? ? ? ? -0.18898224 ? ? ? ? ? ? ? ? NA ? -0.18898224
## cor.t.statistic ? ? ? ? ? ? -0.19245009 ? ? ? ? ? ? ? ? NA ? -0.19245009
## cor.p.value ? ? ? ? ? ? ? ? ?0.87896228 ? ? ? ? ? ? ? ? NA ? ?0.87896228
## lm.t.value ? ? ? ? ? ? ? ? ?-0.19245009 ? ? ? ? ? ? ? ? NA ? -0.19245009
## r.squared ? ? ? ? ? ? ? ? ? ?0.03571429 ? ? ? ? ? ? ? ? NA ? ?0.03571429
## RMSE ? ? ? ? ? ? ? ? ? ? ? ? 0.67357531 ? ? ? ? ?0.8164966 ? ?1.19023807
## MAE ? ? ? ? ? ? ? ? ? ? ? ? ?0.61111111 ? ? ? ? ?0.6666667 ? ?0.83333333
## Accuracy ? ? ? ? ? ? ? ? ? ? 1.00000000 ? ? ? ? ?1.0000000 ? ?0.66666667
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? ? ? ? ?NaN ? ?0.00000000
## Sensitivity ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? ? ? ? ?NaN ? ? ? ? ? NaN
## Specificity ? ? ? ? ? ? ? ? ?1.00000000 ? ? ? ? ?1.0000000 ? ?0.66666667
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? 0.00000000 ? ? ? ? ?0.0000000 ? ?0.00000000
## BalancedAccuracy ? ? ? ? ? ? ? ? ? ?NaN ? ? ? ? ? ? ? ?NaN ? ? ? ? ? NaN
## avg.sentiment.pos.response ? 0.27777778 ? ? ? ? ?0.0000000 ? -0.16666667
## avg.sentiment.neg.response ? ? ? ? ?NaN ? ? ? ? ? ? ? ?NaN ? ? ? ? ? NaN
## ? ? ? ? ? ? ? ? ? ? ? ? ? ?NegativityQDAP PositivityQDAP
## cor ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.18898224 ? ?-0.18898224
## cor.t.statistic ? ? ? ? ? ? ? ?0.19245009 ? ?-0.19245009
## cor.p.value ? ? ? ? ? ? ? ? ? ?0.87896228 ? ? 0.87896228
## lm.t.value ? ? ? ? ? ? ? ? ? ? 0.19245009 ? ?-0.19245009
## r.squared ? ? ? ? ? ? ? ? ? ? ?0.03571429 ? ? 0.03571429
## RMSE ? ? ? ? ? ? ? ? ? ? ? ? ? 0.60858062 ? ? 0.67357531
## MAE ? ? ? ? ? ? ? ? ? ? ? ? ? ?0.44444444 ? ? 0.61111111
## Accuracy ? ? ? ? ? ? ? ? ? ? ? 1.00000000 ? ? 1.00000000
## Precision ? ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? ? ?NaN
## Sensitivity ? ? ? ? ? ? ? ? ? ? ? ? ? NaN ? ? ? ? ? ?NaN
## Specificity ? ? ? ? ? ? ? ? ? ?1.00000000 ? ? 1.00000000
## F1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.00000000 ? ? 0.00000000
## BalancedAccuracy ? ? ? ? ? ? ? ? ? ? ?NaN ? ? ? ? ? ?NaN
## avg.sentiment.pos.response ? ? 0.44444444 ? ? 0.27777778
## avg.sentiment.neg.response ? ? ? ? ? ?NaN ? ? ? ? ? ?NaN
預(yù)處理
如果需要,可以實(shí)施適合特定需求的預(yù)處理階段。如函數(shù)?ngram_tokenize()
?,用于從語(yǔ)料庫(kù)中提取n
-gram。
tdm <- TermDocumentMatrix(corpus,
control=list(wordLengths=c(1,Inf),
tokenize=function(x) ngram_tokenize(x, char=FALSE,
ngmin=1, ngmax=2)))
## Dictionary type: ?weighted (words with individual scores)
## Total entries: ? ?7
## Positive entries: 4 (57.14%)
## Negative entries: 3 (42.86%)
## Neutral entries: ?0 (0%)
##
## Details
## Average score: ? ? ?5.814314e-06
## Median: ? ? ? ? ? ? 1.602469e-16
## Min: ? ? ? ? ? ? ? ?-0.4372794
## Max: ? ? ? ? ? ? ? ?0.4381048
## Standard deviation: 0.301723
## Skewness: ? ? ? ? ? 0.00276835
dict
## Type: weighted (words with individual scores)
## Intercept: -5.102483e-05
## -0.44 不好
## -0.29 非常糟糕
## 0.29 ?好
性能優(yōu)化
## ? SentimentLM
## 1 ? ? ? ? 0.5
## 2 ? ? ? ? 0.5
## 3 ? ? ? ? 0.0
## 4 ? ? ? ?-0.5
## 5 ? ? ? ?-0.5
語(yǔ)言支持和可擴(kuò)展性
?可以適應(yīng)其他語(yǔ)言使用。為此,需要在兩點(diǎn)上進(jìn)行更改:
預(yù)處理:使用參數(shù)?
language=""
?來(lái)執(zhí)行所有預(yù)處理操作。字典:?可以使用附帶的字典生成方法?。然后,這可以自動(dòng)生成可應(yīng)用于給定語(yǔ)言的正負(fù)詞詞典。
下面的示例使用德語(yǔ)示例。最后,我們進(jìn)行情感分析。
documents <- c("Das ist ein gutes Resultat",
"Das Ergebnis war schlecht")
sentiment <- ana(documents,
language="german",
sentiment
## ? GermanSentiment
## 1 ? ? ? ? ? ? 0.0
## 2 ? ? ? ? ? ?-0.5
## [1] positive negative
## Levels: negative positive
同樣,可以使用自定義情感分?jǐn)?shù)來(lái)實(shí)現(xiàn)字典。
woorden <- c("goed","slecht")
scores <- c(0.8,-0.5)
## ? DutchSentiment
## 1 ? ? ? ? ? -0.5
實(shí)例
我們利用了tm
?包中的路透社石油新聞?。
# 分析情感
sentiment <- ana(crude)
# 計(jì)算正面和負(fù)面新聞發(fā)布數(shù)量
table(coToB(sentiment$SentimentLM))
##
## negative positive
## ? ? ? 16 ? ? ? ?4
# 情感最高和最低的新聞
## [1] "HOUSTON OIL <HO> RESERVES STUDY COMPLETED"
crude[[which.min(sentiment$SentimentLM)]]$meta$heading
## [1] "DIAMOND SHAMROCK (DIA) CUTS CRUDE PRICES"
# 查看情感變量的摘要統(tǒng)計(jì)
summary(sentiment$SentimentLM)
## ? ? Min. ?1st Qu. ? Median ? ? Mean ?3rd Qu. ? ? Max.
## -0.08772 -0.04366 -0.02341 -0.02953 -0.01375 ?0.00000
# 可視化標(biāo)準(zhǔn)化情感變量的分布
hist(scale(sentiment$SentimentLM))

# 計(jì)算相關(guān)
## ? ? ? ? ? ? ? SentimentLM SentimentHE SentimentQDAP
## SentimentLM ? ? 1.0000000 ? 0.2769878 ? ? 0.4769730
## SentimentHE ? ? 0.2769878 ? 1.0000000 ? ? 0.6141075
## SentimentQDAP ? 0.4769730 ? 0.6141075 ? ? 1.0000000
# ?1987-02-26 ?1987-03-02之間的原油新聞
plot(senti$Sentime)

plot(SenLM, x=date, cumsum=TRUE)

單詞計(jì)算
對(duì)單詞進(jìn)行計(jì)數(shù)?。
#詞(無(wú)停用詞)
## ? WordCount
## 1 ? ? ? ? 3
# 計(jì)算所有單詞(包括停用詞)
## ? WordCount
## 1 ? ? ? ? 4
參考文獻(xiàn)
Feinerer,Ingo,Kurt Hornik和David Meyer。2008年?!?R中的文本挖掘基礎(chǔ)結(jié)構(gòu)”。?統(tǒng)計(jì)軟件雜志?25(5):1–54。
Tetlock,Paul C.,2007年?!皩?nèi)容傳遞給投資者的情感:媒體在股票市場(chǎng)中的作用。”?金融雜志?62(3):1139–68。

最受歡迎的見(jiàn)解
1.探析大數(shù)據(jù)期刊文章研究熱點(diǎn)
2.618網(wǎng)購(gòu)數(shù)據(jù)盤(pán)點(diǎn)-剁手族在關(guān)注什么
3.r語(yǔ)言文本挖掘tf-idf主題建模,情感分析n-gram建模研究
4.python主題建??梢暬痩da和t-sne交互式可視化
5.疫情下的新聞數(shù)據(jù)觀察
6.python主題lda建模和t-sne可視化
7.r語(yǔ)言中對(duì)文本數(shù)據(jù)進(jìn)行主題模型topic-modeling分析
8.主題模型:數(shù)據(jù)聆聽(tīng)人民網(wǎng)留言板的那些“網(wǎng)事”
9.python爬蟲(chóng)進(jìn)行web抓取lda主題語(yǔ)義數(shù)據(jù)分析