开始使用免费开始使用

缩放后的对比词云

还记得租房评论中极性分数的"成绩膨胀"吗?有时,另一种获取洞见的方式是先把分数缩放到以 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_termsneg_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")
)
编辑并运行代码