-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
450 lines (450 loc) · 14.2 KB
/
.Rhistory
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
message = FALSE)
library(tidyverse)
library(here)
# For text mining:
library(pdftools)
library(tidytext)
library(textdata)
install.packages('textdata')
library(textdata)
library(ggwordcloud)
install.packages('ggwordcloud')
library(ggwordcloud)
get_sentiments(lexicon = "nrc")
ipcc_path <- here("data","ipcc_gw_15.pdf")
ipcc_text <- pdf_text(ipcc_path)
ipcc_p9 <- ipcc_text[9]
ipcc_p9
ipcc_df <- data.frame(ipcc_text) %>%
mutate(text_full = str_split(ipcc_text, pattern = '\\n')) %>%
unnest(text_full) %>%
mutate(text_full = str_trim(text_full))
# Why '\\n' instead of '\n'? Because some symbols (e.g. \, *) need to be called literally with a starting \ to escape the regular expression. For example, \\a for a string actually contains \a. So the string that represents the regular expression '\n' is actually '\\n'.
# More information: https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html
ipcc_tokens <- ipcc_df %>%
unnest_tokens(word, text_full)
# See how this differs from `ipcc_df`
# Each word has its own row!
View(ipcc_tokens)
ipcc_wc <- ipcc_tokens %>%
count(word) %>%
arrange(-n)
View(ipcc_wc)
ipcc_stop <- ipcc_tokens %>%
anti_join(stop_words) %>%
select(-ipcc_text)
View(ipcc_stop)
# This code will filter out numbers by asking:
# If you convert to as.numeric, is it NA (meaning those words)?
# If it IS NA (is.na), then keep it (so all words are kept)
# Anything that is converted to a number is removed
ipcc_no_numeric <- ipcc_stop %>%
filter(is.na(as.numeric(word)))
# There are almost 2000 unique words
length(unique(ipcc_no_numeric$word))
ipcc_top100 <- ipcc_no_numeric %>%
count(word) %>%
arrange(-n) %>%
head(100)
ipcc_cloud <- ggplot(data = ipcc_top100, aes(label = word)) +
geom_text_wordcloud() +
theme_minimal()
ipcc_cloud
ggplot(data = ipcc_top100, aes(label = word, size = n)) +
geom_text_wordcloud_area(aes(color = n), shape = "diamond") +
scale_size_area(max_size = 12) +
scale_color_gradientn(colors = c("darkgreen","blue","red")) +
theme_minimal()
library(tidyverse)
library(here)
# For text mining:
library(pdftools)
library(tidytext)
library(textdata)
library(ggwordcloud)
help <- here("data", "data/FVH_1888-1895A-K.pdf")
help_text <- pdf_text(help)
help <- here("data", "FVH_1888-1895A-K.pdf")
help_text <- pdf_text(help)
help3 <- help_text[3]
help3
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
message = FALSE)
library(tidyverse)
library(here)
# For text mining:
library(pdftools)
library(tidytext)
library(textdata)
library(ggwordcloud)
# Note - Before lab:
# Attach tidytext and textdata packages
# Run: get_sentiments(lexicon = "nrc")
# Should be prompted to install lexicon - choose yes!
# Run: get_sentiments(lexicon = "afinn")
# Should be prompted to install lexicon - choose yes!
idig <- pdf_text('data/Boyd2021_OpenAreaOpenDataARAP.pdf')
idig_df <- data.frame(text = idig) %>%
mutate(page = 1:n())
idig_df
head(idig_df)
main_text <- idig_df %>%
filter(page %in% 1:20) %>%
mutate(text = str_split(text, '\n')) %>%
unnest(text)
idig_p9 <- idig[9]
idig_p9
idig_df <- data.frame(idig) %>%
mutate(text_full = str_split(idig, pattern = '\n')) %>%
unnest(text_full) %>%
mutate(text_full = str_trim(text_full))
idig_tokens <- idig_df %>%
unnest_tokens(word, text_full)
idig_wc <- idig_tokens %>%
count(word) %>%
arrange(-n)
idig_wc
idig_stop <- idig_tokens %>%
anti_join(stop_words) %>%
select(-idig)
idig_swc <- idig_stop %>%
count(word) %>%
arrange(-n)
idig_swc <- idig_stop %>%
count(word) %>%
arrange(-n)
idig_swc
# This code will filter out numbers by asking:
# If you convert to as.numeric, is it NA (meaning those words)?
# If it IS NA (is.na), then keep it (so all words are kept)
# Anything that is converted to a number is removed
idig_no_numeric <- idig_stop %>%
filter(is.na(as.numeric(word)))
# There are almost 2000 unique words
length(unique(idig_no_numeric$word))
# We probably don't want to include them all in a word cloud. Let's filter to only include the top 100 most frequent?
idig_top100 <- idig_no_numeric %>%
count(word) %>%
arrange(-n) %>%
head(100)
# There are almost 2000 unique words
length(unique(idig_no_numeric$word))
idig_top100
idig_cloud <- ggplot(data = idig_top100, aes(label = word)) +
geom_text_wordcloud() +
theme_minimal()
idig_cloud
ggplot(data = idig_top100, aes(label = word, size = n)) +
geom_text_wordcloud_area(aes(color = n), shape = "star") +
scale_size_area(max_size = 12) +
scale_color_gradientn(colors = c("darkgreen","blue","red")) +
theme_minimal()
ggplot(data = idig_top100, aes(label = word, size = n)) +
geom_text_wordcloud_area(aes(color = n), shape = "diamond") +
scale_size_area(max_size = 12) +
scale_color_gradientn(colors = c("darkgreen","blue","red")) +
theme_minimal()
get_sentiments(lexicon = "afinn")
# Note: may be prompted to download (yes)
# Let's look at the pretty positive words:
afinn_pos <- get_sentiments("afinn") %>%
filter(value %in% c(3,4,5))
# Do not look at negative words in class.
afinn_pos
get_sentiments(lexicon = "bing")
get_sentiments(lexicon = "nrc")
idig_afinn <- idig_stop %>%
inner_join(get_sentiments("afinn"))
idig_afinn_hist <- idig_afinn %>%
count(value)
# Plot them:
ggplot(data = idig_afinn_hist, aes(x = value, y = n)) +
geom_col()
# Plot them:
ggplot(data = idig_afinn_hist, aes(x = value, y = n)) +
geom_col(color = rainbow)
# Plot them:
ggplot(data = idig_afinn_hist, aes(x = value, y = n)) +
geom_col()
# What are these '-2' words?
idig_afinn2 <- idig_afinn %>%
filter(value == -2) %>%
head(50)
idig_afinn2
# Check the unique 2-score words:
unique(idig_afinn2$word)
# Count & plot them
idig_afinn2_n <- idig_afinn2 %>%
count(word, sort = TRUE) %>%
mutate(word = fct_reorder(factor(word), n))
ggplot(data = idig_afinn2_n, aes(x = word, y = n)) +
geom_col() +
coord_flip()
# OK most of these are pretty clear. Can you find any ambiguous terms?
idig_summary <- idig_afinn %>%
summarize(
mean_score = mean(value),
median_score = median(value)
)
idig_summary
idig_nrc <- idig_stop %>%
inner_join(get_sentiments("nrc"))
idig_exclude <- idig_stop %>%
anti_join(get_sentiments("nrc"))
# View(idig_exclude)
# Count to find the most excluded:
idig_exclude_n <- idig_exclude %>%
count(word, sort = TRUE)
head(idig_exclude_n)
idig_nrc_n <- idig_nrc %>%
count(sentiment, sort = TRUE)
# And plot them:
ggplot(data = idig_nrc_n, aes(x = sentiment, y = n)) +
geom_col()
idig_nrc_n5 <- idig_nrc %>%
count(word,sentiment, sort = TRUE) %>%
group_by(sentiment) %>%
top_n(5) %>%
ungroup()
idig_nrc_gg <- ggplot(data = idig_nrc_n5, aes(x = reorder(word,n), y = n, fill = sentiment)) +
geom_col(show.legend = FALSE) +
facet_wrap(~sentiment, ncol = 2, scales = "free") +
coord_flip() +
theme_minimal() +
labs(x = "Word", y = "count")
# Show it
idig_nrc_gg
# Save it
ggsave(plot = idig_nrc_gg,
here("figures","idig_nrc_sentiment.png"),
height = 8,
width = 5)
lord <- get_sentiments(lexicon = "nrc") %>%
filter(word == "excavation")
dig <- get_sentiments(lexicon = "nrc") %>%
filter(word == "excavation")
# Yep, check it out:
dig
get_sentiments(lexicon = "nrc")
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
message = FALSE)
library(tidyverse)
library(here)
# For text mining:
library(pdftools)
library(tidytext)
library(textdata)
library(ggwordcloud)
# Note - Before lab:
# Attach tidytext and textdata packages
# Run: get_sentiments(lexicon = "nrc")
# Should be prompted to install lexicon - choose yes!
# Run: get_sentiments(lexicon = "afinn")
# Should be prompted to install lexicon - choose yes!
get_sentiments(lexicon = "nrc")
get_sentiments(lexicon = "afinn")
ipcc_path <- here("data","ipcc_gw_15.pdf")
ipcc_text <- pdf_text(ipcc_path)
ipcc_text
ipcc_p9 <- ipcc_text[9]
ipcc_p9
ipcc_p9 <- ipcc_text[9]
ipcc_p9
ipcc_df <- data.frame(ipcc_text) %>%
mutate(text_full = str_split(ipcc_text, pattern = '\\n')) %>%
unnest(text_full) %>%
mutate(text_full = str_trim(text_full))
# Why '\\n' instead of '\n'? Because some symbols (e.g. \, *) need to be called literally with a starting \ to escape the regular expression. For example, \\a for a string actually contains \a. So the string that represents the regular expression '\n' is actually '\\n'.
# Although, this time round, it is working for me with \n alone. Wonders never cease.
# More information: https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html
ipcc_tokens <- ipcc_df %>%
unnest_tokens(word, text_full)
# See how this differs from `ipcc_df`
# Each word has its own row!
ipcc_tokens
ipcc_df
ipcc_df <- data.frame(ipcc_text) %>%
mutate(text_full = str_split(ipcc_text, pattern = '\\n')) %>%
unnest(text_full) %>%
mutate(text_full = str_trim(text_full))
# Why '\\n' instead of '\n'? Because some symbols (e.g. \, *) need to be called literally with a starting \ to escape the regular expression. For example, \\a for a string actually contains \a. So the string that represents the regular expression '\n' is actually '\\n'.
# Although, this time round, it is working for me with \n alone. Wonders never cease.
# More information: https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html
ipcc_df
ipcc_df <- data.frame(ipcc_text) %>%
mutate(text_full = str_split(ipcc_text, pattern = '\n')) %>%
unnest(text_full) %>%
mutate(text_full = str_trim(text_full))
ipcc_tokens <- ipcc_df %>%
unnest_tokens(word, text_full)
ipcc_df
# See how this differs from `ipcc_df`
# Each word has its own row!
ipcc_wc <- ipcc_tokens %>%
count(word) %>%
arrange(-n)
ipcc_wc
ipcc_tokens
ipcc_stop <- ipcc_tokens %>%
anti_join(stop_words) %>%
select(-ipcc_text)
ipcc_stop <- ipcc_tokens %>%
anti_join(stop_words) %>%
select(-ipcc_text)
ipcc_stop
ipcc_swc <- ipcc_stop %>%
count(word) %>%
arrange(-n)
ipcc_swc
ipcc_swc <- ipcc_stop %>%
count(word) %>%
arrange(-n)
ipcc_swc <- ipcc_stop %>%
count(word) %>%
arrange(-n)
ipcc_swc
# This code will filter out numbers by asking:
# If you convert to as.numeric, is it NA (meaning those words)?
# If it IS NA (is.na), then keep it (so all words are kept)
# Anything that is converted to a number is removed
ipcc_no_numeric <- ipcc_stop %>%
filter(is.na(as.numeric(word)))
# There are almost 2000 unique words
length(unique(ipcc_no_numeric$word))
# We probably don't want to include them all in a word cloud. Let's filter to only include the top 100 most frequent?
ipcc_top100 <- ipcc_no_numeric %>%
count(word) %>%
arrange(-n) %>%
head(100)
# There are almost 2000 unique words
length(unique(ipcc_no_numeric$word))
# We probably don't want to include them all in a word cloud. Let's filter to only include the top 100 most frequent?
ipcc_top100 <- ipcc_no_numeric %>%
count(word) %>%
arrange(-n) %>%
head(100)
ipcc_cloud <- ggplot(data = ipcc_top100, aes(label = word)) +
geom_text_wordcloud() +
theme_minimal()
ipcc_cloud
ggplot(data = ipcc_top100, aes(label = word, size = n)) +
geom_text_wordcloud_area(aes(color = n), shape = "star") +
scale_size_area(max_size = 12) +
scale_color_gradientn(colors = c("darkgreen","blue","red")) +
theme_minimal()
ggplot(data = ipcc_top100, aes(label = word, size = n)) +
geom_text_wordcloud_area(aes(color = n), shape = "diamond") +
scale_size_area(max_size = 12) +
scale_color_gradientn(colors = c("darkgreen","blue","red")) +
theme_minimal()
get_sentiments(lexicon = "afinn")
# Let's look at the pretty positive words:
afinn_pos <- get_sentiments("afinn") %>%
filter(value %in% c(3,4,5))
# Do not look at negative words in class.
afinn_pos
get_sentiments(lexicon = "bing")
get_sentiments(lexicon = "nrc")
ipcc_afinn <- ipcc_stop %>%
inner_join(get_sentiments("afinn"))
ipcc_afinn_hist <- ipcc_afinn %>%
count(value)
# Plot them:
ggplot(data = ipcc_afinn_hist, aes(x = value, y = n)) +
geom_col()
# What are these '2' words?
ipcc_afinn2 <- ipcc_afinn %>%
filter(value == 2)
# Check the unique 2-score words:
unique(ipcc_afinn2$word)
ipcc_afinn2
# Check the unique 2-score words:
unique(ipcc_afinn2$word)
# Count & plot them
ipcc_afinn2_n <- ipcc_afinn2 %>%
count(word, sort = TRUE) %>%
mutate(word = fct_reorder(factor(word), n))
ggplot(data = ipcc_afinn2_n, aes(x = word, y = n)) +
geom_col() +
coord_flip()
ipcc_summary <- ipcc_afinn %>%
summarize(
mean_score = mean(value),
median_score = median(value)
)
ipcc_summary
ipcc_nrc <- ipcc_stop %>%
inner_join(get_sentiments("nrc"))
ipcc_exclude <- ipcc_stop %>%
anti_join(get_sentiments("nrc"))
# View(ipcc_exclude)
# Count to find the most excluded:
ipcc_exclude_n <- ipcc_exclude %>%
count(word, sort = TRUE)
head(ipcc_exclude_n)
ipcc_nrc_n <- ipcc_nrc %>%
count(sentiment, sort = TRUE)
# And plot them:
ggplot(data = ipcc_nrc_n, aes(x = sentiment, y = n)) +
geom_col()
ipcc_nrc_n <- ipcc_nrc %>%
count(sentiment, sort = TRUE)
# And plot them:
ggplot(data = ipcc_nrc_n, aes(x = sentiment, y = n)) +
geom_col()
ipcc_nrc_n5 <- ipcc_nrc %>%
count(word,sentiment, sort = TRUE) %>%
group_by(sentiment) %>%
top_n(5) %>%
ungroup()
ipcc_nrc_gg <- ggplot(data = ipcc_nrc_n5, aes(x = reorder(word,n), y = n, fill = sentiment)) +
geom_col(show.legend = FALSE) +
facet_wrap(~sentiment, ncol = 2, scales = "free") +
coord_flip() +
theme_minimal() +
labs(x = "Word", y = "count")
# Show it
ipcc_nrc_gg
# Save it
ggsave(plot = ipcc_nrc_gg,
here("figures","ipcc_nrc_sentiment.png"),
height = 8,
width = 5)
conf <- get_sentiments(lexicon = "nrc") %>%
filter(word == "confidence")
# Yep, check it out:
conf
knitr::opts_chunk$set(echo = TRUE)
if(!require("devtools")) install.packages("devtools")
devtools::install_github("Guscode/Sentida")
install.packages("devtools")
install.packages("devtools")
install.packages("devtools")
install.packages("devtools")
knitr::opts_chunk$set(echo = TRUE)
devtools::install_github("Guscode/Sentida")
devtools::install_github("Guscode/Sentida")
library(Sentida)
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
message = FALSE)
library(tidyverse)
library(here)
# For text mining:
library(pdftools)
library(tidytext)
library(textdata)
library(ggwordcloud)
# Note - Before lab:
# Attach tidytext and textdata packages
# Run: get_sentiments(lexicon = "nrc")
# Should be prompted to install lexicon - choose yes!
# Run: get_sentiments(lexicon = "afinn")
# Should be prompted to install lexicon - choose yes!
text <- pdf_text("C:/Users/adela/Documents/RStudio/1_Teaching/AUDigitalArchives/output_data/1940_10minutes.txt")
text <- pdf_text("C:/Users/adela/Documents/Professional/Articles/Newest/2013_Book_AppliedSpatialDataAnalysisWith.pdf")
text <- pdf_text("C:/Users/adela/Documents/Professional/Articles/Newest/2013_Book_AppliedSpatialDataAnalysisWith.pdf")