-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapping_all_ww1.Rmd
277 lines (200 loc) · 8.06 KB
/
scrapping_all_ww1.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
---
title: "Data Review"
author: "Etienne Rolland"
date: "11/10/2020"
output: html_document
---
```{r setup, include=FALSE}
#####Make sure you load any required packages.
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
## Import Your Data
*In the following code hunk, import your data.*
**Update :** one problem appear with the CDB90 Battle Dataset I originally choose to use. I needed to do additionnal scrapping, using the url in the battles.csv, to query additionnal data (latitude, longitude, etc). Sadly, the dataset is slightly old, and the links towards the resources of dbpedia were not the same as before. Hence, the query via SPARQL were not valid. Finally, I will just create the dataset using queries to the website.
I am loading the battles.csv, just to got the name of the fronts, to be sure to not forget something in the subsequent queries.
```{r data_import, warning=FALSE}
#### Use read_csv() or another function
#### Make sure your data is converted into a tibble.
#### For demonstration purposes, this example uses the mtcars data.
library(utils)
library(readr)
battles <- read_csv("battles.csv")
battles <- battles[grep(battles$cow_warname, pattern="World War I of 1914-1918"),]
```
```{r}
unique(battles$war)
```
## Scrapping
### Testing queries
If you are wondering what the hell is SPARQL, it is [here](https://www.r-bloggers.com/2013/01/sparql-with-r-in-less-than-5-minutes/).
```{r}
library(SPARQL)
```
```{r}
endpoint <- "https://dbpedia.org/sparql/"
query <-
'select ?lat ?long ?causalties year(?date) as ?year month(?date) as ?month day(?date) as ?day ?name ?campaign ?desc where {
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:lat ?lat .
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:long ?long .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:causalties ?causalties .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:date ?date .
<http://dbpedia.org/resource/Battle_of_Lorraine> foaf:name ?name .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:isPartOfMilitaryConflict ?campaign .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:abstract ?desc .
FILTER (langMatches(lang(?desc),"en"))
}'
query <- str_replace_all(query, "http://dbpedia.org/resource/Battle_of_Lorraine", "http://dbpedia.org/resource/Battle_of_Magdhaba")
qd <- SPARQL(endpoint,query)
df <- qd$results
df
```
```{r}
cleaning_and_concatenate_result <- function(df) {
df_concatenated <- df[1,]
for (column in colnames(df)) {
if (column == "depiction") { # une seule image
df_concatenated[column] <- df[1,]$depiction
next
}
content <- df[column]
content <- unique(content)
content <- paste(content, sep = ", ")
content <- str_replace_all(string=content, pattern = "c\\(", replacement = "")
content <- str_replace_all(string=content, pattern = '\\"', replacement = "")
content <- str_replace_all(string=content, pattern = '\\)$', replacement = "")
content <- str_replace_all(string=content, pattern = '@en', replacement = "")
df_concatenated[column] <- content
}
return(df_concatenated)
}
```
```{r}
library(digest)
# https://stackoverflow.com/questions/33689980/get-thumbnail-image-from-wikimedia-commons
digest("Camel_corps_at_Magdhaba.jpg", algo="md5", serialize=F)
```
```{r}
clean_url_image <- function(depiction) {
# the image inside dbpedia is a relative/redirection path inside wiki commons
# we need the exact url for printing inside a shiny app
#depiction <- "<http://commons.wikimedia.org/wiki/Special:FilePath/Camel_corps_at_Magdhaba.jpg>"
depiction <- str_replace_all(depiction ,"<|>", "")
img_name <- unlist(str_split(depiction, "/"))
img_name <- tail(img_name, n=1) #last one
hash <- digest(img_name, algo="md5", serialize=F)
# hash
# new column for the popup label
depiction <- paste0("https://upload.wikimedia.org/wikipedia/commons/",
substr(hash, 1, 1), # first character
"/",
substr(hash, 1, 2),
"/",
URLencode(img_name))
return(depiction)
}
```
```{r}
clean_url_image("<http://commons.wikimedia.org/wiki/Special:FilePath/Camel_corps_at_Magdhaba.jpg>")
```
### Scrapping for real
```{r}
got_data_dbpedia <- function(url) {
endpoint <- "https://dbpedia.org/sparql/"
query <-
'select ?lat ?long ?strength ?causalties ?campaign ?depiction year(?date) as ?year month(?date) as ?month day(?date) as ?day ?name ?result ?desc where {
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:lat ?lat .
<http://dbpedia.org/resource/Battle_of_Lorraine> geo:long ?long .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:strength ?strength .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:causalties ?causalties .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:isPartOfMilitaryConflict ?campaign .
<http://dbpedia.org/resource/Battle_of_Lorraine> foaf:depiction ?depiction .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:date ?date .
<http://dbpedia.org/resource/Battle_of_Lorraine> foaf:name ?name .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:result ?result .
<http://dbpedia.org/resource/Battle_of_Lorraine> dbo:abstract ?desc .
FILTER (langMatches(lang(?desc),"en"))
}'
query <- str_replace_all(query, "http://dbpedia.org/resource/Battle_of_Lorraine", url)
qd <- SPARQL(endpoint,query)
df <- qd$results
df <- cleaning_and_concatenate_result(df)
df$depiction <- clean_url_image(df$depiction)
df$desc <- str_replace_all(df$desc, "–", "-")
df$name <- iconv(df$name , from="UTF-8", to="LATIN1")
df$desc <- iconv(df$desc , from="UTF-8", to="LATIN1")
df$campaign <- str_replace_all(df$campaign , "<|>", "")
df$campaign <- str_replace_all(df$campaign , "http://dbpedia.org/resource/", "")
return(df)
}
```
```{r}
url <- "http://dbpedia.org/resource/Battle_of_Magdhaba"
```
```{r}
got_data_dbpedia(url)
```
### Politely
See : [the polite package](https://dmi3kno.github.io/polite/index.html).
```{r}
library(polite)
got_data_dbpedia <- politely(got_data_dbpedia, verbose=TRUE)
```
```{r}
# url <- "http://dbpedia.org/resource/Battle_of_Lorraine"
got_data_dbpedia(url)
```
Edit while coding : the recursion add up for 100 battles.
See the Recursion entry below.
```{r}
got_battles_for_front <- function(url) {
endpoint <- "https://dbpedia.org/sparql/"
query <-
'select ?battle where {
?battle dbo:isPartOfMilitaryConflict <balise_to_change>
}'
query <- str_replace_all(query, "balise_to_change", url)
qd <- SPARQL(endpoint,query)
df <- qd$results
vector_battles <- as.vector(unlist(df))
vector_battles <- iconv(vector_battles, from="UTF-8", to="LATIN1")
vector_battles <- str_replace_all(vector_battles, "<|>", "")
# recursion ?
for (url in vector_battles) {
vector_battles <- c(vector_battles, try(got_battles_for_front(url)))
}
return(vector_battles[grepl(vector_battles, pattern="http")])
}
got_battles_for_front <- politely(got_battles_for_front, verbose=TRUE)
```
```{r}
create_front_battle_df <- function(url_front) {
vector_battles <- got_battles_for_front(url_front)
#vector_battles <- head(vector_battles)
list_battles_df<-list()
i <- 0
for (battles in vector_battles) {
i <- i + 1
list_battles_df[[i]] <- try(got_data_dbpedia(battles))
}
list_battles_df <- list_battles_df[sapply(list_battles_df, class) == "data.frame"]
list_battles_df <- do.call(rbind, list_battles_df)
return(list_battles_df)
}
```
### Recursion
Some front are nested inside battles, see for example :
Gallipoli_campaign is queried from inside https://dbpedia.org/page/Middle_Eastern_theatre_of_World_War_I.
Edit : after some refactoring, the recursion must go inside got_battles_for_front
```{r message=FALSE, warning=FALSE}
#head(got_data_dbpedia("http://dbpedia.org/resource/Gallipoli_campaign"))
```
```{r message=FALSE, warning=FALSE}
#got_battles_for_front("http://dbpedia.org/resource/Gallipoli_campaign")
```
## Scrapping
```{r message=FALSE, warning=FALSE}
WWI <- create_front_battle_df("http://dbpedia.org/resource/World_War_I")
dim(WWI)
write.csv(WWI, file='WWI_all_battles.csv', row.names=FALSE)
```