# HG changeset patch # User cavaliet # Date 1378214623 -7200 # Node ID cd4055479ba3897684bd09c7f28dc542806a4c51 # Parent 48ff361f96c85682fa172ffe85860ba9384b264f enhance label sparql request diff -r 48ff361f96c8 -r cd4055479ba3 src/p4l/utils.py --- a/src/p4l/utils.py Tue Sep 03 13:05:55 2013 +0200 +++ b/src/p4l/utils.py Tue Sep 03 15:23:43 2013 +0200 @@ -56,7 +56,7 @@ WHERE { ?uri skos:inScheme <%s> . ?uri skos:prefLabel|skos:label ?label . - FILTER (?uri = $root) + FILTER (%s) } """ query_with_acronym = """ @@ -69,7 +69,7 @@ ?uri skos:inScheme <%s> . ?uri skos:prefLabel|skos:label ?label . OPTIONAL { ?uri skos:altLabel ?acro } - FILTER (?uri = $root) + FILTER (%s) } """ if acronyms: @@ -77,40 +77,45 @@ else: query = query_without_acronym res_dict = {} - for uri in uri_list: + # We build the filter string + filter_str = "" + for i,uri in enumerate(uri_list): res_dict[uri] = "" - res = requests.get( - settings.SPARQL_QUERY_ENDPOINT, - params={'query':query % scheme_uri, 'timeout':10, '$root' : "<"+uri+">"}, - headers={'accept':'application/sparql-results+json'}, - ) - if not res.ok: - continue - elif res.text: - json_res = res.json() - if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0: - # json_res['results']['bindings'] has several languages. If we find french, we save the french label. - # If not, we save the first one. - tmp_dict = {} - first_label = None - # We create a temporary dict with the lang code and the label - for b in json_res['results']['bindings']: - if lang: - if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']: - tmp_dict[b['label']['xml:lang']] = b['label']['value'] - if not first_label: - first_label = b['label']['value'] + if i==0: + filter_str = "?uri = <" + uri + ">" + else: + filter_str += " || ?uri = <" + uri + ">" + # We request the labels + res = requests.get( + settings.SPARQL_QUERY_ENDPOINT, + params={'query':query % (scheme_uri, filter_str), 'timeout':10},#, '$root' : "<"+uri+">"}, + headers={'accept':'application/sparql-results+json'}, + ) + if res.ok and res.text: + json_res = res.json() + if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0: + # json_res['results']['bindings'] has several languages. If we find french, we save the french label. + # If not, we save the first one. + tmp_dict = {} + first_label = None + # We create a temporary dict with the lang code and the label + for b in json_res['results']['bindings']: + if lang: + if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']: + tmp_dict[b['label']['xml:lang']] = b['label']['value'] + if not first_label: + first_label = b['label']['value'] + else: + if 'acro' in b and 'value' in b['acro']: + first_label = b['acro']['value'] + " : " + b['label']['value'] else: - if 'acro' in b and 'value' in b['acro']: - first_label = b['acro']['value'] + " : " + b['label']['value'] - else: - first_label = b['label']['value'] + first_label = b['label']['value'] if lang in tmp_dict or first_label: if lang in tmp_dict: label = tmp_dict[lang] else: label = first_label - res_dict[uri] = label + res_dict[b['uri']['value']] = label return res_dict