Lab Solutions
This page will be updated with Python examples related to the lectures and labs. We will add more examples after each lab has ended. The first examples will use Python's RDFlib. We will introduce other relevant libraries later.
Getting started
Printing the triples of the Graph in a readable way
# The turtle format has the purpose of being more readable for humans.
print(g.serialize(format="turtle"))
Coding Tasks Lab 1
from rdflib import Graph, Namespace, URIRef, BNode, Literal
from rdflib.namespace import RDF, FOAF, XSD
g = Graph()
ex = Namespace("http://example.org/")
g.add((ex.Cade, ex.married, ex.Mary))
g.add((ex.France, ex.capital, ex.Paris))
g.add((ex.Cade, ex.age, Literal("27", datatype=XSD.integer)))
g.add((ex.Mary, ex.age, Literal("26", datatype=XSD.integer)))
g.add((ex.Mary, ex.interest, ex.Hiking))
g.add((ex.Mary, ex.interest, ex.Chocolate))
g.add((ex.Mary, ex.interest, ex.Biology))
g.add((ex.Mary, RDF.type, ex.Student))
g.add((ex.Paris, RDF.type, ex.City))
g.add((ex.Paris, ex.locatedIn, ex.France))
g.add((ex.Cade, ex.characteristic, ex.Kind))
g.add((ex.Mary, ex.characteristic, ex.Kind))
g.add((ex.Mary, RDF.type, FOAF.Person))
g.add((ex.Cade, RDF.type, FOAF.Person))
Basic RDF programming
Different ways to create an address
from rdflib import Graph, Namespace, URIRef, BNode, Literal
from rdflib.namespace import RDF, FOAF, XSD
g = Graph()
ex = Namespace("http://example.org/")
# How to represent the address of Cade Tracey. From probably the worst solution to the best.
# Solution 1 -
# Make the entire address into one Literal. However, Generally we want to separate each part of an address into their own triples. This is useful for instance if we want to find only the streets where people live.
g.add((ex.Cade_Tracey, ex.livesIn, Literal("1516_Henry_Street, Berkeley, California 94709, USA")))
# Solution 2 -
# Seperate the different pieces information into their own triples
g.add((ex.Cade_tracey, ex.street, Literal("1516_Henry_Street")))
g.add((ex.Cade_tracey, ex.city, Literal("Berkeley")))
g.add((ex.Cade_tracey, ex.state, Literal("California")))
g.add((ex.Cade_tracey, ex.zipcode, Literal("94709")))
g.add((ex.Cade_tracey, ex.country, Literal("USA")))
# Solution 3 - Some parts of the addresses can make more sense to be resources than Literals.
# Larger concepts like a city or state are typically represented as resources rather than Literals, but this is not necesarilly a requirement in the case that you don't intend to say more about them.
g.add((ex.Cade_tracey, ex.street, Literal("1516_Henry_Street")))
g.add((ex.Cade_tracey, ex.city, ex.Berkeley))
g.add((ex.Cade_tracey, ex.state, ex.California))
g.add((ex.Cade_tracey, ex.zipcode, Literal("94709")))
g.add((ex.Cade_tracey, ex.country, ex.USA))
# Solution 4
# Grouping of the information into an Address. We can Represent the address concept with its own URI OR with a Blank Node.
# One advantage of this is that we can easily remove the entire address, instead of removing each individual part of the address.
# Solution 4 or 5 is how I would recommend to make addresses. Here, ex.CadeAddress could also be called something like ex.address1 or so on, if you want to give each address a unique ID.
# Address URI - CadeAdress
g.add((ex.Cade_Tracey, ex.address, ex.CadeAddress))
g.add((ex.CadeAddress, RDF.type, ex.Address))
g.add((ex.CadeAddress, ex.street, Literal("1516 Henry Street")))
g.add((ex.CadeAddress, ex.city, ex.Berkeley))
g.add((ex.CadeAddress, ex.state, ex.California))
g.add((ex.CadeAddress, ex.postalCode, Literal("94709")))
g.add((ex.CadeAddress, ex.country, ex.USA))
# OR
# Blank node for Address.
address = BNode()
g.add((ex.Cade_Tracey, ex.address, address))
g.add((address, RDF.type, ex.Address))
g.add((address, ex.street, Literal("1516 Henry Street", datatype=XSD.string)))
g.add((address, ex.city, ex.Berkeley))
g.add((address, ex.state, ex.California))
g.add((address, ex.postalCode, Literal("94709", datatype=XSD.string)))
g.add((address, ex.country, ex.USA))
# Solution 5 using existing vocabularies for address
# (in this case https://schema.org/PostalAddress from schema.org).
# Also using existing ontology for places like California. (like http://dbpedia.org/resource/California from dbpedia.org)
schema = Namespace("https://schema.org/")
dbp = Namespace("https://dpbedia.org/resource/")
g.add((ex.Cade_Tracey, schema.address, ex.CadeAddress))
g.add((ex.CadeAddress, RDF.type, schema.PostalAddress))
g.add((ex.CadeAddress, schema.streetAddress, Literal("1516 Henry Street")))
g.add((ex.CadeAddress, schema.addresCity, dbp.Berkeley))
g.add((ex.CadeAddress, schema.addressRegion, dbp.California))
g.add((ex.CadeAddress, schema.postalCode, Literal("94709")))
g.add((ex.CadeAddress, schema.addressCountry, dbp.United_States))
Typed Literals
from rdflib import Graph, Literal, Namespace
from rdflib.namespace import XSD
g = Graph()
ex = Namespace("http://example.org/")
g.add((ex.Cade, ex.age, Literal(27, datatype=XSD.integer)))
g.add((ex.Cade, ex.gpa, Literal(3.3, datatype=XSD.float)))
g.add((ex.Cade, FOAF.name, Literal("Cade Tracey", datatype=XSD.string)))
g.add((ex.Cade, ex.birthday, Literal("2006-01-01", datatype=XSD.date)))
Writing and reading graphs/files
# Writing the graph to a file on your system. Possible formats = turtle, n3, xml, nt.
g.serialize(destination="triples.txt", format="turtle")
# Parsing a local file
parsed_graph = g.parse(location="triples.txt", format="turtle")
# Parsing a remote endpoint like Dbpedia
dbpedia_graph = g.parse("http://dbpedia.org/resource/Pluto")
Graph Binding
#Graph Binding is useful for at least two reasons:
#(1) We no longer need to specify prefixes with SPARQL queries if they are already binded to the graph.
#(2) When serializing the graph, the serialization will show the correct expected prefix
# instead of default namespace names ns1, ns2 etc.
g = Graph()
ex = Namespace("http://example.org/")
dbp = Namespace("http://dbpedia.org/resource/")
schema = Namespace("https://schema.org/")
g.bind("ex", ex)
g.bind("dbp", dbp)
g.bind("schema", schema)
Collection Example
from rdflib import Graph, Namespace
from rdflib.collection import Collection
# Sometimes we want to add many objects or subjects for the same predicate at once.
# In these cases we can use Collection() to save some time.
# In this case I want to add all countries that Emma has visited at once.
b = BNode()
g.add((ex.Emma, ex.visit, b))
Collection(g, b,
[ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])
# OR
g.add((ex.Emma, ex.visit, ex.EmmaVisits))
Collection(g, ex.EmmaVisits,
[ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])
SPARQL
Also see the SPARQL Examples page!
Querying a local ("in memory") graph
Example contents of the file family.ttl:
@prefix rex: <http://example.org/royal#> . @prefix fam: <http://example.org/family#> . rex:IngridAlexandra fam:hasParent rex:HaakonMagnus . rex:SverreMagnus fam:hasParent rex:HaakonMagnus . rex:HaakonMagnus fam:hasParent rex:Harald . rex:MarthaLouise fam:hasParent rex:Harald . rex:HaakonMagnus fam:hasSister rex:MarthaLouise .
import rdflib g = rdflib.Graph() g.parse("family.ttl", format='ttl') qres = g.query(""" PREFIX fam: <http://example.org/family#> SELECT ?child ?sister WHERE { ?child fam:hasParent ?parent . ?parent fam:hasSister ?sister . }""") for row in qres: print("%s has aunt %s" % row)
With a prepared query, you can write the query once, and then bind some of the variables each time you use it:
import rdflib g = rdflib.Graph() g.parse("family.ttl", format='ttl') q = rdflib.plugins.sparql.prepareQuery( """SELECT ?child ?sister WHERE { ?child fam:hasParent ?parent . ?parent fam:hasSister ?sister . }""", initNs = { "fam": "http://example.org/family#"}) sm = rdflib.URIRef("http://example.org/royal#SverreMagnus") for row in g.query(q, initBindings={'child': sm}): print(row)
Select all contents of lists (rdfllib.Collection)
# rdflib.Collection has a different interntal structure so it requires a slightly more advance query. Here I am selecting all places that Emma has visited.
PREFIX ex: <http://example.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?visit
WHERE {
ex:Emma ex:visit/rdf:rest*/rdf:first ?visit
}
Using parameters/variables in rdflib queries
from rdflib import Graph, Namespace, URIRef
from rdflib.plugins.sparql import prepareQuery
g = Graph()
ex = Namespace("http://example.org/")
g.bind("ex", ex)
g.add((ex.Cade, ex.livesIn, ex.France))
g.add((ex.Anne, ex.livesIn, ex.Norway))
g.add((ex.Sofie, ex.livesIn, ex.Sweden))
g.add((ex.Per, ex.livesIn, ex.Norway))
g.add((ex.John, ex.livesIn, ex.USA))
def find_people_from_country(country):
country = URIRef(ex + country)
q = prepareQuery(
"""
PREFIX ex: <http://example.org/>
SELECT ?person WHERE {
?person ex:livesIn ?country.
}
""")
capital_result = g.query(q, initBindings={'country': country})
for row in capital_result:
print(row)
find_people_from_country("Norway")
SELECTING data from Blazegraph via Python
from SPARQLWrapper import SPARQLWrapper, JSON
# This creates a server connection to the same URL that contains the graphic interface for Blazegraph.
# You also need to add "sparql" to end of the URL like below.
sparql = SPARQLWrapper("http://localhost:9999/blazegraph/sparql")
# SELECT all triples in the database.
sparql.setQuery("""
SELECT DISTINCT ?p WHERE {
?s ?p ?o.
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["p"]["value"])
# SELECT all interests of Cade
sparql.setQuery("""
PREFIX ex: <http://example.org/>
SELECT DISTINCT ?interest WHERE {
ex:Cade ex:interest ?interest.
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["interest"]["value"])
Updating data from Blazegraph via Python
from SPARQLWrapper import SPARQLWrapper, POST, DIGEST
namespace = "kb"
sparql = SPARQLWrapper("http://localhost:9999/blazegraph/namespace/"+ namespace + "/sparql")
sparql.setMethod(POST)
sparql.setQuery("""
PREFIX ex: <http://example.org/>
INSERT DATA{
ex:Cade ex:interest ex:Mathematics.
}
""")
results = sparql.query()
print(results.response.read())
Retrieving data from Wikidata with SparqlWrapper
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
# In the query I want to select all the Vitamins in wikidata.
sparql.setQuery("""
SELECT ?nutrient ?nutrientLabel WHERE
{
?nutrient wdt:P279 wd:Q34956.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["nutrient"]["value"], " ", result["nutrientLabel"]["value"])