【问题标题】:Convert the locations to pages and then compute the average sentiment in each page. Plot that average score by page将位置转换为页面,然后计算每个页面中的平均情绪。按页面绘制平均分数
【发布时间】: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


    【解决方案1】:

    要获得page_number,我们使用商的上限,然后我们group_by page_number 并汇总值

    words_sentiments %>%
      mutate(page_number = ceiling(word_num / 300)) %>%
       group_by(page_number) %>%
        summarise(average=mean(value)) -> summed
    summed %>%  ggplot(aes(x=page_number, y=average)) + geom_point() + geom_smooth()
    

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2014-06-30
      • 2019-01-06
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 2023-03-15
      • 2021-04-30
      相关资源
      最近更新 更多