src/p4l/utils.py
changeset 23 cd4055479ba3
parent 17 b31a67614f76
child 34 cfc090f440d0
equal deleted inserted replaced
22:48ff361f96c8 23:cd4055479ba3
    54 PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
    54 PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
    55 SELECT ?uri ?label
    55 SELECT ?uri ?label
    56 WHERE {
    56 WHERE {
    57     ?uri skos:inScheme <%s> .
    57     ?uri skos:inScheme <%s> .
    58     ?uri skos:prefLabel|skos:label ?label .
    58     ?uri skos:prefLabel|skos:label ?label .
    59     FILTER (?uri = $root)
    59     FILTER (%s)
    60 }
    60 }
    61 """
    61 """
    62     query_with_acronym = """
    62     query_with_acronym = """
    63 PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
    63 PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
    64 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    64 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    67 SELECT ?uri ?label ?acro
    67 SELECT ?uri ?label ?acro
    68 WHERE {
    68 WHERE {
    69     ?uri skos:inScheme <%s> .
    69     ?uri skos:inScheme <%s> .
    70     ?uri skos:prefLabel|skos:label ?label .
    70     ?uri skos:prefLabel|skos:label ?label .
    71     OPTIONAL { ?uri skos:altLabel ?acro }
    71     OPTIONAL { ?uri skos:altLabel ?acro }
    72     FILTER (?uri = $root)
    72     FILTER (%s)
    73 }
    73 }
    74 """
    74 """
    75     if acronyms:
    75     if acronyms:
    76         query = query_with_acronym
    76         query = query_with_acronym
    77     else:
    77     else:
    78         query = query_without_acronym
    78         query = query_without_acronym
    79     res_dict = {}
    79     res_dict = {}
    80     for uri in uri_list:
    80     # We build the filter string
       
    81     filter_str = ""
       
    82     for i,uri in enumerate(uri_list):
    81         res_dict[uri] = ""
    83         res_dict[uri] = ""
    82         res = requests.get(
    84         if i==0:
    83             settings.SPARQL_QUERY_ENDPOINT,
    85             filter_str = "?uri = <" + uri + ">"
    84             params={'query':query % scheme_uri, 'timeout':10, '$root' : "<"+uri+">"},
    86         else:
    85             headers={'accept':'application/sparql-results+json'},
    87             filter_str += " || ?uri = <" + uri + ">"
    86         )
    88     # We request the labels
    87         if not res.ok:
    89     res = requests.get(
    88             continue
    90         settings.SPARQL_QUERY_ENDPOINT,
    89         elif res.text:
    91         params={'query':query % (scheme_uri, filter_str), 'timeout':10},#, '$root' : "<"+uri+">"},
    90             json_res = res.json()
    92         headers={'accept':'application/sparql-results+json'},
    91             if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0:
    93     )
    92                 # json_res['results']['bindings'] has several languages. If we find french, we save the french label.
    94     if res.ok and res.text:
    93                 # If not, we save the first one.
    95         json_res = res.json()
    94                 tmp_dict = {}
    96         if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0:
    95                 first_label = None
    97             # json_res['results']['bindings'] has several languages. If we find french, we save the french label.
    96                 # We create a temporary dict with the lang code and the label
    98             # If not, we save the first one.
    97                 for b in json_res['results']['bindings']:
    99             tmp_dict = {}
    98                     if lang:
   100             first_label = None
    99                         if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']:
   101             # We create a temporary dict with the lang code and the label
   100                             tmp_dict[b['label']['xml:lang']] = b['label']['value']
   102             for b in json_res['results']['bindings']:
   101                             if not first_label:
   103                 if lang:
   102                                 first_label = b['label']['value']
   104                     if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']:
       
   105                         tmp_dict[b['label']['xml:lang']] = b['label']['value']
       
   106                         if not first_label:
       
   107                             first_label = b['label']['value']
       
   108                 else:
       
   109                     if 'acro' in b and 'value' in b['acro']:
       
   110                         first_label = b['acro']['value'] + " : " + b['label']['value']
   103                     else:
   111                     else:
   104                         if 'acro' in b and 'value' in b['acro']:
   112                         first_label = b['label']['value']
   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:
   113                 if lang in tmp_dict or first_label:
   109                     if lang in tmp_dict:
   114                     if lang in tmp_dict:
   110                         label = tmp_dict[lang]
   115                         label = tmp_dict[lang]
   111                     else:
   116                     else:
   112                         label = first_label
   117                         label = first_label
   113                     res_dict[uri] = label
   118                     res_dict[b['uri']['value']] = label
   114     return res_dict
   119     return res_dict
   115 
   120 
   116     
   121     
   117     
   122     
   118 def fill_label_for_model(model, property_uri, scheme_uri):
   123 def fill_label_for_model(model, property_uri, scheme_uri):