【发布时间】:2020-11-18 21:29:41
【问题描述】:
在 9 方面需要帮助。但我已经包含了相关代码。
5. 我们稍后会在书中绘制情感与位置的关系图。为此,将带有单词编号的列添加到表中会很有用。words <- words %>%
mutate(word_num = 1:length(word))
head(words)
6. 从 words 对象中删除停用词和数字。提示:使用anti_join。
data("stop_words")
words_clean <- words %>%
anti_join(stop_words) %>%
filter(!str_detect(word,"^\\d+$"))
head(words_clean)
7. 现在使用 AFINN 词典为每个单词分配一个情绪值。
library(textdata)
afinn <- get_sentiments("afinn")
words_sentiments <- words_clean %>%
inner_join(afinn, by = "word")
head(words_sentiments)
8. 绘制情感分数与书中位置的关系图,并添加更平滑的内容。
library(ggplot2)
words_sentiments %>% ggplot(aes(x= word_num, y=value)) +
geom_point()+
geom_smooth()
9. 假设每页有 300 个单词。将位置转换为页面,然后计算每个页面中的平均情绪。按页面绘制平均分数。添加一个似乎通过数据的平滑器。
# word_sentiments2 <- words_sentiments %>%
cut(words_sentiments$word_num,seq(1,max(words_sentiments$word_num), by=300))
word
【问题讨论】:
标签: r text-mining