缩放后的对比词云
还记得租房评论中极性分数的"成绩膨胀"吗?有时,另一种获取洞见的方式是先把分数缩放到以 0 为均值,然后再对子语料库进行子集划分。由于均值被调整为 0,之前的一些正面评论可能会被划到负面部分,反之亦然。本练习将帮助您缩放分数,并重新绘制 comparison.cloud()。去除"成绩膨胀"有助于提供更多洞见。
此前,您已将 polarity() 应用于 bos_reviews$comments,并创建了一个 comparison.cloud()。在本练习中,您将在创建 comparison.cloud() 之前先对结果使用 scale()。看看可视化是否会呈现不同的信息!
由于这主要是复习练习,很多代码已为您提供,只需补全正确的对象和参数即可!
本练习是课程的一部分
R 中的情感分析
练习说明
- 通过索引
[1:6,1:3]查看预加载的bos_pol$all的一部分。 - 新增一列
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"。 - 最后! 将
comparison.cloud()应用于矩阵对象all_tdm_m。留意新的最常见负向词。也许会发现之前未注意到的洞见!
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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")
)