开始使用免费开始使用

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,作为新的情感数据框。
  • 重新对 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 = ___)
编辑并运行代码