jeudi 19 février 2015

Sparql python Cannot access elements

I am using the following python code in order to extract for a given query all its other names in different languages and the disambiguation names taken from Querying DBpedia for 'Wikipage disambiguates' using SPARQL.


The problem is in the second query. When i try to output the resutls by using:



for result in disamb_res["results"]["bindings"]:
print disamb_res["label"]["value"]


it gives me the following error:



KeyError: 'label'






#! /usr/bin/python
# -*- coding: utf-8 -*-
from SPARQLWrapper import SPARQLWrapper, JSON

#Wikipage disambiguates (redirects)
def disambiguates(name,sparql):
query = "SELECT ?label WHERE { ?label <http://ift.tt/1CO7EjC; <http://ift.tt/177k8UM" + name + ">}"
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
return results

#return names in other languages
def multilingue(name,sparql):
query = "PREFIX rdfs: <http://ift.tt/1cmhdKn; SELECT ?label WHERE { <http://ift.tt/177k8UM" + name + "> rdfs:label ?label }"
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
return results

if __name__ == "__main__":
#by using dbpedia
sparql = SPARQLWrapper("http://ift.tt/UeVHu1")
results = multilingue("Paris",sparql)
for result in results["results"]["bindings"]:
print(result["label"]["xml:lang"] + " " + result["label"]["value"])

#Wikipage disambiguates (redirects)
disamb_res = disambiguates("Paris",sparql)
print disamb_res
for result in disamb_res["results"]["bindings"]:
print disamb_res["label"]["value"]

Aucun commentaire:

Enregistrer un commentaire