縮放後的比較雲
還記得租屋評論的極性分數有「成績膨脹」嗎?有時候,另一種找出洞見的方法是先把分數縮放回以 0 為中心,再來做語料子集切分。這代表部分原本偏正向的評論,可能因為平均值被移到 0,而改被分到負向,反之亦然。本練習會協助你先把分數縮放,接著重新繪製 comparison.cloud()。移除「成績膨脹」有機會帶來更多洞見。
之前你已對 bos_reviews$comments 套用過 polarity(),並建立了 comparison.cloud()。在本練習中,你會在建立 comparison.cloud() 之前,先對結果進行 scale()。看看視覺化是否出現不一樣的觀察!
由於這主要是複習性質的練習,很多程式碼都已提供,你只需要補上正確的物件與參數即可!
本練習屬於課程
R 情感分析
練習說明
- 先檢視預先載入的
bos_pol$all的一部分,索引[1:6,1:3]。 - 新增一個名為
scaled_polarity的欄位,對極性分數欄bos_pol$all$polarity套用scale()。 - 針對正向評論,使用
subset(),條件為新欄位bos_reviews$scaled_polarity大於(>)0。 - 針對負向評論,使用
subset(),條件為新欄位bos_reviews$scaled_polarity小於(<)0。 - 用
paste()對pos_comments建立pos_terms。 - 接著用
paste()對neg_comments建立neg_terms。 - 將壓平後的文件
pos_terms和neg_terms組成單一語料,命名為all_terms。 - 依照慣用的
tm工作流程,將VectorSource()巢狀於VCorpus()中,並套用到all_terms。 - 使用物件
all_corpus建立TermDocumentMatrix()。注意這是一個採用 TfIdf 權重並包含基本清理函式的 TDM。 - 使用
as.matrix()將all_tdm轉為all_tdm_m。然後在既有程式碼中把欄名改為"positive"與"negative"。 - 最後! 對矩陣物件
all_tdm_m套用comparison.cloud()。留意新的最常見負向詞彙,也許能挖掘出先前沒注意到的洞見!
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Review
___
# Scale/center & append
bos_reviews$___ <- scale(___)
# Subset positive comments
pos_comments <- subset(bos_reviews$comments, ___)
# Subset negative comments
neg_comments <- subset(bos_reviews$comments, ___)
# Paste and collapse the positive comments
pos_terms <- paste(___, collapse = " ")
# Paste and collapse the negative comments
neg_terms <- paste(___, collapse = " ")
# Organize
all_terms<- c(___, ___)
# VCorpus
all_corpus <- ___(VectorSource(___))
# TDM
all_tdm <- TermDocumentMatrix(
___,
control = list(
weighting = weightTfIdf,
removePunctuation = TRUE,
stopwords = stopwords(kind = "en")
)
)
# Column names
___ <- as.matrix(___)
colnames(all_tdm_m) <- c("___", "___")
# Comparison cloud
comparison.cloud(
___,
max.words = 100,
colors = c("darkgreen", "darkred")
)