拓端數據tecdat|R語言自然語言處理(NLP):情感分析新聞文本數據


原文鏈接:http://tecdat.cn/?p=19095

 

本文對R中的文本內容進行情感分析。此實現利用了各種現有的字典,此外,還可以創建自定義詞典。自定義詞典使用LASSO正則化作為一種​​統計方法來選擇相關詞語。最后,評估比較所有方法。

介紹

情感分析是自然語言處理(NLP),計算語言學和文本挖掘的核心研究分支。它是指從文本文檔中提取主觀信息的方法。換句話說,它提取表達意見的積極負面極性。人們也可能將情感分析稱為 觀點挖掘 (Pang and Lee 2008)。

研究中的應用

最近,情感分析受到了廣泛的關注(K. Ravi和Ravi 2015; Pang和Lee 2008),我們將在下面進行探討。當前在金融和社會科學領域的研究利用情感分析來理解人類根據文本材料做出的決策。這立即揭示了對從業者以及金融研究和社會科學領域的從業者的多種含義:研究人員可以使用R提取與讀者相關的文本成分,並在此基礎上檢驗其假設。同樣,從業人員可以衡量哪種措辭對他們的讀者而言實際上很重要,並相應地提高他們的寫作水平(Pröllochs,Feuerriegel和Neumann 2015)。在下面的兩個案例研究中,我們從金融和社會科學中論證了增加的收益。

應用

幾個應用程序演示了情感分析在組織和企業中的用途:

  • 金融: 金融市場的投資者在行使股票所有權之前,會以金融新聞披露的形式參考文本信息。有趣的是,它們不僅依賴數據,而且還依賴信息,例如語氣和情感(Henry 2008; Loughran和McDonald 2011; Tetlock 2007),從而極大地影響了股價。通過利用情感分析,自動化交易者可以分析財務披露中傳達的情感,以便進行投資決策。

  • 市場營銷: 市場營銷部門通常對跟蹤品牌形象感興趣。為此,他們從社交媒體上收集了大量用戶意見,並評估個人對品牌,產品和服務的感受。

  • 評級和評論平台: 評級和評論平台通過收集用戶對某些產品和服務的評級或偏好來實現有價值的功能。在這里,人們可以自動處理大量用戶生成的內容(UGC)並利用由此獲得的知識。例如,人們可以確定哪些提示傳達了積極或者負面的意見,甚至可以自動驗證其可信度。

情感分析方法

隨着情感分析被應用於廣泛的領域和文本來源,研究已經設計出各種測量情感的方法。最近的文獻綜述(Pang and Lee 2008)提供了一個全面的,與領域無關的調查。

一方面,當機器學習方法追求高預測性能時,它是首選。但是,機器學習通常充當黑匣子,從而使解釋變得困難。另一方面,基於字典的方法會生成肯定和否定單詞的列表。然后,將這些單詞的相應出現組合為單個情感評分。因此,基本的決定變得可追溯,研究人員可以理解導致特定情感的因素。

另外, SentimentAnalysis 允許生成定制的字典。它們針對特定領域進行了定制,與純字典相比,提高了預測性能,並具有完全的可解釋性。可以在(Pröllochs,Feuerriegel和Neumann 2018)中找到此方法的詳細信息。

在執行情感分析的過程中,必須將正在運行的文本轉換為一種機器可讀的格式。這是通過執行一系列預處理操作來實現的。首先,將文本標記為單個單詞,然后執行常見的預處理步驟:停用詞的刪除,詞干,標點符號的刪除以及小寫的轉換。這些操作也默認在中進行 SentimentAnalysis,但可以根據個人需要進行調整。

 

簡短示范

  1.  
    # 分析單個字符極性(正/負)
  2.  
     
  3.  
    anaSen("是的,這對德國隊來說是一場很棒的足球比賽!")
  1.  
    ## [1] positive
  2.  
    ## Levels: negative positive
  1.  
    # 創建字符串向量
  2.  
     
  3.  
     
  4.  
    documents <- c("哇,我真的很喜歡新的輕型軍刀!",
  5.  
     
  6.  
    "那本書很棒。",
  7.  
     
  8.  
    "R是一種很棒的語言。",
  9.  
     
  10.  
    "這家餐廳的服務很糟糕。"
  11.  
     
  12.  
    "這既不是正面也不是負面。",
  13.  
     
  14.  
    "服務員忘了我的甜點-多么糟糕的服務!")
  15.  
     
  16.  
    # 分析情感
  17.  
     
  18.  
     
  19.  
    anaSen(documents)
  20.  
     
  21.  
    # 根據QDAP詞典提取基於詞典的情感
  22.  
     
  23.  
     
  24.  
    sentiment$SentimentQDAP
## [1]  0.3333333  0.5000000  0.5000000 -0.3333333  0.0000000 -0.4000000
  1.  
    #查看情感方向(即正面,中性和負面)
  2.  
     
  3.  
     
  4.  
    ToDirection(sentiment$SentimentQDAP)
  1.  
    ## [1] positive positive positive negative neutral negative
  2.  
    ## Levels: negative neutral positive
  1.  
    response <- c(+1, +1, +1, -1, 0, -1)
  2.  
     
  3.  
    comToRne(sentiment, response)
  1.  
    ## WordCount SentimentGI NegativityGI
  2.  
    ## cor -0.18569534 0.990011498 -9.974890e-01
  3.  
    ## cor.t.statistic -0.37796447 14.044046450 -2.816913e+01
  4.  
    ## cor.p.value 0.72465864 0.000149157 9.449687e-06
  5.  
    ## lm.t.value -0.37796447 14.044046450 -2.816913e+01
  6.  
    ## r.squared 0.03448276 0.980122766 9.949843e-01
  7.  
    ## RMSE 3.82970843 0.450102869 1.186654e+00
  8.  
    ## MAE 3.33333333 0.400000000 1.100000e+00
  9.  
    ## Accuracy 0.66666667 1.000000000 6.666667e-01
  10.  
    ## Precision NaN 1.000000000 NaN
  11.  
    ## Sensitivity 0.00000000 1.000000000 0.000000e+00
  12.  
    ## Specificity 1.00000000 1.000000000 1.000000e+00
  13.  
    ## F1 0.00000000 0.500000000 0.000000e+00
  14.  
    ## BalancedAccuracy 0.50000000 1.000000000 5.000000e-01
  15.  
    ## avg.sentiment.pos.response 3.25000000 0.333333333 8.333333e-02
  16.  
    ## avg.sentiment.neg.response 4.00000000 -0.633333333 6.333333e-01
  17.  
    ## PositivityGI SentimentHE NegativityHE
  18.  
    ## cor 0.942954167 0.4152274 -0.083045480
  19.  
    ## cor.t.statistic 5.664705543 0.9128709 -0.166666667
  20.  
    ## cor.p.value 0.004788521 0.4129544 0.875718144
  21.  
    ## lm.t.value 5.664705543 0.9128709 -0.166666667
  22.  
    ## r.squared 0.889162562 0.1724138 0.006896552
  23.  
    ## RMSE 0.713624032 0.8416254 0.922958207
  24.  
    ## MAE 0.666666667 0.7500000 0.888888889
  25.  
    ## Accuracy 0.666666667 0.6666667 0.666666667
  26.  
    ## Precision NaN NaN NaN
  27.  
    ## Sensitivity 0.000000000 0.0000000 0.000000000
  28.  
    ## Specificity 1.000000000 1.0000000 1.000000000
  29.  
    ## F1 0.000000000 0.0000000 0.000000000
  30.  
    ## BalancedAccuracy 0.500000000 0.5000000 0.500000000
  31.  
    ## avg.sentiment.pos.response 0.416666667 0.1250000 0.083333333
  32.  
    ## avg.sentiment.neg.response 0.000000000 0.0000000 0.000000000
  33.  
    ## PositivityHE SentimentLM NegativityLM
  34.  
    ## cor 0.3315938 0.7370455 -0.40804713
  35.  
    ## cor.t.statistic 0.7029595 2.1811142 -0.89389841
  36.  
    ## cor.p.value 0.5208394 0.0946266 0.42189973
  37.  
    ## lm.t.value 0.7029595 2.1811142 -0.89389841
  38.  
    ## r.squared 0.1099545 0.5432361 0.16650246
  39.  
    ## RMSE 0.8525561 0.7234178 0.96186547
  40.  
    ## MAE 0.8055556 0.6333333 0.92222222
  41.  
    ## Accuracy 0.6666667 0.8333333 0.66666667
  42.  
    ## Precision NaN 1.0000000 NaN
  43.  
    ## Sensitivity 0.0000000 0.5000000 0.00000000
  44.  
    ## Specificity 1.0000000 1.0000000 1.00000000
  45.  
    ## F1 0.0000000 0.3333333 0.00000000
  46.  
    ## BalancedAccuracy 0.5000000 0.7500000 0.50000000
  47.  
    ## avg.sentiment.pos.response 0.2083333 0.2500000 0.08333333
  48.  
    ## avg.sentiment.neg.response 0.0000000 -0.1000000 0.10000000
  49.  
    ## PositivityLM RatioUncertaintyLM SentimentQDAP
  50.  
    ## cor 0.6305283 NA 0.9865356369
  51.  
    ## cor.t.statistic 1.6247248 NA 12.0642877257
  52.  
    ## cor.p.value 0.1795458 NA 0.0002707131
  53.  
    ## lm.t.value 1.6247248 NA 12.0642877257
  54.  
    ## r.squared 0.3975659 NA 0.9732525629
  55.  
    ## RMSE 0.7757911 0.9128709 0.5398902495
  56.  
    ## MAE 0.7222222 0.8333333 0.4888888889
  57.  
    ## Accuracy 0.6666667 0.6666667 1.0000000000
  58.  
    ## Precision NaN NaN 1.0000000000
  59.  
    ## Sensitivity 0.0000000 0.0000000 1.0000000000
  60.  
    ## Specificity 1.0000000 1.0000000 1.0000000000
  61.  
    ## F1 0.0000000 0.0000000 0.5000000000
  62.  
    ## BalancedAccuracy 0.5000000 0.5000000 1.0000000000
  63.  
    ## avg.sentiment.pos.response 0.3333333 0.0000000 0.3333333333
  64.  
    ## avg.sentiment.neg.response 0.0000000 0.0000000 -0.3666666667
  65.  
    ## NegativityQDAP PositivityQDAP
  66.  
    ## cor -0.944339551 0.942954167
  67.  
    ## cor.t.statistic -5.741148345 5.664705543
  68.  
    ## cor.p.value 0.004560908 0.004788521
  69.  
    ## lm.t.value -5.741148345 5.664705543
  70.  
    ## r.squared 0.891777188 0.889162562
  71.  
    ## RMSE 1.068401367 0.713624032
  72.  
    ## MAE 1.011111111 0.666666667
  73.  
    ## Accuracy 0.666666667 0.666666667
  74.  
    ## Precision NaN NaN
  75.  
    ## Sensitivity 0.000000000 0.000000000
  76.  
    ## Specificity 1.000000000 1.000000000
  77.  
    ## F1 0.000000000 0.000000000
  78.  
    ## BalancedAccuracy 0.500000000 0.500000000
  79.  
    ## avg.sentiment.pos.response 0.083333333 0.416666667
  80.  
    ## avg.sentiment.neg.response 0.366666667 0.000000000
  1.  
    ## WordCount SentimentGI NegativityGI PositivityGI
  2.  
    ## Accuracy 0.6666667 1.0000000 0.66666667 0.6666667
  3.  
    ## Precision NaN 1.0000000 NaN NaN
  4.  
    ## Sensitivity 0.0000000 1.0000000 0.00000000 0.0000000
  5.  
    ## Specificity 1.0000000 1.0000000 1.00000000 1.0000000
  6.  
    ## F1 0.0000000 0.5000000 0.00000000 0.0000000
  7.  
    ## BalancedAccuracy 0.5000000 1.0000000 0.50000000 0.5000000
  8.  
    ## avg.sentiment.pos.response 3.2500000 0.3333333 0.08333333 0.4166667
  9.  
    ## avg.sentiment.neg.response 4.0000000 -0.6333333 0.63333333 0.0000000
  10.  
    ## SentimentHE NegativityHE PositivityHE
  11.  
    ## Accuracy 0.6666667 0.66666667 0.6666667
  12.  
    ## Precision NaN NaN NaN
  13.  
    ## Sensitivity 0.0000000 0.00000000 0.0000000
  14.  
    ## Specificity 1.0000000 1.00000000 1.0000000
  15.  
    ## F1 0.0000000 0.00000000 0.0000000
  16.  
    ## BalancedAccuracy 0.5000000 0.50000000 0.5000000
  17.  
    ## avg.sentiment.pos.response 0.1250000 0.08333333 0.2083333
  18.  
    ## avg.sentiment.neg.response 0.0000000 0.00000000 0.0000000
  19.  
    ## SentimentLM NegativityLM PositivityLM
  20.  
    ## Accuracy 0.8333333 0.66666667 0.6666667
  21.  
    ## Precision 1.0000000 NaN NaN
  22.  
    ## Sensitivity 0.5000000 0.00000000 0.0000000
  23.  
    ## Specificity 1.0000000 1.00000000 1.0000000
  24.  
    ## F1 0.3333333 0.00000000 0.0000000
  25.  
    ## BalancedAccuracy 0.7500000 0.50000000 0.5000000
  26.  
    ## avg.sentiment.pos.response 0.2500000 0.08333333 0.3333333
  27.  
    ## avg.sentiment.neg.response -0.1000000 0.10000000 0.0000000
  28.  
    ## RatioUncertaintyLM SentimentQDAP NegativityQDAP
  29.  
    ## Accuracy 0.6666667 1.0000000 0.66666667
  30.  
    ## Precision NaN 1.0000000 NaN
  31.  
    ## Sensitivity 0.0000000 1.0000000 0.00000000
  32.  
    ## Specificity 1.0000000 1.0000000 1.00000000
  33.  
    ## F1 0.0000000 0.5000000 0.00000000
  34.  
    ## BalancedAccuracy 0.5000000 1.0000000 0.50000000
  35.  
    ## avg.sentiment.pos.response 0.0000000 0.3333333 0.08333333
  36.  
    ## avg.sentiment.neg.response 0.0000000 -0.3666667 0.36666667
  37.  
    ## PositivityQDAP
  38.  
    ## Accuracy 0.6666667
  39.  
    ## Precision NaN
  40.  
    ## Sensitivity 0.0000000
  41.  
    ## Specificity 1.0000000
  42.  
    ## F1 0.0000000
  43.  
    ## BalancedAccuracy 0.5000000
  44.  
    ## avg.sentiment.pos.response 0.4166667
  45.  
    ## avg.sentiment.neg.response 0.0000000

從文本挖掘中執行了一組預處理操作。將標記每個文檔,最后將輸入轉換為文檔項矩陣。

 

輸入

提供了具有其他幾種輸入格式的接口,其中包括

  • 字符串向量。

  • tm 軟件包中實現的DocumentTermMatrix和 TermDocumentMatrix(Feinerer,Hornik和Meyer 2008)。

  • tm 軟件包實現的語料庫對象 (Feinerer,Hornik和Meyer 2008)。

我們在下面提供示例。

向量的字符串

  1.  
    documents <- c("這很好",
  2.  
     
  3.  
    "這不好",
  4.  
     
  5.  
    "這介於兩者之間")
  6.  
    convertToDirection(analyzeSentiment(documents)$SentimentQDAP)
  1.  
    ## [1] positive negative neutral
  2.  
    ## Levels: negative neutral positive

文檔詞語矩陣

  1.  
    corpus <- VCorpus(VectorSource(documents))
  2.  
    convertToDirection(analyzeSentiment(corpus)$SentimentQDAP)
  1.  
    ## [1] positive negative neutral
  2.  
    ## Levels: negative neutral positive

語料庫對象

  1.  
    ## [1] positive negative neutral
  2.  
    ## Levels: negative neutral positive

可以直接與文檔術語矩陣一起使用,因此一開始就可以使用自定義的預處理操作。之后,可以計算情感分數。例如,可以使用其他列表中的停用詞替換停用詞。

字典

可區分三種不同類型的詞典。它們所存儲的數據各不相同,這些數據最終還控制着可以應用哪種情感分析方法。字典如下:

  • SentimentDictionaryWordlist 包含屬於一個類別的單詞列表。

  • SentimentDictionaryBinary 存儲兩個單詞列表,一個用於肯定條目,一個用於否定條目。

  • SentimentDictionaryWeighted 允許單詞的情感評分。

情感詞典詞表

  1.  
    # 替代
  2.  
     
  3.  
    d <- Dictionary(c(“不確定”,“可能”,“有可能”))
  4.  
    summary(d)
  1.  
    ## Dictionary type: word list (single set)
  2.  
    ## Total entries: 3

情感詞典

  1.  
    d <- DictionaryBin(c(“增加”,“上升”,“更多”),
  2.  
    c(“下降”))
  3.  
    summary(d)
  1.  
    ## Dictionary type: binary (positive / negative)
  2.  
    ## Total entries: 5
  3.  
    ## Positive entries: 3 (60%)
  4.  
    ## Negative entries: 2 (40%)

情感詞典加權

  1.  
    d <- SentimentDictionaryWeighted(c(“增加”,“減少”,“退出”),
  2.  
    c(+1, -1, -10),
  3.  
    rep(NA, 3))
  4.  
    summary(d)
  1.  
    ## Dictionary type: weighted (words with individual scores)
  2.  
    ## Total entries: 3
  3.  
    ## Positive entries: 1 (33.33%)
  4.  
    ## Negative entries: 2 (66.67%)
  5.  
    ## Neutral entries: 0 (0%)
  6.  
    ##
  7.  
    ## Details
  8.  
    ## Average score: -3.333333
  9.  
    ## Median: -1
  10.  
    ## Min: -10
  11.  
    ## Max: 1
  12.  
    ## Standard deviation: 5.859465
  13.  
    ## Skewness: -0.6155602
  1.  
    d <- SentimentDictionary(c(“增加”,“減少”,“退出”),
  2.  
    c(+1, -1, -10),
  3.  
    rep(NA, 3))
  4.  
    summary(d)
  1.  
    ## Dictionary type: weighted (words with individual scores)
  2.  
    ## Total entries: 3
  3.  
    ## Positive entries: 1 (33.33%)
  4.  
    ## Negative entries: 2 (66.67%)
  5.  
    ## Neutral entries: 0 (0%)
  6.  
    ##
  7.  
    ## Details
  8.  
    ## Average score: -3.333333
  9.  
    ## Median: -1
  10.  
    ## Min: -10
  11.  
    ## Max: 1
  12.  
    ## Standard deviation: 5.859465
  13.  
    ## Skewness: -0.6155602

字典生成

用向量的形式表示因變量。此外,變量給出了單詞在文檔中出現的次數。然后,該方法估計具有截距和系數的線性模型。估計基於LASSO正則化,它執行變量選擇。這樣,它將某些系數設置為正好為零。然后可以根據剩余單詞的系數按極性對它們進行排序。

  1.  
    # 創建字符串向量
  2.  
     
  3.  
     
  4.  
    documents <- c(“這是一件好事!”,
  5.  
     
  6.  
    “這是一件非常好的事!”,
  7.  
     
  8.  
    “沒關系。”
  9.  
     
  10.  
    “這是一件壞事。”,
  11.  
     
  12.  
    “這是一件非常不好的事情。”
  13.  
     
  14.  
    )
  15.  
    response <- c(1, 0.5, 0, -0.5, -1)
  16.  
     
  17.  
    # 使用LASSO正則化生成字典
  18.  
     
  19.  
     
  20.  
    dict
  1.  
    ## Type: weighted (words with individual scores)
  2.  
    ## Intercept: 5.55333e-05
  3.  
    ## -0.51 bad
  4.  
    ## 0.51 good
summary(dict)
  1.  
    ## Dictionary type: weighted (words with individual scores)
  2.  
    ## Total entries: 2
  3.  
    ## Positive entries: 1 (50%)
  4.  
    ## Negative entries: 1 (50%)
  5.  
    ## Neutral entries: 0 (0%)
  6.  
    ##
  7.  
    ## Details
  8.  
    ## Average score: -5.251165e-05
  9.  
    ## Median: -5.251165e-05
  10.  
    ## Min: -0.5119851
  11.  
    ## Max: 0.5118801
  12.  
    ## Standard deviation: 0.7239821
  13.  
    ## Skewness: 0

有幾種微調選項。只需更改參數,就可以用彈性網絡模型替換LASSO  。

最后,可以使用read() 和 保存和重新加載字典 write()

評估

最終,例程允許人們進一步挖掘生成的字典。一方面,可以通過summary() 例程顯示簡單的概述 。另一方面,核密度估計也可以可視化正詞和負詞的分布。

  1.  
    ## Comparing: wordlist vs weighted
  2.  
    ##
  3.  
    ## Total unique words: 4213
  4.  
    ## Matching entries: 2 (0.0004747211%)
  5.  
    ## Entries with same classification: 0 (0%)
  6.  
    ## Entries with different classification: 2 (0.0004747211%)
  7.  
    ## Correlation between scores of matching entries: 1
  1.  
    ## $totalUniqueWords
  2.  
    ## [1] 4213
  3.  
    ##
  4.  
    ## $totalSameWords
  5.  
    ## [1] 2
  6.  
    ##
  7.  
    ## $ratioSameWords
  8.  
    ## [1] 0.0004747211
  9.  
    ##
  10.  
    ## $numWordsEqualClass
  11.  
    ## [1] 0
  12.  
    ##
  13.  
    ## $numWordsDifferentClass
  14.  
    ## [1] 2
  15.  
    ##
  16.  
    ## $ratioWordsEqualClass
  17.  
    ## [1] 0
  18.  
    ##
  19.  
    ## $ratioWordsDifferentClass
  20.  
    ## [1] 0.0004747211
  21.  
    ##
  22.  
    ## $correlation
  23.  
    ## [1] 1
  1.  
    ## Dictionary
  2.  
    ## cor 0.94868330
  3.  
    ## cor.t.statistic 5.19615237
  4.  
    ## cor.p.value 0.01384683
  5.  
    ## lm.t.value 5.19615237
  6.  
    ## r.squared 0.90000000
  7.  
    ## RMSE 0.23301039
  8.  
    ## MAE 0.20001111
  9.  
    ## Accuracy 1.00000000
  10.  
    ## Precision 1.00000000
  11.  
    ## Sensitivity 1.00000000
  12.  
    ## Specificity 1.00000000
  13.  
    ## F1 0.57142857
  14.  
    ## BalancedAccuracy 1.00000000
  15.  
    ## avg.sentiment.pos.response 0.45116801
  16.  
    ## avg.sentiment.neg.response -0.67675202

下面的示例演示如何將計算出的字典用於預測樣本外數據的情感。然后通過將其與內置詞典進行比較來評估預測性能。

  1.  
    test_documents <- c(“這既不是好事也不是壞事”,
  2.  
     
  3.  
    “真是好主意!”,
  4.  
     
  5.  
    “不錯”
  6.  
    )
  7.  
     
  8.  
    pred <- predict(dict, test_documents)
  9.  
     
  1.  
    ## Dictionary
  2.  
    ## cor 5.922189e-05
  3.  
    ## cor.t.statistic 5.922189e-05
  4.  
    ## cor.p.value 9.999623e-01
  5.  
    ## lm.t.value 5.922189e-05
  6.  
    ## r.squared 3.507232e-09
  7.  
    ## RMSE 8.523018e-01
  8.  
    ## MAE 6.666521e-01
  9.  
    ## Accuracy 3.333333e-01
  10.  
    ## Precision 0.000000e+00
  11.  
    ## Sensitivity NaN
  12.  
    ## Specificity 3.333333e-01
  13.  
    ## F1 0.000000e+00
  14.  
    ## BalancedAccuracy NaN
  15.  
    ## avg.sentiment.pos.response 1.457684e-05
  16.  
    ## avg.sentiment.neg.response NaN

  1.  
    ## WordCount SentimentGI NegativityGI
  2.  
    ## cor -0.8660254 -0.18898224 0.18898224
  3.  
    ## cor.t.statistic -1.7320508 -0.19245009 0.19245009
  4.  
    ## cor.p.value 0.3333333 0.87896228 0.87896228
  5.  
    ## lm.t.value -1.7320508 -0.19245009 0.19245009
  6.  
    ## r.squared 0.7500000 0.03571429 0.03571429
  7.  
    ## RMSE 1.8257419 1.19023807 0.60858062
  8.  
    ## MAE 1.3333333 0.83333333 0.44444444
  9.  
    ## Accuracy 1.0000000 0.66666667 1.00000000
  10.  
    ## Precision NaN 0.00000000 NaN
  11.  
    ## Sensitivity NaN NaN NaN
  12.  
    ## Specificity 1.0000000 0.66666667 1.00000000
  13.  
    ## F1 0.0000000 0.00000000 0.00000000
  14.  
    ## BalancedAccuracy NaN NaN NaN
  15.  
    ## avg.sentiment.pos.response 2.0000000 -0.16666667 0.44444444
  16.  
    ## avg.sentiment.neg.response NaN NaN NaN
  17.  
    ## PositivityGI SentimentHE NegativityHE
  18.  
    ## cor -0.18898224 -0.18898224 NA
  19.  
    ## cor.t.statistic -0.19245009 -0.19245009 NA
  20.  
    ## cor.p.value 0.87896228 0.87896228 NA
  21.  
    ## lm.t.value -0.19245009 -0.19245009 NA
  22.  
    ## r.squared 0.03571429 0.03571429 NA
  23.  
    ## RMSE 0.67357531 0.67357531 0.8164966
  24.  
    ## MAE 0.61111111 0.61111111 0.6666667
  25.  
    ## Accuracy 1.00000000 1.00000000 1.0000000
  26.  
    ## Precision NaN NaN NaN
  27.  
    ## Sensitivity NaN NaN NaN
  28.  
    ## Specificity 1.00000000 1.00000000 1.0000000
  29.  
    ## F1 0.00000000 0.00000000 0.0000000
  30.  
    ## BalancedAccuracy NaN NaN NaN
  31.  
    ## avg.sentiment.pos.response 0.27777778 0.27777778 0.0000000
  32.  
    ## avg.sentiment.neg.response NaN NaN NaN
  33.  
    ## PositivityHE SentimentLM NegativityLM
  34.  
    ## cor -0.18898224 -0.18898224 0.18898224
  35.  
    ## cor.t.statistic -0.19245009 -0.19245009 0.19245009
  36.  
    ## cor.p.value 0.87896228 0.87896228 0.87896228
  37.  
    ## lm.t.value -0.19245009 -0.19245009 0.19245009
  38.  
    ## r.squared 0.03571429 0.03571429 0.03571429
  39.  
    ## RMSE 0.67357531 1.19023807 0.60858062
  40.  
    ## MAE 0.61111111 0.83333333 0.44444444
  41.  
    ## Accuracy 1.00000000 0.66666667 1.00000000
  42.  
    ## Precision NaN 0.00000000 NaN
  43.  
    ## Sensitivity NaN NaN NaN
  44.  
    ## Specificity 1.00000000 0.66666667 1.00000000
  45.  
    ## F1 0.00000000 0.00000000 0.00000000
  46.  
    ## BalancedAccuracy NaN NaN NaN
  47.  
    ## avg.sentiment.pos.response 0.27777778 -0.16666667 0.44444444
  48.  
    ## avg.sentiment.neg.response NaN NaN NaN
  49.  
    ## PositivityLM RatioUncertaintyLM SentimentQDAP
  50.  
    ## cor -0.18898224 NA -0.18898224
  51.  
    ## cor.t.statistic -0.19245009 NA -0.19245009
  52.  
    ## cor.p.value 0.87896228 NA 0.87896228
  53.  
    ## lm.t.value -0.19245009 NA -0.19245009
  54.  
    ## r.squared 0.03571429 NA 0.03571429
  55.  
    ## RMSE 0.67357531 0.8164966 1.19023807
  56.  
    ## MAE 0.61111111 0.6666667 0.83333333
  57.  
    ## Accuracy 1.00000000 1.0000000 0.66666667
  58.  
    ## Precision NaN NaN 0.00000000
  59.  
    ## Sensitivity NaN NaN NaN
  60.  
    ## Specificity 1.00000000 1.0000000 0.66666667
  61.  
    ## F1 0.00000000 0.0000000 0.00000000
  62.  
    ## BalancedAccuracy NaN NaN NaN
  63.  
    ## avg.sentiment.pos.response 0.27777778 0.0000000 -0.16666667
  64.  
    ## avg.sentiment.neg.response NaN NaN NaN
  65.  
    ## NegativityQDAP PositivityQDAP
  66.  
    ## cor 0.18898224 -0.18898224
  67.  
    ## cor.t.statistic 0.19245009 -0.19245009
  68.  
    ## cor.p.value 0.87896228 0.87896228
  69.  
    ## lm.t.value 0.19245009 -0.19245009
  70.  
    ## r.squared 0.03571429 0.03571429
  71.  
    ## RMSE 0.60858062 0.67357531
  72.  
    ## MAE 0.44444444 0.61111111
  73.  
    ## Accuracy 1.00000000 1.00000000
  74.  
    ## Precision NaN NaN
  75.  
    ## Sensitivity NaN NaN
  76.  
    ## Specificity 1.00000000 1.00000000
  77.  
    ## F1 0.00000000 0.00000000
  78.  
    ## BalancedAccuracy NaN NaN
  79.  
    ## avg.sentiment.pos.response 0.44444444 0.27777778
  80.  
    ## avg.sentiment.neg.response NaN NaN

預處理

如果需要,可以實施適合特定需求的預處理階段。如函數 ngram_tokenize() ,用於從語料庫中提取n-gram。

  1.  
    tdm <- TermDocumentMatrix(corpus,
  2.  
    control=list(wordLengths=c(1,Inf),
  3.  
    tokenize=function(x) ngram_tokenize(x, char=FALSE,
  4.  
    ngmin=1, ngmax=2)))
  1.  
    ## Dictionary type: weighted (words with individual scores)
  2.  
    ## Total entries: 7
  3.  
    ## Positive entries: 4 (57.14%)
  4.  
    ## Negative entries: 3 (42.86%)
  5.  
    ## Neutral entries: 0 (0%)
  6.  
    ##
  7.  
    ## Details
  8.  
    ## Average score: 5.814314e-06
  9.  
    ## Median: 1.602469e-16
  10.  
    ## Min: -0.4372794
  11.  
    ## Max: 0.4381048
  12.  
    ## Standard deviation: 0.301723
  13.  
    ## Skewness: 0.00276835
dict
  1.  
    ## Type: weighted (words with individual scores)
  2.  
    ## Intercept: -5.102483e-05
  3.  
    ## -0.44 不好
  4.  
    ## -0.29 非常糟糕
  5.  
    ## 0.29 好
  6.  
     
  7.  
     

性能優化

 

  1.  
    ## SentimentLM
  2.  
    ## 1 0.5
  3.  
    ## 2 0.5
  4.  
    ## 3 0.0
  5.  
    ## 4 -0.5
  6.  
    ## 5 -0.5

語言支持和可擴展性

 可以適應其他語言使用。為此,需要在兩點上進行更改:

  • 預處理:使用參數 language="" 來執行所有預處理操作。

  • 字典: 可以使用附帶的字典生成方法 。然后,這可以自動生成可應用於給定語言的正負詞詞典。

下面的示例使用德語示例。最后,我們進行情感分析。

  1.  
    documents <- c("Das ist ein gutes Resultat",
  2.  
    "Das Ergebnis war schlecht")
  3.  
     
  4.  
    sentiment <- ana(documents,
  5.  
    language="german",
  6.  
    sentiment
  1.  
    ## GermanSentiment
  2.  
    ## 1 0.0
  3.  
    ## 2 -0.5
  1.  
    ## [1] positive negative
  2.  
    ## Levels: negative positive

同樣,可以使用自定義情感分數來實現字典。

  1.  
    woorden <- c("goed","slecht")
  2.  
    scores <- c(0.8,-0.5)
  1.  
    ## DutchSentiment
  2.  
    ## 1 -0.5

 

實例

我們利用了tm 包中的路透社石油新聞 。

  1.  
     
  2.  
    # 分析情感
  3.  
     
  4.  
     
  5.  
    sentiment <- ana(crude)
  6.  
     
  7.  
    # 計算正面和負面新聞發布數量
  8.  
     
  9.  
     
  10.  
    table(coToB(sentiment$SentimentLM))
  1.  
    ##
  2.  
    ## negative positive
  3.  
    ## 16 4
  1.  
    # 情感最高和最低的新聞
  2.  
     
## [1] "HOUSTON OIL <HO> RESERVES STUDY COMPLETED"
crude[[which.min(sentiment$SentimentLM)]]$meta$heading
## [1] "DIAMOND SHAMROCK (DIA) CUTS CRUDE PRICES"
  1.  
    # 查看情感變量的摘要統計
  2.  
     
  3.  
     
  4.  
    summary(sentiment$SentimentLM)
  1.  
    ## Min. 1st Qu. Median Mean 3rd Qu. Max.
  2.  
    ## -0.08772 -0.04366 -0.02341 -0.02953 -0.01375 0.00000
  1.  
    # 可視化標准化情感變量的分布
  2.  
     
  3.  
     
  4.  
    hist(scale(sentiment$SentimentLM))

# 計算相關
  1.  
    ## SentimentLM SentimentHE SentimentQDAP
  2.  
    ## SentimentLM 1.0000000 0.2769878 0.4769730
  3.  
    ## SentimentHE 0.2769878 1.0000000 0.6141075
  4.  
    ## SentimentQDAP 0.4769730 0.6141075 1.0000000
  1.  
    # 1987-02-26 1987-03-02之間的原油新聞
  2.  
     
  3.  
     
  4.  
    plot(senti$Sentime)

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

單詞計算

對單詞進行計數 。

#詞(無停用詞)
  1.  
    ## WordCount
  2.  
    ## 1 3
# 計算所有單詞(包括停用詞)
  1.  
    ## WordCount
  2.  
    ## 1 4

 

參考文獻

Feinerer,Ingo,Kurt Hornik和David Meyer。2008年。“ R中的文本挖掘基礎結構”。 統計軟件雜志 25(5):1–54。

Tetlock,Paul C.,2007年。“將內容傳遞給投資者的情感:媒體在股票市場中的作用。” 金融雜志 62(3):1139–68。


最受歡迎的見解

1.探析大數據期刊文章研究熱點

2.618網購數據盤點-剁手族在關注什么

3.r語言文本挖掘tf-idf主題建模,情感分析n-gram建模研究

4.python主題建模可視化lda和t-sne交互式可視化

5.疫情下的新聞數據觀察

6.python主題lda建模和t-sne可視化

7.r語言中對文本數據進行主題模型topic-modeling分析

8.主題模型:數據聆聽人民網留言板的那些“網事”

9.python爬蟲進行web抓取lda主題語義數據分析

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM