src/p4l/utils.py
changeset 17 b31a67614f76
parent 12 57efd01f1715
child 23 cd4055479ba3
equal deleted inserted replaced
15:be595253ced5 17:b31a67614f76
     2 from django.conf import settings
     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 import requests #@UnresolvedImport
       
     7 import logging
       
     8 
       
     9 
       
    10 logger = logging.getLogger(__name__)
     7 
    11 
     8 def show_progress(current_line, total_line, label, width, writer=None):
    12 def show_progress(current_line, total_line, label, width, writer=None):
     9 
    13 
    10     if writer is None:
    14     if writer is None:
    11         writer = sys.stdout
    15         writer = sys.stdout
    38     if lang_uri.startswith(LANGUAGE_NS):
    42     if lang_uri.startswith(LANGUAGE_NS):
    39         lang_uri = lang_uri[len(LANGUAGE_NS):]
    43         lang_uri = lang_uri[len(LANGUAGE_NS):]
    40 
    44 
    41     return LANGUAGE_URI_MAP.get(lang_uri, None)
    45     return LANGUAGE_URI_MAP.get(lang_uri, None)
    42 
    46 
       
    47 
       
    48 
       
    49 def get_labels_for_uris(uri_list, scheme_uri, lang, acronyms=False):
       
    50     query_without_acronym = """
       
    51 PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
       
    52 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
       
    53 PREFIX owl:<http://www.w3.org/2002/07/owl#>
       
    54 PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
       
    55 SELECT ?uri ?label
       
    56 WHERE {
       
    57     ?uri skos:inScheme <%s> .
       
    58     ?uri skos:prefLabel|skos:label ?label .
       
    59     FILTER (?uri = $root)
       
    60 }
       
    61 """
       
    62     query_with_acronym = """
       
    63 PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
       
    64 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
       
    65 PREFIX owl:<http://www.w3.org/2002/07/owl#>
       
    66 PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
       
    67 SELECT ?uri ?label ?acro
       
    68 WHERE {
       
    69     ?uri skos:inScheme <%s> .
       
    70     ?uri skos:prefLabel|skos:label ?label .
       
    71     OPTIONAL { ?uri skos:altLabel ?acro }
       
    72     FILTER (?uri = $root)
       
    73 }
       
    74 """
       
    75     if acronyms:
       
    76         query = query_with_acronym
       
    77     else:
       
    78         query = query_without_acronym
       
    79     res_dict = {}
       
    80     for uri in uri_list:
       
    81         res_dict[uri] = ""
       
    82         res = requests.get(
       
    83             settings.SPARQL_QUERY_ENDPOINT,
       
    84             params={'query':query % scheme_uri, 'timeout':10, '$root' : "<"+uri+">"},
       
    85             headers={'accept':'application/sparql-results+json'},
       
    86         )
       
    87         if not res.ok:
       
    88             continue
       
    89         elif res.text:
       
    90             json_res = res.json()
       
    91             if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0:
       
    92                 # json_res['results']['bindings'] has several languages. If we find french, we save the french label.
       
    93                 # If not, we save the first one.
       
    94                 tmp_dict = {}
       
    95                 first_label = None
       
    96                 # We create a temporary dict with the lang code and the label
       
    97                 for b in json_res['results']['bindings']:
       
    98                     if lang:
       
    99                         if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']:
       
   100                             tmp_dict[b['label']['xml:lang']] = b['label']['value']
       
   101                             if not first_label:
       
   102                                 first_label = b['label']['value']
       
   103                     else:
       
   104                         if 'acro' in b and 'value' in b['acro']:
       
   105                             first_label = b['acro']['value'] + " : " + b['label']['value']
       
   106                         else:
       
   107                             first_label = b['label']['value']
       
   108                 if lang in tmp_dict or first_label:
       
   109                     if lang in tmp_dict:
       
   110                         label = tmp_dict[lang]
       
   111                     else:
       
   112                         label = first_label
       
   113                     res_dict[uri] = label
       
   114     return res_dict
    43 
   115 
    44     
   116     
    45     
   117     
    46 def fill_label_for_model(model, property_uri, scheme_uri):
   118 def fill_label_for_model(model, property_uri, scheme_uri):
    47     query = """
   119     query = """