# HG changeset patch # User durandn # Date 1467362912 -7200 # Node ID 52ec8e0850134edb06fbfa89859c528aad7249cd # Parent 8125ce36415c4f991ad7159ec0f574ff8deb479c work on extracting label from dbpedia diff -r 8125ce36415c -r 52ec8e085013 src/iconolab/models.py --- a/src/iconolab/models.py Thu Jun 30 18:10:17 2016 +0200 +++ b/src/iconolab/models.py Fri Jul 01 10:48:32 2016 +0200 @@ -243,16 +243,6 @@ def get_tags_json(self): def fetch_from_dbpedia(uri, lang): -# sparql_template = 'PREFIX dbpedia-owl: ' \ -# 'select distinct * where { ' \ -# + 'OPTIONAL { <<%uri%>> rdfs:label ?l FILTER( langMatches( lang(?l), "<%lang%>" ) ) }. ' \ -# + 'OPTIONAL { <<%uri%>> dbpedia-owl:thumbnail ?t }. ' \ -# + 'OPTIONAL { <<%uri%>> dbpedia-owl:abstract ?a FILTER( langMatches( lang(?a), "<%lang%>" ) ) }. ' \ -# + 'OPTIONAL { <<%uri%>> dbpedia-owl:wikiPageRedirects ?r }. ' \ -# + 'OPTIONAL { ?r rdfs:label ?lr FILTER( langMatches( lang(?lr), "<%lang%>" ) ) }. ' \ -# + 'OPTIONAL { ?r dbpedia-owl:thumbnail ?tr }. ' \ -# + 'OPTIONAL { ?r dbpedia-owl:abstract ?ar FILTER( langMatches( lang(?ar), "<%lang%>" ) ) }. ' \ -# + '}' sparql_template = 'select distinct * where { <<%uri%>> rdfs:label ?l FILTER( langMatches( lang(?l), "<%lang%>" ) ) }' sparql_query = re.sub("<%uri%>", uri, re.sub("<%lang%>", lang, sparql_template)) sparql_query_url = "http://dbpedia.org/sparql" @@ -263,11 +253,11 @@ "format": "json" } ) - print(dbpedia_resp.status_code) - print(dbpedia_resp.text) - return "wow" + results = json.loads(dbpedia_resp.text).get("results", {}) + variable_bindings = results.get("bindings") + label_json = variable_bindings.pop() + return label_json.get("l").get("value") - test = fetch_from_dbpedia("http://dbpedia.org/resource/Haiti", "fr") final_list = [] for tagging_info in self.tagginginfo_set.select_related("tag").all(): if tagging_info.tag.is_internal():