Python SPARQL client example

Originally published August 5, 2013

I wrote about using linked data sources like DBPedia yesterday but I should have included some example code for you to play with. I will get you started with these directions:

Start by installing three packages:

sudo easy_install simplejson
sudo easy_install RDFLib
sudo easy_install SPARQLWrapper
If you use the faceted search browser for DBPedia and search for "Berlin" and see a likely URI dbpedia:Berlin_Berlin to start with. The following code finds all triples that have dbpedia:Berlin_Berlin as an object and displays the subjects and predicates. Then, for all of the subjects in these triples, I search again for all triples with these subjects:
from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dbpedia: <http://dbpedia.org/resource/>
    SELECT ?s ?p
    WHERE { ?s ?p dbpedia:Berlin_Berlin } LIMIT 5
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

print("subject and predicate of all triples with object == dbpedia:Berlin_Berlin")

for result in results["results"]["bindings"]:
    print("s=" + result["s"]["value"] + "\tp=" + result["p"]["value"])

print("loop over all subjects returned in first query:")
for result in results["results"]["bindings"]:
    sparql.setQuery("""
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX dbpedia: <http://dbpedia.org/resource/>
        SELECT ?p ?o
        WHERE { <""" + result["s"]["value"] +
          """> ?p ?o } LIMIT 10""")
    results2 = sparql.query().convert()
    for result2 in results2["results"]["bindings"]:
        print("p=" + result2["p"]["value"] + "\to=" + result2["o"]["value"])
This is really a simple (and contrived) example that you can experiment with in just a few minutes, but it shows the mechanics of "spidering" linked data. You will need to learn the SPARQL query language for any real application. You can find many good SPARQL tutorials on the web, or you can grab a free PDF of either the Java or Common Lisp edition of my book "Practical Semantic Web and Linked Data Applications" on my my books web page.

I like to use DBPedia's snorql web app to experiment with SPARQL queries, get the queries working, then implement them in Python or Ruby scripts.

Comments

Popular posts from this blog

My Dad's work with Robert Oppenheimer and Edward Teller

Time and Attention Fragmentation in Our Digital Lives

I am moving back to the Google platform, less excited by what Apple is offering