Stressed Out!
在這裡你會調整負向詞彙,讓它更符合特定文本。接著,你會比較基本與自訂的 polarity() 分數。
Twenty One Pilots 有一首熱門歌曲叫做「Stressed Out」。如果你掃讀歌詞,會發現主題圍繞在年少時的懷舊。整體來看,多數人會認為其極性是負面的。歌詞反覆提到壓力、恐懼與假裝。
我們來用預設的主觀詞典,及自訂的主觀詞典,各自對歌詞做比較。
首先,你需要確認 key.pol 主觀詞典中不已經包含你要加入的詞。檢查的方法之一是使用 grep()。grep() 會回傳符合搜尋樣式之字元的列。以下是用在索引時的範例:
data_frame[grep("search_pattern", data_frame$column), ]
在確認該俚語或新詞不在 key.pol 詞典後,你需要把它加進去。以下程式碼使用 sentiment_frame() 來建構新的詞典。在這段程式中,sentiment_frame() 接受原本的正向詞向量 positive.words。接著,將原本的 negative.words 與「smh」和「kappa」這兩個被視為負面俚語的詞串接起來。雖然你可以指定正負權重,但預設值分別是 1 與 -1,因此以下程式碼未特別標示。
custom_pol <- sentiment_frame(positive.words, c(negative.words, "hate", "pain"))
現在你就能套用極性分析,並引用這個「自訂」的主觀詞典了!
本練習屬於課程
R 情感分析
練習說明
我們已建立 stressed_out,其中包含 Twenty One Pilots 的歌曲「Stressed Out」歌詞。
- 對
stressed_out使用polarity()來查看預設分數。 - 在
key.pol中檢查是否有包含「stress」的詞。使用grep()在x欄位搜尋並據此為資料框建立索引。 - 建立
custom_pol作為新的情感資料框。- 呼叫
sentiment_frame(),並將positive.words作為第一個引數傳入,不需串接任何新詞。 - 接著,使用
c()將negative.words與新詞彙 "stressed" 與 "turn back" 合併。
- 呼叫
- 重新對
stressed_out呼叫polarity(),並加入參數polarity.frame = custom_pol,比較新詞如何讓分數更貼近歌曲的實際表現。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# stressed_out has been pre-defined
head(stressed_out)
# Basic lexicon score
___(___)
# Check the subjectivity lexicon
___[___("stress", x)]
# New lexicon
custom_pol <- ___(___, c(negative.words, "___", "___"))
# Compare new score
___(___, polarity.frame = ___)