-
Notifications
You must be signed in to change notification settings - Fork 0
/
WikiBooksAnalysis.Rmd
449 lines (288 loc) · 11.1 KB
/
WikiBooksAnalysis.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
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
---
output:
html_document:
toc: true
toc_float:
collapsed: false
number_sections: true
title: "Adding ISBNs to Wiki books"
author: "[Matthew-D-Dwyer](https://github.com/Matthew-D-Dwyer)"
date: "`r paste0('Last Run: ', format(Sys.time(), '%A %d-%B-%Y'))`"
params:
param1: "Don't Forget about params"
---
<style>
#TOC {
font-family: Calibri;
font-size: 16px;
border-color: #3D68DF;
background: #3D68DF;
}
body {
font-family: Garamond;
font-size: 16px;
border-color: #D0D0D0;
background-color: #D0D0D0;
color: #1A1A1A;
}
pre {
color: #1A1A1A
background: #D0D0D0;
background-color: #D0D0D0
font-family: Calibri;
}
</style>
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(collapse = TRUE)
knitr::opts_chunk$set(warning = TRUE)
knitr::opts_chunk$set(message = TRUE)
knitr::opts_chunk$set(include = TRUE)
custom_black <- '1A1A1A'
custom_white <- 'C0C0C0'
custom_grey_dark <- '6F6F6F'
custom_grey_light <- 'B2B2B2'
custom_accent_blue <- '3D6BFF'
```
```{r libraries etc., message = FALSE }
library(tidyverse)
library(dplyr)
library(RSQLite)
```
### How many books?
The first thing I wanted to check is how many books there are in each of the different languages.
This code threw errors on my machine indicating that not all rows were retrieved however I was able to check the source and my result is a full data set.
```{r dataload}
wb_data_base <- file.path('/Users',
'macatron',
'Documents',
'Data Analysis',
'R',
'wiki--books',
'wikibooks.sqlite')
#creating connection
con <- DBI::dbConnect(RSQLite::SQLite(), wb_data_base)
# Lookup to get the full language from the two character part
Languages <- data.frame(Abbv = c('de',
'en',
'es',
'fr',
'he',
'hu',
'it',
'ja',
'nl',
'pl',
'pt',
'ru'),
full_name = c('German',
'English',
'Spanish',
'French',
'Greek',
'Hungarian',
'Italian',
'Japanese',
'Dutch',
'Polish',
'Portuguese',
'Russian'))
names_stripper <- function(full_title)
#Function that removes 'Wikibooks: ' from the start of the title and everything after and including the first slash
{
title_no_prefix <- full_title %>%
str_remove_all('Wikibooks: ')
slash_loc <- str_locate(title_no_prefix, '/')[1]
if (is.na(slash_loc)) {
title_no_suffix <- title_no_prefix }
else if (slash_loc > 0) {
title_no_suffix <- title_no_prefix %>%
str_sub(1, slash_loc-1)} else {
print('Error Error Does Not Compute')
title_no_suffix <- 'Error Error Does Not Compute'
}
title_no_suffix }
title_counter <- function(languages_brv)
# function that counts how many titles there ar in the data base for that language
{
con <- DBI::dbConnect(RSQLite::SQLite(), wb_data_base)
x <- dbSendQuery(con, paste0("SELECT count (*) FROM ", languages_brv), n = Inf) %>%
dbFetch(n =Inf)
x <- data.frame(titles = x, Abbv = languages_brv)
}
Titles_table <- map_dfr(Languages$Abbv, title_counter)
Titles_table <- Titles_table %>%
left_join(Languages, by = c('Abbv' = 'Abbv')) %>%
arrange(desc(count....))
Titles_table <- Titles_table %>%
mutate(Language = factor(full_name, levels = Titles_table$full_name)) %>%
rename(Titles = count....)
```
### Plotting the number of books in each language
The vast majority of books are English
```{r }
Titles_table %>%
ggplot(aes(x = Language, y = Titles)) +
geom_col(fill = 'cornflower blue') +
geom_label(aes(label = Titles), size = 2.5) +
theme_minimal() +
labs(title = 'Wikibooks Number of Titles by Language',
subtitle = 'Extraction date: Sep 09 2021',
caption = 'Source: Kaggle, starter-wikibooks-dataset') +
coord_flip()
```
### Trying to extract the ISBN
The book title information and the data base in general is pretty messy.
But the full text of the books often contains the ISBN and if this number can be extracted, even for a fraction of the books then this can be used to link the full text to higher quality metadata.
Starting with one book that does have ISBN in it.
```{r}
#Pulling the first English Book
Sample_full_text <- dbSendQuery(con, "SELECT * FROM en",
n = 1) %>%
dbFetch(n = 1)
ISBN_Loc <- str_locate(Sample_full_text$body_text, "ISBN: ")
```
Looks like no ISBN in that one, but what if I test it for some text I know is
in the book.
```{r}
Oncology_loc <- str_locate(Sample_full_text$body_text, "Oncology")
```
Test passed so it won't work for that book, what if I cycle through 1000 books?
Again this throws errors but the result has 1,000 observations so I'm OK.
```{r}
books_1000 <- dbSendQuery(con, "SELECT * FROM en",
n = 1000) %>%
dbFetch(n = 1000)
```
Cycling through them all, printing found one if the 'ISBN: ' search returns a result
```{r}
book_list <- books_1000$body_text
for (book in book_list) {
ISBN_Loc <- str_locate(book, 'ISBN: ')
if (!is.na(ISBN_Loc[1])) {
print('Found One!')
}}
```
Looks like there was one in that first thousand, does that mean about 83 in the whole English data set?
Again with the errors, but all the books load.
```{r}
all_books <- dbSendQuery(con, "SELECT * FROM en",
n = Inf) %>%
dbFetch(n = Inf)
Books_with_ISBN <- data.frame(title = NULL,
ISBN_Loc = NULL,
Book_no = NULL)
for (i in 1:length(all_books$title)) {
ISBN_Loc <- str_locate(all_books$body_text[i], 'ISBN')
if (!is.na(ISBN_Loc[1])) {
Books_with_ISBN <- rbind(Books_with_ISBN,
data.frame(title = all_books$title[i],
ISBN_Loc = ISBN_Loc[1],
Book_no = i))
} }
print('Books with a possible ISBN: ')
print(length(Books_with_ISBN$title))
```
Initially I only got 84 which is exactly 1 in a thousand, but then I re ran it without the colon after ISBN this got a much higher (but still lower than I hoped) hit rate of about 30% (2481 books).
### Pulling the actual ISBNs
An ISBN is 10 or 13 digits not including the hyphens colons spaces or whatever
So to start I'll just take the next 30 characters - to be cleaned up later.
Printing 6 at the end so we can get an idea of what further cleaning may be required.
```{r}
indexes <- Books_with_ISBN$Book_no
ISBN_Loc <- Books_with_ISBN$ISBN_Loc
isbn <- c()
Books_with_ISBN$Full_Text <- all_books$body_text[indexes]
Books_with_ISBN$ISBN <- substr(Books_with_ISBN$Full_Text,
Books_with_ISBN$ISBN_Loc,
Books_with_ISBN$ISBN_Loc + 30)
# Removing the full text to make it a bit more manageable copute wise
Books_with_ISBN <- Books_with_ISBN %>% select(-Full_Text)
head(Books_with_ISBN$ISBN)
```
Looking like a good start, all these records above look like they can be cleanable into a real ISBN numbers.
### Cleaning the ISBNs
First remove anything that isn't a number and make into a single character.
```{r}
isbn_cleaner <- function(un_clean_isbn) {
un_clean_isbn %>%
str_extract_all("[0-9]") %>%
unlist() %>%
str_c(collapse = '') }
Books_with_ISBN$ISBN_clean <- map_chr(Books_with_ISBN$ISBN,
isbn_cleaner)
Books_with_ISBN$ISBN_clean[1:20]
```
Looking at the above first 20 ISBN's it it looking pretty good.
ISBNs should have 10 or 13 digits lets see what the distribution is like.
### Extracting German ISBNs
```{r }
german_books <- dbSendQuery(con, "SELECT * FROM de") %>%
dbFetch()
book_list <- german_books$body_text
for (book in book_list) {
ISBN_Loc <- str_locate(book, 'ISBN')
if (!is.na(ISBN_Loc[1])) {
print('Found One!')
}}
```
Looks like lots of German ones have them!
Making a table of them
```{r}
### Note need to change for German books
indexes <- Books_with_ISBN$Book_no
ISBN_Loc <- Books_with_ISBN$ISBN_Loc
isbn <- c()
Books_with_ISBN$Full_Text <- all_books$body_text[indexes]
Books_with_ISBN$ISBN <- substr(Books_with_ISBN$Full_Text,
Books_with_ISBN$ISBN_Loc,
Books_with_ISBN$ISBN_Loc + 30)
# Removing the full text to make it a bit more manageable copute wise
Books_with_ISBN <- Books_with_ISBN %>% select(-Full_Text)
head(Books_with_ISBN$ISBN)
```
What about Japanese? Will the same method work
### Testing method on Japanese title
Making a table of Japanese books with ISBN them, how many are there?
```{r }
all_books <- dbSendQuery(con, "SELECT * FROM ja",
n = Inf) %>%
dbFetch(n = Inf)
Books_with_ISBN <- data.frame(title = NULL,
ISBN_Loc = NULL,
Book_no = NULL)
for (i in 1:length(all_books$title)) {
ISBN_Loc <- str_locate(all_books$body_text[i], 'ISBN')
if (!is.na(ISBN_Loc[1])) {
Books_with_ISBN <- rbind(Books_with_ISBN,
data.frame(title = all_books$title[i],
ISBN_Loc = ISBN_Loc[1],
Book_no = i))
} }
print('Japenese Books with a possible ISBN:')
print(length(Books_with_ISBN$title))
```
```{r}
indexes <- Books_with_ISBN$Book_no
ISBN_Loc <- Books_with_ISBN$ISBN_Loc
isbn <- c()
Books_with_ISBN$Full_Text <- all_books$body_text[indexes]
Books_with_ISBN$ISBN <- substr(Books_with_ISBN$Full_Text,
Books_with_ISBN$ISBN_Loc,
Books_with_ISBN$ISBN_Loc + 30)
# Removing the full text to make it a bit more manageable copute wise
Books_with_ISBN <- Books_with_ISBN %>% select(-Full_Text)
head(Books_with_ISBN$ISBN)
Books_with_ISBN$ISBN_clean <- map_chr(Books_with_ISBN$ISBN,
isbn_cleaner)
Books_with_ISBN$ISBN_clean[1:20]
```
It works for Japanese too!
# Creating a meta data table for all languages
```{r}
ISBN_puller <- function(book_text) {
ISBN_Loc <- str_locate(book_text, 'ISBN')
unclean_ISBN <- substr(book_text,
ISBN_Loc,
ISBN_Loc + 30)}
```