3

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?

emax
  • 6,965
  • 19
  • 74
  • 141
  • A SPARQL `CONSTRUCT` query returns a set of RDF triples. – UninformedUser Nov 14 '17 at 18:56
  • @AKSW Is it possible to have an example? – emax Nov 22 '17 at 12:03
  • It's similar to SELECT queries: `prefix dbpedia: 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) }` – UninformedUser Nov 22 '17 at 12:40
  • how can I add this information to my graph `g`? – emax Nov 22 '17 at 16:21

1 Answers1

3

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
UninformedUser
  • 8,397
  • 1
  • 14
  • 23