-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_sentiment_analysis.R
47 lines (40 loc) · 1.29 KB
/
03_sentiment_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
library(tidyverse)
library(feather)
library(tidytext)
user_word_groups <- read_feather("data/user_word_groups.feather")
# Sentiment Analysis by User Group
afinn <- user_word_groups %>%
inner_join(get_sentiments("afinn")) %>%
group_by(topic) %>%
summarize(value = sum(value)) %>%
ungroup() %>%
mutate(lexicon = "afinn")
bing <- user_word_groups %>%
inner_join(get_sentiments("bing")) %>%
mutate(value = ifelse(sentiment == "positive", 1, -1)) %>%
group_by(topic) %>%
summarize(value = sum(value)) %>%
ungroup() %>%
mutate(lexicon = "bing")
nrc <- user_word_groups %>%
inner_join(get_sentiments("nrc")) %>%
mutate(
value = str_replace(sentiment, "anticipation|trust|joy|surprise", "positive"),
value = str_replace(sentiment, "sadness|fear|anger|disgust", "negative"),
value = ifelse(sentiment == "positive", 1, -1)
) %>%
group_by(topic) %>%
summarize(value = sum(value)) %>%
ungroup() %>%
mutate(lexicon = "nrc")
# Sentiment Analysis Plot
bind_rows(afinn, bing, nrc) %>%
ggplot(aes(topic, value, fill = lexicon)) +
geom_col(show.legend = F) +
facet_wrap(~ lexicon, ncol = 1, scales = 'free_y') +
labs(
x = 'Time',
y = 'Sentiment',
title = 'Sentiment Analysis by User Group'
)
ggsave("plot/sentiment_analysis_by_group.png", width = 12, height = 8)