Python Examples

From Info216
Revision as of 11:46, 17 April 2020 by Say004 (talk | contribs)

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.

Lecture 1: Python, RDFlib, and PyCharm

Printing the triples of the Graph in a readable way

# The turtle format has the purpose of being more readable for humans. 

Coding Tasks Lab 1

from rdflib import Graph, Namespace, URIRef, BNode, Literal
from rdflib.namespace import RDF, FOAF, XSD

g = Graph()
ex = Namespace("")

g.add((ex.Cade, ex.married, ex.Mary))
g.add((ex.France,, 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))

Lecture 2: 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("")

# 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,, 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,, 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.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.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.Berkeley))
g.add((ex.CadeAddress, ex.state, ex.California))
g.add((ex.CadeAddress, ex.postalCode, Literal("94709")))
g.add((ex.CadeAddress,, 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.Berkeley))
g.add((address, ex.state, ex.California))
g.add((address, ex.postalCode, Literal("94709", datatype=XSD.string)))
g.add((address,, ex.USA))

# Solution 5 using existing vocabularies for address 

# (in this case from 
# Also using existing ontology for places like California. (like from

schema = ""
dbp = ""

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("")

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,, Literal("Cade Tracey", datatype=XSD.string)))
g.add((ex.Cade, ex.birthday, Literal("2006-01-01",

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("")

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])

Lecture 3: SPARQL

SPARQL queries from the lecture

    ?s ?p ?o .
PREFIX rdf: <>

    ?s rdf:type ?t .
PREFIX owl: <>
    ?s owl:sameAs ?o2 . 
    ?s owl:sameAs ?o .
    FILTER(REGEX(STR(?o), "^http://www\\.", "s"))
    BIND(URI(REPLACE(STR(?o), "^http://www\\.", "http://", "s")) AS ?o2)

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:   <>
PREFIX rdf: <>

SELECT ?visit
  ex:Emma ex:visit/rdf:rest*/rdf:first ?visit


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.

    ?s ?p ?o.
results = sparql.query().convert()

for result in results["results"]["bindings"]:

# SELECT all interests of Cade

    PREFIX ex: <>
    ex:Cade ex:interest ?interest.
results = sparql.query().convert()

for result in results["results"]["bindings"]:

Updating data from Blazegraph via Python

from SPARQLWrapper import SPARQLWrapper, POST, DIGEST

namespace = "kb"
sparql = SPARQLWrapper("http://localhost:9999/blazegraph/namespace/"+ namespace + "/sparql")

    PREFIX ex: <>
    ex:Cade ex:interest ex:Mathematics.

results = sparql.query()

Lecture 5: RDFS

RDFS inference with RDFLib

You can use the OWL-RL package to add inference capabilities to RDFLib. Download it GitHub and copy the owlrl subfolder into your project folder next to your Python files.

OWL-RL documentation.

Example program to get started:

import rdflib.plugins.sparql.update
import owlrl.RDFSClosure

g = rdflib.Graph()

ex = rdflib.Namespace('')
g.bind('', ex)

PREFIX ex: <>
PREFIX owl: <>
    ex:Socrates rdf:type ex:Man .
    ex:Man rdfs:subClassOf ex:Mortal .

# The next three lines add inferred triples to g.
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)

b = g.query("""
PREFIX ex: <>
    ex:Socrates rdf:type ex:Mortal .
print('Result: ' + bool(b))

Languaged tagged RDFS labels

from rdflib import Graph, Namespace, Literal
from rdflib.namespace import RDFS

g = Graph()
ex = Namespace("")

g.add((ex.France, RDFS.label, Literal("Frankrike", lang="no")))
g.add((ex.France, RDFS.label, Literal("France", lang="en")))
g.add((ex.France, RDFS.label, Literal("Francia", lang="es")))

Lecture 6: RDFS Plus / OWL

RDFS Plus / OWL inference with RDFLib

You can use the OWL-RL package again as for Lecture 5.

Instead of:

# The next three lines add inferred triples to g.
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)

you can write this to get both RDFS and basic RDFS Plus / OWL inference:

# The next three lines add inferred triples to g.
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)

Example updates and queries:

PREFIX rdfs: <>
PREFIX owl: <>
PREFIX ex: <>

    ex:Socrates ex:hasWife ex:Xanthippe .
    ex:hasHusband owl:inverseOf ex:hasWife .
   ex:Xanthippe ex:hasHusband ex:Socrates .
   ex:Socrates ^ex:hasHusband ex:Xanthippe .
    ex:hasWife rdfs:subPropertyOf ex:hasSpouse .
    ex:hasSpouse rdf:type owl:SymmetricProperty . 
   ex:Socrates ex:hasSpouse ex:Xanthippe .
   ex:Socrates ^ex:hasSpouse ex:Xanthippe .

Lab 9

Download from BlazeGraph

Dumps a database to a local RDF file.
You need to install the SPARQLWrapper package first...

import datetime
from SPARQLWrapper import SPARQLWrapper, RDFXML

# your namespace, the default is 'kb'
ns = 'kb'

# the SPARQL endpoint
endpoint = '' + ns + '/sparql'

# - the endpoint just moved, the old one was:
# endpoint = '' + ns + '/sparql'

# create wrapper
wrapper = SPARQLWrapper(endpoint)

# prepare the SPARQL update
wrapper.setQuery('CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }')

# execute the SPARQL update and convert the result to an rdflib.Graph 
graph = wrapper.query().convert()

# the destination file, with code to make it timestamped
destfile = 'rdf_dumps/slr-kg4news-' +'%Y%m%d-%H%M') + '.rdf'

# serialize the result to file
graph.serialize(destination=destfile, format='ttl')

# report and quit
print('Wrote %u triples to file %s .' %
      (len(res), destfile))

Semantic Lifting - CSV

from rdflib import Graph, Literal, Namespace, URIRef
from rdflib.namespace import RDF, FOAF, RDFS, OWL
import pandas as pd

g = Graph()
ex = Namespace("")
g.bind("ex", ex)

# Load the CSV data as a pandas Dataframe.
csv_data = pd.read_csv("task1.csv")

# Here I deal with spaces (" ") in the data. I replace them with "_" so that URI's become valid.
csv_data = csv_data.replace(to_replace=" ", value="_", regex=True)

# Here I mark all missing/empty data as "unknown". This makes it easy to delete triples containing this later.
csv_data = csv_data.fillna("unknown")

# Loop through the CSV data, and then make RDF triples.
for index, row in csv_data.iterrows():
    # The names of the people act as subjects.
    subject = row['Name']
    # Create triples: e.g. "Cade_Tracey - age - 27"
    g.add((URIRef(ex + subject), URIRef(ex + "age"), Literal(row["Age"])))
    g.add((URIRef(ex + subject), URIRef(ex + "married"), URIRef(ex + row["Spouse"])))
    g.add((URIRef(ex + subject), URIRef(ex + "country"), URIRef(ex + row["Country"])))

    # If We want can add additional RDF/RDFS/OWL information e.g
    g.add((URIRef(ex + subject), RDF.type, FOAF.Person))

# I remove triples that I marked as unknown earlier.
g.remove((None, None, URIRef("")))

# Clean printing of the graph.

CSV file for above example

"Cade Tracey","26","Mary Jackson","US"
"Bob Johnson","21","","Canada"
"Mary Jackson","25","","France"
"Phil Philips","32","Catherine Smith","Japan"

Semantic Lifting - XML

from rdflib import Graph, Literal, Namespace, URIRef
from rdflib.namespace import RDF, XSD, RDFS
import xml.etree.ElementTree as ET

g = Graph()
ex = Namespace("")
prov = Namespace("")
g.bind("ex", ex)
g.bind("prov", prov)

tree = ET.parse("tv_shows.xml")
root = tree.getroot()

for tv_show in root.findall('tv_show'):
    show_id = tv_show.attrib["id"]
    title = tv_show.find("title").text

    g.add((URIRef(ex + show_id), ex.title, Literal(title, datatype=XSD.string)))
    g.add((URIRef(ex + show_id), RDF.type, ex.TV_Show))

    for actor in tv_show.findall("actor"):
        first_name = actor.find("firstname").text
        last_name = actor.find("lastname").text
        full_name = first_name + "_" + last_name

        g.add((URIRef(ex + full_name), ex.starsIn, URIRef(title)))
        g.add((URIRef(ex + full_name), RDF.type, ex.Actor))


XML Data for above example

    <tv_show id="1050">
    <tv_show id="1066">

WEB API Calls (In this case JSON)

import requests
import json
import pprint

# Retrieve JSON data from API service URL. Then load it with the json library as a json object.
url = ""
data = requests.get(url).content.decode("utf-8")
data = json.loads(data)


import rdflib

g = rdflib.Graph()

example = """
  "@context": {
    "name": "",
    "homepage": {
      "@id": "",
      "@type": "@id"
  "@id": "",
  "name": "Markus Lanthaler",
  "homepage": ""

# json-ld parsing automatically deals with @contexts
g.parse(data=example, format='json-ld')

# serialisation does expansion by default
for line in g.serialize(format='json-ld').decode().splitlines():

# by supplying a context object, serialisation can do compaction
context = {
    "foaf": ""
for line in g.serialize(format='json-ld', context=context).decode().splitlines():

INFO216, UiB, 2017-2020. All code examples are CC0.