-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapter-6-sentiment-analysis.R
430 lines (406 loc) · 11 KB
/
chapter-6-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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Sentiment analysis of the biographies in the corpus using syuzhet and vader
USE_SAVED_SCORES <- TRUE
library(tidyverse)
get_vader_sentiment <- function(text) {
purrr::map_chr(
text,
\(x) vader::get_vader(x)[2], # compound score at idx 2,
.progress = "Vader scores"
)
}
# Based on the normalisation function from the vader package
# https://github.com/csrajath/vaderSentiment/blob/master/vaderSentiment/vaderSentiment.py#L106
normalise <- function(x, alpha = 15) {
norm_score <- x / sqrt((x * x) + alpha)
norm_score <- ifelse(norm_score > 1.0, 1.0, norm_score)
norm_score <- ifelse(norm_score < -1.0, -1.0, norm_score)
norm_score
}
bio_author_only <- function(corpus) {
corpus %>%
filter(sent_author == bio_author) %>%
group_by(biography) %>%
mutate(sent_idx = seq(n())) %>%
ungroup()
}
if (!USE_SAVED_SCORES) {
corpus <- readr::read_csv("data/biography-corpus/all-sentences.csv") %>%
mutate(
syuzhet = syuzhet::get_sentiment(text, method="syuzhet"),
bing = syuzhet::get_sentiment(text, method="bing"),
afinn = syuzhet::get_sentiment(text, method="afinn"),
vader = get_vader_sentiment(text)
) %>%
mutate(
across(syuzhet:afinn, normalise),
vader = as.numeric(vader)
)
write_csv(corpus, "data/biography-corpus/scored-sentences.csv")
} else {
corpus <- readr::read_csv("data/biography-corpus/scored-sentences.csv")
}
# Figure 6.2: Plot all the biographies
bio_labeller <- as_labeller(function(x) str_to_title(x) %>% str_remove("\\.xml|\\.txt"))
break_every <- function(n) {
function(limits) {
lower = limits[1]
upper = limits[2]
num_breaks = upper %/% n
rep
}
}
figure_6_1 <- corpus %>%
bio_author_only() %>%
pivot_longer(syuzhet:vader, names_to = "model", values_to = "score") %>%
group_by(biography, model) %>%
mutate(score = zoo::rollmean(score, k = 250, fill = NA)) %>%
ungroup() %>%
ggplot(aes(sent_idx, score)) +
facet_grid(vars(model), vars(biography), scales = "free_x", space = "free_x", labeller = bio_labeller) +
scale_x_continuous(breaks = scales::breaks_width(2000)) +
geom_line() +
labs(
x = "Sentence",
y = "Sentiment score (rolling mean)"
)
ggsave("figures/figure_6_1.tiff", plot = figure_6_1, width = 13, height = 9)
# Are the sentiment scores normally distributed?
corpus %>%
bio_author_only() %>%
pivot_longer(syuzhet:vader, names_to = "model", values_to = "score") %>%
ggplot(aes(score, after_stat(density))) +
facet_grid(vars(biography), vars(model)) +
geom_histogram(bins = 10)
# Table 6.2: Tabulate some statistics
table_6_2 <- corpus %>%
bio_author_only() %>%
pivot_longer(syuzhet:vader, names_to = "model", values_to = "score") %>%
group_by(biography, model) %>%
summarise(
mean = mean(score, na.rm = T),
sd = sd(score, na.rm = T),
range = diff(range(score, na.rm = T)),
gradient = coef(lm(score ~ sent_idx))[2] * 10000, # slope per 10000 sentences
.groups = "drop"
) %>%
group_by(model, .drop = T) %>%
mutate(
across(mean:gradient, rank, .names="{.col}_rank")
)
# Only export the mean, sd and range
table_6_2 %>%
select(biography:range) %>%
mutate(across(where(is.numeric), \(x) round(x, digits = 3))) %>%
write_excel_csv("figures/table_6_2.csv") # Need to convert to xlsx later
# Visualise Moore and Galt with annotations
figure_6_2_data <- corpus %>%
bio_author_only() %>%
filter(biography %in% c("galt.xml", "moore.xml")) %>%
group_by(biography) %>%
mutate(
score = zoo::rollmean(syuzhet, 250, fill = NA)
)
# Indicate boundary between the two volumes of Moore's Byron
moore_volume_line <- corpus %>%
bio_author_only() %>%
filter(biography == "moore.xml") %>%
filter(str_detect(text, "The circumstances under which")) %>%
select(biography, sent_idx) %>%
mutate(
label = c("Volume 1", "Volume 2") %>% list(),
offset = c(sent_idx - 310, sent_idx + 310) %>% list(),
y = -0.12
)
# Annotate key plot points with text labels
create_label_row <-
function(x,
.new_data,
.bio,
.pattern,
.off,
.extract,
.lab_off,
.x_off = 0) {
x %>%
rows_append(
.new_data %>%
filter(biography == .bio) %>%
filter(str_detect(text, .pattern)) %>%
transmute(
biography = biography,
score = score,
offset = score + .off,
extract = .extract,
label_offset = score + .lab_off,
sent_idx = sent_idx,
label_x = sent_idx + .x_off
)
)
}
# Helper for finding local maxima on the graph
find_local_optimum <-
function(data = figure_6_2_data,
.biography = "moore.xml",
.after = 0,
.before = Inf,
.optimum = max) {
data %>%
filter(biography == .biography,
sent_idx >= .after,
sent_idx <= .before,) %>%
filter(score == .optimum(score, na.rm = TRUE))
}
figure_6_2_annotations <- tibble::tibble(
biography = as.character(),
# for facet
score = as.numeric(),
# for 'y' parameter of geom_segment
offset = as.numeric(),
# for 'yend' parameter of geom_segment
sent_idx = as.numeric(),
# for x values of geom_segment
extract = as.character(),
# for 'label' of geom_text
label_offset = as.numeric(),
# for 'y' parameter of geom_text
label_x = as.numeric()
# for 'x' parameter of geom_text
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"he sailed for Ostend",
0.115,
"'... he sailed\nfor Ostend.'",
0.15
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"he was no more!",
0.13,
"'... he was\nno more!'",
0.16) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"bore him towards his beloved Greece",
0.08,
"'the breeze ... bore him\ntowards his beloved Greece'",
0.11
) %>%
create_label_row(
figure_6_2_data,
"galt.xml",
"animal passions mastered",
0.1,
"His 'animal passions'\nmaster him",
0.13
) %>%
create_label_row(
figure_6_2_data,
"galt.xml",
"the hollow valley",
-0.09,
"Byron gloomy and metaphysical\non his first trip to Greece",
-0.125
) %>%
create_label_row(
figure_6_2_data,
"galt.xml",
"after committing murder",
0.1,
"Byron contemplates\nmurder",
0.13
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"lone and unfriended",
-0.1,
"'lone and unfriended'\nin the House of Lords",
-0.13
) %>%
create_label_row(
figure_6_2_data,
"galt.xml",
"chiefs of the factions",
0.09,
"Byron attempts to 'reconcile' the\n'factions' in Missolonghi",
0.13
) %>%
create_label_row(
figure_6_2_data,
"galt.xml",
"never awoke again",
0.1,
"He dies",
0.11
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"first had the happiness",
0.1,
"'It was at this period\nI first had the happiness\nof seeing ... Lord Byron'",
0.15,
200
) %>%
# create_label_row(
# figure_6_4_data,
# "moore.xml",
# "nights of the same description",
# 0.1,
# "Moore hobnobs with Byron\nin London",
# 0.13
# ) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"county of Durham",
-0.12,
"Byron marries\nAnnabella Milbanke",
-0.145
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"she had breathed her last",
-0.15,
"Byron's mother dies",
-0.16
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"eleven years from this period",
0.16,
"Byron swims the Hellespont",
0.185
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"dictated by justice or by vanity",
0.1,
"The 'justice' of Byron's\nfeelings towards his wife",
0.125
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"fair object of this last",
-0.11,
"Byron meets his 'last love',\n Teresa Guiccioli",
-0.135
) %>%
# create_label_row(
# figure_6_4_data,
# "moore.xml",
# "in the shadow of the Alps",
# -0.1,
# "Byron defends himself in\n a bitter pamphlet",
# -0.125
# ) %>%
# create_label_row(
# figure_6_4_data,
# "moore.xml",
# "all the followers of Pope",
# 0.08,
# "Byron enthuses about\nAlexander Pope",
# 0.105
# ) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"death of his daughter",
-0.1,
"Byron's daughter\nAllegra dies",
-0.125
) %>%
create_label_row(
figure_6_2_data,
"moore.xml",
"love of solitary rambles",
0.1,
"Young Byron's 'love of\nsolitary rambles'",
0.15,
15
)
figure_6_2 <- figure_6_2_data %>%
ggplot(aes(sent_idx, score)) +
geom_line() +
geom_smooth(aes(sent_idx, syuzhet), method = "loess", alpha = 0.2) +
facet_grid(rows = vars(biography), labeller = bio_labeller) +
labs(
x = "Sentence",
y = "Sentiment score (syuzhet; rolling mean)"
) +
geom_vline(
aes(xintercept = sent_idx),
linetype = 2,
color = "darkgrey",
data = moore_volume_line,
) +
geom_text(
aes(x = offset, label = label, y = y),
data = unnest(moore_volume_line, cols = c(label, offset))
) +
geom_segment(
aes(x = sent_idx, xend = sent_idx, y = score, yend = offset),
data = figure_6_4_annotations,
color = "red"
) +
geom_text(
aes(x = label_x, y = label_offset, label = extract),
data = figure_6_4_annotations
)
ggsave("figures/figure_6_2.tiff", figure_6_2, width = 11, height = 7)
# Volume 1 vs Volume 2
corpus %>%
bio_author_only() %>%
filter(biography == "moore.xml") %>%
mutate(
volume = if_else(sent_idx <= moore_volume_line$sent_idx, 1, 2)
) %>%
group_by(volume) %>%
summarise(
across(syuzhet:vader, \(x) sd(x, na.rm = T))
) %>%
summarise(
across(syuzhet:vader, \(x) diff(x)/sum(x))
)
# The peak of Figure 6.2
find_local_optimum() # moore
find_local_optimum(.biography = "galt.xml")
# Appendix 8.3
# Moore's biography with all the letters etc. included
moore_volume_line_2 <- corpus %>%
filter(biography == "moore.xml") %>%
filter(str_detect(text, "The circumstances under which")) %>%
.$sent_idx
appendix_8_3 <- corpus %>%
filter(biography == "moore.xml") %>%
mutate(
syuzhet = zoo::rollmean(syuzhet, k = 500, fill = NA),
sent_author = case_when(
sent_author == "ThMoore1852" ~ "Moore",
sent_author == "LdByron" ~ "Byron",
.default = "Other"
)
) %>%
ggplot(aes(sent_idx, syuzhet, color = sent_author)) +
geom_path(aes(group = 1)) +
geom_vline(
xintercept = moore_volume_line_2,
linetype = 2,
color = "darkgrey"
) +
labs(
x = "Sentence",
y = "Sentiment score (syuzhet; rolling mean)",
color = "Author"
) +
annotate("text", x = moore_volume_line_2 - 1000, y = 0, label = "Volume 1") +
annotate("text", x = moore_volume_line_2 + 1000, y = 0, label = "Volume 2")
ggsave("figures/appendix_8_3.tiff", plot = appendix_8_3, width = 11, height = 5)