Difference between revisions of "Python Examples"

 
(67 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
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.
 
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.
  
 +
=Example lab solutions=
  
==Lecture 1: Python, RDFlib, and PyCharm==
+
==Getting started==
 +
 
 +
<syntaxhighlight>
 +
 
 +
from rdflib.collection import Collection
 +
from rdflib import Graph, Namespace, Literal, URIRef
 +
from rdflib.namespace import RDF, FOAF, XSD
 +
 
 +
g = Graph()
 +
EX = Namespace('http://EXample.org/')
 +
RL = Namespace('http://purl.org/vocab/relationship/')
 +
DBO = Namespace('https://dbpedia.org/ontology/')
 +
DBR = Namespace('https://dbpedia.org/page/')
 +
 
 +
g.namespace_manager.bind('exampleURI', EX)
 +
g.namespace_manager.bind('relationship', RL)
 +
g.namespace_manager.bind('dbpediaOntology', DBO)
 +
g.namespace_manager.bind('dbpediaPage', DBR)
 +
 
 +
g.add((EX.Cade, RDF.type, FOAF.Person))
 +
g.add((EX.Mary, RDF.type, FOAF.Person))
 +
g.add((EX.Cade, RL.spouseOf, EX.Mary)) # a symmetrical relation from an established namespace
 +
g.add((DBR.France, DBO.capital, DBR.Paris))
 +
g.add((EX.Cade, FOAF.age, Literal(27)))
 +
g.add((EX.Mary, FOAF.age, Literal('26', datatype=XSD.int)))
 +
Collection (g, EX.MaryInterests, [EX.hiking, EX.choclate, EX.biology])
 +
g.add((EX.Mary, EX.hasIntrest, EX.MaryInterests))
 +
g.add((EX.Mary, RDF.type, EX.student))
 +
g.add((DBO.capital, EX.range, EX.city))
 +
g.add((EX.Mary, RDF.type, EX.kind))
 +
g.add((EX.Cade, RDF.type, EX.kindPerson))
 +
 
 +
#hobbies = ['hiking', 'choclate', 'biology']
 +
#for i in hobbies:
 +
#    g.add((EX.Mary, FOAF.interest, EX[i]))
 +
 
 +
print(g.serialize(format="turtle"))
 +
</syntaxhighlight>
 +
 
 +
==RDFlib==
 +
<syntaxhighlight>
 +
 
 +
from rdflib.namespace import RDF, XSD, FOAF
 +
from rdflib import Graph, Namespace, Literal, BNode
 +
from rdflib.collection import Collection
 +
 
 +
 
 +
g = Graph()
 +
ex = Namespace('http://example.org/')
 +
schema = Namespace("https://schema.org/")
 +
dbp = Namespace("https://dbpedia.org/resource/")
 +
 
 +
g.bind("ex", ex)
 +
g.bind("dbp", dbp)
 +
g.bind("schema", schema)
 +
 
 +
address = BNode()
 +
degree = BNode()
 +
 
 +
# from lab 1
 +
g.add((ex.Cade, FOAF.name, Literal("Cade Tracey", datatype=XSD.string)))
 +
g.add((ex.Mary, FOAF.name, Literal("Mary", datatype=XSD.string)))
 +
g.add((ex.Cade, RDF.type, FOAF.Person))
 +
g.add((ex.Mary, RDF.type, FOAF.Person))
 +
g.add((ex.Mary, RDF.type, ex.Student))
 +
g.add((ex.Cade, ex.married, ex.Mary))
 +
g.add((ex.Cade, FOAF.age, Literal('27', datatype=XSD.int)))
 +
g.add((ex.Mary, FOAF.age, Literal('26', datatype=XSD.int)))
 +
g.add((ex.Paris, RDF.type, ex.City))
 +
g.add((ex.France, ex.Capital, ex.Paris))
 +
g.add((ex.Mary, FOAF.interest, ex.hiking))
 +
g.add((ex.Mary, FOAF.interest, ex.Chocolate))
 +
g.add((ex.Mary, FOAF.interest, ex.biology))
 +
g.add((ex.France, ex.City, ex.Paris))
 +
g.add((ex.Mary, ex.Characterostic, ex.kind))
 +
g.add((ex.Cade, ex.Characterostic, ex.kind))
 +
g.add((ex.France, RDF.type, ex.Country))
 +
g.add((ex.Cade, schema.address, address))
 +
 
 +
# BNode address
 +
g.add((address, RDF.type, schema.PostalAdress))
 +
g.add((address, schema.streetAddress, Literal('1516 Henry Street')))
 +
g.add((address, schema.addresCity, dbp.Berkeley))
 +
g.add((address, schema.addressRegion, dbp.California))
 +
g.add((address, schema.postalCode, Literal('94709')))
 +
g.add((address, schema.addressCountry, dbp.United_States))
 +
 
 +
# More info about Cade
 +
g.add((ex.Cade, ex.Degree, degree))
 +
g.add((degree, ex.Field, dbp.Biology))
 +
g.add((degree, RDF.type, dbp.Bachelors_degree))
 +
g.add((degree, ex.Universety, dbp.University_of_California))
 +
g.add((degree, ex.year, Literal('2001', datatype=XSD.gYear)))
 +
 
 +
# Emma
 +
emma_degree = BNode()
 +
g.add((ex.Emma, FOAF.name, Literal("Emma Dominguez", datatype=XSD.string)))
 +
g.add((ex.Emma, RDF.type, FOAF.Person))
 +
g.add((ex.Emma, ex.Degree, emma_degree))
 +
g.add((degree, ex.Field, dbp.Chemistry))
 +
g.add((degree, RDF.type, dbp.Masters_degree))
 +
g.add((degree, ex.Universety, dbp.University_of_Valencia))
 +
g.add((degree, ex.year, Literal('2015', datatype=XSD.gYear)))
 +
 
 +
# Address
 +
emma_address = BNode()
 +
g.add((ex.Emma, schema.address, emma_address))
 +
g.add((emma_address, RDF.type, schema.PostalAdress))
 +
g.add((emma_address, schema.streetAddress,
 +
      Literal('Carrer de la Guardia Civil 20')))
 +
g.add((emma_address, schema.addressRegion, dbp.Valencia))
 +
g.add((emma_address, schema.postalCode, Literal('46020')))
 +
g.add((emma_address, schema.addressCountry, dbp.Spain))
 +
 
 +
b = BNode()
 +
g.add((ex.Emma, ex.visit, b))
 +
Collection(g, b,
 +
          [dbp.Portugal, dbp.Italy, dbp.France, dbp.Germany, dbp.Denmark, dbp.Sweden])
 +
 
 +
</syntaxhighlight>
 +
 
 +
==SPARQL - Blazegraph==
 +
<syntaxhighlight>
 +
PREFIX ex: <http://example.org/>
 +
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 +
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 +
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 +
PREFIX xml: <http://www.w3.org/XML/1998/namespace>
 +
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 +
 
 +
 
 +
#select all triplets in graph
 +
SELECT ?s ?p ?o
 +
WHERE {
 +
    ?s ?p ?o .
 +
}
 +
#select the interestes of Cade
 +
SELECT ?cadeInterest
 +
WHERE {
 +
    ex:Cade ex:interest ?cadeInterest .
 +
}
 +
#select the country and city where Emma lives
 +
SELECT ?emmaCity ?emmaCountry
 +
WHERE {
 +
    ex:Emma ex:address ?address .
 +
  ?address ex:city ?emmaCity .
 +
  ?address ex:country ?emmaCountry .
 +
}
 +
#select the people who are over 26 years old
 +
SELECT ?person ?age
 +
WHERE {
 +
    ?person ex:age ?age .
 +
  FILTER(?age > 26) .   
 +
}
 +
#select people who graduated with Bachelor
 +
SELECT ?person ?degree
 +
WHERE {
 +
    ?person ex:degree ?degree .
 +
  ?degree ex:degreeLevel "Bachelor" .
 +
         
 +
}
 +
# delete cades photography interest
 +
DELETE DATA
 +
{
 +
    ex:Cade ex:interest ex:Photography .
 +
}
 +
 
 +
# delete and insert university of valencia
 +
DELETE { ?s ?p ex:University_of_Valencia }
 +
INSERT { ?s ?p ex:Universidad_de_Valencia }
 +
WHERE  { ?s ?p ex:University_of_Valencia }
 +
 
 +
#check if the deletion worked
 +
SELECT ?s ?o2
 +
WHERE  {
 +
  ?s ex:degree ?o .
 +
  ?o ex:degreeSource ?o2 .
 +
      }
 +
#describe sergio
 +
DESCRIBE ex:Sergio ?o
 +
WHERE {
 +
  ex:Sergio ?p ?o .
 +
  ?o ?p2 ?o2 .
 +
  }
 +
</syntaxhighlight>
 +
 
 +
==SPARQL - RDFlib==
 +
<syntaxhighlight>
 +
from SPARQLWrapper import SPARQLWrapper, JSON, POST, GET, TURTLE
 +
 
 +
namespace = "lab4"
 +
sparql = SPARQLWrapper("http://10.111.21.183:9999/blazegraph/namespace/"+ namespace + "/sparql")
 +
 
 +
# Print out Cades interests
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    SELECT * WHERE {
 +
    ex:Cade ex:interest ?interest.
 +
    }
 +
""")
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
for result in results["results"]["bindings"]:
 +
    print(result["interest"]["value"])
 +
 
 +
# Print Emmas city and country
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    SELECT ?emmaCity ?emmaCountry
 +
    WHERE {
 +
        ex:Emma ex:address ?address .
 +
        ?address ex:city ?emmaCity .
 +
        ?address ex:country ?emmaCountry .
 +
        }
 +
""")
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
for result in results["results"]["bindings"]:
 +
    print("Emma's city is "+result["emmaCity"]["value"]+" and Emma's country is " + result["emmaCountry"]["value"])
 +
 
 +
#Select the people who are over 26 years old
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    SELECT ?person ?age
 +
    WHERE {
 +
        ?person ex:age ?age .
 +
        FILTER(?age > 26) . 
 +
        }
 +
        """)
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
for result in results["results"]["bindings"]:
 +
    print("All people who are over 26 years old: "+result["person"]["value"])
 +
 
 +
#Select people who graduated with Bachelor
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    SELECT ?person ?degree
 +
    WHERE {
 +
        ?person ex:degree ?degree .
 +
        ?degree ex:degreeLevel "Bachelor" .
 +
        }
 +
        """)
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
for result in results["results"]["bindings"]:
 +
    print("People who graduated with Bachelor: "+result["person"]["value"])
 +
 
 +
#Delete cades photography interest
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    DELETE DATA {
 +
        ex:Cade ex:interest ex:Photography .
 +
        }
 +
        """)
 +
sparql.setMethod(POST)
 +
results = sparql.query()
 +
print(results.response.read())
 +
 
 +
# Print out Cades interests again
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    SELECT * WHERE {
 +
    ex:Cade ex:interest ?interest.
 +
    }
 +
""")
 +
sparql.setReturnFormat(JSON)
 +
sparql.setMethod(GET)
 +
results = sparql.query().convert()
 +
for result in results["results"]["bindings"]:
 +
    print(result["interest"]["value"])
 +
 
 +
# Check university names
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    SELECT ?s ?o2
 +
    WHERE  {
 +
        ?s ex:degree ?o .
 +
        ?o ex:degreeSource ?o2 .
 +
      }
 +
    """)
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
for result in results["results"]["bindings"]:
 +
    print(result["o2"]["value"])
 +
 
 +
 
 +
#Delete and insert university of valencia
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    DELETE { ?s ?p ex:University_of_Valencia }
 +
    INSERT { ?s ?p ex:Universidad_de_Valencia }
 +
    WHERE  { ?s ?p ex:University_of_Valencia }
 +
        """)
 +
sparql.setMethod(POST)
 +
results = sparql.query()
 +
print(results.response.read())
 +
 
 +
# Check university names again
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    SELECT ?s ?o2
 +
    WHERE  {
 +
        ?s ex:degree ?o .
 +
        ?o ex:degreeSource ?o2 .
 +
      }
 +
    """)
 +
sparql.setReturnFormat(JSON)
 +
sparql.setMethod(GET)
 +
results = sparql.query().convert()
 +
for result in results["results"]["bindings"]:
 +
    print(result["o2"]["value"])
 +
 
 +
#Insert Sergio
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 +
    INSERT DATA {
 +
        ex:Sergio a foaf:Person ;
 +
        ex:address [ a ex:Address ;
 +
                ex:city ex:Valenciay ;
 +
                ex:country ex:Spain ;
 +
                ex:postalCode "46021"^^xsd:string ;
 +
                ex:state ex:California ;
 +
                ex:street "4_Carrer_del_Serpis"^^xsd:string ] ;
 +
        ex:degree [ ex:degreeField ex:Computer_science ;
 +
                ex:degreeLevel "Master"^^xsd:string ;
 +
                ex:degreeSource ex:University_of_Valencia ;
 +
                ex:year "2008"^^xsd:gYear ] ;
 +
        ex:expertise ex:Big_data,
 +
            ex:Semantic_technologies,
 +
            ex:Machine_learning;
 +
        foaf:name "Sergio_Pastor"^^xsd:string .
 +
        }
 +
    """)
 +
sparql.setMethod(POST)
 +
results = sparql.query()
 +
print(results.response.read())
 +
sparql.setMethod(GET)
 +
 
 +
# Describe Sergio
 +
sparql.setReturnFormat(TURTLE)
 +
sparql.setQuery("""
 +
    PREFIX ex: <http://example.org/>
 +
    DESCRIBE ex:Sergio ?o
 +
    WHERE {
 +
        ex:Sergio ?p ?o .
 +
        ?o ?p2 ?o2 .
 +
    }
 +
    """)
 +
results = sparql.query().convert()
 +
print(results.serialize(format='turtle'))
 +
 
 +
# Construct that any city is in the country in an address
 +
sparql.setQuery("""
 +
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 +
    PREFIX ex: <http://example.org/>
 +
    CONSTRUCT {?city ex:locatedIn ?country}
 +
    Where {
 +
        ?s rdf:type ex:Address .
 +
        ?s ex:city ?city .
 +
        ?s ex:country ?country.
 +
        }
 +
    """)
 +
sparql.setReturnFormat(TURTLE)
 +
results = sparql.query().convert()
 +
print(results.serialize(format='turtle'))
 +
 
 +
</syntaxhighlight>
 +
==Web APIs and JSON-LD==
 +
 
 +
<syntaxhighlight>
 +
import requests
 +
from rdflib import FOAF, Namespace, Literal, RDF, Graph, TURTLE
 +
 
 +
r = requests.get('http://api.open-notify.org/astros.json').json()
 +
g = Graph()
 +
EX = Namespace('http://EXample.org/')
 +
g.bind("ex", EX)
 +
 
 +
for item in r['people']:
 +
    craft = item['craft'].replace(" ","_")
 +
    person = item['name'].replace(" ","_")
 +
    g.add((EX[person], EX.onCraft, EX[craft]))
 +
    g.add((EX[person], RDF.type, FOAF.Person))
 +
    g.add((EX[person], FOAF.name, Literal(item['name'])))
 +
    g.add((EX[craft], FOAF.name, Literal(item['craft'])))
 +
res = g.query("""
 +
    CONSTRUCT {?person1 foaf:knows ?person2}
 +
    WHERE {
 +
        ?person1 ex:onCraft ?craft .
 +
        ?person2 ex:onCraft ?craft .
 +
        }
 +
""")
 +
 
 +
for triplet in res:
 +
    # (we don't need to add that they know themselves)
 +
    if (triplet[0] != triplet[2]):
 +
        g.add((triplet))
 +
       
 +
print(g.serialize(format="turtle"))
 +
</syntaxhighlight>
 +
 
 +
==Semantic lifting - CSV==
 +
<syntaxhighlight>
 +
import pandas as pd
 +
from rdflib import Graph, Namespace, URIRef, Literal
 +
from rdflib.namespace import RDF, XSD
 +
import spotlight
 +
from spotlight import SpotlightException
 +
 
 +
 
 +
# Parameter given to spotlight to filter out results with confidence lower than this value
 +
CONFIDENCE = 0.5
 +
SERVER = "https://api.dbpedia-spotlight.org/en/annotate"
 +
 
 +
def annotate_entity(entity):
 +
annotations = []
 +
try:
 +
annotations = spotlight.annotate(address=SERVER,text=entity, confidence=CONFIDENCE)
 +
    # This catches errors thrown from Spotlight, including when no resource is found in DBpedia
 +
except SpotlightException as e:
 +
print(e)
 +
return annotations
 +
 
 +
 
 +
ex = Namespace("http://example.org/")
 +
dbr = Namespace("http://dbpedia.org/resource/")
 +
dbp = Namespace("https://dbpedia.org/property/")
 +
dbpage = Namespace("https://dbpedia.org/page/")
 +
sem = Namespace("http://semanticweb.cs.vu.nl/2009/11/sem/")
 +
tl = Namespace("http://purl.org/NET/c4dm/timeline.owl#")
 +
 
 +
g = Graph()
 +
g.bind("ex", ex)
 +
g.bind("dbr", dbr)
 +
g.bind("dbp", dbp)
 +
g.bind("dbpage", dbpage)
 +
g.bind("sem", sem)
 +
g.bind("tl", tl)
 +
 
 +
df = pd.read_csv("russia-investigations.csv")
 +
# We need to correct the type of the columns in the DataFrame, as Pandas assigns an incorrect type when it reads the file (for me at least). We use .astype("str") to convert the content of the columns to a string.
 +
df["name"] = df["name"].astype("str")
 +
df["type"] = df["type"].astype("str")
 +
 
 +
# iterrows creates an iterable object (list of rows)
 +
for index, row in df.iterrows():
 +
investigation = URIRef(ex + row['investigation'])
 +
investigation_spotlight = annotate_entity(row['investigation'])
 +
investigation_start = Literal(row['investigation-start'], datatype=XSD.date)
 +
investigation_end = Literal(row['investigation-end'], datatype=XSD.date)
 +
investigation_days = Literal(row['investigation-days'], datatype=XSD.integer)
 +
name = Literal(row['name'], datatype=XSD.string)
 +
name_underscore = URIRef(dbpage + row['name'].replace(" ", "_"))
 +
investigation_result = URIRef(
 +
ex + row['investigation'] + "_investigation_" + row['name'].replace(" ", "_"))
 +
indictment_days = Literal(row['indictment-days'], datatype=XSD.integer)
 +
type = URIRef(dbr + row['type'].replace(" ", "_"))
 +
cp_date = Literal(row['cp-date'], datatype=XSD.date)
 +
cp_days = Literal(row['cp-days'], datatype=XSD.duration)
 +
overturned = Literal(row['overturned'], datatype=XSD.boolean)
 +
pardoned = Literal(row['pardoned'], datatype=XSD.boolean)
 +
american = Literal(row['american'], datatype=XSD.boolean)
 +
president = Literal(row['president'], datatype=XSD.string)
 +
president_underscore = URIRef(dbr + row['president'].replace(" ", "_"))
 +
president_spotlight = annotate_entity(row['president'])
 +
 
 +
try:
 +
g.add((( URIRef(investigation_spotlight[0]["URI"]), RDF.type, sem.Event)))
 +
except:
 +
g.add((investigation, RDF.type, sem.Event))
 +
try:
 +
g.add((( URIRef(investigation_spotlight[0]["URI"]), sem.hasBeginTimeStamp, investigation_start)))
 +
except:
 +
g.add((investigation, sem.hasBeginTimeStamp, investigation_start))
 +
try:
 +
g.add((( URIRef(investigation_spotlight[0]["URI"]), sem.hasEndTimeStamp, investigation_end)))
 +
except:
 +
g.add((investigation, sem.hasEndTimeStamp, investigation_end))
 +
try:
 +
g.add((URIRef(investigation_spotlight[0]["URI"]), tl.duration, investigation_days))
 +
except:
 +
g.add((investigation, tl.duration, investigation_days))
 +
try:
 +
g.add((URIRef(investigation_spotlight[0]["URI"]), dbp.president, URIRef(president_spotlight[0]["URI"])))
 +
except:
 +
g.add((investigation, dbp.president, dbr.president_underscore))
 +
try:
 +
g.add((URIRef(investigation_spotlight[0]["URI"]), sem.hasSubEvent, investigation_result))
 +
except:
 +
g.add((investigation, sem.hasSubEvent, investigation_result))
 +
 +
g.add((investigation_result, ex.resultType, type))
 +
g.add((investigation_result, ex.objectOfInvestigation, name_underscore))
 +
g.add((investigation_result, ex.isAmerican, american))
 +
g.add((investigation_result, ex.indictmentDuration, indictment_days))
 +
g.add((investigation_result, ex.caseSolved, cp_date))
 +
g.add((investigation_result, ex.daysBeforeCaseSolved, cp_days))
 +
g.add((investigation_result, ex.overturned, overturned))
 +
g.add((investigation_result, ex.pardoned, pardoned))
 +
 
 +
g.serialize("output.ttl", format="ttl")
 +
</syntaxhighlight>
 +
 
 +
==RDFS==
 +
<syntaxhighlight>
 +
from rdflib.namespace import RDF, FOAF, XSD, RDFS
 +
from rdflib import OWL, Graph, Namespace, URIRef, Literal, BNode
 +
from rdflib.namespace import RDF, RDFS, XSD, OWL
 +
import owlrl
 +
 
 +
ex = Namespace("http://example.org/")
 +
dbr = Namespace("http://dbpedia.org/resource/")
 +
dbp = Namespace("https://dbpedia.org/property/")
 +
dbpage = Namespace("https://dbpedia.org/page/")
 +
sem = Namespace("http://semanticweb.cs.vu.nl/2009/11/sem/")
 +
tl = Namespace("http://purl.org/NET/c4dm/timeline.owl#")
 +
 
 +
g = Graph()
 +
g.bind("ex", ex)
 +
g.bind("dbr", dbr)
 +
g.bind("dbp", dbp)
 +
g.bind("dbpage", dbpage)
 +
g.bind("sem", sem)
 +
g.bind("tl", tl)
 +
 
 +
g.parse(location="exampleTTL.ttl", format="turtle")
 +
 
 +
# University of California and University of Valencia are both Universities.
 +
g.add((ex.University_of_California, RDF.type, ex.University))
 +
g.add((ex.University_of_Valencia, RDF.type, ex.University))
 +
# All universities are higher education institutions (HEIs).
 +
g.add((ex.University, RDFS.subClassOf, ex.Higher_education))
 +
# Only persons can have an expertise, and what they have expertise in is always a subject.
 +
g.add((ex.expertise, RDFS.domain, FOAF.Person))
 +
g.add((ex.expertise, RDFS.range, ex.subject))
 +
# Only persons can graduate from a HEI.
 +
g.add((ex.graduatedFromHEI, RDFS.domain, FOAF.Person))
 +
g.add((ex.graduatedFromHEI, RDFS.range, ex.Higher_education))
 +
# If you are a student, you are in fact a person as well.
 +
g.add((ex.Student, RDFS.subClassOf, FOAF.Person))
 +
# That a person is married to someone, means that they know them.
 +
g.add((ex.married, RDFS.subPropertyOf, FOAF.knows))
 +
# Finally, if a person has a name, that name is also the label of that entity."
 +
g.add((FOAF.name, RDFS.subPropertyOf, RDFS.label))
 +
 
 +
# Having a degree from a HEI means that you have also graduated from that HEI.
 +
g.add((ex.graduatedFromHEI, RDFS.subPropertyOf, ex.degree))
 +
# That a city is a capital of a country means that this city is located in that country.
 +
g.add((ex.capital, RDFS.domain, ex.Country))
 +
g.add((ex.capital, RDFS.range, ex.City))
 +
g.add((ex.capital, RDFS.subPropertyOf, ex.hasLocation))
 +
# That someone was involved in a meeting, means that they have met the other participants.
 +
    # This question was bad for the RDFS lab because we need complex OWL or easy sparql.
 +
res = g.query("""
 +
    CONSTRUCT {?person1 ex:haveMet ?person2}
 +
    WHERE {
 +
        ?person1 ex:meeting ?Meeting .
 +
        ?Meeting ex:involved ?person2 .
 +
        }
 +
""")
 +
for triplet in res:
 +
    #we don't need to add that people have met themselves
 +
    if (triplet[0] != triplet[2]):
 +
        g.add((triplet))
 +
# If someone partook in a meeting somewhere, means that they have visited that place"
 +
    # This question was bad for the RDFS lab for the same reason.
 +
res = g.query("""
 +
    CONSTRUCT {?person ex:hasVisited ?place}
 +
    WHERE {
 +
        ?person1 ex:meeting ?Meeting .
 +
        ?Meeting ex:location ?place .
 +
        }
 +
""")
 +
for triplet in res:
 +
        g.add((triplet))
 +
 
 +
rdfs = owlrl.OWLRL.OWLRL_Semantics(g, False, False, False)
 +
rdfs.closure()
 +
rdfs.flush_stored_triples()
 +
g.serialize("output.ttl",format="ttl")
 +
</syntaxhighlight>
 +
 
 +
==OWL 1==
 +
<syntaxhighlight>
 +
import owlrl
 +
from rdflib import Graph, Namespace, Literal, URIRef
 +
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL
 +
from rdflib.collection import Collection
 +
 
 +
g = Graph()
 +
print()
 +
# Namespaces
 +
ex = Namespace("http://example.org/")
 +
dbp = Namespace("http://dbpedia.org/resource/")
 +
geo = Namespace("http://sws.geonames.org/")
 +
schema = Namespace("https://schema.org/")
 +
akt = Namespace("http://www.aktors.org/ontology/portal#")
 +
vcard = Namespace("http://www.w3.org/2006/vcard/ns#")
 +
 
 +
g.bind("ex", ex)
 +
g.bind("owl", OWL)
 +
 
 +
g.parse(location="lab8turtle.txt", format="turtle")
 +
 
 +
# Cade and Emma are two different persons.
 +
g.add((ex.Cade, OWL.differentFrom, ex.Emma))
 +
# The country USA above is the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames resource http://sws.geonames.org/6252001/ (gn:6252001).
 +
g.add((ex.USA, OWL.sameAs, dbp.United_States))
 +
g.add((ex.USA, OWL.sameAs, geo["6252001"]))
 +
# The person class (the RDF type the Cade and Emma resources) in your graph is the same as FOAF's, schema.org's and AKT's person classes
 +
    # (they are http://xmlns.com/foaf/0.1/Person, http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively.
 +
g.add((FOAF.Person, OWL.sameAs, schema.Person))
 +
g.add((FOAF.Person, OWL.sameAs, akt.Person))
 +
# Nothing can be any two of a person, a university, or a city at the same time.
 +
Collection(g, ex.DisjointClasses, [FOAF.Person, ex.University, ex.City])
 +
g.add((OWL.AllDifferent, OWL.distinctMembers, ex.DisjointClasses))
 +
# The property you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US
 +
    # is a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code).
 +
g.add((ex.postalCode, RDFS.subPropertyOf, vcard["postal-code"]))
 +
# No two US cities can have the same postal code.
 +
    # We have to add a relation from city to postal code first
 +
res = g.query("""
 +
    PREFIX RDF: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 +
    PREFIX ex: <http://example.org/>
 +
    CONSTRUCT {?usa_city ex:us_city_postal_code ?postalcode}
 +
    WHERE {
 +
        ?address RDF:type ex:Address .
 +
        ?address ex:country ex:USA .
 +
        ?address ex:city ?usa_city .
 +
        ?address ex:postalCode ?postalcode
 +
        }
 +
""")
 +
for triplet in res:
 +
        g.add((triplet))
 +
    # Now we can make us cities have distinct postal codes
 +
g.add((ex.us_city_postal_code, RDF.type, OWL.FunctionalProperty))
 +
g.add((ex.us_city_postal_code, RDF.type, OWL.InverseFunctionalProperty))
 +
g.add((ex.us_city_postal_code, RDFS.subPropertyOf, ex.postalcode))
 +
 
 +
# The property you have used for Emma living in Valencia is the same property as FOAF's based-near property
 +
    # (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property (http://dbpedia.org/ontology/hometown, dbo:hometown).
 +
g.add((ex.city, OWL.sameAs, FOAF.based_near))
 +
g.add((ex.city, OWL.inverseOf, dbp.hometown))
 +
 
 +
g.add((ex.Cade, ex.married, ex.Mary))
 +
g.add((ex.Cade, ex.livesWith, ex.Mary))
 +
g.add((ex.Cade, ex.sibling, ex.Andrew))
 +
g.add((ex.Cade, ex.hasFather, ex.Bob))
 +
g.add((ex.Bob, ex.fatherOf, ex.Cade))
 +
 
 +
 
 +
#Look through the predicates(properties) above and add new triples for each one that describes them as any of the following:
 +
    # a reflexive , irreflexive, symmetric, asymmetric, transitive, functional, or an Inverse Functional Property.
 +
g.add((ex.married, RDF.type, OWL.SymmetricProperty))
 +
g.add((ex.married, RDF.type, OWL.FunctionalProperty))
 +
g.add((ex.married, RDF.type, OWL.InverseFunctionalProperty))
 +
g.add((ex.married, RDF.type, OWL.IrreflexiveProperty))
 +
 
 +
g.add((ex.livesWith, RDF.type, OWL.SymmetricProperty))
 +
g.add((ex.livesWith, RDF.type, OWL.ReflexiveProperty))
 +
g.add((ex.livesWith, RDF.type, OWL.TransitiveProperty))
 +
 
 +
g.add((ex.sibling, RDF.type, OWL.SymmetricProperty))
 +
 
 +
g.add((ex.hasFather, RDF.type, OWL.AsymmetricProperty))
 +
g.add((ex.hasFather, RDF.type, OWL.FunctionalProperty))
 +
g.add((ex.hasFather, RDF.type, OWL.IrreflexiveProperty))
 +
 
 +
g.add((ex.fatherOf, RDF.type, OWL.AsymmetricProperty))
 +
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))
 +
g.add((ex.fatherOf, RDF.type, OWL.InverseFunctionalProperty))
 +
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))
 +
 
 +
# These three lines add inferred triples to the graph.
 +
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
 +
owl.closure()
 +
owl.flush_stored_triples()
 +
 
 +
g.serialize("lab8output.xml",format="xml")
 +
</syntaxhighlight>
 +
 
 +
==Semantic lifting - XML==
 +
<syntaxhighlight>
 +
from rdflib import Graph, Literal, Namespace, URIRef
 +
from rdflib.namespace import RDF
 +
import xml.etree.ElementTree as ET
 +
import requests
 +
 
 +
g = Graph()
 +
ex = Namespace("http://example.org/")
 +
prov = Namespace("http://www.w3.org/ns/prov#")
 +
g.bind("ex", ex)
 +
g.bind("prov", prov)
 +
 
 +
 
 +
# URL of xml data
 +
url = 'http://feeds.bbci.co.uk/news/rss.xml'
 +
# Retrieve the xml data from the web-url.
 +
resp = requests.get(url)
 +
# Creating an ElementTree from the response content
 +
tree = ET.ElementTree(ET.fromstring(resp.content))
 +
root = tree.getroot()
 +
 
 +
# I just realized this is cheating, but whatever, you should do it with xmltree
 +
writerDict = {
 +
    "Mon":"Thomas_Smith",
 +
    "Tue":"Thomas_Smith",
 +
    "Wed":"Thomas_Smith",
 +
    "Thu":"Joseph_Olson",
 +
    "Fri":"Joseph_Olson",
 +
    "Sat":"Sophia_Cruise",
 +
    "Sun":"Sophia_Cruise"
 +
}
 +
copyright = Literal(root.findall("./channel")[0].find("copyright").text)
 +
 
 +
for item in root.findall("./channel/item"):
 +
    copyright = Literal(root.findall("./channel")[0].find("copyright").text)
 +
 
 +
    News_article_id = URIRef(item.find("guid").text)
 +
    title = Literal(item.find("title").text)
 +
    description = Literal(item.find("description").text)
 +
    link = URIRef(item.find("link").text)
 +
    pubDate = Literal(item.find("pubDate").text)
 +
    writerName = ex[writerDict[pubDate[:3]]]
 +
 
 +
    g.add((News_article_id, ex.title, title))
 +
    g.add((News_article_id, ex.description, description))
 +
    g.add((News_article_id, ex.source_link, link))
 +
    g.add((News_article_id, ex.pubDate, pubDate))
 +
    g.add((News_article_id, ex.copyright, copyright))
 +
    g.add((News_article_id, RDF.type, ex.News_article))
 +
    g.add((News_article_id, RDF.type, prov.Entity))
 +
 
 +
    g.add((News_article_id, ex.authoredBy, writerName))
 +
    g.add((writerName, RDF.type, prov.Person))
 +
    g.add((writerName, RDF.type, prov.Agent))
 +
    g.add((ex.authoredBy, RDF.type, prov.Generation))
 +
 
 +
print(g.serialize(format="turtle"))
 +
</syntaxhighlight>
 +
 
 +
==OWL 2==
 +
<syntaxhighlight>
 +
from rdflib import Graph, Literal, Namespace, BNode
 +
from rdflib.namespace import RDF, OWL, RDFS
 +
from rdflib.collection import Collection
 +
 
 +
g = Graph()
 +
ex = Namespace("http://example.org/")
 +
g.bind("ex", ex)
 +
g.bind("owl", OWL)
 +
 
 +
# anyone who is a graduate has at least one degree
 +
br = BNode()
 +
g.add((br, RDF.type, OWL.Restriction))
 +
g.add((br, OWL.onProperty, ex.degree))
 +
g.add((br, OWL.minCardinality, Literal(1)))
 +
bi = BNode()
 +
Collection(g, bi, [ex.Person, br])
 +
g.add((ex.Graduate, OWL.intersectionOf, bi))
 +
 
 +
#anyone who is a university graduate has at least one degree from a university
 +
br = BNode()
 +
g.add((br, RDF.type, OWL.Restriction))
 +
g.add((br, OWL.onProperty, ex.degree))
 +
g.add((br, OWL.someValuesFrom, ex.University))
 +
bi = BNode()
 +
Collection(g, bi, [ex.Graduate, br])
 +
                #[ex.Person, br] also someValueFrom implies a cardinality of at least one so they would be equivalent.
 +
                #[ex.Person, ex.Graduate, br] would be redundant since intersection is associative.
 +
g.add((ex.University_graduate, OWL.intersectionOf, bi))
 +
 
 +
#a grade is either an A, B, C, D, E or F
 +
 
 +
bi = BNode()
 +
Collection(g, bi, [Literal("A"), Literal("B"), Literal("C"), Literal("D"), Literal("E"), Literal("F")])
 +
b1 = BNode()
 +
g.add((b1, RDF.type, RDFS.Datatype))
 +
g.add((b1, OWL.oneOf, bi))
 +
 
 +
g.add((ex.grade, RDFS.range, b1))
 +
 
 +
#a straight A student is a student that has only A grades
 +
b1 = BNode()
 +
g.add((b1, RDF.type, OWL.Restriction))
 +
g.add((b1, OWL.onProperty, ex.grade))
 +
g.add((b1, OWL.allValuesFrom, Literal("A")))
 +
 
 +
b2 = BNode()
 +
g.add((b2, RDF.type, OWL.Restriction))
 +
g.add((b2, OWL.onProperty, ex.grade))
 +
g.add((b2, OWL.someValuesFrom, Literal("A")))
 +
 
 +
bi = BNode()
 +
Collection(g, bi, [ex.Student, b1, b2])
 +
g.add((ex.Straight_A_student, OWL.intersectionOf, bi))
 +
 
 +
#a graduate has no F grades
 +
b3 = BNode()
 +
Collection(g, b3, [Literal("A"), Literal("B"), Literal("C"), Literal("D"), Literal("E")])
 +
b4 = BNode()
 +
g.add((b4, RDF.type, RDFS.Datatype))
 +
g.add((b4, OWL.oneOf, b3))
 +
b5 = BNode()
 +
g.add((b5, RDF.type, OWL.Restriction))
 +
g.add((b5, OWL.onProperty, ex.grade))
 +
g.add((b5, OWL.allValuesFrom, b4))
 +
 
 +
b6 = BNode()
 +
Collection(g, b6, [ex.Person, b1, b5])
 +
g.add((ex.Graduate, OWL.intersectionOf, b6))
 +
 
 +
#a student has a unique student number
 +
g.add((ex.student_number, RDF.type, OWL.FunctionalProperty))
 +
g.add((ex.student_number, RDF.type, OWL.InverseFunctionalProperty))
 +
 
 +
#each student has exactly one average grade
 +
b1 = BNode()
 +
g.add((b1, RDF.type, OWL.Restriction))
 +
g.add((b1, OWL.onProperty, ex.average_grade))
 +
g.add((b1, OWL.cardinality, Literal(1)))
 +
 
 +
b2 = BNode()
 +
g.add((b2, RDF.type, OWL.Restriction))
 +
g.add((b2, OWL.onProperty, ex.student_number))
 +
g.add((b2, OWL.cardinality, Literal(1)))
 +
 
 +
Collection(g, b3, [ex.Person, b1, b2])
 +
g.add((ex.Student, OWL.intersectionOf, b3))
 +
 
 +
#a course is either a bachelor, a master or a Ph.D course
 +
bi = BNode()
 +
Collection(g, bi, [ex.Bachelor_course, ex.Master_course, ex["Ph.D_course"]])
 +
b1 = BNode()
 +
#g.add((b1, RDF.type, OWL.Class))
 +
g.add((b1, OWL.oneOf, bi))
 +
 
 +
g.add((ex.Course, RDF.type, b1))
 +
 
 +
#a bachelor student takes only bachelor courses
 +
g.add((ex.Bachelor_student, RDFS.subClassOf, ex.Student))
 +
b1 = BNode()
 +
g.add((b1, RDF.type, OWL.Restriction))
 +
g.add((b1, OWL.onProperty, ex.hasCourse))
 +
g.add((b1, OWL.allValuesFrom, ex.Bachelor_course))
 +
 
 +
b2 = BNode()
 +
Collection(g, b2, [ex.Student, b1])
 +
g.add((ex.Bachelor_student, OWL.intersectionOf, b2))
 +
 
 +
#a masters student takes only master courses and at most one bachelor course
 +
 
 +
b1 = BNode()
 +
g.add((b1, RDF.type, OWL.Restriction))
 +
g.add((b1, OWL.onProperty, ex.hasCourse))
 +
g.add((b1, OWL.maxQualifiedCardinality, Literal(1)))
 +
g.add((b1, OWL.onClass, ex.Bachelor_course))
 +
 
 +
b2 = BNode()
 +
g.add((b2, RDF.type, OWL.Restriction))
 +
g.add((b2, OWL.onProperty, ex.hasCourse))
 +
g.add((b2, OWL.someValuesFrom, ex.Master_course))
 +
 
 +
b3 = BNode()
 +
Collection(g, b3, [ex.Master_course, ex.Bachelor_course])
 +
 
 +
b5 = BNode()
 +
g.add((b5, RDF.type, OWL.Restriction))
 +
g.add((b5, OWL.onProperty, ex.hasCourse))
 +
g.add((b5, OWL.allValuesFrom, b3))
 +
 
 +
b6 = BNode()
 +
Collection(g, b6, [ex.Student, b1, b2, b5])
 +
g.add((ex.Master_student, OWL.intersectionOf, b6))
 +
 
 +
#a Ph.D student takes only Ph.D and at most two masters courses
 +
b1 = BNode()
 +
g.add((b1, RDF.type, OWL.Restriction))
 +
g.add((b1, OWL.onProperty, ex.hasCourse))
 +
g.add((b1, OWL.maxQualifiedCardinality, Literal(2)))
 +
g.add((b1, OWL.onClass, ex.Master_course))
 +
 
 +
b2 = BNode()
 +
g.add((b2, RDF.type, OWL.Restriction))
 +
g.add((b2, OWL.onProperty, ex.hasCourse))
 +
g.add((b2, OWL.someValuesFrom, ex["Ph.D_course"]))
 +
 
 +
b3 = BNode()
 +
Collection(g, b3, [ex.Master_course, ex["Ph.D_course"]])
 +
 
 +
b5 = BNode()
 +
g.add((b5, RDF.type, OWL.Restriction))
 +
g.add((b5, OWL.onProperty, ex.hasCourse))
 +
g.add((b5, OWL.allValuesFrom, b3))
 +
 
 +
b6 = BNode()
 +
Collection(g, b6, [ex.Student, b1, b2, b5])
 +
g.add((ex["Ph.D_student"], OWL.intersectionOf, b6))
 +
#a Ph.D. student cannot take a bachelor course
 +
    #NA, it's already true
 +
</syntaxhighlight>
 +
 
 +
==Lab 11: Semantic Lifting - HTML==
 +
 
 +
<syntaxhighlight>
 +
from bs4 import BeautifulSoup as bs
 +
from rdflib import Graph, Literal, URIRef, Namespace
 +
from rdflib.namespace import RDF, SKOS, XSD
 +
import requests
 +
 
 +
 
 +
g = Graph()
 +
ex = Namespace("http://example.org/")
 +
g.bind("ex", ex)
 +
 
 +
# Download html from URL and parse it with BeautifulSoup.
 +
url = "https://www.semanticscholar.org/topic/Knowledge-Graph/159858"
 +
page = requests.get(url)
 +
html = bs(page.content, features="html.parser")
 +
# print(html.prettify())
 +
 
 +
# Find the html that surrounds all the papers
 +
papers = html.find_all('div', attrs={'class': 'flex-container'})
 +
# Find the html that surrounds the info box
 +
topic = html.find_all(
 +
    'div', attrs={'class': 'flex-item__left-column entity-header'})
 +
 
 +
 
 +
# Iterate through each paper to make triples:
 +
for paper in papers:
 +
    # e.g selecting title.
 +
    title = paper.find('div', attrs={'class': 'timeline-paper-title'}).text
 +
    author = paper.find('span', attrs={'class': 'author-list'}).text
 +
    papper_year = paper.find(
 +
        'li', attrs={'data-selenium-selector': "paper-year"}).text
 +
    corpus_ID = paper.find(
 +
        'li', attrs={'data-selenium-selector': "corpus-id"}).text
 +
    corpus_ID = corpus_ID.replace(" ", "_")
 +
    c_id = corpus_ID.replace("Corpus_ID:_", "")
 +
 
 +
    article = URIRef(ex + c_id)
 +
 
 +
    # Adding tripels
 +
    g.add((article, RDF.type, ex.paper))
 +
    g.add((article, ex.HasID, Literal(c_id, datatype=XSD.int)))
 +
    g.add((article, ex.HasTitle, Literal(title, datatype=XSD.string)))
 +
    g.add((article, ex.Publisher_year, Literal(papper_year, datatype=XSD.year)))
 +
 
 +
    author = author.split(", ")
 +
    for x in author:
 +
        name = x.replace(" ", "_")
 +
        name = URIRef(ex + name)
 +
 
 +
        g.add((article, ex.hasAuthor, name))
 +
 
 +
# Iterate through the info box to make triples:
 +
    for items in topic:
 +
        main_topic = items.find('h1', attrs={'class': 'entity-name'}).text
 +
        related_topic = items.find(
 +
            'div', attrs={'class': 'entity-aliases'}).text
 +
        related_topic = related_topic.replace("Known as: ", "")
 +
        related_topic = related_topic.replace(f'\xa0Expand', "")
 +
        related_topic = related_topic.replace(" ", "")
 +
        main_topic = main_topic.replace(" ", "_")
 +
 
 +
        main_topic = URIRef(ex + main_topic)
 +
 
 +
        g.add((article, RDF.type, SKOS.Concept))
 +
        g.add((article, SKOS.hasTopConcept, main_topic))
 +
 
 +
    related_topic = related_topic.split(',')
 +
 
 +
    for related_labels in related_topic:
 +
        related_topic = URIRef(ex + related_labels)
 +
        g.add((article, SKOS.broader, related_topic))
 +
 
 +
 
 +
print(g.serialize(format='turtle'))
 +
</syntaxhighlight>
 +
 
 +
==Owlready2==
 +
Martin's solution. NOTE: intead of using "is_a" to define classes like I have mostly done, use "equivalent_to" to make the resoner more powerful (and work at all in this case). 
 +
<syntaxhighlight>
 +
from owlready2 import *
 +
from rdflib import Graph, Namespace
 +
 
 +
BASE = 'http://info216.uib.no/owlready2-lab/'
 +
onto = get_ontology(BASE)
 +
 
 +
def clean_onto(onto):
 +
    with onto:
 +
        for ind in onto.individuals():
 +
            destroy_entity(ind)
 +
        for prop in onto.properties():
 +
            destroy_entity(prop)
 +
        for cls in onto.classes():
 +
            destroy_entity(cls)
 +
 
 +
def onto2graph(onto):
 +
    graph = Graph()
 +
    onto.save('temp_owlready2.nt', format='ntriples')
 +
    graph.parse('temp_owlready2.nt', format='ntriples')
 +
    return graph
 +
 
 +
def print_onto(onto):
 +
    g = onto2graph(onto)
 +
    g.bind('', Namespace(BASE))
 +
    print(g.serialize(format='ttl'))
 +
 
 +
clean_onto(onto)
 +
 
 +
# anyone who is a graduate has at least one degree
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Degree(Thing): pass
 +
    class hasDegree(Student >> Degree): pass
 +
    class Graduate(Student):
 +
        is_a = [hasDegree.some(Degree)]
 +
 
 +
#anyone who is a university graduate has at least one degree from a university
 +
with onto:
 +
    class UniversityDegree(Degree): pass
 +
    class UniversityGraduate(Graduate):
 +
        is_a = [hasDegree.some(UniversityDegree)]
 +
 
 +
 
 +
#a grade is either an A, B, C, D, E or F
 +
with onto:
 +
    class Grade(Thing): pass
 +
    class A(Grade): pass
 +
    class B(Grade): pass
 +
    class C(Grade): pass
 +
    class D(Grade): pass
 +
    class E(Grade): pass
 +
    class F(Grade): pass
 +
 
 +
Grade.is_a.append(OneOf([A, B, C, D, E, F]))
 +
 
 +
#a straight A student is a student that has only A grades
 +
with onto:
 +
    class hasGrade(Student >> Grade): pass
 +
    class StraightAStudent(Student):
 +
        is_a = [hasGrade.only(A)]
 +
 
 +
 
 +
#a graduate has no F grades
 +
#Graduate.is_a.append(hasGrade.only(OneOf[A,B,C,D,E]))
 +
 
 +
#a student has a unique student number
 +
with onto:
 +
    class StudentNumber(Thing):pass
 +
    class hasStudentNumber(Student >> StudentNumber, FunctionalProperty, InverseFunctionalProperty):pass
 +
 
 +
#each student has exactly one average grade
 +
with onto:
 +
    class AverageGrade(Grade):pass
 +
    class hasAverageGrade(Student >> AverageGrade):pass
 +
Student.is_a.append(hasAverageGrade.exactly(1,AverageGrade))
 +
Student.is_a.append(hasStudentNumber.exactly(1,StudentNumber))
 +
   
 +
 
 +
#a course is either a bachelor, a master or a Ph.D course
 +
with onto:
 +
    class Course(Thing):pass
 +
    class BachelorCourse(Course):pass
 +
    class MasterCourse(Course):pass
 +
    class PhDCourse(Course):pass
 +
   
 +
Course.is_a.append(OneOf([BachelorCourse, MasterCourse, PhDCourse]))
 +
 
 +
#a bachelor student takes only bachelor courses
 +
with onto:
 +
    class takesCourse(Student>>Course):pass
 +
    class BachelorStudent(Student):
 +
        is_a = [
 +
            takesCourse.only(BachelorCourse) &
 +
            takesCourse.some(Course)
 +
        ]
 +
       
 +
 
 +
#a masters student takes only master courses and at most one bachelor course
 +
with onto:
 +
    class MasterOrBachelorCourse(Course):pass
 +
    class MasterStudent(Student):
 +
        is_a = [
 +
            takesCourse.only(Not(PhDCourse)) &
 +
            takesCourse.max(1,BachelorCourse) &
 +
            takesCourse.some(MasterCourse)
 +
            ]
 +
 
 +
 
 +
#a Ph.D student takes only Ph.D and at most two masters courses
 +
with onto:
 +
    class PhDStudent(Student):
 +
        is_a = [
 +
            takesCourse.only(Not(BachelorCourse))&
 +
            takesCourse.max(2,MasterCourse)&
 +
            takesCourse.some(PhDCourse)
 +
            ]
 +
 
 +
# In comparison to lab 10..
 +
"""
 +
b1 = BNode()
 +
g.add((b1, RDF.type, OWL.Restriction))
 +
g.add((b1, OWL.onProperty, ex.hasCourse))
 +
g.add((b1, OWL.maxQualifiedCardinality, Literal(2)))
 +
g.add((b1, OWL.onClass, ex.Master_course))
 +
 
 +
b2 = BNode()
 +
g.add((b2, RDF.type, OWL.Restriction))
 +
g.add((b2, OWL.onProperty, ex.hasCourse))
 +
g.add((b2, OWL.someValuesFrom, ex["Ph.D_course"]))
 +
 
 +
b3 = BNode()
 +
Collection(g, b3, [ex.Master_course, ex["Ph.D_course"]])
 +
 
 +
b5 = BNode()
 +
g.add((b5, RDF.type, OWL.Restriction))
 +
g.add((b5, OWL.onProperty, ex.hasCourse))
 +
g.add((b5, OWL.allValuesFrom, b3))
 +
 
 +
b6 = BNode()
 +
Collection(g, b6, [ex.Student, b1, b2, b5])
 +
g.add((ex["Ph.D_student"], OWL.intersectionOf, b6))
 +
"""
 +
#a Ph.D. student cannot take a bachelor course
 +
    #NA, it's already true
 +
 
 +
#print(onto2graph(onto).serialize(format="turtle"))
 +
 
 +
clean_onto(onto)
 +
 
 +
# anyone who is a graduate has at least one degree
 +
# a graduate is a student with at least one degree
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Degree(Thing): pass
 +
    class hasDegree(Student >> Degree): pass
 +
    class Graduate(Student):
 +
        equivalent_to = [hasDegree.some(Degree)]
 +
 
 +
# test with individual
 +
with onto:
 +
    cade = Student()
 +
    infosci = Degree()
 +
    cade.hasDegree.append(infosci)
 +
from owlready2 import sync_reasoner
 +
 
 +
print(onto.Graduate in cade.is_a)
 +
sync_reasoner()
 +
print(onto.Graduate in cade.is_a)
 +
 
 +
 
 +
print("graduate is: ", Graduate.is_a)
 +
print("cade is: ", cade.is_a)
 +
 
 +
</syntaxhighlight>
 +
 
 +
Alternative solution. More pro from Andreas, but it's a not so thoroughly tested draft for us teacher assistents he stresses (it's new material for us), so you might need to make some changes (like the one recommended above: equivalent_to instead of is_a).
 +
 
 +
<syntaxhighlight>
 +
from owlready2 import get_ontology, Thing, ObjectProperty
 +
from rdflib import Graph, Namespace
 +
 
 +
BASE = 'http://info216.uib.no/owlready2-lab/'
 +
onto = get_ontology(BASE)
 +
 
 +
def onto2graph(onto):
 +
    graph = Graph()
 +
    onto.save('temp.nt', format='ntriples')
 +
    graph.parse('temp.nt', format='ntriples')
 +
    return graph
 +
 
 +
def print_onto(onto):
 +
    g = onto2graph(onto)
 +
    g.bind('', Namespace(BASE))
 +
    print(g.serialize(format='ttl'))
 +
 
 +
from owlready2 import destroy_entity
 +
def clean_onto(onto):
 +
    with onto:
 +
        for ind in onto.individuals():
 +
            destroy_entity(ind)
 +
        for prop in onto.properties():
 +
            destroy_entity(prop)
 +
        for cls in onto.classes():
 +
            destroy_entity(cls)
 +
 
 +
# anyone who is a graduate has at least one degree
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Degree(Thing): pass
 +
    class hasDegree(Student >> Degree): pass
 +
    class Graduate(Student):
 +
        is_a = [hasDegree.some(Degree)]
 +
 
 +
# anyone who is a university graduate has at least one degree from a university
 +
with onto:
 +
    class hasDegree(ObjectProperty): pass
 +
    class degreeFrom(ObjectProperty): pass
 +
    class Degree(Thing): pass
 +
    class University(Thing): pass
 +
    class UniversityGraduate(Thing):
 +
        hasDegree: Degree
 +
        is_a = [hasDegree.some(Degree & degreeFrom.some(University))]
 +
print_onto(onto)
 +
 
 +
from owlready2 import declare_datatype
 +
class XSDString(object):
 +
    def __init__(self, value): self.value = value
 +
def str_parser(s): return s
 +
def str_unparser(s): return s
 +
declare_datatype(XSDString, 'http://www.w3.org/2001/XMLSchema#string', str_parser, str_unparser)
 +
 
 +
# a grade is either an A, B, C, D, E or F
 +
from owlready2 import OneOf
 +
with onto:
 +
    class Grade(Thing): pass
 +
    class charGrade(Grade >> XSDString): pass
 +
    grade_A = Grade()
 +
    grade_A.charGrade = ['A']
 +
    grade_B = Grade()
 +
    grade_B.charGrade = ['B']
 +
    grade_C = Grade()
 +
    grade_C.charGrade = ['C']
 +
    grade_D = Grade()
 +
    grade_D.charGrade = ['D']
 +
    grade_E = Grade()
 +
    grade_E.charGrade = ['E']
 +
    grade_F = Grade()
 +
    grade_F.charGrade = ['F']
 +
    Grade.equivalent_to.append(OneOf([
 +
        grade_A, grade_B, grade_C, grade_D, grade_E, grade_F
 +
    ]))
 +
print_onto(onto)
 +
 
 +
# a straight A student is a student that has only A grades
 +
with onto:
 +
    class Grade(Thing): pass
 +
    class charGrade(Grade >> XSDString): pass
 +
    grade_A = Grade()
 +
    grade_A.charGrade = ['A']
 +
    grade_B = Grade()
 +
    grade_B.charGrade = ['B']
 +
    # ...
 +
    Grade.equivalent_to.append(OneOf([
 +
        grade_A, grade_B,  # ...
 +
    ]))
 +
 
 +
    class Student(Thing): pass
 +
    class hasGrade(Student >> Grade): pass
 +
    class GradeA(Grade):
 +
        equivalent_to = [OneOf([grade_A])]
 +
    class StraightAStudent(Student):
 +
        equivalent_to = [
 +
            hasGrade.some(GradeA) & hasGrade.only(GradeA)
 +
        ]
 +
print_onto(onto)
 +
 
 +
# a graduate has no F grades
 +
with onto:
 +
    class Grade(Thing): pass
 +
    class charGrade(Grade >> XSDString): pass
 +
    grade_A = Grade()
 +
    grade_A.charGrade = ['A']
 +
    # ...
 +
    grade_F.charGrade = ['F']
 +
    Grade.equivalent_to.append(OneOf([
 +
        grade_A,  # ...
 +
        grade_F
 +
    ]))
 +
 
 +
    class Student(Thing): pass
 +
    class hasGrade(Student >> Grade): pass
 +
    class GradeF(Grade):
 +
        equivalent_to = [OneOf([grade_F])]
 +
    class Graduate(Student):
 +
        equivalent_to = [Student & ~ hasGrade.some(GradeF)]
 +
print_onto(onto)
 +
 
 +
# a student has a single unique student number
 +
class XSDInt(object):
 +
    def __init__(self, value): self.value = value
 +
def int_parser(s): return int(s)
 +
def int_unparser(i): return str(i)
 +
declare_datatype(XSDInt, 'http://www.w3.org/2001/XMLSchema#int', int_parser, int_unparser)
 +
 
 +
from owlready2 import FunctionalProperty, InverseFunctionalProperty
 +
with onto:
 +
    class Student(Thing): pass
 +
    class hasStudentNumber(Student >> XSDInt):
 +
        is_a = [FunctionalProperty, InverseFunctionalProperty]
 +
print_onto(onto)
 +
 
 +
# each student has exactly one average grade
 +
class XSDFloat(object):
 +
    def __init__(self, value): self.value = value
 +
def int_parser(s): return float(s)
 +
def int_unparser(f): return str(f)
 +
declare_datatype(XSDFloat, 'http://www.w3.org/2001/XMLSchema#float', int_parser, int_unparser)
 +
 
 +
with onto:
 +
    class Student(Thing): pass
 +
    class hasAverageGrade(Grade >> XSDFloat): pass
 +
    Student.is_a.append(hasAverageGrade.exactly(1, XSDFloat))
 +
print_onto(onto)
 +
 
 +
# a course is either a bachelor, a master or a Ph.D course
 +
from owlready2 import AllDisjoint
 +
with onto:
 +
    class Course(Thing): pass
 +
    class BachelorCourse(Course): pass
 +
    class MasterCourse(Course): pass
 +
    class PhDCourse(Course): pass
 +
    AllDisjoint([BachelorCourse, MasterCourse, PhDCourse])
 +
print_onto(onto)
 +
 
 +
# a bachelor student takes only bachelor courses
 +
from owlready2 import AllDisjoint
 +
with onto:
 +
    class Course(Thing): pass
 +
    class BachelorCourse(Course): pass
 +
    class MasterCourse(Course): pass
 +
    class PhDCourse(Course): pass
 +
    AllDisjoint([BachelorCourse, MasterCourse, PhDCourse])
 +
print_onto(onto)
 +
 
 +
# a masters student takes only master courses, except for at most one bachelor course
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Course(Thing): pass
 +
    class takesCourse(Student >> Course): pass
 +
    class BachelorCourse(Course): pass
 +
    class MasterCourse(Course): pass
 +
    class MasterStudent(Student):
 +
        is_a = [
 +
            takesCourse.some(MasterCourse) &
 +
            takesCourse.only(MasterCourse | BachelorCourse) &
 +
            takesCourse.max(1, BachelorCourse)
 +
        ]
 +
print_onto(onto)
 +
 
 +
# a Ph.D student takes only Ph.D courses, except for at most two masters courses
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Course(Thing): pass
 +
    class takesCourse(Student >> Course): pass
 +
    class MasterCourse(Course): pass
 +
    class PhDCourse(Course): pass
 +
    class PhDStudent(Student):
 +
        is_a = [
 +
            takesCourse.some(PhDCourse) &
 +
            takesCourse.only(PhDCourse | MasterCourse) &
 +
            takesCourse.max(2, MasterCourse)
 +
        ]
 +
print_onto(onto)
 +
 
 +
# a Ph.D. student cannot take a bachelor course
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Course(Thing): pass
 +
    class takesCourse(Student >> Course): pass
 +
    class BachelorCourse(Course): pass
 +
    class PhDStudent(Student):
 +
        is_a = [
 +
            takesCourse.max(0, BachelorCourse)
 +
        ]
 +
print_onto(onto)
 +
 
 +
# ...alternative solution
 +
clean_onto(onto)
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Course(Thing): pass
 +
    class takesCourse(Student >> Course): pass
 +
    class BachelorCourse(Course): pass
 +
    class PhDStudent(Student):
 +
        is_a = [Student & ~ takesCourse.some(BachelorCourse)]
 +
print_onto(onto)
 +
 
 +
# a graduate is a student with at least one degree
 +
with onto:
 +
    class Student(Thing): pass
 +
    class Degree(Thing): pass
 +
    class hasDegree(Student >> Degree): pass
 +
    class Graduate(Student):
 +
        equivalent_to = [Student & hasDegree.some(Degree)]
 +
 
 +
# test with individual
 +
with onto:
 +
    cade = Student()
 +
    infosci = Degree()
 +
    cade.hasDegree.append(infosci)
 +
 
 +
from owlready2 import sync_reasoner
 +
 
 +
print(onto.Graduate in cade.is_a)
 +
sync_reasoner()
 +
print(onto.Graduate in cade.is_a)
 +
 
 +
# if you have more time:
 +
# populate the ontology with individuals
 +
# a straight A student is a student that has only A grades
 +
clean_onto(onto)
 +
with onto:
 +
    class Grade(Thing): pass
 +
    class charGrade(Grade >> XSDString): pass
 +
    grade_A = Grade()
 +
    grade_A.charGrade = ['A']
 +
    grade_B = Grade()
 +
    grade_B.charGrade = ['B']
 +
    # ...
 +
    Grade.equivalent_to.append(OneOf([
 +
        grade_A, grade_B,  # ...
 +
    ]))
 +
 
 +
    class Student(Thing): pass
 +
    class hasGrade(Student >> Grade): pass
 +
    class GradeA(Grade):
 +
        equivalent_to = [OneOf([grade_A])]
 +
    class StraightAStudent(Student):
 +
        equivalent_to = [
 +
            Student &
 +
            hasGrade.some(GradeA) & hasGrade.only(GradeA)
 +
        ]
 +
    # add individual
 +
    cade = Student()
 +
    cade.hasGrade.append(grade_A)
 +
print_onto(onto)
 +
 
 +
from owlready2 import sync_reasoner
 +
print(onto.StraightAStudent in cade.is_a)
 +
sync_reasoner()
 +
print(onto.StraightAStudent in cade.is_a)
 +
 
 +
from owlready2 import close_world
 +
close_world(onto)  # because of the "only"-restriction
 +
sync_reasoner()
 +
print(onto.StraightAStudent in cade.is_a)
 +
</syntaxhighlight>
 +
 
 +
==Knowledge graph embeddings==
 +
https://colab.research.google.com/drive/1sHusTjvmHtV6PkzIatLTMPzHuseAF6N1?usp=sharing
 +
 
 +
 
 +
=More miscellaneous examples=
  
  
Line 8: Line 1,456:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
# The turtle format has the purpose of being more readable for humans.  
 
# The turtle format has the purpose of being more readable for humans.  
print(g.serialize(format="turtle").decode())
+
print(g.serialize(format="turtle"))
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 33: Line 1,481:
 
g.add((ex.Mary, RDF.type, FOAF.Person))
 
g.add((ex.Mary, RDF.type, FOAF.Person))
 
g.add((ex.Cade, RDF.type, FOAF.Person))
 
g.add((ex.Cade, RDF.type, FOAF.Person))
 +
 +
# OR
 +
 +
g = Graph()
 +
 +
ex = Namespace('http://example.org/')
 +
 +
g.add((ex.Cade, FOAF.name, Literal("Cade", datatype=XSD.string)))
 +
g.add((ex.Mary, FOAF.name, Literal("Mary", datatype=XSD.string)))
 +
g.add((ex.Cade, RDF.type, FOAF.Person))
 +
g.add((ex.Mary, RDF.type, FOAF.Person))
 +
g.add((ex.Mary, RDF.type, ex.Student))
 +
g.add((ex.Cade, ex.Married, ex.Mary))
 +
g.add((ex.Cade, FOAF.age, Literal('27', datatype=XSD.int)))
 +
g.add((ex.Mary, FOAF.age, Literal('26', datatype=XSD.int)))
 +
g.add((ex.Paris, RDF.type, ex.City))
 +
g.add((ex.France, ex.Capital, ex.Paris))
 +
g.add((ex.Mary, FOAF.interest, ex.hiking))
 +
g.add((ex.Mary, FOAF.interest, ex.Chocolate))
 +
g.add((ex.Mary, FOAF.interest, ex.biology))
 +
g.add((ex.France, ex.City, ex.Paris))
 +
g.add((ex.Mary, ex.characteristic, ex.kind))
 +
g.add((ex.Cade, ex.characteristic, ex.kind))
 +
g.add((ex.France, RDF.type, ex.Country))
 +
 +
 +
print(g.serialize(format="turtle"))
  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Lecture 2: RDF programming==
+
==Basic RDF programming==
  
 
===Different ways to create an address===
 
===Different ways to create an address===
Line 110: Line 1,585:
 
# Also using existing ontology for places like California. (like http://dbpedia.org/resource/California from dbpedia.org)
 
# Also using existing ontology for places like California. (like http://dbpedia.org/resource/California from dbpedia.org)
  
schema = "https://schema.org/"
+
schema = Namespace("https://schema.org/")
dbp = "https://dpbedia.org/resource/"
+
dbp = Namespace("https://dpbedia.org/resource/")
  
 
g.add((ex.Cade_Tracey, schema.address, ex.CadeAddress))
 
g.add((ex.Cade_Tracey, schema.address, ex.CadeAddress))
Line 150: Line 1,625:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
===Graph Binding===
 +
<syntaxhighlight>
 +
#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)
 +
</syntaxhighlight>
  
 
===Collection Example===
 
===Collection Example===
Line 175: Line 1,667:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Lecture 3: SPARQL==
+
==SPARQL==
  
===SPARQL queries from the lecture===
+
Also see the [[SPARQL Examples]] page!
<syntaxhighlight>
+
 
SELECT DISTINCT ?p WHERE {
+
===Querying a local ("in memory") graph===
    ?s ?p ?o .
 
}
 
</syntaxhighlight>
 
  
<syntaxhighlight>
+
Example contents of the file family.ttl:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+
@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 .
  
SELECT DISTINCT ?t WHERE {
+
import rdflib
    ?s rdf:type ?t .
+
}
+
g = rdflib.Graph()
</syntaxhighlight>
+
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)
  
<syntaxhighlight>
+
With a prepared query, you can write the query once, and then bind some of the variables each time you use it:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
+
import rdflib
CONSTRUCT {  
+
    ?s owl:sameAs ?o2 .  
+
g = rdflib.Graph()
} WHERE {
+
g.parse("family.ttl", format='ttl')
    ?s owl:sameAs ?o .
+
    FILTER(REGEX(STR(?o), "^http://www\\.", "s"))
+
q = rdflib.plugins.sparql.prepareQuery(
    BIND(URI(REPLACE(STR(?o), "^http://www\\.", "http://", "s")) AS ?o2)
+
        """SELECT ?child ?sister WHERE {
}
+
                  ?child fam:hasParent ?parent .
</syntaxhighlight>
+
                  ?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)===
 
===Select all contents of lists (rdfllib.Collection)===
Line 217: Line 1,729:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Lecture 4- SPARQL PROGRAMMING==
+
 
 +
===Using parameters/variables in rdflib queries===
 +
 
 +
<syntaxhighlight>
 +
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")
 +
</syntaxhighlight>
  
 
===SELECTING data from Blazegraph via Python===
 
===SELECTING data from Blazegraph via Python===
Line 276: Line 1,822:
  
  
 +
</syntaxhighlight>
 +
===Retrieving data from Wikidata with SparqlWrapper===
 +
<syntaxhighlight>
 +
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"])
 +
</syntaxhighlight>
 +
 +
 +
More examples can be found in the example section on the official query service here: https://query.wikidata.org/.
 +
 +
===Download from BlazeGraph===
 +
 +
<syntaxhighlight>
 +
"""
 +
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 = 'http://info216.i2s.uib.no/bigdata/namespace/' + ns + '/sparql'
 +
 +
# - the endpoint just moved, the old one was:
 +
# endpoint = 'http://i2s.uib.no:8888/bigdata/namespace/' + ns + '/sparql'
 +
 +
# create wrapper
 +
wrapper = SPARQLWrapper(endpoint)
 +
 +
# prepare the SPARQL update
 +
wrapper.setQuery('CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }')
 +
wrapper.setReturnFormat(RDFXML)
 +
 +
# 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-' + datetime.datetime.now().strftime('%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))
 +
</syntaxhighlight>
 +
 +
===Query Dbpedia with SparqlWrapper===
 +
 +
<syntaxhighlight>
 +
from SPARQLWrapper import SPARQLWrapper, JSON
 +
 +
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
 +
 +
sparql.setQuery("""
 +
    PREFIX dbr: <http://dbpedia.org/resource/>
 +
    PREFIX dbo: <http://dbpedia.org/ontology/>
 +
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 +
    SELECT ?comment
 +
    WHERE {
 +
    dbr:Barack_Obama rdfs:comment ?comment.
 +
    FILTER (langMatches(lang(?comment),"en"))
 +
    }
 +
""")
 +
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
 +
for result in results["results"]["bindings"]:
 +
    print(result["comment"]["value"])
 +
</syntaxhighlight>
 +
 +
==Lifting CSV to RDF==
 +
 +
<syntaxhighlight>
 +
from rdflib import Graph, Literal, Namespace, URIRef
 +
from rdflib.namespace import RDF, FOAF, RDFS, OWL
 +
import pandas as pd
 +
 +
g = Graph()
 +
ex = Namespace("http://example.org/")
 +
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("http://example.org/unknown")))
 +
 +
# Clean printing of the graph.
 +
print(g.serialize(format="turtle").decode())
 +
</syntaxhighlight>
 +
 +
===CSV file for above example===
 +
 +
<syntaxhighlight>
 +
"Name","Age","Spouse","Country"
 +
"Cade Tracey","26","Mary Jackson","US"
 +
"Bob Johnson","21","","Canada"
 +
"Mary Jackson","25","","France"
 +
"Phil Philips","32","Catherine Smith","Japan"
 +
</syntaxhighlight>
 +
 +
 +
=Coding Tasks Lab 6=
 +
<syntaxhighlight>
 +
import pandas as pd
 +
 +
 +
from rdflib import Graph, Namespace, URIRef, Literal, BNode
 +
from rdflib.namespace import RDF, XSD
 +
 +
 +
ex = Namespace("http://example.org/")
 +
sem = Namespace("http://semanticweb.cs.vu.nl/2009/11/sem/")
 +
 +
g = Graph()
 +
g.bind("ex", ex)
 +
g.bind("sem", sem)
 +
 +
 +
# Removing unwanted characters
 +
df = pd.read_csv('russia-investigation.csv')
 +
# Here I deal with spaces (" ") in the data. I replace them with "_" so that URI's become valid.
 +
df = df.replace(to_replace=" ", value="_", regex=True)
 +
# This may seem odd, but in the data set we have a name like this:("Scooter"). So we have to remove quotation marks
 +
df = df.replace(to_replace=f'"', value="", regex=True)
 +
# # Here I mark all missing/empty data as "unknown". This makes it easy to delete triples containing this later.
 +
df = df.fillna("unknown")
 +
 +
# Loop through the CSV data, and then make RDF triples.
 +
for index, row in df.iterrows():
 +
    name = row['investigation']
 +
    investigation = URIRef(ex + name)
 +
    g.add((investigation, RDF.type, sem.Event))
 +
    investigation_start = row["investigation-start"]
 +
    g.add((investigation, sem.hasBeginTimeStamp, Literal(
 +
        investigation_start, datatype=XSD.datetime)))
 +
    investigation_end = row["investigation-end"]
 +
    g.add((investigation, sem.hasEndTimeStamp, Literal(
 +
        investigation_end, datatype=XSD.datetime)))
 +
    investigation_end = row["investigation-days"]
 +
    g.add((investigation, sem.hasXSDDuration, Literal(
 +
        investigation_end, datatype=XSD.Days)))
 +
    person = row["name"]
 +
    person = URIRef(ex + person)
 +
    g.add((investigation, sem.Actor, person))
 +
    result = row['type']
 +
    g.add((investigation, sem.hasSubEvent, Literal(result, datatype=XSD.string)))
 +
    overturned = row["overturned"]
 +
    g.add((investigation, ex.overtuned, Literal(overturned, datatype=XSD.boolean)))
 +
    pardoned = row["pardoned"]
 +
    g.add((investigation, ex.pardon, Literal(pardoned, datatype=XSD.boolean)))
 +
 +
g.serialize("output.ttl", format="ttl")
 +
print(g.serialize(format="turtle"))
 +
 +
 +
</syntaxhighlight>
 +
 +
==RDFS==
 +
 +
===RDFS-plus (OWL) Properties===
 +
<syntaxhighlight>
 +
g.add((ex.married, RDF.type, OWL.SymmetricProperty))
 +
g.add((ex.married, RDF.type, OWL.IrreflexiveProperty))
 +
g.add((ex.livesWith, RDF.type, OWL.ReflexiveProperty))
 +
g.add((ex.livesWith, RDF.type, OWL.SymmetricProperty))
 +
g.add((ex.sibling, RDF.type, OWL.TransitiveProperty))
 +
g.add((ex.sibling, RDF.type, OWL.SymmetricProperty))
 +
g.add((ex.sibling, RDF.type, OWL.IrreflexiveProperty))
 +
g.add((ex.hasFather, RDF.type, OWL.FunctionalProperty))
 +
g.add((ex.hasFather, RDF.type, OWL.AsymmetricProperty))
 +
g.add((ex.hasFather, RDF.type, OWL.IrreflexiveProperty))
 +
g.add((ex.fatherOf, RDF.type, OWL.AsymmetricProperty))
 +
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))
 +
 +
# Sometimes there is no definite answer, and it comes down to how we want to model our properties
 +
# e.g is livesWith a transitive property? Usually yes, but we can also want to specify that a child lives with both of her divorced parents.
 +
# which means that: (mother livesWith child % child livesWith father) != mother livesWith father. Which makes it non-transitive.
 +
</syntaxhighlight>
 +
 +
<!--
 +
==Lifting XML to RDF==
 +
<syntaxhighlight>
 +
from rdflib import Graph, Literal, Namespace, URIRef
 +
from rdflib.namespace import RDF, XSD, RDFS
 +
import xml.etree.ElementTree as ET
 +
 +
g = Graph()
 +
ex = Namespace("http://example.org/TV/")
 +
prov = Namespace("http://www.w3.org/ns/prov#")
 +
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 + show_id), ex.stars, URIRef(ex + full_name)))
 +
        g.add((URIRef(ex + full_name), ex.starsIn, URIRef(title)))
 +
        g.add((URIRef(ex + full_name), RDF.type, ex.Actor))
 +
 +
print(g.serialize(format="turtle").decode())
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Lecture 5: RDFS==
+
 
 +
 
  
 
===RDFS inference with RDFLib===
 
===RDFS inference with RDFLib===
You can use the OWL-RL package to add inference capabilities to RDFLib. Download it [https://github.com/RDFLib/OWL-RL GitHub] and copy the ''owlrl'' subfolder into your project folder next to your Python files.
+
You can use the OWL-RL package to add inference capabilities to RDFLib. It can be installed using the pip install command:
 +
<syntaxhighlight>
 +
pip install owlrl
 +
</syntaxhighlight>
 +
Or download it from [https://github.com/RDFLib/OWL-RL GitHub] and copy the ''owlrl'' subfolder into your project folder next to your Python files.
  
 
[https://owl-rl.readthedocs.io/en/latest/owlrl.html OWL-RL documentation.]
 
[https://owl-rl.readthedocs.io/en/latest/owlrl.html OWL-RL documentation.]
  
Example program to get started:
+
Example program to get you started. In this example we are creating the graph using sparql.update, but it is also possible to parse the data from a file.
 
<syntaxhighlight>
 
<syntaxhighlight>
 
import rdflib.plugins.sparql.update
 
import rdflib.plugins.sparql.update
Line 304: Line 2,107:
 
}""")
 
}""")
  
# The next three lines add inferred triples to g.
 
 
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
 
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
 +
# RDF_Semantics parameters:
 +
# - graph (rdflib.Graph) – The RDF graph to be extended.
 +
# - axioms (bool) – Whether (non-datatype) axiomatic triples should be added or not.
 +
# - daxioms (bool) – Whether datatype axiomatic triples should be added or not.
 +
# - rdfs (bool) – Whether RDFS inference is also done (used in subclassed only).
 +
# For now, you will in most cases use all False in RDFS_Semtantics.
 +
 +
# Generates the closure of the graph - generates the new entailed triples, but does not add them to the graph.
 
rdfs.closure()
 
rdfs.closure()
 +
# Adds the new triples to the graph and empties the RDFS triple-container.
 
rdfs.flush_stored_triples()
 
rdfs.flush_stored_triples()
  
 +
# Ask-query to check whether a new triple has been generated from the entailment.
 
b = g.query("""
 
b = g.query("""
 
PREFIX ex: <http://example.org#>
 
PREFIX ex: <http://example.org#>
Line 318: Line 2,130:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Languaged tagged RDFS labels===  
+
===Language tagged RDFS labels===  
 
<syntaxhighlight>
 
<syntaxhighlight>
 
from rdflib import Graph, Namespace, Literal
 
from rdflib import Graph, Namespace, Literal
Line 333: Line 2,145:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Lecture 6: RDFS Plus / OWL ==  
+
==OWL==  
===RDFS Plus / OWL inference with RDFLib===  
+
===Basic inference with RDFLib===  
  
 
You can use the OWL-RL package again as for Lecture 5.
 
You can use the OWL-RL package again as for Lecture 5.
Line 396: Line 2,208:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Lab 9 ==
 
 
===Download from BlazeGraph===
 
 
<syntaxhighlight>
 
"""
 
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 = 'http://info216.i2s.uib.no/bigdata/namespace/' + ns + '/sparql'
 
  
# - the endpoint just moved, the old one was:
 
# endpoint = 'http://i2s.uib.no:8888/bigdata/namespace/' + ns + '/sparql'
 
  
# create wrapper
 
wrapper = SPARQLWrapper(endpoint)
 
  
# prepare the SPARQL update
+
===XML Data for above example===
wrapper.setQuery('CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }')
+
<syntaxhighlight>
wrapper.setReturnFormat(RDFXML)
+
<data>
 
+
    <tv_show id="1050">
# execute the SPARQL update and convert the result to an rdflib.Graph
+
        <title>The_Sopranos</title>
graph = wrapper.query().convert()
+
        <actor>
 
+
            <firstname>James</firstname>
# the destination file, with code to make it timestamped
+
            <lastname>Gandolfini</lastname>
destfile = 'rdf_dumps/slr-kg4news-' + datetime.datetime.now().strftime('%Y%m%d-%H%M') + '.rdf'
+
        </actor>
 
+
    </tv_show>
# serialize the result to file
+
    <tv_show id="1066">
graph.serialize(destination=destfile, format='ttl')
+
        <title>Seinfeld</title>
 
+
        <actor>
# report and quit
+
            <firstname>Jerry</firstname>
print('Wrote %u triples to file %s .' %
+
            <lastname>Seinfeld</lastname>
      (len(res), destfile))
+
        </actor>
 +
        <actor>
 +
            <firstname>Julia</firstname>
 +
            <lastname>Louis-dreyfus</lastname>
 +
        </actor>
 +
        <actor>
 +
            <firstname>Jason</firstname>
 +
            <lastname>Alexander</lastname>
 +
        </actor>
 +
    </tv_show>
 +
</data>
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Semantic Lifting of CSV==
+
==Lifting HTML to RDF==
 
 
 
<syntaxhighlight>
 
<syntaxhighlight>
from rdflib import Graph, Literal, Namespace, URIRef
+
from bs4 import BeautifulSoup as bs, NavigableString
from rdflib.namespace import RDF, FOAF, RDFS, OWL
+
from rdflib import Graph, URIRef, Namespace
import pandas as pd
+
from rdflib.namespace import RDF
  
 
g = Graph()
 
g = Graph()
Line 450: Line 2,251:
 
g.bind("ex", ex)
 
g.bind("ex", ex)
  
# Load the CSV data as a pandas Dataframe.
+
html = open("tv_shows.html").read()
csv_data = pd.read_csv("task1.csv")
+
html = bs(html, features="html.parser")
 
 
# 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.
+
shows = html.find_all('li', attrs={'class': 'show'})
for index, row in csv_data.iterrows():
+
for show in shows:
     # The names of the people act as subjects.
+
    title = show.find("h3").text
    subject = row['Name']
+
     actors = show.find('ul', attrs={'class': 'actor_list'})
     # Create triples: e.g. "Cade_Tracey - age - 27"
+
     for actor in actors:
    g.add((URIRef(ex + subject), URIRef(ex + "age"), Literal(row["Age"])))
+
        if isinstance(actor, NavigableString):
    g.add((URIRef(ex + subject), URIRef(ex + "married"), URIRef(ex + row["Spouse"])))
+
            continue
    g.add((URIRef(ex + subject), URIRef(ex + "country"), URIRef(ex + row["Country"])))
+
        else:
 +
            actor = actor.text.replace(" ", "_")
 +
            g.add((URIRef(ex + title), ex.stars, URIRef(ex + actor)))
 +
            g.add((URIRef(ex + actor), RDF.type, ex.Actor))
  
    # If We want can add additional RDF/RDFS/OWL information e.g
+
     g.add((URIRef(ex + title), RDF.type, ex.TV_Show))
     g.add((URIRef(ex + subject), RDF.type, FOAF.Person))
 
  
# I remove triples that I marked as unknown earlier.
 
g.remove((None, None, URIRef("http://example.org/unknown")))
 
  
# Clean printing of the graph.
 
 
print(g.serialize(format="turtle").decode())
 
print(g.serialize(format="turtle").decode())
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===CSV file for above example===
+
===HTML code for the example above===
 
 
 
<syntaxhighlight>
 
<syntaxhighlight>
"Name","Age","Spouse","Country"
+
<!DOCTYPE html>
"Cade Tracey","26","Mary Jackson","US"
+
<html>
"Bob Johnson","21","","Canada"
+
<head>
"Mary Jackson","25","","France"
+
    <meta charset="utf-8">
"Phil Philips","32","Catherine Smith","Japan"
+
    <title></title>
 +
</head>
 +
<body>
 +
    <div class="tv_shows">
 +
        <ul>
 +
            <li class="show">
 +
                <h3>The_Sopranos</h3>
 +
                <div class="irrelevant_data"></div>
 +
                <ul class="actor_list">
 +
                    <li>James Gandolfini</li>
 +
                </ul>
 +
            </li>
 +
            <li class="show">
 +
                <h3>Seinfeld</h3>
 +
                <div class="irrelevant_data"></div>
 +
                <ul class="actor_list">
 +
                    <li >Jerry Seinfeld</li>
 +
                    <li>Jason Alexander</li>
 +
                    <li>Julia Louis-Dreyfus</li>
 +
                </ul>
 +
            </li>
 +
        </ul>
 +
    </div>
 +
</body>
 +
</html>
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==WEB API Calls (In this case JSON)==
+
==Web APIs with JSON==
 +
<syntaxhighlight>
 
import requests
 
import requests
 
import json
 
import json
Line 498: Line 2,316:
 
data = json.loads(data)
 
data = json.loads(data)
 
pprint.pprint(data)
 
pprint.pprint(data)
 +
</syntaxhighlight>
  
  
Line 539: Line 2,358:
  
 
<div class="credits" style="text-align: right; direction: ltr; margin-left: 1em;">''INFO216, UiB, 2017-2020. All code examples are [https://creativecommons.org/choose/zero/ CC0].'' </div>
 
<div class="credits" style="text-align: right; direction: ltr; margin-left: 1em;">''INFO216, UiB, 2017-2020. All code examples are [https://creativecommons.org/choose/zero/ CC0].'' </div>
 +
 +
==OWL - Complex Classes and Restrictions==
 +
<syntaxhighlight>
 +
import owlrl
 +
from rdflib import Graph, Literal, Namespace, BNode
 +
from rdflib.namespace import RDF, OWL, RDFS
 +
from rdflib.collection import Collection
 +
 +
g = Graph()
 +
ex = Namespace("http://example.org/")
 +
g.bind("ex", ex)
 +
g.bind("owl", OWL)
 +
 +
# a Season is either Autumn, Winter, Spring, Summer
 +
seasons = BNode()
 +
Collection(g, seasons, [ex.Winter, ex.Autumn, ex.Spring, ex.Summer])
 +
g.add((ex.Season, OWL.oneOf, seasons))
 +
 +
# A Parent is a Father or Mother
 +
b = BNode()
 +
Collection(g, b, [ex.Father, ex.Mother])
 +
g.add((ex.Parent, OWL.unionOf, b))
 +
 +
# A Woman is a person who has the "female" gender
 +
br = BNode()
 +
g.add((br, RDF.type, OWL.Restriction))
 +
g.add((br, OWL.onProperty, ex.gender))
 +
g.add((br, OWL.hasValue, ex.Female))
 +
bi = BNode()
 +
Collection(g, bi, [ex.Person, br])
 +
g.add((ex.Woman, OWL.intersectionOf, bi))
 +
 +
# A vegetarian is a Person who only eats vegetarian food
 +
br = BNode()
 +
g.add((br, RDF.type, OWL.Restriction))
 +
g.add((br, OWL.onProperty, ex.eats))
 +
g.add((br, OWL.allValuesFrom, ex.VeganFood))
 +
bi = BNode()
 +
Collection(g, bi, [ex.Person, br])
 +
g.add((ex.Vegetarian, OWL.intersectionOf, bi))
 +
 +
# A vegetarian is a Person who can not eat meat.
 +
br = BNode()
 +
g.add((br, RDF.type, OWL.Restriction))
 +
g.add((br, OWL.onProperty, ex.eats))
 +
g.add((br, OWL.QualifiedCardinality, Literal(0)))
 +
g.add((br, OWL.onClass, ex.Meat))
 +
bi = BNode()
 +
Collection(g, bi, [ex.Person, br])
 +
g.add((ex.Vegetarian, OWL.intersectionOf, bi))
 +
 +
# A Worried Parent is a parent who has at least one sick child
 +
br = BNode()
 +
g.add((br, RDF.type, OWL.Restriction))
 +
g.add((br, OWL.onProperty, ex.hasChild))
 +
g.add((br, OWL.QualifiedMinCardinality, Literal(1)))
 +
g.add((br, OWL.onClass, ex.Sick))
 +
bi = BNode()
 +
Collection(g, bi, [ex.Parent, br])
 +
g.add((ex.WorriedParent, OWL.intersectionOf, bi))
 +
 +
# using the restriction above, If we now write...:
 +
g.add((ex.Bob, RDF.type, ex.Parent))
 +
g.add((ex.Bob, ex.hasChild, ex.John))
 +
g.add((ex.John, RDF.type, ex.Sick))
 +
# ...we can infer with owl reasoning that Bob is a worried Parent even though we didn't specify it ourselves because Bob fullfills the restriction and Parent requirements.
 +
 +
</syntaxhighlight>
 +
 +
==Protege-OWL reasoning with HermiT==
 +
 +
[[:File:DL-reasoning-RoyalFamily-final.owl.txt | Example file]] from Lecture 13 about OWL-DL, rules and reasoning.
 +
 +
-->

Latest revision as of 13:42, 11 May 2022

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.

Example lab solutions

Getting started

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

g = Graph()
EX = Namespace('http://EXample.org/')
RL = Namespace('http://purl.org/vocab/relationship/')
DBO = Namespace('https://dbpedia.org/ontology/')
DBR = Namespace('https://dbpedia.org/page/')

g.namespace_manager.bind('exampleURI', EX)
g.namespace_manager.bind('relationship', RL)
g.namespace_manager.bind('dbpediaOntology', DBO)
g.namespace_manager.bind('dbpediaPage', DBR)

g.add((EX.Cade, RDF.type, FOAF.Person)) 
g.add((EX.Mary, RDF.type, FOAF.Person))
g.add((EX.Cade, RL.spouseOf, EX.Mary)) # a symmetrical relation from an established namespace
g.add((DBR.France, DBO.capital, DBR.Paris)) 
g.add((EX.Cade, FOAF.age, Literal(27)))
g.add((EX.Mary, FOAF.age, Literal('26', datatype=XSD.int)))
Collection (g, EX.MaryInterests, [EX.hiking, EX.choclate, EX.biology])
g.add((EX.Mary, EX.hasIntrest, EX.MaryInterests))
g.add((EX.Mary, RDF.type, EX.student))
g.add((DBO.capital, EX.range, EX.city))
g.add((EX.Mary, RDF.type, EX.kind))
g.add((EX.Cade, RDF.type, EX.kindPerson))

#hobbies = ['hiking', 'choclate', 'biology']
#for i in hobbies:
#    g.add((EX.Mary, FOAF.interest, EX[i]))

print(g.serialize(format="turtle"))

RDFlib

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


g = Graph()
ex = Namespace('http://example.org/')
schema = Namespace("https://schema.org/")
dbp = Namespace("https://dbpedia.org/resource/")

g.bind("ex", ex)
g.bind("dbp", dbp)
g.bind("schema", schema)

address = BNode()
degree = BNode()

# from lab 1
g.add((ex.Cade, FOAF.name, Literal("Cade Tracey", datatype=XSD.string)))
g.add((ex.Mary, FOAF.name, Literal("Mary", datatype=XSD.string)))
g.add((ex.Cade, RDF.type, FOAF.Person))
g.add((ex.Mary, RDF.type, FOAF.Person))
g.add((ex.Mary, RDF.type, ex.Student))
g.add((ex.Cade, ex.married, ex.Mary))
g.add((ex.Cade, FOAF.age, Literal('27', datatype=XSD.int)))
g.add((ex.Mary, FOAF.age, Literal('26', datatype=XSD.int)))
g.add((ex.Paris, RDF.type, ex.City))
g.add((ex.France, ex.Capital, ex.Paris))
g.add((ex.Mary, FOAF.interest, ex.hiking))
g.add((ex.Mary, FOAF.interest, ex.Chocolate))
g.add((ex.Mary, FOAF.interest, ex.biology))
g.add((ex.France, ex.City, ex.Paris))
g.add((ex.Mary, ex.Characterostic, ex.kind))
g.add((ex.Cade, ex.Characterostic, ex.kind))
g.add((ex.France, RDF.type, ex.Country))
g.add((ex.Cade, schema.address, address))

# BNode address
g.add((address, RDF.type, schema.PostalAdress))
g.add((address, schema.streetAddress, Literal('1516 Henry Street')))
g.add((address, schema.addresCity, dbp.Berkeley))
g.add((address, schema.addressRegion, dbp.California))
g.add((address, schema.postalCode, Literal('94709')))
g.add((address, schema.addressCountry, dbp.United_States))

# More info about Cade
g.add((ex.Cade, ex.Degree, degree))
g.add((degree, ex.Field, dbp.Biology))
g.add((degree, RDF.type, dbp.Bachelors_degree))
g.add((degree, ex.Universety, dbp.University_of_California))
g.add((degree, ex.year, Literal('2001', datatype=XSD.gYear)))

# Emma
emma_degree = BNode()
g.add((ex.Emma, FOAF.name, Literal("Emma Dominguez", datatype=XSD.string)))
g.add((ex.Emma, RDF.type, FOAF.Person))
g.add((ex.Emma, ex.Degree, emma_degree))
g.add((degree, ex.Field, dbp.Chemistry))
g.add((degree, RDF.type, dbp.Masters_degree))
g.add((degree, ex.Universety, dbp.University_of_Valencia))
g.add((degree, ex.year, Literal('2015', datatype=XSD.gYear)))

# Address
emma_address = BNode()
g.add((ex.Emma, schema.address, emma_address))
g.add((emma_address, RDF.type, schema.PostalAdress))
g.add((emma_address, schema.streetAddress,
       Literal('Carrer de la Guardia Civil 20')))
g.add((emma_address, schema.addressRegion, dbp.Valencia))
g.add((emma_address, schema.postalCode, Literal('46020')))
g.add((emma_address, schema.addressCountry, dbp.Spain))

b = BNode()
g.add((ex.Emma, ex.visit, b))
Collection(g, b,
           [dbp.Portugal, dbp.Italy, dbp.France, dbp.Germany, dbp.Denmark, dbp.Sweden])

SPARQL - Blazegraph

PREFIX ex: <http://example.org/> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX xml: <http://www.w3.org/XML/1998/namespace> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 


#select all triplets in graph
SELECT ?s ?p ?o
WHERE {
    ?s ?p ?o .
} 
#select the interestes of Cade
SELECT ?cadeInterest
WHERE {
    ex:Cade ex:interest ?cadeInterest .
} 
#select the country and city where Emma lives
SELECT ?emmaCity ?emmaCountry
WHERE {
    ex:Emma ex:address ?address .
  	?address ex:city ?emmaCity .
  	?address ex:country ?emmaCountry .
} 
#select the people who are over 26 years old
SELECT ?person ?age
WHERE {
    ?person ex:age ?age .
  	FILTER(?age > 26) .     
} 
#select people who graduated with Bachelor
SELECT ?person ?degree
WHERE {
    ?person ex:degree ?degree .
  	?degree ex:degreeLevel "Bachelor" .
          
} 
# delete cades photography interest
DELETE DATA
{
    ex:Cade ex:interest ex:Photography .
} 

# delete and insert university of valencia
DELETE { ?s ?p ex:University_of_Valencia }
INSERT { ?s ?p ex:Universidad_de_Valencia }
WHERE  { ?s ?p ex:University_of_Valencia } 

#check if the deletion worked
SELECT ?s ?o2
WHERE  { 
  ?s ex:degree ?o .
  ?o ex:degreeSource ?o2 .
       	} 
#describe sergio
DESCRIBE ex:Sergio ?o
WHERE {
  ex:Sergio ?p ?o .
   ?o ?p2 ?o2 .
  }

SPARQL - RDFlib

from SPARQLWrapper import SPARQLWrapper, JSON, POST, GET, TURTLE

namespace = "lab4"
sparql = SPARQLWrapper("http://10.111.21.183:9999/blazegraph/namespace/"+ namespace + "/sparql")

# Print out Cades interests
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT * WHERE {
    ex:Cade ex:interest ?interest.
    }
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print(result["interest"]["value"])

# Print Emmas city and country
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT ?emmaCity ?emmaCountry
    WHERE {
        ex:Emma ex:address ?address .
        ?address ex:city ?emmaCity .
        ?address ex:country ?emmaCountry .
        } 
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print("Emma's city is "+result["emmaCity"]["value"]+" and Emma's country is " + result["emmaCountry"]["value"])

#Select the people who are over 26 years old
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT ?person ?age
    WHERE {
        ?person ex:age ?age .
        FILTER(?age > 26) .  
        }
        """)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print("All people who are over 26 years old: "+result["person"]["value"])

#Select people who graduated with Bachelor
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT ?person ?degree
    WHERE {
        ?person ex:degree ?degree .
        ?degree ex:degreeLevel "Bachelor" .
        } 
        """)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print("People who graduated with Bachelor: "+result["person"]["value"])

#Delete cades photography interest
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    DELETE DATA {
        ex:Cade ex:interest ex:Photography .
        } 
        """)
sparql.setMethod(POST)
results = sparql.query()
print(results.response.read())

# Print out Cades interests again
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT * WHERE {
    ex:Cade ex:interest ?interest.
    }
""")
sparql.setReturnFormat(JSON)
sparql.setMethod(GET)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print(result["interest"]["value"])

# Check university names
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT ?s ?o2
    WHERE  { 
        ?s ex:degree ?o .
        ?o ex:degreeSource ?o2 .
      	}
    """)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print(result["o2"]["value"])


#Delete and insert university of valencia
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    DELETE { ?s ?p ex:University_of_Valencia }
    INSERT { ?s ?p ex:Universidad_de_Valencia }
    WHERE  { ?s ?p ex:University_of_Valencia } 
        """)
sparql.setMethod(POST)
results = sparql.query()
print(results.response.read())

# Check university names again
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT ?s ?o2
    WHERE  { 
        ?s ex:degree ?o .
        ?o ex:degreeSource ?o2 .
      	}
    """)
sparql.setReturnFormat(JSON)
sparql.setMethod(GET)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print(result["o2"]["value"])

#Insert Sergio
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
    INSERT DATA {
        ex:Sergio a foaf:Person ;
        ex:address [ a ex:Address ;
                ex:city ex:Valenciay ;
                ex:country ex:Spain ;
                ex:postalCode "46021"^^xsd:string ;
                ex:state ex:California ;
                ex:street "4_Carrer_del_Serpis"^^xsd:string ] ;
        ex:degree [ ex:degreeField ex:Computer_science ;
                ex:degreeLevel "Master"^^xsd:string ;
                ex:degreeSource ex:University_of_Valencia ;
                ex:year "2008"^^xsd:gYear ] ;
        ex:expertise ex:Big_data,
            ex:Semantic_technologies,
            ex:Machine_learning;
        foaf:name "Sergio_Pastor"^^xsd:string .
        }
    """)
sparql.setMethod(POST)
results = sparql.query()
print(results.response.read())
sparql.setMethod(GET)

# Describe Sergio
sparql.setReturnFormat(TURTLE)
sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    DESCRIBE ex:Sergio ?o
    WHERE {
        ex:Sergio ?p ?o .
        ?o ?p2 ?o2 .
    }
    """)
results = sparql.query().convert()
print(results.serialize(format='turtle'))

# Construct that any city is in the country in an address
sparql.setQuery("""
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
    PREFIX ex: <http://example.org/>
    CONSTRUCT {?city ex:locatedIn ?country}
    Where {
        ?s rdf:type ex:Address .
        ?s ex:city ?city .
        ?s ex:country ?country.
        }
    """)
sparql.setReturnFormat(TURTLE)
results = sparql.query().convert()
print(results.serialize(format='turtle'))

Web APIs and JSON-LD

import requests
from rdflib import FOAF, Namespace, Literal, RDF, Graph, TURTLE

r = requests.get('http://api.open-notify.org/astros.json').json()
g = Graph()
EX = Namespace('http://EXample.org/')
g.bind("ex", EX)

for item in r['people']:
    craft = item['craft'].replace(" ","_")
    person = item['name'].replace(" ","_")
    g.add((EX[person], EX.onCraft, EX[craft]))
    g.add((EX[person], RDF.type, FOAF.Person))
    g.add((EX[person], FOAF.name, Literal(item['name'])))
    g.add((EX[craft], FOAF.name, Literal(item['craft'])))
res = g.query("""
    CONSTRUCT {?person1 foaf:knows ?person2}
    WHERE {
        ?person1 ex:onCraft ?craft .
        ?person2 ex:onCraft ?craft .
        }
""")

for triplet in res:
    # (we don't need to add that they know themselves)
    if (triplet[0] != triplet[2]):
        g.add((triplet))
        
print(g.serialize(format="turtle"))

Semantic lifting - CSV

import pandas as pd
from rdflib import Graph, Namespace, URIRef, Literal
from rdflib.namespace import RDF, XSD
import spotlight
from spotlight import SpotlightException


# Parameter given to spotlight to filter out results with confidence lower than this value
CONFIDENCE = 0.5
SERVER = "https://api.dbpedia-spotlight.org/en/annotate"

def annotate_entity(entity):
	annotations = []
	try:
		annotations = spotlight.annotate(address=SERVER,text=entity, confidence=CONFIDENCE)
    # This catches errors thrown from Spotlight, including when no resource is found in DBpedia
	except SpotlightException as e:
		print(e)
	return annotations


ex = Namespace("http://example.org/")
dbr = Namespace("http://dbpedia.org/resource/")
dbp = Namespace("https://dbpedia.org/property/")
dbpage = Namespace("https://dbpedia.org/page/")
sem = Namespace("http://semanticweb.cs.vu.nl/2009/11/sem/")
tl = Namespace("http://purl.org/NET/c4dm/timeline.owl#")

g = Graph()
g.bind("ex", ex)
g.bind("dbr", dbr)
g.bind("dbp", dbp)
g.bind("dbpage", dbpage)
g.bind("sem", sem)
g.bind("tl", tl)

df = pd.read_csv("russia-investigations.csv")
# We need to correct the type of the columns in the DataFrame, as Pandas assigns an incorrect type when it reads the file (for me at least). We use .astype("str") to convert the content of the columns to a string.
df["name"] = df["name"].astype("str")
df["type"] = df["type"].astype("str")

# iterrows creates an iterable object (list of rows)
for index, row in df.iterrows():
	investigation = URIRef(ex + row['investigation'])
	investigation_spotlight = annotate_entity(row['investigation'])
	investigation_start = Literal(row['investigation-start'], datatype=XSD.date)
	investigation_end = Literal(row['investigation-end'], datatype=XSD.date)
	investigation_days = Literal(row['investigation-days'], datatype=XSD.integer)
	name = Literal(row['name'], datatype=XSD.string)
	name_underscore = URIRef(dbpage + row['name'].replace(" ", "_"))
	investigation_result = URIRef(
		ex + row['investigation'] + "_investigation_" + row['name'].replace(" ", "_"))
	indictment_days = Literal(row['indictment-days'], datatype=XSD.integer)
	type = URIRef(dbr + row['type'].replace(" ", "_"))
	cp_date = Literal(row['cp-date'], datatype=XSD.date)
	cp_days = Literal(row['cp-days'], datatype=XSD.duration)
	overturned = Literal(row['overturned'], datatype=XSD.boolean)
	pardoned = Literal(row['pardoned'], datatype=XSD.boolean)
	american = Literal(row['american'], datatype=XSD.boolean)
	president = Literal(row['president'], datatype=XSD.string)
	president_underscore = URIRef(dbr + row['president'].replace(" ", "_"))
	president_spotlight = annotate_entity(row['president'])

	try:
		g.add((( URIRef(investigation_spotlight[0]["URI"]), RDF.type, sem.Event)))
	except:
		g.add((investigation, RDF.type, sem.Event))
	try:
		g.add((( URIRef(investigation_spotlight[0]["URI"]), sem.hasBeginTimeStamp, investigation_start)))
	except:
		g.add((investigation, sem.hasBeginTimeStamp, investigation_start))
	try:
		g.add((( URIRef(investigation_spotlight[0]["URI"]), sem.hasEndTimeStamp, investigation_end)))
	except:
		g.add((investigation, sem.hasEndTimeStamp, investigation_end))
	try:
		g.add((URIRef(investigation_spotlight[0]["URI"]), tl.duration, investigation_days))
	except:
		g.add((investigation, tl.duration, investigation_days))
	try:
		g.add((URIRef(investigation_spotlight[0]["URI"]), dbp.president, URIRef(president_spotlight[0]["URI"])))
	except:
		g.add((investigation, dbp.president, dbr.president_underscore))
	try:
		g.add((URIRef(investigation_spotlight[0]["URI"]), sem.hasSubEvent, investigation_result))
	except:
		g.add((investigation, sem.hasSubEvent, investigation_result))
	
	g.add((investigation_result, ex.resultType, type))
	g.add((investigation_result, ex.objectOfInvestigation, name_underscore))
	g.add((investigation_result, ex.isAmerican, american))
	g.add((investigation_result, ex.indictmentDuration, indictment_days))
	g.add((investigation_result, ex.caseSolved, cp_date))
	g.add((investigation_result, ex.daysBeforeCaseSolved, cp_days))
	g.add((investigation_result, ex.overturned, overturned))
	g.add((investigation_result, ex.pardoned, pardoned))

g.serialize("output.ttl", format="ttl")

RDFS

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

ex = Namespace("http://example.org/")
dbr = Namespace("http://dbpedia.org/resource/")
dbp = Namespace("https://dbpedia.org/property/")
dbpage = Namespace("https://dbpedia.org/page/")
sem = Namespace("http://semanticweb.cs.vu.nl/2009/11/sem/")
tl = Namespace("http://purl.org/NET/c4dm/timeline.owl#")

g = Graph()
g.bind("ex", ex)
g.bind("dbr", dbr)
g.bind("dbp", dbp)
g.bind("dbpage", dbpage)
g.bind("sem", sem)
g.bind("tl", tl)

g.parse(location="exampleTTL.ttl", format="turtle")

# University of California and University of Valencia are both Universities. 
g.add((ex.University_of_California, RDF.type, ex.University))
g.add((ex.University_of_Valencia, RDF.type, ex.University))
# All universities are higher education institutions (HEIs). 
g.add((ex.University, RDFS.subClassOf, ex.Higher_education))
# Only persons can have an expertise, and what they have expertise in is always a subject. 
g.add((ex.expertise, RDFS.domain, FOAF.Person))
g.add((ex.expertise, RDFS.range, ex.subject))
# Only persons can graduate from a HEI. 
g.add((ex.graduatedFromHEI, RDFS.domain, FOAF.Person))
g.add((ex.graduatedFromHEI, RDFS.range, ex.Higher_education))
# If you are a student, you are in fact a person as well. 
g.add((ex.Student, RDFS.subClassOf, FOAF.Person))
# That a person is married to someone, means that they know them. 
g.add((ex.married, RDFS.subPropertyOf, FOAF.knows))
# Finally, if a person has a name, that name is also the label of that entity."
g.add((FOAF.name, RDFS.subPropertyOf, RDFS.label))

# Having a degree from a HEI means that you have also graduated from that HEI. 
g.add((ex.graduatedFromHEI, RDFS.subPropertyOf, ex.degree))
# That a city is a capital of a country means that this city is located in that country. 
g.add((ex.capital, RDFS.domain, ex.Country))
g.add((ex.capital, RDFS.range, ex.City))
g.add((ex.capital, RDFS.subPropertyOf, ex.hasLocation))
# That someone was involved in a meeting, means that they have met the other participants. 
    # This question was bad for the RDFS lab because we need complex OWL or easy sparql.
res = g.query("""
    CONSTRUCT {?person1 ex:haveMet ?person2}
    WHERE {
        ?person1 ex:meeting ?Meeting .
        ?Meeting ex:involved ?person2 .
        }
""")
for triplet in res:
    #we don't need to add that people have met themselves
    if (triplet[0] != triplet[2]):
        g.add((triplet))
# If someone partook in a meeting somewhere, means that they have visited that place"
    # This question was bad for the RDFS lab for the same reason.
res = g.query("""
    CONSTRUCT {?person ex:hasVisited ?place}
    WHERE {
        ?person1 ex:meeting ?Meeting .
        ?Meeting ex:location ?place .
        }
""")
for triplet in res:
        g.add((triplet))

rdfs = owlrl.OWLRL.OWLRL_Semantics(g, False, False, False)
rdfs.closure()
rdfs.flush_stored_triples()
g.serialize("output.ttl",format="ttl")

OWL 1

import owlrl
from rdflib import Graph, Namespace, Literal, URIRef
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL
from rdflib.collection import Collection

g = Graph()
print()
# Namespaces
ex = Namespace("http://example.org/")
dbp = Namespace("http://dbpedia.org/resource/")
geo = Namespace("http://sws.geonames.org/")
schema = Namespace("https://schema.org/")
akt = Namespace("http://www.aktors.org/ontology/portal#")
vcard = Namespace("http://www.w3.org/2006/vcard/ns#")

g.bind("ex", ex)
g.bind("owl", OWL)

g.parse(location="lab8turtle.txt", format="turtle")

# Cade and Emma are two different persons. 
g.add((ex.Cade, OWL.differentFrom, ex.Emma))
# The country USA above is the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames resource http://sws.geonames.org/6252001/ (gn:6252001). 
g.add((ex.USA, OWL.sameAs, dbp.United_States))
g.add((ex.USA, OWL.sameAs, geo["6252001"]))
# The person class (the RDF type the Cade and Emma resources) in your graph is the same as FOAF's, schema.org's and AKT's person classes 
    # (they are http://xmlns.com/foaf/0.1/Person, http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively. 
g.add((FOAF.Person, OWL.sameAs, schema.Person))
g.add((FOAF.Person, OWL.sameAs, akt.Person))
# Nothing can be any two of a person, a university, or a city at the same time. 
Collection(g, ex.DisjointClasses, [FOAF.Person, ex.University, ex.City])
g.add((OWL.AllDifferent, OWL.distinctMembers, ex.DisjointClasses))
# The property you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US 
    # is a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). 
g.add((ex.postalCode, RDFS.subPropertyOf, vcard["postal-code"]))
# No two US cities can have the same postal code. 
    # We have to add a relation from city to postal code first
res = g.query("""
    PREFIX RDF: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX ex: <http://example.org/>
    CONSTRUCT {?usa_city ex:us_city_postal_code ?postalcode}
    WHERE {
        ?address RDF:type ex:Address .
        ?address ex:country ex:USA .
        ?address ex:city ?usa_city .
        ?address ex:postalCode ?postalcode
        }
""")
for triplet in res:
        g.add((triplet))
    # Now we can make us cities have distinct postal codes
g.add((ex.us_city_postal_code, RDF.type, OWL.FunctionalProperty))
g.add((ex.us_city_postal_code, RDF.type, OWL.InverseFunctionalProperty))
g.add((ex.us_city_postal_code, RDFS.subPropertyOf, ex.postalcode))

# The property you have used for Emma living in Valencia is the same property as FOAF's based-near property 
    # (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property (http://dbpedia.org/ontology/hometown, dbo:hometown). 
g.add((ex.city, OWL.sameAs, FOAF.based_near))
g.add((ex.city, OWL.inverseOf, dbp.hometown))

g.add((ex.Cade, ex.married, ex.Mary))
g.add((ex.Cade, ex.livesWith, ex.Mary))
g.add((ex.Cade, ex.sibling, ex.Andrew))
g.add((ex.Cade, ex.hasFather, ex.Bob))
g.add((ex.Bob, ex.fatherOf, ex.Cade))


#Look through the predicates(properties) above and add new triples for each one that describes them as any of the following: 
    # a reflexive , irreflexive, symmetric, asymmetric, transitive, functional, or an Inverse Functional Property.
g.add((ex.married, RDF.type, OWL.SymmetricProperty))
g.add((ex.married, RDF.type, OWL.FunctionalProperty))
g.add((ex.married, RDF.type, OWL.InverseFunctionalProperty))
g.add((ex.married, RDF.type, OWL.IrreflexiveProperty))

g.add((ex.livesWith, RDF.type, OWL.SymmetricProperty))
g.add((ex.livesWith, RDF.type, OWL.ReflexiveProperty))
g.add((ex.livesWith, RDF.type, OWL.TransitiveProperty))

g.add((ex.sibling, RDF.type, OWL.SymmetricProperty))

g.add((ex.hasFather, RDF.type, OWL.AsymmetricProperty))
g.add((ex.hasFather, RDF.type, OWL.FunctionalProperty))
g.add((ex.hasFather, RDF.type, OWL.IrreflexiveProperty))

g.add((ex.fatherOf, RDF.type, OWL.AsymmetricProperty))
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))
g.add((ex.fatherOf, RDF.type, OWL.InverseFunctionalProperty))
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))

# These three lines add inferred triples to the graph.
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
owl.closure()
owl.flush_stored_triples()

g.serialize("lab8output.xml",format="xml")

Semantic lifting - XML

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

g = Graph()
ex = Namespace("http://example.org/")
prov = Namespace("http://www.w3.org/ns/prov#")
g.bind("ex", ex)
g.bind("prov", prov)


# URL of xml data
url = 'http://feeds.bbci.co.uk/news/rss.xml'
# Retrieve the xml data from the web-url.
resp = requests.get(url)
# Creating an ElementTree from the response content
tree = ET.ElementTree(ET.fromstring(resp.content))
root = tree.getroot()

# I just realized this is cheating, but whatever, you should do it with xmltree
writerDict = {
    "Mon":"Thomas_Smith",
    "Tue":"Thomas_Smith",
    "Wed":"Thomas_Smith",
    "Thu":"Joseph_Olson",
    "Fri":"Joseph_Olson",
    "Sat":"Sophia_Cruise",
    "Sun":"Sophia_Cruise"
}
copyright = Literal(root.findall("./channel")[0].find("copyright").text)

for item in root.findall("./channel/item"):
    copyright = Literal(root.findall("./channel")[0].find("copyright").text)

    News_article_id = URIRef(item.find("guid").text)
    title = Literal(item.find("title").text)
    description = Literal(item.find("description").text)
    link = URIRef(item.find("link").text)
    pubDate = Literal(item.find("pubDate").text)
    writerName = ex[writerDict[pubDate[:3]]]

    g.add((News_article_id, ex.title, title))
    g.add((News_article_id, ex.description, description))
    g.add((News_article_id, ex.source_link, link))
    g.add((News_article_id, ex.pubDate, pubDate))
    g.add((News_article_id, ex.copyright, copyright))
    g.add((News_article_id, RDF.type, ex.News_article))
    g.add((News_article_id, RDF.type, prov.Entity))

    g.add((News_article_id, ex.authoredBy, writerName))
    g.add((writerName, RDF.type, prov.Person))
    g.add((writerName, RDF.type, prov.Agent))
    g.add((ex.authoredBy, RDF.type, prov.Generation))

print(g.serialize(format="turtle"))

OWL 2

from rdflib import Graph, Literal, Namespace, BNode
from rdflib.namespace import RDF, OWL, RDFS
from rdflib.collection import Collection

g = Graph()
ex = Namespace("http://example.org/")
g.bind("ex", ex)
g.bind("owl", OWL)

# anyone who is a graduate has at least one degree
br = BNode()
g.add((br, RDF.type, OWL.Restriction))
g.add((br, OWL.onProperty, ex.degree))
g.add((br, OWL.minCardinality, Literal(1)))
bi = BNode()
Collection(g, bi, [ex.Person, br])
g.add((ex.Graduate, OWL.intersectionOf, bi))

#anyone who is a university graduate has at least one degree from a university
br = BNode()
g.add((br, RDF.type, OWL.Restriction))
g.add((br, OWL.onProperty, ex.degree))
g.add((br, OWL.someValuesFrom, ex.University))
bi = BNode()
Collection(g, bi, [ex.Graduate, br]) 
                #[ex.Person, br] also someValueFrom implies a cardinality of at least one so they would be equivalent.
                #[ex.Person, ex.Graduate, br] would be redundant since intersection is associative. 
g.add((ex.University_graduate, OWL.intersectionOf, bi))

#a grade is either an A, B, C, D, E or F

bi = BNode()
Collection(g, bi, [Literal("A"), Literal("B"), Literal("C"), Literal("D"), Literal("E"), Literal("F")])
b1 = BNode()
g.add((b1, RDF.type, RDFS.Datatype))
g.add((b1, OWL.oneOf, bi))

g.add((ex.grade, RDFS.range, b1))

#a straight A student is a student that has only A grades
b1 = BNode()
g.add((b1, RDF.type, OWL.Restriction))
g.add((b1, OWL.onProperty, ex.grade))
g.add((b1, OWL.allValuesFrom, Literal("A")))

b2 = BNode()
g.add((b2, RDF.type, OWL.Restriction))
g.add((b2, OWL.onProperty, ex.grade))
g.add((b2, OWL.someValuesFrom, Literal("A")))

bi = BNode()
Collection(g, bi, [ex.Student, b1, b2])
g.add((ex.Straight_A_student, OWL.intersectionOf, bi))

#a graduate has no F grades
b3 = BNode()
Collection(g, b3, [Literal("A"), Literal("B"), Literal("C"), Literal("D"), Literal("E")])
b4 = BNode()
g.add((b4, RDF.type, RDFS.Datatype))
g.add((b4, OWL.oneOf, b3))
b5 = BNode()
g.add((b5, RDF.type, OWL.Restriction))
g.add((b5, OWL.onProperty, ex.grade))
g.add((b5, OWL.allValuesFrom, b4))

b6 = BNode()
Collection(g, b6, [ex.Person, b1, b5]) 
g.add((ex.Graduate, OWL.intersectionOf, b6))

#a student has a unique student number
g.add((ex.student_number, RDF.type, OWL.FunctionalProperty))
g.add((ex.student_number, RDF.type, OWL.InverseFunctionalProperty))

#each student has exactly one average grade
b1 = BNode()
g.add((b1, RDF.type, OWL.Restriction))
g.add((b1, OWL.onProperty, ex.average_grade))
g.add((b1, OWL.cardinality, Literal(1)))

b2 = BNode()
g.add((b2, RDF.type, OWL.Restriction))
g.add((b2, OWL.onProperty, ex.student_number))
g.add((b2, OWL.cardinality, Literal(1)))

Collection(g, b3, [ex.Person, b1, b2]) 
g.add((ex.Student, OWL.intersectionOf, b3))

#a course is either a bachelor, a master or a Ph.D course
bi = BNode()
Collection(g, bi, [ex.Bachelor_course, ex.Master_course, ex["Ph.D_course"]])
b1 = BNode()
#g.add((b1, RDF.type, OWL.Class))
g.add((b1, OWL.oneOf, bi))

g.add((ex.Course, RDF.type, b1))

#a bachelor student takes only bachelor courses
g.add((ex.Bachelor_student, RDFS.subClassOf, ex.Student))
b1 = BNode()
g.add((b1, RDF.type, OWL.Restriction))
g.add((b1, OWL.onProperty, ex.hasCourse))
g.add((b1, OWL.allValuesFrom, ex.Bachelor_course))

b2 = BNode()
Collection(g, b2, [ex.Student, b1])
g.add((ex.Bachelor_student, OWL.intersectionOf, b2))

#a masters student takes only master courses and at most one bachelor course

b1 = BNode()
g.add((b1, RDF.type, OWL.Restriction))
g.add((b1, OWL.onProperty, ex.hasCourse))
g.add((b1, OWL.maxQualifiedCardinality, Literal(1)))
g.add((b1, OWL.onClass, ex.Bachelor_course))

b2 = BNode()
g.add((b2, RDF.type, OWL.Restriction))
g.add((b2, OWL.onProperty, ex.hasCourse))
g.add((b2, OWL.someValuesFrom, ex.Master_course))

b3 = BNode()
Collection(g, b3, [ex.Master_course, ex.Bachelor_course])

b5 = BNode()
g.add((b5, RDF.type, OWL.Restriction))
g.add((b5, OWL.onProperty, ex.hasCourse))
g.add((b5, OWL.allValuesFrom, b3))

b6 = BNode()
Collection(g, b6, [ex.Student, b1, b2, b5])
g.add((ex.Master_student, OWL.intersectionOf, b6))

#a Ph.D student takes only Ph.D and at most two masters courses
b1 = BNode()
g.add((b1, RDF.type, OWL.Restriction))
g.add((b1, OWL.onProperty, ex.hasCourse))
g.add((b1, OWL.maxQualifiedCardinality, Literal(2)))
g.add((b1, OWL.onClass, ex.Master_course))

b2 = BNode()
g.add((b2, RDF.type, OWL.Restriction))
g.add((b2, OWL.onProperty, ex.hasCourse))
g.add((b2, OWL.someValuesFrom, ex["Ph.D_course"]))

b3 = BNode()
Collection(g, b3, [ex.Master_course, ex["Ph.D_course"]])

b5 = BNode()
g.add((b5, RDF.type, OWL.Restriction))
g.add((b5, OWL.onProperty, ex.hasCourse))
g.add((b5, OWL.allValuesFrom, b3))

b6 = BNode()
Collection(g, b6, [ex.Student, b1, b2, b5])
g.add((ex["Ph.D_student"], OWL.intersectionOf, b6))
#a Ph.D. student cannot take a bachelor course
    #NA, it's already true

Lab 11: Semantic Lifting - HTML

from bs4 import BeautifulSoup as bs
from rdflib import Graph, Literal, URIRef, Namespace
from rdflib.namespace import RDF, SKOS, XSD
import requests


g = Graph()
ex = Namespace("http://example.org/")
g.bind("ex", ex)

# Download html from URL and parse it with BeautifulSoup.
url = "https://www.semanticscholar.org/topic/Knowledge-Graph/159858"
page = requests.get(url)
html = bs(page.content, features="html.parser")
# print(html.prettify())

# Find the html that surrounds all the papers
papers = html.find_all('div', attrs={'class': 'flex-container'})
# Find the html that surrounds the info box
topic = html.find_all(
    'div', attrs={'class': 'flex-item__left-column entity-header'})


# Iterate through each paper to make triples:
for paper in papers:
    # e.g selecting title.
    title = paper.find('div', attrs={'class': 'timeline-paper-title'}).text
    author = paper.find('span', attrs={'class': 'author-list'}).text
    papper_year = paper.find(
        'li', attrs={'data-selenium-selector': "paper-year"}).text
    corpus_ID = paper.find(
        'li', attrs={'data-selenium-selector': "corpus-id"}).text
    corpus_ID = corpus_ID.replace(" ", "_")
    c_id = corpus_ID.replace("Corpus_ID:_", "")

    article = URIRef(ex + c_id)

    # Adding tripels
    g.add((article, RDF.type, ex.paper))
    g.add((article, ex.HasID, Literal(c_id, datatype=XSD.int)))
    g.add((article, ex.HasTitle, Literal(title, datatype=XSD.string)))
    g.add((article, ex.Publisher_year, Literal(papper_year, datatype=XSD.year)))

    author = author.split(", ")
    for x in author:
        name = x.replace(" ", "_")
        name = URIRef(ex + name)

        g.add((article, ex.hasAuthor, name))

# Iterate through the info box to make triples:
    for items in topic:
        main_topic = items.find('h1', attrs={'class': 'entity-name'}).text
        related_topic = items.find(
            'div', attrs={'class': 'entity-aliases'}).text
        related_topic = related_topic.replace("Known as: ", "")
        related_topic = related_topic.replace(f'\xa0Expand', "")
        related_topic = related_topic.replace(" ", "")
        main_topic = main_topic.replace(" ", "_")

        main_topic = URIRef(ex + main_topic)

        g.add((article, RDF.type, SKOS.Concept))
        g.add((article, SKOS.hasTopConcept, main_topic))

    related_topic = related_topic.split(',')

    for related_labels in related_topic:
        related_topic = URIRef(ex + related_labels)
        g.add((article, SKOS.broader, related_topic))


print(g.serialize(format='turtle'))

Owlready2

Martin's solution. NOTE: intead of using "is_a" to define classes like I have mostly done, use "equivalent_to" to make the resoner more powerful (and work at all in this case).

from owlready2 import *
from rdflib import Graph, Namespace

BASE = 'http://info216.uib.no/owlready2-lab/'
onto = get_ontology(BASE)

def clean_onto(onto):
    with onto:
        for ind in onto.individuals():
            destroy_entity(ind)
        for prop in onto.properties():
            destroy_entity(prop)
        for cls in onto.classes():
            destroy_entity(cls)

def onto2graph(onto):
    graph = Graph()
    onto.save('temp_owlready2.nt', format='ntriples')
    graph.parse('temp_owlready2.nt', format='ntriples')
    return graph

def print_onto(onto):
    g = onto2graph(onto)
    g.bind('', Namespace(BASE))
    print(g.serialize(format='ttl'))

clean_onto(onto)

# anyone who is a graduate has at least one degree
with onto:
    class Student(Thing): pass
    class Degree(Thing): pass
    class hasDegree(Student >> Degree): pass
    class Graduate(Student): 
        is_a = [hasDegree.some(Degree)]

#anyone who is a university graduate has at least one degree from a university
with onto:
    class UniversityDegree(Degree): pass
    class UniversityGraduate(Graduate): 
        is_a = [hasDegree.some(UniversityDegree)]


#a grade is either an A, B, C, D, E or F
with onto:
    class Grade(Thing): pass
    class A(Grade): pass
    class B(Grade): pass
    class C(Grade): pass
    class D(Grade): pass
    class E(Grade): pass
    class F(Grade): pass

Grade.is_a.append(OneOf([A, B, C, D, E, F]))

#a straight A student is a student that has only A grades
with onto:
    class hasGrade(Student >> Grade): pass
    class StraightAStudent(Student):
        is_a = [hasGrade.only(A)]


#a graduate has no F grades
#Graduate.is_a.append(hasGrade.only(OneOf[A,B,C,D,E]))

#a student has a unique student number
with onto:
    class StudentNumber(Thing):pass
    class hasStudentNumber(Student >> StudentNumber, FunctionalProperty, InverseFunctionalProperty):pass

#each student has exactly one average grade
with onto:
    class AverageGrade(Grade):pass
    class hasAverageGrade(Student >> AverageGrade):pass
Student.is_a.append(hasAverageGrade.exactly(1,AverageGrade))
Student.is_a.append(hasStudentNumber.exactly(1,StudentNumber))
    

#a course is either a bachelor, a master or a Ph.D course
with onto:
    class Course(Thing):pass
    class BachelorCourse(Course):pass
    class MasterCourse(Course):pass
    class PhDCourse(Course):pass
    
Course.is_a.append(OneOf([BachelorCourse, MasterCourse, PhDCourse]))

#a bachelor student takes only bachelor courses
with onto:
    class takesCourse(Student>>Course):pass
    class BachelorStudent(Student):
        is_a = [
            takesCourse.only(BachelorCourse) &
            takesCourse.some(Course)
        ]
        

#a masters student takes only master courses and at most one bachelor course
with onto:
    class MasterOrBachelorCourse(Course):pass
    class MasterStudent(Student):
        is_a = [
            takesCourse.only(Not(PhDCourse)) &
            takesCourse.max(1,BachelorCourse) &
            takesCourse.some(MasterCourse)
            ]


#a Ph.D student takes only Ph.D and at most two masters courses
with onto:
    class PhDStudent(Student):
        is_a = [
            takesCourse.only(Not(BachelorCourse))&
            takesCourse.max(2,MasterCourse)&
            takesCourse.some(PhDCourse)
            ]

# In comparison to lab 10..
"""
b1 = BNode()
g.add((b1, RDF.type, OWL.Restriction))
g.add((b1, OWL.onProperty, ex.hasCourse))
g.add((b1, OWL.maxQualifiedCardinality, Literal(2)))
g.add((b1, OWL.onClass, ex.Master_course))

b2 = BNode()
g.add((b2, RDF.type, OWL.Restriction))
g.add((b2, OWL.onProperty, ex.hasCourse))
g.add((b2, OWL.someValuesFrom, ex["Ph.D_course"]))

b3 = BNode()
Collection(g, b3, [ex.Master_course, ex["Ph.D_course"]])

b5 = BNode()
g.add((b5, RDF.type, OWL.Restriction))
g.add((b5, OWL.onProperty, ex.hasCourse))
g.add((b5, OWL.allValuesFrom, b3))

b6 = BNode()
Collection(g, b6, [ex.Student, b1, b2, b5])
g.add((ex["Ph.D_student"], OWL.intersectionOf, b6))
"""
#a Ph.D. student cannot take a bachelor course
    #NA, it's already true

#print(onto2graph(onto).serialize(format="turtle"))

clean_onto(onto)

# anyone who is a graduate has at least one degree
 # a graduate is a student with at least one degree
with onto:
    class Student(Thing): pass
    class Degree(Thing): pass
    class hasDegree(Student >> Degree): pass
    class Graduate(Student): 
        equivalent_to = [hasDegree.some(Degree)]

 # test with individual
with onto:
    cade = Student()
    infosci = Degree()
    cade.hasDegree.append(infosci)
from owlready2 import sync_reasoner

print(onto.Graduate in cade.is_a)
sync_reasoner()
print(onto.Graduate in cade.is_a)


print("graduate is: ", Graduate.is_a)
print("cade is: ", cade.is_a)

Alternative solution. More pro from Andreas, but it's a not so thoroughly tested draft for us teacher assistents he stresses (it's new material for us), so you might need to make some changes (like the one recommended above: equivalent_to instead of is_a).

from owlready2 import get_ontology, Thing, ObjectProperty
from rdflib import Graph, Namespace

BASE = 'http://info216.uib.no/owlready2-lab/'
onto = get_ontology(BASE)

def onto2graph(onto):
    graph = Graph()
    onto.save('temp.nt', format='ntriples')
    graph.parse('temp.nt', format='ntriples')
    return graph

def print_onto(onto):
    g = onto2graph(onto)
    g.bind('', Namespace(BASE))
    print(g.serialize(format='ttl'))

from owlready2 import destroy_entity
def clean_onto(onto):
    with onto:
        for ind in onto.individuals():
            destroy_entity(ind)
        for prop in onto.properties():
            destroy_entity(prop)
        for cls in onto.classes():
            destroy_entity(cls)

# anyone who is a graduate has at least one degree
with onto:
    class Student(Thing): pass
    class Degree(Thing): pass
    class hasDegree(Student >> Degree): pass
    class Graduate(Student): 
        is_a = [hasDegree.some(Degree)]

# anyone who is a university graduate has at least one degree from a university
with onto:
    class hasDegree(ObjectProperty): pass
    class degreeFrom(ObjectProperty): pass
    class Degree(Thing): pass
    class University(Thing): pass
    class UniversityGraduate(Thing): 
        hasDegree: Degree
        is_a = [hasDegree.some(Degree & degreeFrom.some(University))]
print_onto(onto)

from owlready2 import declare_datatype
class XSDString(object):
    def __init__(self, value): self.value = value
def str_parser(s): return s
def str_unparser(s): return s
declare_datatype(XSDString, 'http://www.w3.org/2001/XMLSchema#string', str_parser, str_unparser)

# a grade is either an A, B, C, D, E or F
from owlready2 import OneOf
with onto:
    class Grade(Thing): pass
    class charGrade(Grade >> XSDString): pass
    grade_A = Grade()
    grade_A.charGrade = ['A']
    grade_B = Grade()
    grade_B.charGrade = ['B']
    grade_C = Grade()
    grade_C.charGrade = ['C']
    grade_D = Grade()
    grade_D.charGrade = ['D']
    grade_E = Grade()
    grade_E.charGrade = ['E']
    grade_F = Grade()
    grade_F.charGrade = ['F']
    Grade.equivalent_to.append(OneOf([
        grade_A, grade_B, grade_C, grade_D, grade_E, grade_F
    ])) 
print_onto(onto)

 # a straight A student is a student that has only A grades
with onto:
    class Grade(Thing): pass
    class charGrade(Grade >> XSDString): pass
    grade_A = Grade()
    grade_A.charGrade = ['A']
    grade_B = Grade()
    grade_B.charGrade = ['B']
    # ...
    Grade.equivalent_to.append(OneOf([
        grade_A, grade_B,  # ...
    ])) 

    class Student(Thing): pass
    class hasGrade(Student >> Grade): pass
    class GradeA(Grade):
        equivalent_to = [OneOf([grade_A])]
    class StraightAStudent(Student):
        equivalent_to = [
            hasGrade.some(GradeA) & hasGrade.only(GradeA)
        ]
print_onto(onto)

 # a graduate has no F grades
with onto:
    class Grade(Thing): pass
    class charGrade(Grade >> XSDString): pass
    grade_A = Grade()
    grade_A.charGrade = ['A']
    # ...
    grade_F.charGrade = ['F']
    Grade.equivalent_to.append(OneOf([
        grade_A,  # ...
        grade_F
    ])) 

    class Student(Thing): pass
    class hasGrade(Student >> Grade): pass
    class GradeF(Grade):
        equivalent_to = [OneOf([grade_F])]
    class Graduate(Student):
        equivalent_to = [Student & ~ hasGrade.some(GradeF)]
print_onto(onto)

# a student has a single unique student number
class XSDInt(object):
    def __init__(self, value): self.value = value
def int_parser(s): return int(s)
def int_unparser(i): return str(i)
declare_datatype(XSDInt, 'http://www.w3.org/2001/XMLSchema#int', int_parser, int_unparser)

from owlready2 import FunctionalProperty, InverseFunctionalProperty
with onto:
    class Student(Thing): pass
    class hasStudentNumber(Student >> XSDInt): 
        is_a = [FunctionalProperty, InverseFunctionalProperty]
print_onto(onto)

# each student has exactly one average grade
class XSDFloat(object):
    def __init__(self, value): self.value = value
def int_parser(s): return float(s)
def int_unparser(f): return str(f)
declare_datatype(XSDFloat, 'http://www.w3.org/2001/XMLSchema#float', int_parser, int_unparser)

with onto:
    class Student(Thing): pass
    class hasAverageGrade(Grade >> XSDFloat): pass
    Student.is_a.append(hasAverageGrade.exactly(1, XSDFloat))
print_onto(onto)

 # a course is either a bachelor, a master or a Ph.D course
from owlready2 import AllDisjoint
with onto:
    class Course(Thing): pass
    class BachelorCourse(Course): pass
    class MasterCourse(Course): pass
    class PhDCourse(Course): pass
    AllDisjoint([BachelorCourse, MasterCourse, PhDCourse])
print_onto(onto)

# a bachelor student takes only bachelor courses
from owlready2 import AllDisjoint
with onto:
    class Course(Thing): pass
    class BachelorCourse(Course): pass
    class MasterCourse(Course): pass
    class PhDCourse(Course): pass
    AllDisjoint([BachelorCourse, MasterCourse, PhDCourse])
print_onto(onto)

 # a masters student takes only master courses, except for at most one bachelor course
with onto:
    class Student(Thing): pass
    class Course(Thing): pass
    class takesCourse(Student >> Course): pass
    class BachelorCourse(Course): pass
    class MasterCourse(Course): pass
    class MasterStudent(Student):
        is_a = [
            takesCourse.some(MasterCourse) &
            takesCourse.only(MasterCourse | BachelorCourse) &
            takesCourse.max(1, BachelorCourse)
        ]
print_onto(onto)

 # a Ph.D student takes only Ph.D courses, except for at most two masters courses
with onto:
    class Student(Thing): pass
    class Course(Thing): pass
    class takesCourse(Student >> Course): pass
    class MasterCourse(Course): pass
    class PhDCourse(Course): pass
    class PhDStudent(Student):
        is_a = [
            takesCourse.some(PhDCourse) &
            takesCourse.only(PhDCourse | MasterCourse) &
            takesCourse.max(2, MasterCourse)
        ]
print_onto(onto)

 # a Ph.D. student cannot take a bachelor course
with onto:
    class Student(Thing): pass
    class Course(Thing): pass
    class takesCourse(Student >> Course): pass
    class BachelorCourse(Course): pass
    class PhDStudent(Student):
        is_a = [
            takesCourse.max(0, BachelorCourse)
        ]
print_onto(onto)

# ...alternative solution
clean_onto(onto)
with onto:
    class Student(Thing): pass
    class Course(Thing): pass
    class takesCourse(Student >> Course): pass
    class BachelorCourse(Course): pass
    class PhDStudent(Student):
        is_a = [Student & ~ takesCourse.some(BachelorCourse)]
print_onto(onto)

 # a graduate is a student with at least one degree
with onto:
    class Student(Thing): pass
    class Degree(Thing): pass
    class hasDegree(Student >> Degree): pass
    class Graduate(Student): 
        equivalent_to = [Student & hasDegree.some(Degree)]

 # test with individual
with onto:
    cade = Student()
    infosci = Degree()
    cade.hasDegree.append(infosci)

from owlready2 import sync_reasoner

print(onto.Graduate in cade.is_a)
sync_reasoner()
print(onto.Graduate in cade.is_a)

# if you have more time: 
# populate the ontology with individuals
# a straight A student is a student that has only A grades
clean_onto(onto)
with onto:
    class Grade(Thing): pass
    class charGrade(Grade >> XSDString): pass
    grade_A = Grade()
    grade_A.charGrade = ['A']
    grade_B = Grade()
    grade_B.charGrade = ['B']
    # ...
    Grade.equivalent_to.append(OneOf([
        grade_A, grade_B,  # ...
    ])) 

    class Student(Thing): pass
    class hasGrade(Student >> Grade): pass
    class GradeA(Grade):
        equivalent_to = [OneOf([grade_A])]
    class StraightAStudent(Student):
        equivalent_to = [
            Student &
            hasGrade.some(GradeA) & hasGrade.only(GradeA)
        ]
    # add individual
    cade = Student()
    cade.hasGrade.append(grade_A)
print_onto(onto)

from owlready2 import sync_reasoner
print(onto.StraightAStudent in cade.is_a)
sync_reasoner()
print(onto.StraightAStudent in cade.is_a)

from owlready2 import close_world
close_world(onto)  # because of the "only"-restriction
sync_reasoner()
print(onto.StraightAStudent in cade.is_a)

Knowledge graph embeddings

https://colab.research.google.com/drive/1sHusTjvmHtV6PkzIatLTMPzHuseAF6N1?usp=sharing


More miscellaneous examples

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

# OR

g = Graph()

ex = Namespace('http://example.org/')

g.add((ex.Cade, FOAF.name, Literal("Cade", datatype=XSD.string)))
g.add((ex.Mary, FOAF.name, Literal("Mary", datatype=XSD.string)))
g.add((ex.Cade, RDF.type, FOAF.Person))
g.add((ex.Mary, RDF.type, FOAF.Person))
g.add((ex.Mary, RDF.type, ex.Student))
g.add((ex.Cade, ex.Married, ex.Mary))
g.add((ex.Cade, FOAF.age, Literal('27', datatype=XSD.int)))
g.add((ex.Mary, FOAF.age, Literal('26', datatype=XSD.int)))
g.add((ex.Paris, RDF.type, ex.City))
g.add((ex.France, ex.Capital, ex.Paris))
g.add((ex.Mary, FOAF.interest, ex.hiking))
g.add((ex.Mary, FOAF.interest, ex.Chocolate))
g.add((ex.Mary, FOAF.interest, ex.biology))
g.add((ex.France, ex.City, ex.Paris))
g.add((ex.Mary, ex.characteristic, ex.kind))
g.add((ex.Cade, ex.characteristic, ex.kind))
g.add((ex.France, RDF.type, ex.Country))


print(g.serialize(format="turtle"))

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


More examples can be found in the example section on the official query service here: https://query.wikidata.org/.

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 = 'http://info216.i2s.uib.no/bigdata/namespace/' + ns + '/sparql'

# - the endpoint just moved, the old one was:
# endpoint = 'http://i2s.uib.no:8888/bigdata/namespace/' + ns + '/sparql'

# create wrapper
wrapper = SPARQLWrapper(endpoint)

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

# 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-' + datetime.datetime.now().strftime('%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))

Query Dbpedia with SparqlWrapper

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("http://dbpedia.org/sparql")

sparql.setQuery("""
    PREFIX dbr: <http://dbpedia.org/resource/>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?comment
    WHERE {
    dbr:Barack_Obama rdfs:comment ?comment.
    FILTER (langMatches(lang(?comment),"en"))
    }
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["comment"]["value"])

Lifting CSV to RDF

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

g = Graph()
ex = Namespace("http://example.org/")
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("http://example.org/unknown")))

# Clean printing of the graph.
print(g.serialize(format="turtle").decode())

CSV file for above example

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


Coding Tasks Lab 6

import pandas as pd


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


ex = Namespace("http://example.org/")
sem = Namespace("http://semanticweb.cs.vu.nl/2009/11/sem/")

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


# Removing unwanted characters
df = pd.read_csv('russia-investigation.csv')
# Here I deal with spaces (" ") in the data. I replace them with "_" so that URI's become valid.
df = df.replace(to_replace=" ", value="_", regex=True)
# This may seem odd, but in the data set we have a name like this:("Scooter"). So we have to remove quotation marks
df = df.replace(to_replace=f'"', value="", regex=True)
# # Here I mark all missing/empty data as "unknown". This makes it easy to delete triples containing this later.
df = df.fillna("unknown")

# Loop through the CSV data, and then make RDF triples.
for index, row in df.iterrows():
    name = row['investigation']
    investigation = URIRef(ex + name)
    g.add((investigation, RDF.type, sem.Event))
    investigation_start = row["investigation-start"]
    g.add((investigation, sem.hasBeginTimeStamp, Literal(
        investigation_start, datatype=XSD.datetime)))
    investigation_end = row["investigation-end"]
    g.add((investigation, sem.hasEndTimeStamp, Literal(
        investigation_end, datatype=XSD.datetime)))
    investigation_end = row["investigation-days"]
    g.add((investigation, sem.hasXSDDuration, Literal(
        investigation_end, datatype=XSD.Days)))
    person = row["name"]
    person = URIRef(ex + person)
    g.add((investigation, sem.Actor, person))
    result = row['type']
    g.add((investigation, sem.hasSubEvent, Literal(result, datatype=XSD.string)))
    overturned = row["overturned"]
    g.add((investigation, ex.overtuned, Literal(overturned, datatype=XSD.boolean)))
    pardoned = row["pardoned"]
    g.add((investigation, ex.pardon, Literal(pardoned, datatype=XSD.boolean)))

g.serialize("output.ttl", format="ttl")
print(g.serialize(format="turtle"))

RDFS

RDFS-plus (OWL) Properties

g.add((ex.married, RDF.type, OWL.SymmetricProperty))
g.add((ex.married, RDF.type, OWL.IrreflexiveProperty))
g.add((ex.livesWith, RDF.type, OWL.ReflexiveProperty))
g.add((ex.livesWith, RDF.type, OWL.SymmetricProperty))
g.add((ex.sibling, RDF.type, OWL.TransitiveProperty))
g.add((ex.sibling, RDF.type, OWL.SymmetricProperty))
g.add((ex.sibling, RDF.type, OWL.IrreflexiveProperty))
g.add((ex.hasFather, RDF.type, OWL.FunctionalProperty))
g.add((ex.hasFather, RDF.type, OWL.AsymmetricProperty))
g.add((ex.hasFather, RDF.type, OWL.IrreflexiveProperty))
g.add((ex.fatherOf, RDF.type, OWL.AsymmetricProperty))
g.add((ex.fatherOf, RDF.type, OWL.IrreflexiveProperty))

# Sometimes there is no definite answer, and it comes down to how we want to model our properties
# e.g is livesWith a transitive property? Usually yes, but we can also want to specify that a child lives with both of her divorced parents.
# which means that: (mother livesWith child % child livesWith father) != mother livesWith father. Which makes it non-transitive.