-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikidata.py
558 lines (435 loc) · 18.8 KB
/
wikidata.py
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import requests
import re
import queries
import entities
import eol
def is_good_label(palabra):
if palabra[0] == "(" and palabra[len(palabra)-1] == ")":
return False
else:
return True
def actual_word(palabra):
palabra = palabra.strip()
if palabra[0] == "(" and palabra[len(palabra) - 1] == ")":
return palabra[1:-1]
elif "*" in palabra:
return None
else:
return palabra.strip()
def search_label(query, category_data):
results = []
for label in list(category_data.keys()):
if query == label:
results.append({label: category_data.get(label)})
if len(results) >= 1:
# print(results[0])
return results[0]
else:
return None
def remove_accents(term):
term = re.sub("á", "a", term)
term = re.sub("é", "e", term)
term = re.sub("í", "i", term)
term = re.sub("ó", "o", term)
term = re.sub("ú", "u", term)
return term
def get_variants(term):
variants = []
term_con_acentos = term.lower()
term_sin_acentos = remove_accents(term)
# Variantes con la primera mayúscula
term_con_upper = "".join([term_con_acentos[0].upper()+term_con_acentos[1:]])
term_sin_upper = "".join([term_sin_acentos[0].upper()+term_sin_acentos[1:]])
variants.append(term_con_acentos)
variants.append(term_sin_acentos)
if term_con_upper not in variants:
variants.append(term_con_upper)
if term_sin_upper not in variants:
variants.append(term_sin_upper)
# Get variantes búsqueda wikipedia
return variants
def execute_sparql_query(query):
# print(query)
endpoint = 'https://query.wikidata.org/sparql'
params = {
'query': query
}
headers = {
'Accept': 'application/sparql-results+json',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:59.0) Gecko/20100101 Firefox/59.0'
}
response = requests.get(endpoint, params=params, headers=headers)
response.raise_for_status()
results = response.json()
return results
def is_parent_taxon_of(entity_animal, entity_taxon):
query = re.sub("#SPECIE#", entity_animal, queries.query_ask)
query = re.sub("#ENTITY#", entity_taxon, query)
# print(query)
results = execute_sparql_query(query)
result = results['boolean']
return bool(result)
def is_zoological_category(entity_uri, category_uri):
query = re.sub("#SPECIE#", entity_uri, queries.query_parent_classes_taxon)
results = execute_sparql_query(query)
for result in results["results"]["bindings"]:
# print("Parent taxon zoological category: " + result["taxon"]["value"])
entity_superclass = result["taxon"]["value"]
if is_parent_taxon_of(entity_superclass, category_uri):
return True
return False
def is_category(entity_uri, category_entity):
query = re.sub("#SPECIE#", entity_uri, queries.query_ask)
query = re.sub("#ENTITY#", category_entity, query)
print(query)
results = execute_sparql_query(query)
result = results['boolean']
return bool(result)
def is_common_name_specie(entity_uri):
query = re.sub("#SPECIE#", entity_uri, queries.query_ask_common)
results = execute_sparql_query(query)
result = results['boolean']
return bool(result)
def is_product_of_category(entity_uri, category_entity):
query = re.sub("#ENTITY#", entity_uri, queries.query_product)
print(query)
results = execute_sparql_query(query)
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
taxon_entity = result["taxon"]["value"]
if is_category(taxon_entity, category_entity):
return True
return False
def is_subclass_of_category(entity_uri, category_entity):
query = re.sub("#SPECIE#", entity_uri, queries.query_subclass)
query = re.sub("#ENTITY#", category_entity, query)
print(query)
results = execute_sparql_query(query)
result = results['boolean']
return bool(result)
def lematize_word(query, lemmas):
if query in lemmas.keys():
lema = lemmas.get(query)
# print("LEMA : ", lema)
return lema
def get_lemmas(path_to_lemmas):
lemma_data = {}
with open(path_to_lemmas, "r", encoding='UTF-8') as file:
lineas = file.readlines()
for linea in lineas:
lemma = linea.split("\t")[0].strip()
wordform = linea.split("\t")[1].strip()
lemma_data.update({wordform: lemma})
# print(lemmas)
return lemma_data
def is_subclass_of_taxon_in_category(entity_uri, taxon_uri):
query = re.sub("#SPECIE#", entity_uri, queries.query_parent_classes_taxon)
results = execute_sparql_query(query)
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
entity_uri = result["taxon"]["value"]
if is_category(entity_uri, taxon_uri):
return True
return False
def is_for_human_use(entity_uri, type_of_use_id):
query = re.sub("#SPECIE#", entity_uri, queries.query_animal_use)
query = re.sub("#ENTITY#", type_of_use_id, query)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
return False
def is_transport(entity_uri):
for query_transport in [queries.query_ask_transport_1, queries.query_ask_transport_2,
queries.query_ask_transport_3, queries.query_ask_transport_4]:
query = re.sub("#VEHICLE#", entity_uri, query_transport)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
return False
def is_vehicle(entity_uri):
for query_vehicle in [queries.query_ask_vehicle_1, queries.query_ask_vehicle_2,
queries.query_ask_vehicle_3, queries.query_ask_vehicle_4]:
query = re.sub("#VEHICLE#", entity_uri, query_vehicle)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
return False
def is_clothing(entity_uri):
for query_clothing in [queries.query_clothing_1, queries.query_clothing_2,
queries.query_clothing_3, queries.query_clothing_4]:
query = re.sub("#CLOTHING#", entity_uri, query_clothing)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
return False
def is_type_of_plant(entity_uri, entity_id):
for query_planta in [queries.query_tipo_planta_1, queries.query_tipo_planta_2, queries.query_tipo_planta_3,
queries.query_tipo_planta_4]:
query = re.sub("#ENTITY#", entity_uri, query_planta)
query = re.sub("#TYPE#", entity_id, query)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
def is_plant_category(entity_uri, entity_id):
for query_planta in [queries.query_ask_planta_1, queries.query_ask_planta_2,
queries.query_ask_planta_3, queries.query_ask_planta_4,
queries.query_ask_planta_5]:
query = re.sub("#ENTITY#", entity_uri, query_planta)
query = re.sub("#TYPE#", entity_id, query)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
def search_wikidata_plant(term, category_entity):
variants = get_variants(term)
# print(variants)
for variant in variants:
query = re.sub("#QUERY#", variant, queries.query_labels_species)
results = execute_sparql_query(query)
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
entity_uri = result["specie"]["value"]
print("Es planta " + entity_uri + " ?")
if is_category(entity_uri, category_entity):
print("IN")
return {term: entity_uri}
elif is_product_of_category(entity_uri, category_entity):
print("IN")
return {term: entity_uri}
elif is_subclass_of_category(entity_uri, category_entity):
return {term: entity_uri}
return None
def search_wikidata(term, category_entity):
term = actual_word(term)
if category_entity == entities.entity_plant:
return search_wikidata_plant(term, category_entity)
elif category_entity == entities.entity_animal:
return search_wikidata_animal(term, category_entity)
elif category_entity == entities.entity_clothing:
return search_wikidata_clothing(term)
elif category_entity == entities.entity_vehicle:
return search_wikidata_vehicle(term)
else:
return None
def search_wikidata_vehicle(term):
variants = get_variants(term)
# print(variants)
for variant in variants:
query = re.sub("#QUERY#", variant, queries.query_labels)
results = execute_sparql_query(query)
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
entity_uri = result["item"]["value"]
if is_transport(entity_uri):
return {term: entity_uri}
elif is_vehicle(entity_uri):
return {term: entity_uri}
return None
def search_wikidata_clothing(term):
variants = get_variants(term)
# print(variants)
for variant in variants:
query = re.sub("#QUERY#", variant, queries.query_labels)
results = execute_sparql_query(query)
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
entity_uri = result["item"]["value"]
if is_clothing(entity_uri):
return {term: entity_uri}
return None
def search_wikidata_animal(term, category_entity):
variants = get_variants(term)
# print(variants)
for variant in variants:
query = re.sub("#QUERY#", variant, queries.query_labels_species)
print(query)
results = execute_sparql_query(query)
print(results)
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
entity_uri = result["specie"]["value"]
if is_category(entity_uri, category_entity):
return {term: entity_uri}
elif is_common_name_specie(entity_uri):
return {term: entity_uri}
elif is_subclass_of_category(entity_uri, category_entity):
return {term: entity_uri}
elif is_subclass_of_taxon_in_category(entity_uri, category_entity):
return {term: entity_uri}
# elif is_product_of_category(entity_uri, category_entity):
# return {term: entity_uri}
return None
def get_clothing_subcategories(entity_uri):
subcategorias_ropa = type_of_clothing(entity_uri)
print(subcategorias_ropa)
def type_of_clothing(entity_uri):
types = {}
tipos_prendas = {"wd:Q198763": "underwear",
"wd:Q14952": "headgear",
"wd:Q645292": "sportswear",
"wd:Q4388799": "outerwear",
"wd:Q7434": "uniform",
"wd:Q1065579": "costume accessory",
"wd:Q9053464": "costume"}
for entity_id, entity_name in tipos_prendas.items():
if is_clothing_category(entity_uri, entity_id):
types.update({entity_id: 1})
else:
types.update({entity_id: 0})
return types
def is_clothing_category(entity_uri, entity_id):
for query_clothing in [queries.query_tipo_ropa_1, queries.query_tipo_ropa_2, queries.query_tipo_ropa_3]:
query = re.sub("#ENTITY#", entity_uri, query_clothing)
query = re.sub("#TYPE#", entity_id, query)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
def get_vehicle_subcategories(entity_uri):
subcategorias_vehiculo = type_of_vehicle(entity_uri)
print(subcategorias_vehiculo)
def type_of_vehicle(entity_uri):
types = {}
tipos_vehiculo = {"wd:Q1515493": "road",
"wd:Q350783": "rail",
"wd:Q16335899": "watercraft",
"wd:Q11436": "aircraft"}
for entity_id, entity_name in tipos_vehiculo.items():
if is_vehicle_category(entity_uri, entity_id):
types.update({entity_id: 1})
else:
types.update({entity_id: 0})
return types
def is_vehicle_category(entity_uri, entity_id):
for query_vehicle in [queries.query_tipo_vehiculo_1, queries.query_tipo_vehiculo_2, queries.query_tipo_vehiculo_3]:
query = re.sub("#ENTITY#", entity_uri, query_vehicle)
query = re.sub("#TYPE#", entity_id, query)
results = execute_sparql_query(query)
result = results['boolean']
if bool(result):
return True
def get_plant_subcategories(entity_uri):
subcategorias_planta = type_of_plant(entity_uri)
print(subcategorias_planta)
subcategorias_vegetal = type_of_vegetable(entity_uri)
print(subcategorias_vegetal)
def type_of_plant(entity_uri):
types = {}
tipos_planta = {"wd:Q10884": "tree", "wd:Q42295": "shrub",
"wd:Q1364": "fruit", "wd:Q506": "flower", "wd:Q11004": "vegetable", "wd:Q207123": "herb",
"wd:Q917284": "climbing plant", "wd:Q40763": "seed"}
tipos_taxon = {"wd:Q764": "fungus"}
for entity_id, entity_name in tipos_taxon.items():
if is_plant_category(entity_uri, entity_id):
types.update({entity_id: 1})
else:
types.update({entity_id: 0})
for entity_id, entity_name in tipos_planta.items():
if is_type_of_plant(entity_uri, entity_id):
types.update({entity_id: 1})
else:
types.update({entity_id: 0})
return types
def type_of_vegetable(entity_uri):
types = {}
tipos_vegetal = {"wd:Q20134": "leaf vegetable", "wd:Q20136": "root vegetable", "wd:Q244599": "bulb vegetable",
"wd:Q3314483": "fruits", "wd:Q12533094": "eggplant", "wd:Q145909": "legume"}
for entity_id, entity_name in tipos_vegetal.items():
if is_type_of_plant(entity_uri, entity_id):
types.update({entity_id: 1})
else:
types.update({entity_id: 0})
return types
def get_animal_subcategories(entity_uri):
subcategorias_taxon = zoological_categories(entity_uri)
subcategorias_human_use = human_use_categories(entity_uri)
subcategorias_eol = get_eol_categories(entity_uri)
print(subcategorias_taxon)
print(subcategorias_human_use)
print(subcategorias_eol)
def get_eol_ids(entity_uri):
query = re.sub("#SPECIE#", entity_uri, queries.query_eol_identifier)
results = execute_sparql_query(query)
eol_ids = []
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
eol_id = result["id"]["value"]
eol_ids.append(eol_id)
return eol_ids
def get_eol_categories(entity_uri):
# Cojo el(los identificador/es de la entidad en EOL
eol_ids = get_eol_ids(entity_uri)
# Diccionario a devolver
entity_eol_categories = {}
# Tengo un diccionario con las subcategorías
eol_categories = {"http://eol.org/schema/terms/Habitat": "habitat_includes",
"http://rs.tdwg.org/dwc/terms/habitat": "habitat_is",
"http://purl.obolibrary.org/obo/GAZ_00000071": "biogeographic_realm",
"https://www.wikidata.org/wiki/Q295469": "ecoregion",
"http://www.wikidata.org/entity/Q1053008": "trophic_level_wikidata",
"http://eol.org/schema/terms/TrophicLevel": "trophic_level_eol"}
for eol_id in eol_ids:
if eol.test_identifier(eol_id):
for cat_uri, cat_name in eol_categories.items():
eol_objects = eol.get_eol_objects(eol_id, cat_uri)
if len(eol_objects) > 0:
entity_eol_categories.update({cat_name: eol_objects})
return entity_eol_categories
return entity_eol_categories
def human_use_categories(entity_uri):
"""Devuelve un diccionario indicando las categorías de uso humano a las que el animal pertenece"""
# Diccionario a devolver
entity_human_use_cats = {}
# Tengo un diccionario con las subcategorías
human_use_cats = {"wd:Q1797813": "productive", "wd:Q228534": "working", "wd:Q622377": "pack",
"wd:Q11637629": "draft", "wd:Q622852": "domesticated", "wd:Q39201": "mascota"}
for cat_id, cat_name in human_use_cats.items():
if is_for_human_use(entity_uri, cat_id):
entity_human_use_cats.update({cat_name: 1})
else:
entity_human_use_cats.update({cat_name: 0})
return entity_human_use_cats
def zoological_categories(entity_uri):
"""Devuelve un diccionario indicando las superespecies a las que pertenece un animal dado por su uri"""
# Diccionario a devolver
entity_zool_cats = {}
# Tengo un diccionario con las subcategorías
zool_cats = {"wd:Q5113": "bird", "wd:Q22724": "bovine", "wd:Q25324": "canine", "wd:Q23390": "deer",
"wd:Q25265": "feline", "wd:Q152": "fish", "wd:Q1390": "insect", "wd:Q7380": "primate",
"wd:Q25900": "rabbit", "wd:Q10811": "reptile", "wd:Q10908": "amphibian", "wd:Q10850": "rodent",
"wd:Q28521": "weasel"}
for cat_id, cat_name in zool_cats.items():
if is_zoological_category(entity_uri, cat_id):
entity_zool_cats.update({cat_name: 1})
else:
entity_zool_cats.update({cat_name: 0})
return entity_zool_cats
def get_description(entity_uri):
query = re.sub("#ENTITY#", entity_uri, queries.query_retrieve_description)
results = execute_sparql_query(query)
if results["results"]["bindings"]:
return results["results"]["bindings"][0]["description"]["value"]
def get_labels(entity_uri):
labels = []
query = re.sub("#ENTITY#", entity_uri, queries.query_retrieve_labels)
results = execute_sparql_query(query)
if len(results["results"]["bindings"]) > 0:
for result in results["results"]["bindings"]:
label = result["label"]["value"]
labels.append(label)
return labels
def get_subcategories(entity_uri, category_entity):
if category_entity == entities.entity_animal:
return get_animal_subcategories(entity_uri)
elif category_entity == entities.entity_plant:
return get_plant_subcategories(entity_uri)
elif category_entity == entities.entity_vehicle:
return get_vehicle_subcategories(entity_uri)
elif category_entity == entities.entity_clothing:
return get_clothing_subcategories(entity_uri)
loaded_lemmas = get_lemmas("lemmatization-es.txt")