-
Notifications
You must be signed in to change notification settings - Fork 0
/
wdMapping.js
396 lines (354 loc) · 14.4 KB
/
wdMapping.js
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
/*
* GUIの情報を使ってSPARQLクエリを作る
*/
async function makeMappingQuery2(query,uri,textLABEL,mode){
let conditions = "{\n";
let ids;
//名称の処理
if(textLABEL!=""){
conditions += 'BIND (<'+uri+'> AS ?uri)';
conditions += 'BIND ("'+textLABEL+'" AS ?lbl)';
if(mode == 'IRI'){//「目的語が指定したIRIの時=マッピング済み」の主語を取得する
conditions+= '{?item ?p <'+uri+'> .}\n';
}
else if(mode == 'LabelFull'){
conditions+= '{?item rdfs:label|skos:altLabel "'+textLABEL+'"@ja.}\n';
conditions+= 'UNION {?item rdfs:label|skos:altLabel "'+textLABEL+'"@en.}\n';
}
else if(mode == 'LabelForward'){
ids = await getWdIDsBySE(textLABEL);
//得られたID一覧の数が上限(=50)になったら,「続きを検索」表示をON
if(ids.length==50){
contQueryIds = true;
}
const vals = ids.join(" ").replaceAll("Q","wd:Q");
conditions+= 'VALUES ?item {'+vals+'}\n';
}
else if(mode == 'LabelAmbi'){
ids = await getWdIDs(textLABEL);
//得られたID一覧の数が上限(=50)になったら,「続きを検索」表示をON
if(ids.length==50){
contQueryIds = true;
}
const vals = ids.join(" ").replaceAll("Q","wd:Q");
conditions+= 'VALUES ?item {'+vals+'}\n';
}
else if(mode == 'LabelPart'){
conditions+= '?item rdfs:label ?label.\n';
conditions+= 'FILTER(contains(?label,"'+textLABEL+'"))' ;
}
}
//検索条件・検索項目の更新
// document.getElementById('settings_area').value
// ="let search_cond = \n" + JSON.stringify(saveSearchConds(),null,' ') +";\n\n"
// +"let search_prop = \n" + JSON.stringify(saveSearchProps(),null,' ') +";\n";
//検索条件の設定
for(let i=0; i<search_cond.length ;i++){
const condID = search_cond[i].id;
const textCond = search_cond[i].cond;
const textVal = search_cond[i].val;
if(textCond!="" && textVal!=""){
if(search_cond[i].type =="ID"){
conditions+= '?item '+textCond+' '+textVal+ '.\n';
}
else if(search_cond[i].type =="getID"){
const qid = await getWdIDse(textVal); //IDを検索で取得
conditions+= '?item '+textCond+' '+"wd:"+qid+ '.\n';
}
else if(search_cond[i].type =="STR-ja"){
conditions+= '?item '+textCond+' "'+textVal+ '"@ja .\n';
}
else if(search_cond[i].type =="REPLACE"){
conditions+= textCond.replace('####',textVal)+'\n';
}
}
}
//検索項目の設定
let opt_select = "";
let options = "";
for(let i=0;i<search_prop.length;i++){
const optid = search_prop[i].id;
const textOpt = search_prop[i].prop;
const textPname = search_prop[i].pname;
if(textOpt!=""){
opt_select += '?'+optid+'Label';
if(search_prop[i].optional){
options += 'OPTIONAL{?item '+ textOpt+' ?'+optid+' .}\n';
}
else{
options += '?item '+ textOpt+' ?'+optid+' .\n';
}
}
}
query = query.replace("{",conditions+options)
.replace("?itemLabel","?itemLabel "+opt_select);
if(offset!=0 && !contQueryIds){
query += "OFFSET "+offset;
}
return query;
}
/*
* GUIの情報を使ってSPARQLクエリを作る
*/
async function makeMappingQuery(query,textLABEL,mode){
let conditions = "{\n";
let ids;
//名称の処理
if(textLABEL!=""){
conditions += 'BIND ("'+textLABEL+'" AS ?lbl)';
if(mode == 'LabelFull'){
conditions+= '{?item rdfs:label|skos:altLabel "'+textLABEL+'"@ja.}\n';
conditions+= 'UNION {?item rdfs:label|skos:altLabel "'+textLABEL+'"@en.}\n';
}
else if(mode == 'LabelForward'){
ids = await getWdIDsBySE(textLABEL);
//得られたID一覧の数が上限(=50)になったら,「続きを検索」表示をON
if(ids.length==50){
contQueryIds = true;
}
const vals = ids.join(" ").replaceAll("Q","wd:Q");
conditions+= 'VALUES ?item {'+vals+'}\n';
}
else if(mode == 'LabelAmbi'){
ids = await getWdIDs(textLABEL);
//得られたID一覧の数が上限(=50)になったら,「続きを検索」表示をON
if(ids.length==50){
contQueryIds = true;
}
const vals = ids.join(" ").replaceAll("Q","wd:Q");
conditions+= 'VALUES ?item {'+vals+'}\n';
}
else if(mode == 'LabelPart'){
conditions+= '?item rdfs:label ?label.\n';
conditions+= 'FILTER(contains(?label,"'+textLABEL+'"))' ;
}
}
// //検索条件・検索項目の更新
// document.getElementById('settings_area').value
// ="let search_cond = \n" + JSON.stringify(saveSearchConds(),null,' ') +";\n\n"
// +"let search_prop = \n" + JSON.stringify(saveSearchProps(),null,' ') +";\n";
//検索条件の設定
for(let i=0; i<search_cond.length ;i++){
const condID = search_cond[i].id;
const textCond = search_cond[i].cond;
const textVal = search_cond[i].val;
if(textCond!="" && textVal!=""){
if(search_cond[i].type =="ID"){
conditions+= '?item '+textCond+' '+textVal+ '.\n';
}
else if(search_cond[i].type =="getID"){
const qid = await getWdIDse(textVal); //IDを検索で取得
conditions+= '?item '+textCond+' '+"wd:"+qid+ '.\n';
}
else if(search_cond[i].type =="STR-ja"){
conditions+= '?item '+textCond+' "'+textVal+ '"@ja .\n';
}
else if(search_cond[i].type =="REPLACE"){
conditions+= textCond.replace('####',textVal)+'\n';
}
}
}
//検索項目の設定
let opt_select = "";
let options = "";
for(let i=0;i<search_prop.length;i++){
const optid = search_prop[i].id;
const textOpt = search_prop[i].prop;
const textPname = search_prop[i].pname;
if(textOpt!=""){
opt_select += '?'+optid+'Label';
if(search_prop[i].optional){
options += 'OPTIONAL{?item '+ textOpt+' ?'+optid+' .}\n';
}
else{
options += '?item '+ textOpt+' ?'+optid+' .\n';
}
}
}
query = query.replace("{",conditions+options)
.replace("?itemLabel","?itemLabel "+opt_select);
if(offset!=0 && !contQueryIds){
query += "OFFSET "+offset;
}
return query;
}
/*
* GUIの情報を使ってSPARQLクエリを作る
*/
async function makeMappingQuery_org(query,textLABEL){
let conditions = "{\n";
let ids;
//名称の処理
if(textLABEL!=""){
if(document.getElementById('LabelFull').checked){
conditions+= '{?item rdfs:label|skos:altLabel "'+textLABEL+'"@ja.}\n';
conditions+= 'UNION {?item rdfs:label|skos:altLabel "'+textLABEL+'"@en.}\n';
}
else if(document.getElementById('LabelForward').checked){
ids = await getWdIDsBySE(textLABEL);
//得られたID一覧の数が上限(=50)になったら,「続きを検索」表示をON
if(ids.length==50){
contQueryIds = true;
}
const vals = ids.join(" ").replaceAll("Q","wd:Q");
conditions+= 'VALUES ?item {'+vals+'}\n';
}
else if(document.getElementById('LabelAmbi').checked){
ids = await getWdIDs(textLABEL);
//得られたID一覧の数が上限(=50)になったら,「続きを検索」表示をON
if(ids.length==50){
contQueryIds = true;
}
const vals = ids.join(" ").replaceAll("Q","wd:Q");
conditions+= 'VALUES ?item {'+vals+'}\n';
}
else if(document.getElementById('LabelPart').checked){
conditions+= '?item rdfs:label ?label.\n';
conditions+= 'FILTER(contains(?label,"'+textLABEL+'"))' ;
}
}
//検索条件・検索項目の更新
// document.getElementById('settings_area').value
// ="let search_cond = \n" + JSON.stringify(saveSearchConds(),null,' ') +";\n\n"
// +"let search_prop = \n" + JSON.stringify(saveSearchProps(),null,' ') +";\n";
//検索条件の設定
for(let i=0; i<search_cond.length ;i++){
const condID = search_cond[i].id;
const textCond = search_cond[i].cond;
const textVal = search_cond[i].val;
if(textCond!="" && textVal!=""){
if(search_cond[i].type =="ID"){
conditions+= '?item '+textCond+' '+textVal+ '.\n';
}
else if(search_cond[i].type =="getID"){
const qid = await getWdIDse(textVal); //IDを検索で取得
conditions+= '?item '+textCond+' '+"wd:"+qid+ '.\n';
}
else if(search_cond[i].type =="STR-ja"){
conditions+= '?item '+textCond+' "'+textVal+ '"@ja .\n';
}
else if(search_cond[i].type =="REPLACE"){
conditions+= textCond.replace('####',textVal)+'\n';
}
}
}
//検索項目の設定
let opt_select = "";
let options = "";
for(let i=0;i<search_prop.length;i++){
const optid = search_prop[i].id;
const textOpt = search_prop[i].prop;
const textPname = search_prop[i].pname;
if(textOpt!=""){
opt_select += '?'+optid+'Label';
if(search_prop[i].optional){
options += 'OPTIONAL{?item '+ textOpt+' ?'+optid+' .}\n';
}
else{
options += '?item '+ textOpt+' ?'+optid+' .\n';
}
}
}
query = query.replace("{",conditions+options)
.replace("?itemLabel","?itemLabel "+opt_select);
if(offset!=0 && !contQueryIds){
query += "OFFSET "+offset;
}
return query;
}
function downloadText(fileName, text) {
const blob = new Blob([text], { type: 'text/plain' });
const aTag = document.createElement('a');
aTag.href = URL.createObjectURL(blob);
aTag.target = '_blank';
aTag.download = fileName;
aTag.click();
URL.revokeObjectURL(aTag.href);
}
function saveTable(fileName, text){
const head = '<!DOCTYPE html>\n<html lang="ja">\n<head>\n'+
'<meta charset="UTF-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n'+
'<meta http-equiv="X-UA-Compatible" content="ie=edge">\n'+
'<title>Wikidataへのマッピングデータ</title>\n'+
'<link rel="stylesheet" href="style.css">\n</head>\n<body>\n';
const foot = '</body>\n</html>';
downloadText(fileName,head+text+foot);
}
/*
* 各ボタンの設定
*/
function setButtonsForMapping(){
const sendButton = document.getElementById('send');
const contButton = document.getElementById('cont');
const showQueryCondButton = document.getElementById('showQueryCond');
const hideQueryCondButton = document.getElementById('hideQueryCond');
const dispQueryButton = document.getElementById('dispQuery');
const hideQueryButton = document.getElementById('hideQuery');
// const serchCondDiv = document.getElementById('search_cond_div');
// const serchPropDiv = document.getElementById('search_prop_div');
// serchCondDiv.innerHTML = loadSearchConds(false);//詳細検索画面の設定
// serchPropDiv.innerHTML = loadSearchProps();//検索条件設定画面の設定
//検索実行ボタンの処理
sendButton.addEventListener('click', async () => {
offset = 0;
contQueryIds = false;
contQuery = false;
document.getElementById("result_div").innerHTML="";
contButton.style.display="none";
makeQuery();
//makeWikidataQuery();
}, false);
// Enterキー押下時、送信処理が実行する
// window.document.onkeydown = function(event){
// if (event.key === 'Enter') {
// offset = 0;
// contQueryIds = false;
// contQuery = false;
// document.getElementById("result_div").innerHTML="";
// contButton.style.display="none";
// makeQuery();
// }
// }
//詳細検索表示ボタンの処理
showQueryCondButton.addEventListener('click', () => {
document.getElementById('query').style.display = 'block';
// document.getElementById('QueryCond_div').style.display = 'block';
showQueryCondButton.style.display = 'none';
hideQueryCondButton.style.display = 'block';
// serchCondDiv.innerHTML = loadSearchConds(false);//詳細検索画面の設定
});
hideQueryCondButton.addEventListener('click', () => {
document.getElementById('query').style.display = 'none';
// document.getElementById('QueryCond_div').style.display = 'none';
showQueryCondButton.style.display = 'block';
hideQueryCondButton.style.display = 'none';
document.getElementById('query').style.display = 'none';
// dispQueryButton.style.display = 'block';
// hideQueryButton.style.display = 'none';
// saveSearchConds();
// saveSearchProps();
});
/*
//クエリ表示ボタンの処理
dispQueryButton.addEventListener('click', () => {
document.getElementById('query').style.display = 'block';
dispQueryButton.style.display = 'none';
hideQueryButton.style.display = 'block';
saveSearchConds();
serchCondDiv.innerHTML = loadSearchConds(true);//詳細検索画面の設定
});
//クエリ非表示ボタンの処理
hideQueryButton.addEventListener('click', () => {
document.getElementById('query').style.display = 'none';
dispQueryButton.style.display = 'block';
hideQueryButton.style.display = 'none';
saveSearchConds();
saveSearchProps();
serchCondDiv.innerHTML = loadSearchConds(false);//詳細検索画面の設定
});
//続きを検索実行ボタンの処理
contButton.addEventListener('click', async () => {
offset += 50;
makeQuery();
}, false);*/
}