1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 |
2 from django.conf import settings |
3 import sys |
3 import sys |
4 import codecs #@UnresolvedImport |
4 import codecs #@UnresolvedImport |
5 import math |
5 import math |
|
6 import requests #@UnresolvedImport |
6 |
7 |
7 def show_progress(current_line, total_line, label, width, writer=None): |
8 def show_progress(current_line, total_line, label, width, writer=None): |
8 |
9 |
9 if writer is None: |
10 if writer is None: |
10 writer = sys.stdout |
11 writer = sys.stdout |
36 return None |
37 return None |
37 if lang_uri.startswith(LANGUAGE_NS): |
38 if lang_uri.startswith(LANGUAGE_NS): |
38 lang_uri = lang_uri[len(LANGUAGE_NS):] |
39 lang_uri = lang_uri[len(LANGUAGE_NS):] |
39 |
40 |
40 return LANGUAGE_URI_MAP.get(lang_uri, None) |
41 return LANGUAGE_URI_MAP.get(lang_uri, None) |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 def fill_label_for_model(model, property_uri, scheme_uri): |
|
47 query = """ |
|
48 PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
49 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
50 PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
51 PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
52 SELECT ?uri ?label |
|
53 WHERE { |
|
54 ?uri skos:inScheme <%s> . |
|
55 ?uri skos:prefLabel|skos:label ?label . |
|
56 FILTER (?uri = $root) |
|
57 } |
|
58 """ |
|
59 # Loads Models label from sparkl query |
|
60 objs = model.objects.filter(label=None) |
|
61 total_objs = len(objs) |
|
62 writer = None |
|
63 i = 0 |
|
64 found = 0 |
|
65 for o in objs: |
|
66 i += 1 |
|
67 res = requests.get( |
|
68 settings.SPARQL_QUERY_ENDPOINT, |
|
69 params={'query':query % scheme_uri, 'timeout':10, '$root' : "<"+getattr(o, property_uri)+">"}, |
|
70 headers={'accept':'application/sparql-results+json'}, |
|
71 ) |
|
72 if not res.ok: |
|
73 continue |
|
74 elif res.text: |
|
75 json_res = res.json() |
|
76 if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0: |
|
77 # json_res['results']['bindings'] has several languages. If we find french, we save the french label. |
|
78 # If not, we save the first one. |
|
79 tmp_dict = {} |
|
80 first_label = None |
|
81 # We create a temporary dict with the lang code and the label |
|
82 for b in json_res['results']['bindings']: |
|
83 if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']: |
|
84 tmp_dict[b['label']['xml:lang']] = b['label']['value'] |
|
85 if not first_label: |
|
86 first_label = b['label']['value'] |
|
87 if 'fr' in tmp_dict or first_label: |
|
88 if 'fr' in tmp_dict: |
|
89 o.label = tmp_dict['fr'] |
|
90 else: |
|
91 o.label = first_label |
|
92 l = o.label |
|
93 o.save() |
|
94 found += 1 |
|
95 writer = show_progress(i, total_objs, l, 50, writer=writer) |
|
96 print("Processing Sparql Done. %d found on %d" % (found, total_objs)) |