Reputation: 7245
I am creating an ontology and I want to add data from dbpedia
. For instance, I want to add data regarding Switzerland.
This is what I am doing.
g = Graph()
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
query = """
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT DISTINCT ?s ?pop ?code
WHERE{
?s rdf:type dbo:PopulatedPlace.
?s dbp:iso3166code ?code.
?s dbo:populationTotal ?pop.
FILTER (?s = dbpedia:Switzerland)
}
"""
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
Then I add information I extracted to my graph
for result in results["results"]["bindings"]:
N = int(result["pop"]["value"])
totPopulation = Literal(N)
g.add(( cName, RDF.type, dbo.Country))
g.add(( cName, dbo.populationTotal, totPopulation ))
g.add(( cName, dbp.iso3166code, Literal(str(result["code"]["value"])) ))
Is there not an easier way to do that?
Upvotes: 3
Views: 1983
Reputation: 8465
Using a SPARQL CONSTRUCT
query might be easier as this query type returns a set of native RDF triples:
from rdflib import Graph
from SPARQLWrapper import SPARQLWrapper, RDF
# create empty graph
g = Graph()
# execute SPARQL CONSTRUCT query to get a set of RDF triples
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.addDefaultGraph("http://dbpedia.org")
query = """
PREFIX dbpedia: <http://dbpedia.org/resource/>
CONSTRUCT {
?s rdf:type dbo:PopulatedPlace.
?s dbp:iso3166code ?code.
?s dbo:populationTotal ?pop.
} WHERE{
?s rdf:type dbo:PopulatedPlace.
?s dbp:iso3166code ?code.
?s dbo:populationTotal ?pop.
FILTER (?s = dbpedia:Switzerland)
}
"""
sparql.setQuery(query)
try :
sparql.setReturnFormat(RDF)
results = sparql.query()
triples = results.convert() # this converts directly to an RDFlib Graph object
except:
print "query failed"
# add triples to graph
g += triples
Upvotes: 3