A tibble: 6 × 2
word | n |
---|---|
good | 359 |
friend | 166 |
hope | 143 |
happy | 125 |
love | 117 |
deal | 92 |
我们在这里看到的大多是关于
friend
和love
的积极、快乐的话语。
我们还可以检查每部小说的情绪如何变化。我们只需几行主要是 dplyr 函数就可以做到这一点。首先,我们使用 Bing 词典和 inner_join() 找到每个单词的情感分数。
接下来,我们计算每本书的某些位置有多少积极和消极的词。这样我们可以分析情绪的变化情况。我们在这里定义一个索引来跟踪我们在叙述中的位置;该索引(使用整数除法)对 80 行文本的部分进行计数。
一小段文本可能没有足够的单词来很好地估计情绪,但对于太大的文本可能会导致正负情绪抵消。对于这些书,使用80行效果很好,但这可能会因单个文本、行的开头长度等而有所不同。然后我们使用 pivot_wider() 以便我们在不同的列中拥有消极和积极的情绪,最后计算净情绪(正面 - 负面)。
library(tidyr)
jane_austen_sentiment <- tidy_books %>%
inner_join(get_sentiments("bing")) %>%#使用bing情绪词典进行内连接
count(book, index = linenumber %/% 80, sentiment) %>%#按八十行为一个小段进行记数
pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>%#将数据转换成宽数据
mutate(sentiment = positive - negative)#计算净情绪,如果大于0说明是积极情绪,小于0说明是消极的
jane_austen_sentiment %>% head()
A tibble: 6 × 5
book | index | negative | positive | sentiment |
---|---|---|---|---|
Sense & Sensibility | 0 | 16 | 32 | 16 |
Sense & Sensibility | 1 | 19 | 53 | 34 |
Sense & Sensibility | 2 | 12 | 31 | 19 |
Sense & Sensibility | 3 | 15 | 31 | 16 |
Sense & Sensibility | 4 | 16 | 34 | 18 |
Sense & Sensibility | 5 | 16 | 51 | 35 |
现在我们可以在每部小说的情节轨迹上绘制这些情绪分数。请注意,我们绘制在x轴上的索引,是对应的小段文本,即每隔八十行为一段,这也反应了每本书的时间情绪变化
library(ggplot2)
ggplot(jane_austen_sentim