-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment.Rmd
225 lines (187 loc) · 5.63 KB
/
sentiment.Rmd
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
title: "Sentiment Hitchhikers Guide trilogy"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
> “The ships hung in the sky in much the same way that bricks don't.” - Hitchhiker's Guide to the Galaxy
These are the beautiful words of 'Hitchhiker's Guide to the Galaxy', book one in the trilogy
of five. Douglas Adams wrote in way that I really really like. Many many of the
sentences make me laugh, it is a beautiful high paced advanture. Just check these quotes:
> “For a moment, nothing happened. Then, after a second or so, nothing continued to happen.”
a lovely anticlimax.
![My hardcopy version of the books](data/images/IMG_20180608_183720120.jpg)
There many things we can do with the books. But in this case I will look at sentiment
and common words.
## Sentiment
```{r}
source("R/01_load_data.R")
library(tidytext)
unnestedHHGTTG<-
HHGTTG %>%
group_by(book, chapter) %>%
unnest_tokens(output = "word",input = content, token = "words") %>%
ungroup()
# use afinn and numbers
# NRC surprise, fear, joy, sadness and count
joy <- get_sentiments("nrc") %>%
filter(sentiment == "joy")
surprise <- get_sentiments("nrc") %>%
filter(sentiment == "surprise")
fear <- get_sentiments("nrc") %>%
filter(sentiment == "fear")
sadness <- get_sentiments("nrc") %>%
filter(sentiment == "sadness")
```
Using the NRC sentiment corpus we can look at words that signal joy, surprise, fear or sadness.
```{r}
unnestedHHGTTG %>%
inner_join(joy) %>%
group_by(book) %>%
count(word, sort = TRUE) %>%
top_n(15, wt = n) %>%
ungroup() %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = book))+
geom_col(show.legend = FALSE)+
facet_wrap(~book,scales = "free")+
coord_flip()+
labs(
title = "Top 25 most frequent joy-words",
subtitle = "",
x = "", y = ""
)
unnestedHHGTTG %>%
inner_join(surprise) %>%
group_by(book) %>%
count(word, sort = TRUE) %>%
top_n(15, wt = n) %>%
ungroup() %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = book))+
geom_col(show.legend = FALSE)+
facet_wrap(~book,scales = "free")+
coord_flip()+
labs(
title = "Top 25 most frequent surprise-words",
subtitle = "",
x = "", y = ""
)
unnestedHHGTTG %>%
inner_join(fear) %>%
group_by(book) %>%
count(word, sort = TRUE) %>%
top_n(15, wt = n) %>%
ungroup() %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = book))+
geom_col(show.legend = FALSE)+
facet_wrap(~book,scales = "free")+
coord_flip()+
labs(
title = "Top 25 most frequent fear-words",
subtitle = "",
x = "", y = ""
)
unnestedHHGTTG %>%
inner_join(sadness) %>%
group_by(book) %>%
count(word, sort = TRUE) %>%
top_n(15, wt = n) %>%
ungroup() %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = book))+
geom_col(show.legend = FALSE)+
facet_wrap(~book,scales = "free")+
coord_flip()+
labs(
title = "Top 25 most frequent sad-words",
subtitle = "",
x = "", y = ""
)
```
But we can also look at the sentiment per chapter, the bing corpus has words
divided in positive and negative words. We can count the number of negative and positive
words and check the result.
```{r}
sentiment_chapter <-
unnestedHHGTTG %>%
inner_join(get_sentiments("bing")) %>%
count(book, chapter, sentiment) %>%
spread(sentiment, n, fill = 0) %>%
mutate(
sentiment = positive - negative,
N_sent_words = positive+negative,
relative_sentiment = sentiment / N_sent_words
)
sentiment_chapter %>%
ggplot(aes(chapter, sentiment, fill = book)) +
geom_col(show.legend = FALSE) +
facet_wrap(~book, ncol = 2, scales = "free_x")+
labs(
title = "Sentiment per chapter",
subtitle = "Total sentiment per chapter"
)
sentiment_chapter %>%
ggplot(aes(chapter, relative_sentiment, fill = book)) +
geom_col(show.legend = FALSE) +
facet_wrap(~book, ncol = 2, scales = "free_x")+
labs(
title = "Sentiment per chapter",
subtitle = "relative sentiment per chapter"
)
```
We can also look at the sentiment per word.
```{r}
bing_word_counts <- unnestedHHGTTG %>%
inner_join(get_sentiments("bing")) %>%
count(word, sentiment, sort = TRUE) %>%
ungroup()
bing_word_counts %>% head(8)
```
And then
```{r}
bing_word_counts %>%
group_by(sentiment) %>%
top_n(10) %>%
ungroup() %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = sentiment)) +
geom_col(show.legend = FALSE) +
facet_wrap(~sentiment, scales = "free_y") +
labs(y = "Contribution to sentiment",
x = NULL) +
coord_flip()
```
### using tf-idf
Find the more typical words per book.
```{r}
book_words <- unnestedHHGTTG %>%
count(book, word, sort = TRUE) %>%
bind_tf_idf(word, book, n) %>%
arrange(-tf_idf)
total_words <- book_words %>%
group_by(book) %>%
summarize(total = sum(n))
book_words <- left_join(book_words, total_words)
ggplot(book_words, aes(n/total, fill = book)) +
geom_histogram(show.legend = FALSE) +
xlim(NA, 0.0009) +
facet_wrap(~book, ncol = 2, scales = "free_y")
book_words
```
```{r}
book_words %>%
arrange(desc(tf_idf)) %>%
mutate(word = factor(word, levels = rev(unique(word)))) %>%
group_by(book) %>%
top_n(15,wt = tf_idf) %>%
ungroup() %>%
ggplot(aes(word, tf_idf, fill = book)) +
geom_col(show.legend = FALSE) +
labs(x = NULL, y = "tf-idf") +
facet_wrap(~book, ncol = 2, scales = "free") +
coord_flip()
```
.