-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.R
101 lines (91 loc) · 2.68 KB
/
queries.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
library(WikidataR)
library(dplyr)
library(tidyr)
library(magrittr)
library(SPARQL)
library(stringr)
library(shiny)
library(wordcloud)
selectionnerID<-function(nom){
liste <- find_item(nom)
id <-""
for(i in 1:length(liste)){
if(is.null(liste[[i]]$description)){
next
}
if(grepl("given name",liste[[i]]$description)){
id <- liste[[i]]$id
break # we break on the first given name we find
}
}
# If we haven't found given name, then we take the first id
if(id==""){
id<-liste[[1]]$id
}
return(id)
}
endpoint <- "https://query.wikidata.org/bigdata/namespace/wdq/sparql"
#endpoint <- "https://query.wikidata.org/sparql"
prefix<-c("wd","<http://www.wikidata.org/entity/>",
"wdt", "<http://www.wikidata.org/prop/direct/>",
"wikibase","<http://wikiba.se/ontology#>")
generic_query <-"SELECT ?item ?itemLabel ?occupationLabel ?paysLabel ?annee
WHERE
{
?item wdt:P735 wd:REPLACE_ID .
?item wdt:P106 ?occupation .
OPTIONAL {?item wdt:P569 ?anneeN} .
OPTIONAL {?item wdt:P27 ?pays} .
BIND(YEAR(?anneeN) as ?annee) .
SERVICE wikibase:label { bd:serviceParam wikibase:language \"fr,en\" }
}
ORDER BY DESC (?annee)"
selectionnerNom<-function(x,colonne,nom) {
a<-x %>%
separate_(colonne,c("debut",nom,"fin"),sep="\"",remove=TRUE) %>%
select(-debut,-fin)
a[[nom]]<-ordered(a[[nom]])
a[[nom]]<-addNA(a[[nom]])
levels(a[[nom]])[length(levels(a[[nom]]))]<-"Inconnu"
return(a)
}
cleaningRes<-function(resultats){
resultats<-selectionnerNom(resultats,"itemLabel","nom")
resultats<-selectionnerNom(resultats,"occupationLabel","metier")
resultats<-selectionnerNom(resultats,"paysLabel","pays")
return(resultats)
}
queryStream <- . %>%
selectionnerID %>%
sub("REPLACE_ID", . ,generic_query) %>%
mySPARQL(endpoint, . ,
ns=prefix,format = "xml") %>%
use_series(results) %>%
cleaningRes()
queryStreamWithProgress <- . %>%
selectionnerID %>%
{incProgress(0.1,detail="\nBuilding query")
sub("REPLACE_ID", . ,generic_query)} %>%
{incProgress(0.1,detail="\nGetting data")
mySPARQL(endpoint, . ,
ns=prefix,format = "xml")} %>%
use_series(results) %>%
{incProgress(0.6,detail="\nCleaning data")
cleaningRes(.)}
couperMot<-function(mot){
#coupe une chaine de caractère sur l'espace le plus proche du milieu
z=as.character(mot)
mi=ceiling(str_length(z)/2)
a<-str_locate_all(z,"[ -]")[[1]]
if (length(a)==0){ #S'il n'y a pas d'espace ou tiret
return(z)
}
else { #on prend le premier espace après le milieu
b<-a[(a-mi>0)[,1],1][1]
if (length(b)==0){ #Si les espaces ou tiret sont avant le mileu
b<-a[1,1]
}
str_sub(z,b,b)<-"\n"
return(z)
}
}