Python: how to add information from Dbpedia to my graph using rdflib?

Viewed 1348

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?

1 Answers
Related