Difference between revisions of "Python Examples"

From Info216
(Proposed solution for Lab 13 - Web APIs and JSON-LD)
 
(90 intermediate revisions by 6 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 labs as the course progresses.
  
 +
=Examples from the lectures=
  
==Lecture 1: Python, RDFlib, and PyCharm==
+
==Lecture 1: Introduction to KGs==
 +
Turtle example:
 +
<syntaxhighlight>
 +
@prefix ex: <http://example.org/> .
 +
ex:Roger_Stone
 +
    ex:name "Roger Stone" ;
 +
    ex:occupation ex:lobbyist ;
 +
    ex:significant_person ex:Donald_Trump .
 +
ex:Donald_Trump
 +
    ex:name "Donald Trump" .
 +
</syntaxhighlight>
 +
 
 +
==Lecture 2: RDF==
 +
Blank nodes for anonymity, or when we have not decided on a URI:
 +
<syntaxhighlight lang="Python">
 +
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD
 +
 
 +
EX = Namespace('http://example.org/')
 +
 
 +
g = Graph()
 +
g.bind('ex', EX)  # this is why the line '@prefix ex: <http://example.org/> .'
 +
                  # and the 'ex.' prefix are used when we print out Turtle later
 +
 
 +
robertMueller = BNode()
 +
g.add((robertMueller, RDF.type, EX.Human))
 +
g.add((robertMueller, FOAF.name, Literal('Robert Mueller', lang='en')))
 +
g.add((robertMueller, EX.position_held, Literal('Director of the Federal Bureau of Investigation', lang='en')))
 +
 
 +
print(g.serialize(format='turtle'))
 +
</syntaxhighlight>
 +
 
 +
Blank nodes used to group related properties:
 +
<syntaxhighlight>
 +
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD
 +
 
 +
EX = Namespace('http://example.org/')
 +
 
 +
g = Graph()
 +
g.bind('ex', EX)
 +
 
 +
# This is a task in Exercise 2
  
 +
print(g.serialize(format='turtle'))
 +
</syntaxhighlight>
  
===Printing the triples of the Graph in a readable way===
+
Literals:
 
<syntaxhighlight>
 
<syntaxhighlight>
# The turtle format has the purpose of being more readable for humans.  
+
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD
print(g.serialize(format="turtle").decode())
+
 
 +
EX = Namespace('http://example.org/')
 +
 
 +
g = Graph()
 +
g.bind('ex', EX)
 +
 
 +
g.add((EX.Robert_Mueller, RDF.type, EX.Human))
 +
g.add((EX.Robert_Mueller, FOAF.name, Literal('Robert Mueller', lang='en')))
 +
g.add((EX.Robert_Mueller, FOAF.name, Literal('رابرت مولر', lang='fa')))
 +
g.add((EX.Robert_Mueller, DC.description, Literal('sixth director of the FBI', datatype=XSD.string)))
 +
g.add((EX.Robert_Mueller, EX.start_time, Literal(2001, datatype=XSD.integer)))
 +
 
 +
print(g.serialize(format='turtle'))
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Coding Tasks Lab 1===
+
Alternative container (open):
 
<syntaxhighlight>
 
<syntaxhighlight>
from rdflib import Graph, Namespace, URIRef, BNode, Literal
+
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD
from rdflib.namespace import RDF, FOAF, XSD
+
 
 +
EX = Namespace('http://example.org/')
  
 
g = Graph()
 
g = Graph()
ex = Namespace("http://example.org/")
+
g.bind('ex', EX)
 +
 
 +
muellerReportArchives = BNode()
 +
g.add((muellerReportArchives, RDF.type, RDF.Alt))
 +
 
 +
archive1 = 'https://archive.org/details/MuellerReportVolume1Searchable/' \
 +
                    'Mueller%20Report%20Volume%201%20Searchable/'
 +
archive2 = 'https://edition.cnn.com/2019/04/18/politics/full-mueller-report-pdf/index.html'
 +
archive3 = 'https://www.politico.com/story/2019/04/18/mueller-report-pdf-download-text-file-1280891'
 +
 
 +
g.add((muellerReportArchives, RDFS.member, Literal(archive1, datatype=XSD.anyURI)))
 +
g.add((muellerReportArchives, RDFS.member, Literal(archive2, datatype=XSD.anyURI)))
 +
g.add((muellerReportArchives, RDFS.member, Literal(archive3, datatype=XSD.anyURI)))
  
g.add((ex.Cade, ex.married, ex.Mary))
+
g.add((EX.Mueller_Report, RDF.type, FOAF.Document))
g.add((ex.France, ex.capital, ex.Paris))
+
g.add((EX.Mueller_Report, DC.contributor, EX.Robert_Mueller))
g.add((ex.Cade, ex.age, Literal("27", datatype=XSD.integer)))
+
g.add((EX.Mueller_Report, SCHEMA.archivedAt, muellerReportArchives))
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))
 
  
 +
print(g.serialize(format='turtle'))
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Lecture 2: RDF programming==
+
Sequence container (open):
 +
<syntaxhighlight>
 +
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD
  
===Different ways to create an address===
+
EX = Namespace('http://example.org/')
  
 +
g = Graph()
 +
g.bind('ex', EX)
 +
 +
donaldTrumpSpouses = BNode()
 +
g.add((donaldTrumpSpouses, RDF.type, RDF.Seq))
 +
g.add((donaldTrumpSpouses, RDF._1, EX.IvanaTrump))
 +
g.add((donaldTrumpSpouses, RDF._2, EX.MarlaMaples))
 +
g.add((donaldTrumpSpouses, RDF._3, EX.MelaniaTrump))
 +
 +
g.add((EX.Donald_Trump, SCHEMA.spouse, donaldTrumpSpouses))
 +
 +
print(g.serialize(format='turtle'))
 +
</syntaxhighlight>
 +
 +
Collection (closed list):
 
<syntaxhighlight>
 
<syntaxhighlight>
 +
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD
  
from rdflib import Graph, Namespace, URIRef, BNode, Literal
+
EX = Namespace('http://example.org/')
from rdflib.namespace import RDF, FOAF, XSD
 
  
 
g = Graph()
 
g = Graph()
ex = Namespace("http://example.org/")
+
g.bind('ex', EX)
 +
 
 +
from rdflib.collection import Collection
 +
 
 +
g = Graph()
 +
g.bind('ex', EX)
  
 +
donaldTrumpSpouses = BNode()
 +
Collection(g, donaldTrumpSpouses, [
 +
    EX.IvanaTrump, EX.MarlaMaples, EX.MelaniaTrump
 +
])
 +
g.add((EX.Donald_Trump, SCHEMA.spouse, donaldTrumpSpouses))
  
# How to represent the address of Cade Tracey. From probably the worst solution to the best.
+
print(g.serialize(format='turtle'))
 +
g.serialize(destination='s02_Donald_Trump_spouses_list.ttl', format='turtle')
  
# Solution 1 -
+
print(g.serialize(format='turtle'))
# 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.
+
</syntaxhighlight>
  
g.add((ex.Cade_Tracey, ex.livesIn, Literal("1516_Henry_Street, Berkeley, California 94709, USA")))
+
=Example lab solutions=
  
 +
==Getting started (Lab 1)==
  
# Solution 2 -
+
<syntaxhighlight>
# Seperate the different pieces information into their own triples
 
  
g.add((ex.Cade_tracey, ex.street, Literal("1516_Henry_Street")))
+
from rdflib import Graph, Namespace
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")))
 
  
 +
g = Graph()
  
# Solution 3 - Some parts of the addresses can make more sense to be resources than Literals.
+
ex = Namespace('http://example.org/')
# 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.bind("ex", ex)
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))
 
  
 +
#The Mueller Investigation was lead by Robert Mueller.
 +
g.add((ex.Mueller_Investigation, ex.leadBy, ex.Robert_Muller))
  
# Solution 4
+
#It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, and Roger Stone.
# Grouping of the information into an Address. We can Represent the address concept with its own URI OR with a Blank Node.  
+
g.add((ex.Mueller_Investigation, ex.involved, ex.Paul_Manafort))
# One advantage of this is that we can easily remove the entire address, instead of removing each individual part of the address.  
+
g.add((ex.Mueller_Investigation, ex.involved, ex.Rick_Gates))
# 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.  
+
g.add((ex.Mueller_Investigation, ex.involved, ex.George_Papadopoulos))
 +
g.add((ex.Mueller_Investigation, ex.involved, ex.Michael_Flynn))
 +
g.add((ex.Mueller_Investigation, ex.involved, ex.Michael_Cohen))
 +
g.add((ex.Mueller_Investigation, ex.involved, ex.Roger_Stone))
  
# Address URI - CadeAdress
+
# --- Paul Manafort ---
 +
#Paul Manafort was business partner of Rick Gates.
 +
g.add((ex.Paul_Manafort, ex.businessManager, ex.Rick_Gates))
 +
# He was campaign chairman for Trump
 +
g.add((ex.Paul_Manafort, ex.campaignChairman, ex.Donald_Trump))
  
g.add((ex.Cade_Tracey, ex.address, ex.CadeAddress))
+
# He was charged with money laundering, tax evasion, and foreign lobbying.
g.add((ex.CadeAddress, RDF.type, ex.Address))
+
g.add((ex.Paul_Manafort, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.CadeAddress, ex.street, Literal("1516 Henry Street")))
+
g.add((ex.Paul_Manafort, ex.chargedWith, ex.TaxEvasion))
g.add((ex.CadeAddress, ex.city, ex.Berkeley))
+
g.add((ex.Paul_Manafort, ex.chargedWith, ex.ForeignLobbying))
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
+
# He was convicted for bank and tax fraud.
 +
g.add((ex.Paul_Manafort, ex.convictedFor, ex.BankFraud))
 +
g.add((ex.Paul_Manafort, ex.convictedFor, ex.TaxFraud))
  
# Blank node for Address.
+
# He pleaded guilty to conspiracy.
address = BNode()
+
g.add((ex.Paul_Manafort, ex.pleadGuiltyTo, ex.Conspiracy))
g.add((ex.Cade_Tracey, ex.address, address))
+
# He was sentenced to prison.
g.add((address, RDF.type, ex.Address))
+
g.add((ex.Paul_Manafort, ex.sentencedTo, ex.Prison))
g.add((address, ex.street, Literal("1516 Henry Street", datatype=XSD.string)))
+
# He negotiated a plea agreement.
g.add((address, ex.city, ex.Berkeley))
+
g.add((ex.Paul_Manafort, ex.negoiated, ex.PleaBargain))
g.add((address, ex.state, ex.California))
 
g.add((address, ex.postalCode, Literal("94709", datatype=XSD.string)))
 
g.add((address, ex.country, ex.USA))
 
  
 +
# --- Rick Gates ---
 +
#Rick Gates was charged with money laundering, tax evasion and foreign lobbying.
 +
g.add((ex.Rick_Gates, ex.chargedWith, ex.MoneyLaundering))
 +
g.add((ex.Rick_Gates, ex.chargedWith, ex.TaxEvasion))
 +
g.add((ex.Rick_Gates, ex.chargedWith, ex.ForeignLobbying))
  
# Solution 5 using existing vocabularies for address
+
#He pleaded guilty to conspiracy and lying to FBI.
 +
g.add((ex.Rick_Gates, ex.pleadGuiltyTo, ex.Conspiracy))
 +
g.add((ex.Rick_Gates, ex.pleadGuiltyTo, ex.LyingToFBI))
  
# (in this case https://schema.org/PostalAddress from schema.org).
+
#Use the serialize method to write out the model in different formats on screen
# Also using existing ontology for places like California. (like http://dbpedia.org/resource/California from dbpedia.org)
+
print(g.serialize(format="ttl"))
 +
# g.serialize("lab1.ttl", format="ttl") #or to file
  
schema = "https://schema.org/"
+
#Loop through the triples in the model to print out all triples that have pleading guilty as predicate
dbp = "https://dpbedia.org/resource/"
+
for subject, object in g[ : ex.pleadGuiltyTo : ]:
 +
    print(subject, ex.pleadGuiltyTo, object)
  
g.add((ex.Cade_Tracey, schema.address, ex.CadeAddress))
+
# Michael Cohen, Michael Flynn and the lying is part of lab 2 and therefore the answer is not provided this week
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))
 
  
 +
#Write a method (function) that submits your model for rendering and saves the returned image to file.
 +
import requests
 +
import shutil
 +
 +
def graphToImage(graph):
 +
    data = {"rdf":graph, "from":"ttl", "to":"png"}
 +
    link = "http://www.ldf.fi/service/rdf-grapher"
 +
    response = requests.get(link, params = data, stream=True)
 +
    # print(response.content)
 +
    print(response.raw)
 +
    with open("lab1.png", "wb") as fil:
 +
        shutil.copyfileobj(response.raw, fil)
 +
 +
graph = g.serialize(format="ttl")
 +
graphToImage(graph)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Typed Literals===
+
==RDF programming with RDFlib (Lab 2)==
 +
 
 
<syntaxhighlight>
 
<syntaxhighlight>
from rdflib import Graph, Literal, Namespace
+
 
from rdflib.namespace import XSD
+
from rdflib import Graph, URIRef, Namespace, Literal, XSD, BNode
 +
from rdflib.collection import Collection
 +
 
 
g = Graph()
 
g = Graph()
ex = Namespace("http://example.org/")
+
g.parse("lab1.ttl", format="ttl") #Retrives the triples from lab 1
 +
 
 +
ex = Namespace('http://example.org/')
  
g.add((ex.Cade, ex.age, Literal(27, datatype=XSD.integer)))
+
# --- Michael Cohen ---
g.add((ex.Cade, ex.gpa, Literal(3.3, datatype=XSD.float)))
+
#Michael Cohen was Donald Trump's attorney.
g.add((ex.Cade, FOAF.name, Literal("Cade Tracey", datatype=XSD.string)))
+
g.add((ex.Michael_Cohen, ex.attorneyTo, ex.Donald_Trump))
g.add((ex.Cade, ex.birthday, Literal("2006-01-01", datatype=XSD.date)))
+
#He pleaded guilty to lying to the FBI.
</syntaxhighlight>
+
g.add((ex.Michael_Cohen, ex.pleadGuiltyTo, ex.LyingToCongress))
  
 +
# --- Michael Flynn ---
 +
#Michael Flynn was adviser to Trump.
 +
g.add((ex.Michael_Flynn, ex.adviserTo, ex.Donald_Trump))
 +
#He pleaded guilty to lying to the FBI.
 +
g.add((ex.Michael_Flynn, ex.pleadGuiltyTo, ex.LyingToFBI))
 +
# He negotiated a plea agreement.
 +
g.add((ex.Michael_Flynn, ex.negoiated, ex.PleaBargain))
  
===Writing and reading graphs/files===
+
#How can you modify your knowledge graph to account for the different lying?
 +
#Remove these to not have duplicates
 +
g.remove((ex.Michael_Flynn, ex.pleadGuiltyTo, ex.LyingToFBI))
 +
g.remove((ex.Michael_Flynn, ex.negoiated, ex.PleaBargain))
 +
g.remove((ex.Rick_Gates, ex.pleadGuiltyTo, ex.LyingToFBI))
 +
g.remove((ex.Rick_Gates, ex.pleadGuiltyTo, ex.Conspiracy))
 +
g.remove((ex.Rick_Gates, ex.chargedWith, ex.ForeignLobbying))
 +
g.remove((ex.Rick_Gates, ex.chargedWith, ex.MoneyLaundering))
 +
g.remove((ex.Rick_Gates, ex.chargedWith, ex.TaxEvasion))
 +
g.remove((ex.Michael_Cohen, ex.pleadGuiltyTo, ex.LyingToCongress))
  
<syntaxhighlight>
+
# --- Michael Flynn ---
  # Writing the graph to a file on your system. Possible formats = turtle, n3, xml, nt.
+
FlynnLying = BNode()
g.serialize(destination="triples.txt", format="turtle")
+
g.add((FlynnLying, ex.crime, ex.LyingToFBI))
 +
g.add((FlynnLying, ex.pleadGulityOn, Literal("2017-12-1", datatype=XSD.date)))
 +
g.add((FlynnLying, ex.liedAbout, Literal("His communications with a former Russian ambassador during the presidential transition", datatype=XSD.string)))
 +
g.add((FlynnLying, ex.pleaBargain, Literal("true", datatype=XSD.boolean)))
 +
g.add((ex.Michael_Flynn, ex.pleadGuiltyTo, FlynnLying))
  
  # Parsing a local file
+
# --- Rick Gates ---
parsed_graph = g.parse(location="triples.txt", format="turtle")
+
GatesLying = BNode()
 +
Crimes = BNode()
 +
Charged = BNode()
 +
Collection(g, Crimes, [ex.LyingToFBI, ex.Conspiracy])
 +
Collection(g, Charged, [ex.ForeignLobbying, ex.MoneyLaundering, ex.TaxEvasion])
 +
g.add((GatesLying, ex.crime, Crimes))
 +
g.add((GatesLying, ex.chargedWith, Charged))
 +
g.add((GatesLying, ex.pleadGulityOn, Literal("2018-02-23", datatype=XSD.date)))
 +
g.add((GatesLying, ex.pleaBargain, Literal("true", datatype=XSD.boolean)))
 +
g.add((ex.Rick_Gates, ex.pleadGuiltyTo, GatesLying))
  
  # Parsing a remote endpoint like Dbpedia
+
# --- Michael Cohen ---
dbpedia_graph = g.parse("http://dbpedia.org/resource/Pluto")
+
CohenLying = BNode()
</syntaxhighlight>
+
g.add((CohenLying, ex.crime, ex.LyingToCongress))
 +
g.add((CohenLying, ex.liedAbout, ex.TrumpRealEstateDeal))
 +
g.add((CohenLying, ex.prosecutorsAlleged, Literal("In an August 2017 letter Cohen sent to congressional committees investigating Russian election interference, he falsely stated that the project ended in January 2016", datatype=XSD.string)))
 +
g.add((CohenLying, ex.mullerInvestigationAlleged, Literal("Cohen falsely stated that he had never agreed to travel to Russia for the real estate deal and that he did not recall any contact with the Russian government about the project", datatype=XSD.string)))
 +
g.add((CohenLying, ex.pleadGulityOn, Literal("2018-11-29", datatype=XSD.date)))
 +
g.add((CohenLying, ex.pleaBargain, Literal("true", datatype=XSD.boolean)))
 +
g.add((ex.Michael_Cohen, ex.pleadGuiltyTo, CohenLying))
  
 +
print(g.serialize(format="ttl"))
  
===Collection Example===
+
#Save (serialize) your graph to a Turtle file.
 +
# g.serialize("lab2.ttl", format="ttl")
  
<syntaxhighlight>
+
#Add a few triples to the Turtle file with more information about Donald Trump.
from rdflib import Graph, Namespace
+
'''
from rdflib.collection import Collection
+
ex:Donald_Trump ex:address [ ex:city ex:Palm_Beach ;
 +
            ex:country ex:United_States ;
 +
            ex:postalCode 33480 ;
 +
            ex:residence ex:Mar_a_Lago ;
 +
            ex:state ex:Florida ;
 +
            ex:streetName "1100 S Ocean Blvd"^^xsd:string ] ;
 +
    ex:previousAddress [ ex:city ex:Washington_DC ;
 +
            ex:country ex:United_States ;
 +
            ex:phoneNumber "1 202 456 1414"^^xsd:integer ;
 +
            ex:postalCode "20500"^^xsd:integer ;
 +
            ex:residence ex:The_White_House ;
 +
            ex:streetName "1600 Pennsylvania Ave."^^xsd:string ];
 +
    ex:marriedTo ex:Melania_Trump;
 +
    ex:fatherTo (ex:Ivanka_Trump ex:Donald_Trump_Jr ex: ex:Tiffany_Trump ex:Eric_Trump ex:Barron_Trump).
 +
'''
  
 +
#Read (parse) the Turtle file back into a Python program, and check that the new triples are there
 +
def serialize_Graph():
 +
    newGraph = Graph()
 +
    newGraph.parse("lab2.ttl")
 +
    print(newGraph.serialize())
  
# Sometimes we want to add many objects or subjects for the same predicate at once.
+
# serialize_Graph() #Don't need this to run until after adding the triples above to the ttl file
# 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()
+
#Write a method (function) that starts with Donald Trump prints out a graph depth-first to show how the other graph nodes are connected to him
g.add((ex.Emma, ex.visit, b))
+
visited_nodes = set()
Collection(g, b,
 
    [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])
 
  
# OR
+
def create_Tree(model, nodes):
 +
    #Traverse the model breadth-first to create the tree.
 +
    global visited_nodes
 +
    tree = Graph()
 +
    children = set()
 +
    visited_nodes |= set(nodes)
 +
    for s, p, o in model:
 +
        if s in nodes and o not in visited_nodes:
 +
            tree.add((s, p, o))
 +
            visited_nodes.add(o)
 +
            children.add(o)
 +
        if o in nodes and s not in visited_nodes:
 +
            invp = URIRef(f'{p}_inv') #_inv represents inverse of
 +
            tree.add((o, invp, s))
 +
            visited_nodes.add(s)
 +
            children.add(s)
 +
    if len(children) > 0:
 +
        children_tree = create_Tree(model, children)
 +
        for triple in children_tree:
 +
            tree.add(triple)
 +
    return tree
  
g.add((ex.Emma, ex.visit, ex.EmmaVisits))
+
def print_Tree(tree, root, indent=0):
Collection(g, ex.EmmaVisits,
+
    #Print the tree depth-first.
     [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])
+
    print(str(root))
 +
    for s, p, o in tree:
 +
        if s==root:
 +
            print('    '*indent + '  ' + str(p), end=' ')
 +
            print_Tree(tree, o, indent+1)
 +
      
 +
tree = create_Tree(g, [ex.Donald_Trump])
 +
print_Tree(tree, ex.Donald_Trump)
  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Lecture 3: SPARQL==
+
==SPARQL Programming (Lab 4)==
 +
'''NOTE: These tasks were performed on the old dataset, with the new dataset, some of these answers would be different.'''
 +
<syntaxhighlight>
 +
 
 +
from rdflib import Graph, Namespace, RDF, FOAF
 +
from SPARQLWrapper import SPARQLWrapper, JSON, POST, GET, TURTLE
 +
 
 +
g = Graph()
 +
g.parse("Russia_investigation_kg.ttl")
 +
 
 +
# ----- RDFLIB -----
 +
ex = Namespace('http://example.org#')
 +
 
 +
NS = {
 +
    '': ex,
 +
    'rdf': RDF,
 +
    'foaf': FOAF,
 +
}
  
===SPARQL queries from the lecture===
+
# Print out a list of all the predicates used in your graph.
<syntaxhighlight>
+
task1 = g.query("""
SELECT DISTINCT ?p WHERE {
+
SELECT DISTINCT ?p WHERE{
 
     ?s ?p ?o .
 
     ?s ?p ?o .
 
}
 
}
</syntaxhighlight>
+
""", initNs=NS)
  
<syntaxhighlight>
+
print(list(task1))
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 
  
SELECT DISTINCT ?t WHERE {
+
# Print out a sorted list of all the presidents represented in your graph.
     ?s rdf:type ?t .
+
task2 = g.query("""
 +
SELECT DISTINCT ?president WHERE{
 +
     ?s :president ?president .
 
}
 
}
</syntaxhighlight>
+
ORDER BY ?president
 +
""", initNs=NS)
 +
 
 +
print(list(task2))
 +
 
 +
# Create dictionary (Python dict) with all the represented presidents as keys. For each key, the value is a list of names of people indicted under that president.
 +
task3_dic = {}
  
<syntaxhighlight>
+
task3 = g.query("""
PREFIX owl: <http://www.w3.org/2002/07/owl#>
+
SELECT ?president ?person WHERE{
CONSTRUCT {
+
     ?s :president ?president;
    ?s owl:sameAs ?o2 .
+
      :name ?person;
} WHERE {
+
      :outcome :indictment.
     ?s owl:sameAs ?o .
 
    FILTER(REGEX(STR(?o), "^http://www\\.", "s"))
 
    BIND(URI(REPLACE(STR(?o), "^http://www\\.", "http://", "s")) AS ?o2)
 
 
}
 
}
</syntaxhighlight>
+
""", initNs=NS)
  
===Select all contents of lists (rdfllib.Collection)===
+
for president, person in task3:
<syntaxhighlight>
+
    if president not in task3_dic:
 +
        task3_dic[president] = [person]
 +
    else:
 +
        task3_dic[president].append(person)
  
# 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.
+
print(task3_dic)
  
PREFIX ex:  <http://example.org/>
+
# Use an ASK query to investigate whether Donald Trump has pardoned more than 5 people.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 
  
SELECT ?visit
+
# This task is a lot trickier than it needs to be. As far as I'm aware RDFLib has no HAVING support, so a query like this:
WHERE {
+
task4 = g.query("""
  ex:Emma ex:visit/rdf:rest*/rdf:first ?visit
+
ASK {
 +
  SELECT (COUNT(?s) as ?count) WHERE{
 +
    ?s :pardoned :true;
 +
    :president :Bill_Clinton  .
 +
    }
 +
    HAVING (?count > 5)
 
}
 
}
</syntaxhighlight>
+
""", initNs=NS)
 +
 
 +
print(task4.askAnswer)
 +
 
 +
# Which works fine in Blazegraph and is a valid SPARQL query will always provide false in RDFLib, cause it uses HAVING. Instead you have to use a nested SELECT query like below, where you use FILTER instead of HAVING. Donald Trump has no pardons, so I have instead chosen Bill Clinton (which has 13 pardons) to check if the query works.
 +
 
 +
task4 = g.query("""
 +
    ASK{
 +
        SELECT ?count WHERE{{
 +
          SELECT (COUNT(?s) as ?count) WHERE{
 +
            ?s :pardoned :true;
 +
                  :president :Bill_Clinton  .
 +
                }}
 +
        FILTER (?count > 5)
 +
        }
 +
    }
 +
""", initNs=NS)
 +
 
 +
print(task4.askAnswer)
 +
 
 +
# Use a DESCRIBE query to create a new graph with information about Donald Trump. Print out the graph in Turtle format.
 +
 
 +
# By all accounts, it seems DESCRIBE queries are yet to be implemented in RDFLib, but they are attempting to implement it: https://github.com/RDFLib/rdflib/pull/2221 (Issue and proposed solution raised) & https://github.com/RDFLib/rdflib/commit/2325b4a81724c1ccee3a131067db4fbf9b4e2629 (Solution committed to RDFLib). This solution does not work. However, this proposed solution should work if DESCRIBE is implemented in RDFLib
 +
 
 +
# task5 = g.query("""
 +
# DESCRIBE :Donald_Trump
 +
# """, initNs=NS)
 +
 
 +
# print(task5.serialize())
  
==Lecture 4- SPARQL PROGRAMMING==
+
# ----- SPARQLWrapper -----
  
===SELECTING data from Blazegraph via Python===
+
namespace = "kb" #Default namespace
<syntaxhighlight>
+
sparql = SPARQLWrapper("http://localhost:9999/blazegraph/namespace/"+ namespace + "/sparql") #Replace localhost:9999 with your URL
  
from SPARQLWrapper import SPARQLWrapper, JSON
+
# The current dates are URIs, we would want to change them to Literals with datatype "date" for task 1 & 2
 +
update_str = """
 +
    PREFIX ns1: <http://example.org#>
  
# This creates a server connection to the same URL that contains the graphic interface for Blazegraph.  
+
    DELETE {
# You also need to add "sparql" to end of the URL like below.
+
        ?s ns1:cp_date ?cp;
 +
            ns1:investigation_end ?end;
 +
            ns1:investigation_start ?start.
 +
    }
 +
    INSERT{
 +
        ?s ns1:cp_date ?cpDate;
 +
            ns1:investigation_end ?endDate;
 +
            ns1:investigation_start ?startDate.
 +
    }
 +
    WHERE{
 +
        ?s ns1:cp_date ?cp . #Date conviction was recieved
 +
        BIND (replace(str(?cp), str(ns1:), "")  AS ?cpRemoved)
 +
        BIND (STRDT(STR(?cpRemoved), xsd:date) AS ?cpDate)
 +
       
 +
        ?s ns1:investigation_end ?end . #Investigation End
 +
        BIND (replace(str(?end), str(ns1:), "")  AS ?endRemoved)
 +
        BIND (STRDT(STR(?endRemoved), xsd:date) AS ?endDate)
 +
       
 +
        ?s ns1:investigation_start ?start . #Investigation Start
 +
        BIND (replace(str(?start), str(ns1:), "")  AS ?startRemoved)
 +
        BIND (STRDT(STR(?startRemoved), xsd:date) AS ?startDate)
 +
}"""
  
sparql = SPARQLWrapper("http://localhost:9999/blazegraph/sparql")
+
sparql.setQuery(update_str)
 +
sparql.setMethod(POST)
 +
sparql.query()
  
# SELECT all triples in the database.
+
# Ask whether there was an ongoing indictment on the date 1990-01-01.
 +
sparql.setQuery("""
 +
    PREFIX ns1: <http://example.org#>
 +
    ASK {
 +
        SELECT ?end ?start
 +
        WHERE{
 +
            ?s ns1:investigation_end ?end;
 +
              ns1:investigation_start ?start;
 +
              ns1:outcome ns1:indictment.
 +
            FILTER(?start <= "1990-01-01"^^xsd:date && ?end >= "1990-01-01"^^xsd:date)
 +
    }
 +
    }
 +
""")
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
print(f"Are there any investigation on the 1990-01-01: {results['boolean']}")
  
 +
# List ongoing indictments on that date 1990-01-01.
 
sparql.setQuery("""
 
sparql.setQuery("""
     SELECT DISTINCT ?p WHERE {
+
    PREFIX ns1: <http://example.org#>
    ?s ?p ?o.
+
     SELECT ?s
 +
    WHERE{
 +
        ?s ns1:investigation_end ?end;
 +
          ns1:investigation_start ?start;
 +
          ns1:outcome ns1:indictment.
 +
        FILTER(?start <= "1990-01-01"^^xsd:date && ?end >= "1990-01-01"^^xsd:date)
 
     }
 
     }
 
""")
 
""")
 +
 
sparql.setReturnFormat(JSON)
 
sparql.setReturnFormat(JSON)
 
results = sparql.query().convert()
 
results = sparql.query().convert()
  
 +
print("The ongoing investigations on the 1990-01-01 are:")
 
for result in results["results"]["bindings"]:
 
for result in results["results"]["bindings"]:
     print(result["p"]["value"])
+
     print(result["s"]["value"])
 +
 
 +
# Describe investigation number 100 (muellerkg:investigation_100).
 +
sparql.setQuery("""
 +
    PREFIX ns1: <http://example.org#>
 +
    DESCRIBE ns1:investigation_100
 +
""")
 +
 
 +
sparql.setReturnFormat(TURTLE)
 +
results = sparql.query().convert()
  
# SELECT all interests of Cade
+
print(results.serialize())
  
 +
# Print out a list of all the types used in your graph.
 
sparql.setQuery("""
 
sparql.setQuery("""
     PREFIX ex: <http://example.org/>
+
     PREFIX ns1: <http://example.org#>
     SELECT DISTINCT ?interest WHERE {
+
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    ex:Cade ex:interest ?interest.
+
 
 +
     SELECT DISTINCT ?types
 +
    WHERE{
 +
        ?s rdf:type ?types .  
 
     }
 
     }
 
""")
 
""")
 +
 
sparql.setReturnFormat(JSON)
 
sparql.setReturnFormat(JSON)
 
results = sparql.query().convert()
 
results = sparql.query().convert()
 +
 +
rdf_Types = []
  
 
for result in results["results"]["bindings"]:
 
for result in results["results"]["bindings"]:
     print(result["interest"]["value"])
+
     rdf_Types.append(result["types"]["value"])
</syntaxhighlight>
+
 
 +
print(rdf_Types)
  
===Updating data from Blazegraph via Python===
+
# Update the graph to that every resource that is an object in a muellerkg:investigation triple has the rdf:type muellerkg:Investigation.
<syntaxhighlight>
+
update_str = """
from SPARQLWrapper import SPARQLWrapper, POST, DIGEST
+
    PREFIX ns1: <http://example.org#>
 +
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  
namespace = "kb"
+
    INSERT{
sparql = SPARQLWrapper("http://localhost:9999/blazegraph/namespace/"+ namespace + "/sparql")
+
        ?invest rdf:type ns1:Investigation .
 +
    }
 +
    WHERE{
 +
        ?s ns1:investigation ?invest .
 +
}"""
  
 +
sparql.setQuery(update_str)
 
sparql.setMethod(POST)
 
sparql.setMethod(POST)
 +
sparql.query()
 +
 +
#To Test
 
sparql.setQuery("""
 
sparql.setQuery("""
     PREFIX ex: <http://example.org/>
+
    prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
     INSERT DATA{
+
     PREFIX ns1: <http://example.org#>
    ex:Cade ex:interest ex:Mathematics.
+
 
 +
     ASK{
 +
        ns1:watergate rdf:type ns1:Investigation.
 
     }
 
     }
 
""")
 
""")
  
results = sparql.query()
+
sparql.setReturnFormat(JSON)
print(results.response.read())
+
results = sparql.query().convert()
 +
print(results['boolean'])
  
 +
# Update the graph to that every resource that is an object in a muellerkg:person triple has the rdf:type muellerkg:IndictedPerson.
 +
update_str = """
 +
    PREFIX ns1: <http://example.org#>
 +
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 +
 +
    INSERT{
 +
        ?person rdf:type ns1:IndictedPerson .
 +
    }
 +
    WHERE{
 +
        ?s ns1:person ?person .
 +
}"""
 +
 +
sparql.setQuery(update_str)
 +
sparql.setMethod(POST)
 +
sparql.query()
 +
 +
#To test, run the query in the above task, replacing the ask query with e.g. ns1:Deborah_Gore_Dean rdf:type ns1:IndictedPerson
 +
 +
# Update the graph so all the investigation nodes (such as muellerkg:watergate) become the subject in a dc:title triple with the corresponding string (watergate) as the literal.
 +
update_str = """
 +
    PREFIX ns1: <http://example.org#>
 +
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 +
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
 +
 +
    INSERT{
 +
        ?invest dc:title ?investString.
 +
    }
 +
    WHERE{
 +
        ?s ns1:investigation ?invest .
 +
        BIND (replace(str(?invest), str(ns1:), "")  AS ?investString)
 +
}"""
 +
 +
sparql.setQuery(update_str)
 +
sparql.setMethod(POST)
 +
sparql.query()
 +
 +
#Same test as above, replace it with e.g. ns1:watergate dc:title "watergate"
 +
 +
# Print out a sorted list of all the indicted persons represented in your graph.
 +
sparql.setQuery("""
 +
    PREFIX ns1: <http://example.org#>
 +
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  
 +
    SELECT ?name
 +
    WHERE{
 +
    ?s  ns1:person ?name;
 +
        ns1:outcome ns1:indictment.
 +
    }
 +
    ORDER BY ?name
 +
""")
  
</syntaxhighlight>
+
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
  
== Lecture 5: RDFS==
+
names = []
  
===RDFS inference with RDFLib===
+
for result in results["results"]["bindings"]:
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.
+
    names.append(result["name"]["value"])
  
[https://owl-rl.readthedocs.io/en/latest/owlrl.html OWL-RL documentation.]
+
print(names)
  
Example program to get started:
+
# Print out the minimum, average and maximum indictment days for all the indictments in the graph.
<syntaxhighlight>
+
sparql.setQuery("""
import rdflib.plugins.sparql.update
+
    prefix xsd: <http://www.w3.org/2001/XMLSchema#>
import owlrl.RDFSClosure
+
    PREFIX ns1: <http://example.org#>
  
g = rdflib.Graph()
+
    SELECT (AVG(?daysRemoved) as ?avg) (MAX(?daysRemoved) as ?max) (MIN(?daysRemoved) as ?min)  WHERE{
 +
        ?s  ns1:indictment_days ?days;
 +
            ns1:outcome ns1:indictment.
 +
   
 +
    BIND (replace(str(?days), str(ns1:), "")  AS ?daysR)
 +
    BIND (STRDT(STR(?daysR), xsd:float) AS ?daysRemoved)
 +
}
 +
""")
  
ex = rdflib.Namespace('http://example.org#')
+
sparql.setReturnFormat(JSON)
g.bind('', ex)
+
results = sparql.query().convert()
  
g.update("""
+
for result in results["results"]["bindings"]:
PREFIX ex: <http://example.org#>
+
    print(f'The longest an investigation lasted was: {result["max"]["value"]}')
PREFIX owl: <http://www.w3.org/2002/07/owl#>
+
     print(f'The shortest an investigation lasted was: {result["min"]["value"]}')
INSERT DATA {
+
     print(f'The average investigation lasted: {result["avg"]["value"]}')
     ex:Socrates rdf:type ex:Man .
 
     ex:Man rdfs:subClassOf ex:Mortal .
 
}""")
 
  
# The next three lines add inferred triples to g.
+
# Print out the minimum, average and maximum indictment days for all the indictments in the graph per investigation.
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
+
sparql.setQuery("""
rdfs.closure()
+
    prefix xsd: <http://www.w3.org/2001/XMLSchema#>
rdfs.flush_stored_triples()
+
    PREFIX ns1: <http://example.org#>
  
b = g.query("""
+
    SELECT ?investigation (AVG(?daysRemoved) as ?avg) (MAX(?daysRemoved) as ?max) (MIN(?daysRemoved) as ?min)  WHERE{
PREFIX ex: <http://example.org#>
+
    ?s  ns1:indictment_days ?days;
ASK {
+
        ns1:outcome ns1:indictment;
     ex:Socrates rdf:type ex:Mortal .
+
        ns1:investigation ?investigation.
}  
+
   
 +
     BIND (replace(str(?days), str(ns1:), "")  AS ?daysR)
 +
    BIND (STRDT(STR(?daysR), xsd:float) AS ?daysRemoved)
 +
    }
 +
    GROUP BY ?investigation
 
""")
 
""")
print('Result: ' + bool(b))
+
 
 +
sparql.setReturnFormat(JSON)
 +
results = sparql.query().convert()
 +
 
 +
for result in results["results"]["bindings"]:
 +
    print(f'{result["investigation"]["value"]} - min: {result["min"]["value"]}, max: {result["max"]["value"]}, avg: {result["avg"]["value"]}')
 +
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Languaged tagged RDFS labels===  
+
==CSV To RDF (Lab 5)==
 
<syntaxhighlight>
 
<syntaxhighlight>
from rdflib import Graph, Namespace, Literal
+
 
from rdflib.namespace import RDFS
+
#Imports
 +
import re
 +
from pandas import *
 +
from numpy import nan
 +
from rdflib import Graph, Namespace, URIRef, Literal, RDF, XSD, FOAF
 +
from spotlight import SpotlightException, annotate
 +
 
 +
SERVER = "https://api.dbpedia-spotlight.org/en/annotate"
 +
# Test around with the confidence, and see how many names changes depending on the confidence. However, be aware that anything lower than this (0.83) it will replace James W. McCord and other names that includes James with LeBron James
 +
CONFIDENCE = 0.83
 +
 
 +
def annotate_entity(entity, filters={'types': 'DBpedia:Person'}):
 +
annotations = []
 +
try:
 +
annotations = annotate(address=SERVER, text=entity, confidence=CONFIDENCE, filters=filters)
 +
except SpotlightException as e:
 +
print(e)
 +
return annotations
  
 
g = Graph()
 
g = Graph()
 
ex = Namespace("http://example.org/")
 
ex = Namespace("http://example.org/")
 +
g.bind("ex", ex)
  
g.add((ex.France, RDFS.label, Literal("Frankrike", lang="no")))
+
#Pandas' read_csv function to load russia-investigation.csv
g.add((ex.France, RDFS.label, Literal("France", lang="en")))
+
df = read_csv("russia-investigation.csv")
g.add((ex.France, RDFS.label, Literal("Francia", lang="es")))
+
#Replaces all instances of nan to None type with numpy's nan
 +
df = df.replace(nan, None)
  
 +
#Function that prepares the values to be added to the graph as a URI or Literal
 +
def prepareValue(row):
 +
if row == None: #none type
 +
value = Literal(row)
 +
elif isinstance(row, str) and re.match(r'\d{4}-\d{2}-\d{2}', row): #date
 +
value = Literal(row, datatype=XSD.date)
 +
elif isinstance(row, bool): #boolean value (true / false)
 +
value = Literal(row, datatype=XSD.boolean)
 +
elif isinstance(row, int): #integer
 +
value = Literal(row, datatype=XSD.integer)
 +
elif isinstance(row, str): #string
 +
value = URIRef(ex + row.replace('"', '').replace(" ", "_").replace(",","").replace("-", "_"))
 +
elif isinstance(row, float): #float
 +
value = Literal(row, datatype=XSD.float)
  
</syntaxhighlight>
+
return value
 +
 
 +
#Convert the non-semantic CSV dataset into a semantic RDF
 +
def csv_to_rdf(df):
 +
for index, row in df.iterrows():
 +
id = URIRef(ex + "Investigation_" + str(index))
 +
investigation = prepareValue(row["investigation"])
 +
investigation_start = prepareValue(row["investigation-start"])
 +
investigation_end = prepareValue(row["investigation-end"])
 +
investigation_days = prepareValue(row["investigation-days"])
 +
indictment_days = prepareValue(row["indictment-days "])
 +
cp_date = prepareValue(row["cp-date"])
 +
cp_days = prepareValue(row["cp-days"])
 +
overturned = prepareValue(row["overturned"])
 +
pardoned = prepareValue(row["pardoned"])
 +
american = prepareValue(row["american"])
 +
outcome = prepareValue(row["type"])
 +
name_ex = prepareValue(row["name"])
 +
president_ex = prepareValue(row["president"])
  
== Lecture 6: RDFS Plus / OWL ==
+
#Spotlight Search
===RDFS Plus / OWL inference with RDFLib===  
+
name = annotate_entity(str(row['name']))
 +
                # Removing the period as some presidents won't be found with it
 +
president = annotate_entity(str(row['president']).replace(".", ""))
 +
 +
#Adds the tripples to the graph
 +
g.add((id, RDF.type, ex.Investigation))
 +
g.add((id, ex.investigation, investigation))
 +
g.add((id, ex.investigation_start, investigation_start))
 +
g.add((id, ex.investigation_end, investigation_end))
 +
g.add((id, ex.investigation_days, investigation_days))
 +
g.add((id, ex.indictment_days, indictment_days))
 +
g.add((id, ex.cp_date, cp_date))
 +
g.add((id, ex.cp_days, cp_days))
 +
g.add((id, ex.overturned, overturned))
 +
g.add((id, ex.pardoned, pardoned))
 +
g.add((id, ex.american, american))
 +
g.add((id, ex.outcome, outcome))
  
You can use the OWL-RL package again as for Lecture 5.
+
#Spotlight search
 +
#Name
 +
try:
 +
g.add((id, ex.person, URIRef(name[0]["URI"])))
 +
except:
 +
g.add((id, ex.person, name_ex))
  
Instead of:  
+
#President
<syntaxhighlight>
+
try:
# The next three lines add inferred triples to g.
+
g.add((id, ex.president, URIRef(president[0]["URI"])))
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
+
except:
rdfs.closure()
+
g.add((id, ex.president, president_ex))
rdfs.flush_stored_triples()
 
</syntaxhighlight>
 
you can write this to get both RDFS and basic RDFS Plus / OWL inference:
 
<syntaxhighlight>
 
# The next three lines add inferred triples to g.
 
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
 
owl.closure()
 
owl.flush_stored_triples()
 
</syntaxhighlight>
 
  
Example updates and queries:
+
csv_to_rdf(df)
<syntaxhighlight>
+
print(g.serialize())
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 
PREFIX owl: <http://www.w3.org/2002/07/owl#>
 
PREFIX ex: <http://example.org#>
 
  
INSERT DATA {
 
    ex:Socrates ex:hasWife ex:Xanthippe .
 
    ex:hasHusband owl:inverseOf ex:hasWife .
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
==SHACL (Lab 6)==
 
<syntaxhighlight>
 
<syntaxhighlight>
ASK {
 
  ex:Xanthippe ex:hasHusband ex:Socrates .
 
}
 
</syntaxhighlight>
 
  
<syntaxhighlight>
+
from pyshacl import validate
ASK {
+
from rdflib import Graph
  ex:Socrates ^ex:hasHusband ex:Xanthippe .
+
 
}
+
data_graph = Graph()
</syntaxhighlight>
+
# parses the Turtle examples from the lab
 +
data_graph.parse("data_graph.ttl")
  
<syntaxhighlight>
+
# Remember to test you need to change the rules so they conflict with the data graph (or vice versa). For example, change "exactly one name" to have exactly two, and see the output
INSERT DATA {
+
shape_graph = """
    ex:hasWife rdfs:subPropertyOf ex:hasSpouse .
+
@prefix ex: <http://example.org/> .
    ex:hasSpouse rdf:type owl:SymmetricProperty .  
+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
}
+
@prefix sh: <http://www.w3.org/ns/shacl#> .
</syntaxhighlight>
+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
 +
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
  
<syntaxhighlight>
+
ex:LabTasks_Shape
ASK {
+
    a sh:NodeShape ;
  ex:Socrates ex:hasSpouse ex:Xanthippe .
+
    sh:targetClass ex:PersonUnderInvestigation ;
}
+
    sh:property [
</syntaxhighlight>
+
        sh:path foaf:name ;
 +
        sh:minCount 1 ; #Every person under investigation has exactly one name.
 +
        sh:maxCount 1 ; #Every person under investigation has exactly one name.
 +
        sh:datatype rdf:langString ; #All person names must be language-tagged
 +
    ] ;
 +
    sh:property [
 +
        sh:path ex:chargedWith ;
 +
        sh:nodeKind sh:IRI ; #The object of a charged with property must be a URI.
 +
        sh:class ex:Offense ; #The object of a charged with property must be an offense.
 +
    ] .
  
<syntaxhighlight>
+
# --- If you have more time tasks ---
ASK {
+
ex:MoreTime_Shape rdf:type sh:NodeShape;
  ex:Socrates ^ex:hasSpouse ex:Xanthippe .
+
    sh:targetClass ex:Indictment;
}
+
   
</syntaxhighlight>
+
    # The only allowed values for ex:american are true, false or unknown.
 +
    sh:property [
 +
        sh:path ex:american;
 +
        sh:pattern "(true|false|unknown)" ;
 +
    ] ;
 +
   
 +
    # The value of a property that counts days must be an integer.
 +
    sh:property [
 +
        sh:path ex:indictment_days;
 +
        sh:datatype xsd:integer;
 +
    ] ; 
 +
    sh:property [
 +
        sh:path ex:investigation_days;
 +
        sh:datatype xsd:integer;
 +
    ] ;
 +
   
 +
    # The value of a property that indicates a start date must be xsd:date.
 +
    sh:property [
 +
        sh:path ex:investigation_start;
 +
        sh:datatype xsd:date;
 +
    ] ;
  
== Lab 9 ==
+
    # The value of a property that indicates an end date must be xsd:date or unknown (tip: you can use sh:or (...) ).
 +
    sh:property [
 +
        sh:path ex:investigation_end;
 +
        sh:or (
 +
        [ sh:datatype xsd:date ]
 +
        [ sh:hasValue "unknown" ]
 +
    )] ;
 +
   
 +
    # Every indictment must have exactly one FOAF name for the investigated person.
 +
    sh:property [
 +
        sh:path foaf:name;
 +
        sh:minCount 1;
 +
        sh:maxCount 1;
 +
    ] ;
 +
   
 +
    # Every indictment must have exactly one investigated person property, and that person must have the type ex:PersonUnderInvestigation.
 +
    sh:property [
 +
        sh:path ex:investigatedPerson ;
 +
        sh:minCount 1 ;
 +
        sh:maxCount 1 ;
 +
        sh:class ex:PersonUnderInvestigation ;
 +
        sh:nodeKind sh:IRI ;
 +
    ] ;
  
===Download from BlazeGraph===
+
    # No URI-s can contain hyphens ('-').
 +
    sh:property [
 +
        sh:path ex:outcome ;
 +
        sh:nodeKind sh:IRI ;
 +
        sh:pattern "^[^-]*$" ;
 +
    ] ;
  
<syntaxhighlight>
+
    # Presidents must be identified with URIs.
"""
+
    sh:property [
Dumps a database to a local RDF file.
+
        sh:path ex:president ;
You need to install the SPARQLWrapper package first...
+
        sh:class ex:President ;
 +
        sh:nodeKind sh:IRI ;
 +
    ] .
 
"""
 
"""
  
import datetime
+
shacl_graph = Graph()
from SPARQLWrapper import SPARQLWrapper, RDFXML
+
# parses the contents of a shape_graph made in the tasks
 +
shacl_graph.parse(data=shape_graph)
 +
 
 +
# uses pySHACL's validate method to apply the shape_graph constraints to the data_graph
 +
results = validate(
 +
    data_graph,
 +
    shacl_graph=shacl_graph,
 +
    inference='both'
 +
)
  
# your namespace, the default is 'kb'
+
# prints out the validation result
ns = 'kb'
+
boolean_value, results_graph, results_text = results
  
# the SPARQL endpoint
+
# print(boolean_value)
endpoint = 'http://info216.i2s.uib.no/bigdata/namespace/' + ns + '/sparql'
+
print(results_graph.serialize(format='ttl'))
 +
# print(results_text)
  
# - the endpoint just moved, the old one was:
+
#Write a SPARQL query to print out each distinct sh:resultMessage in the results_graph
# endpoint = 'http://i2s.uib.no:8888/bigdata/namespace/' + ns + '/sparql'
+
distinct_messages = """
 +
PREFIX sh: <http://www.w3.org/ns/shacl#>
  
# create wrapper
+
SELECT DISTINCT ?message WHERE {
wrapper = SPARQLWrapper(endpoint)
+
    [] sh:result ?errorBlankNode.
 +
    ?errorBlankNode sh:resultMessage ?message.   
  
# prepare the SPARQL update
+
    # Alternativ and cleaner solution, look at https://www.w3.org/TR/sparql11-query/#pp-language (9.1 Property Path Syntax)
wrapper.setQuery('CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }')
+
    # [] sh:result / sh:resultMessage ?message .
wrapper.setReturnFormat(RDFXML)
+
}
 +
"""
 +
messages = results_graph.query(distinct_messages)
 +
for row in messages:
 +
    print(row.message)
  
# execute the SPARQL update and convert the result to an rdflib.Graph
+
#each sh:resultMessage in the results_graph once, along with the number of times that message has been repeated in the results
graph = wrapper.query().convert()
+
count_messages = """
 +
PREFIX sh: <http://www.w3.org/ns/shacl#>
  
# the destination file, with code to make it timestamped
+
SELECT ?message (COUNT(?node) AS ?num_messages) WHERE {
destfile = 'rdf_dumps/slr-kg4news-' + datetime.datetime.now().strftime('%Y%m%d-%H%M') + '.rdf'
+
    [] sh:result ?errorBlankNode .
 +
    ?errorBlankNode sh:resultMessage ?message ;
 +
                    sh:focusNode ?node .
 +
}
 +
GROUP BY ?message
 +
ORDER BY DESC(?count) ?message
 +
"""
  
# serialize the result to file
+
messages = results_graph.query(count_messages)
graph.serialize(destination=destfile, format='ttl')
+
for row in messages:
 +
    print(f"COUNT: {row.num_messages} | MESSAGE: {row.message}")
  
# report and quit
 
print('Wrote %u triples to file %s .' %
 
      (len(res), destfile))
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Semantic Lifting - CSV==
+
==RDFS (Lab 7)==
 +
<syntaxhighlight>
  
<syntaxhighlight>
+
import owlrl
from rdflib import Graph, Literal, Namespace, URIRef
+
from rdflib import Graph, RDF, Namespace, FOAF, RDFS
from rdflib.namespace import RDF, FOAF, RDFS, OWL
 
import pandas as pd
 
  
 
g = Graph()
 
g = Graph()
ex = Namespace("http://example.org/")
+
ex = Namespace('http://example.org/')
 +
 
 
g.bind("ex", ex)
 
g.bind("ex", ex)
 +
g.bind("foaf", FOAF)
  
# Load the CSV data as a pandas Dataframe.
+
NS = {
csv_data = pd.read_csv("task1.csv")
+
    'ex': ex,
 +
    'rdf': RDF,
 +
    'rdfs': RDFS,
 +
    'foaf': FOAF,
 +
}
  
# Here I deal with spaces (" ") in the data. I replace them with "_" so that URI's become valid.
+
#Write a small function that computes the RDFS closure on your graph.
csv_data = csv_data.replace(to_replace=" ", value="_", regex=True)
+
def flush():
 +
    owlrl.DeductiveClosure(owlrl.RDFS_Semantics).expand(g)
  
# Here I mark all missing/empty data as "unknown". This makes it easy to delete triples containing this later.
+
#Rick Gates was charged with money laundering and tax evasion.
csv_data = csv_data.fillna("unknown")
+
g.add((ex.Rick_Gates, ex.chargedWith, ex.MoneyLaundering))
 +
g.add((ex.Rick_Gates, ex.chargedWith, ex.TaxEvasion))
  
# Loop through the CSV data, and then make RDF triples.
+
#When one thing that is charged with another thing,
for index, row in csv_data.iterrows():
+
g.add((ex.chargedWith, RDFS.domain, ex.PersonUnderInvestigation)) #the first thing is a person under investigation and
    # The names of the people act as subjects.
+
g.add((ex.chargedWith, RDFS.range, ex.Offense)) #the second thing is an offense.
    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
+
#Write a SPARQL query that checks the RDF type(s) of Rick Gates and money laundering in your RDF graph.
    g.add((URIRef(ex + subject), RDF.type, FOAF.Person))
+
print('Is Rick Gates a ex:PersonUnderInvestigation:', g.query('ASK {ex:Rick_Gates rdf:type ex:PersonUnderInvestigation}', initNs=NS).askAnswer)
 +
print('Is Money Laundering a ex:Offense:', g.query('ASK {ex:MoneyLaundering rdf:type ex:Offense}', initNs=NS).askAnswer)
 +
flush()
 +
print('Is Rick Gates a ex:PersonUnderInvestigation:', g.query('ASK {ex:Rick_Gates rdf:type ex:PersonUnderInvestigation}', initNs=NS).askAnswer)
 +
print('Is Money Laundering a ex:Offense:', g.query('ASK {ex:MoneyLaundering rdf:type ex:Offense}', initNs=NS).askAnswer)
  
# I remove triples that I marked as unknown earlier.
+
#A person under investigation is a FOAF person
g.remove((None, None, URIRef("http://example.org/unknown")))
+
g.add((ex.PersonUnderInvestigation, RDFS.subClassOf, FOAF.Person))
 +
print('Is Rick Gates a foaf:Person:', g.query('ASK {ex:Rick_Gates rdf:type foaf:Person}', initNs=NS).askAnswer)
 +
flush()
 +
print('Is Rick Gates a foaf:Person:', g.query('ASK {ex:Rick_Gates rdf:type foaf:Person}', initNs=NS).askAnswer)
  
# Clean printing of the graph.
+
#Paul Manafort was convicted for tax evasion.
print(g.serialize(format="turtle").decode())
+
g.add((ex.Paul_Manafort, ex.convictedFor, ex.TaxEvasion))
</syntaxhighlight>
+
#the first thing is also charged with the second thing
 +
g.add((ex.convictedFor, RDFS.subPropertyOf, ex.chargedWith))
 +
print('Is Paul Manafort charged with Tax Evasion:', g.query('ASK {ex:Paul_Manafort ex:chargedWith ex:TaxEvasion}', initNs=NS).askAnswer)
 +
flush()
 +
print('Is Paul Manafort charged with Tax Evasion:', g.query('ASK {ex:Paul_Manafort ex:chargedWith ex:TaxEvasion}', initNs=NS).askAnswer)
  
===CSV file for above example===
+
print(g.serialize())
  
<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>
 
</syntaxhighlight>
  
==Semantic Lifting - XML==
+
==OWL 1 (Lab 8)==
 
<syntaxhighlight>
 
<syntaxhighlight>
from rdflib import Graph, Literal, Namespace, URIRef
+
 
from rdflib.namespace import RDF, XSD, RDFS
+
from rdflib import Graph, RDFS, Namespace, RDF, FOAF, BNode, OWL, URIRef, Literal, XSD
import xml.etree.ElementTree as ET
+
from rdflib.collection import Collection
 +
import owlrl
  
 
g = Graph()
 
g = Graph()
ex = Namespace("http://example.org/TV/")
+
ex = Namespace('http://example.org/')
prov = Namespace("http://www.w3.org/ns/prov#")
+
schema = Namespace('http://schema.org/')
 +
dbr = Namespace('https://dbpedia.org/page/')
 +
 
 
g.bind("ex", ex)
 
g.bind("ex", ex)
g.bind("prov", prov)
+
# g.bind("schema", schema)
 +
g.bind("foaf", FOAF)
 +
 
 +
# Donald Trump and Robert Mueller are two different persons.
 +
g.add((ex.Donald_Trump, OWL.differentFrom, ex.Robert_Mueller))
 +
 
 +
# Actually, all the names mentioned in connection with the Mueller investigation refer to different people.
 +
b1 = BNode()
 +
b2 = BNode()
 +
Collection(g, b2, [ex.Robert_Mueller, ex.Paul_Manafort, ex.Rick_Gates, ex.George_Papadopoulos, ex.Michael_Flynn, ex.Michael_Cohen, ex.Roger_Stone, ex.Donald_Trump])
 +
g.add((b1, RDF.type, OWL.AllDifferent))
 +
g.add((b1, OWL.distinctMembers, b2))
  
tree = ET.parse("tv_shows.xml")
+
# All these people are foaf:Persons as well as schema:Persons
root = tree.getroot()
+
g.add((FOAF.Person, OWL.equivalentClass, schema.Person))
  
for tv_show in root.findall('tv_show'):
+
# Tax evation is a kind of bank and tax fraud.
    show_id = tv_show.attrib["id"]
+
g.add((ex.TaxEvation, RDFS.subClassOf, ex.BankFraud))
    title = tv_show.find("title").text
+
g.add((ex.TaxEvation, RDFS.subClassOf, ex.TaxFraud))
  
    g.add((URIRef(ex + show_id), ex.title, Literal(title, datatype=XSD.string)))
+
# The Donald Trump involved in the Mueller investigation is dbpedia:Donald_Trump and not dbpedia:Donald_Trump_Jr.
    g.add((URIRef(ex + show_id), RDF.type, ex.TV_Show))
+
g.add((ex.Donald_Trump, OWL.sameAs, dbr.Donald_Trump))
 +
g.add((ex.Donald_Trump, OWL.differentFrom, URIRef(dbr + "Donald_Trump_Jr.")))
  
    for actor in tv_show.findall("actor"):
+
# Congress, FBI and the Mueller investigation are foaf:Organizations.
        first_name = actor.find("firstname").text
+
g.add((ex.Congress, RDF.type, FOAF.Organization))
        last_name = actor.find("lastname").text
+
g.add((ex.FBI, RDF.type, FOAF.Organization))
        full_name = first_name + "_" + last_name
+
g.add((ex.Mueller_Investigation, RDF.type, FOAF.Organization))
       
+
 
        g.add((URIRef(ex + show_id), ex.stars, URIRef(ex + full_name)))
+
# Nothing can be both a person and an organization.
        g.add((URIRef(ex + full_name), ex.starsIn, URIRef(title)))
+
g.add((FOAF.Person, OWL.disjointWith, FOAF.Organization))
        g.add((URIRef(ex + full_name), RDF.type, ex.Actor))
+
 
 +
# Leading an organization is a way of being involved in an organization.
 +
g.add((ex.leading, RDFS.subPropertyOf, ex.involved))
 +
 
 +
# Being a campaign manager or an advisor for is a way of supporting someone.
 +
g.add((ex.campaignManagerTo, RDFS.subPropertyOf, ex.supports))
 +
g.add((ex.advisorTo, RDFS.subPropertyOf, ex.supports))
 +
 
 +
# Donald Trump is a politician and a Republican.
 +
g.add((ex.Donald_Trump, RDF.type, ex.Politician))
 +
g.add((ex.Donald_Trump, RDF.type, ex.Republican))
 +
 
 +
# A Republican politician is both a politician and a Republican.
 +
g.add((ex.RepublicanPolitician, RDFS.subClassOf, ex.Politician))
 +
g.add((ex.RepublicanPolitician, RDFS.subClassOf, ex.Republican))
 +
 
 +
#hasBusinessPartner
 +
g.add((ex.Paul_Manafort, ex.hasBusinessPartner, ex.Rick_Gates))
 +
g.add((ex.hasBusinessPartner, RDF.type, OWL.SymmetricProperty))
 +
g.add((ex.hasBusinessPartner, RDF.type, OWL.IrreflexiveProperty))
 +
 
 +
#adviserTo
 +
g.add((ex.Michael_Flynn, ex.adviserTo, ex.Donald_Trump))
 +
g.add((ex.adviserTo, RDF.type, OWL.IrreflexiveProperty))
 +
# Not necessarily asymmetric as it's not a given that they couldn't be advisors to each other 
 +
 
 +
#wasLyingTo
 +
g.add((ex.Rick_Gates_Lying, ex.wasLyingTo, ex.FBI))
 +
g.add((ex.wasLyingTo, RDF.type, OWL.IrreflexiveProperty))
 +
# Not asymmetric as the subject and object could lie to each other; also in this context, the FBI can lie to you
 +
 
 +
#presidentOf
 +
g.add((ex.Donald_Trump, ex.presidentOf, ex.USA))
 +
g.add((ex.presidentOf, RDF.type, OWL.AsymmetricProperty))
 +
g.add((ex.presidentOf, RDF.type, OWL.IrreflexiveProperty))
 +
g.add((ex.presidentOf, RDF.type, OWL.FunctionalProperty)) #can only be president of one country
 +
#not inversefunctionalproperty as Bosnia has 3 presidents https://www.culturalworld.org/do-any-countries-have-more-than-one-president.htm
  
print(g.serialize(format="turtle").decode())
+
#hasPresident
</syntaxhighlight>
+
g.add((ex.USA, ex.hasPresident, ex.Donald_Trump))
 +
g.add((ex.hasPresident, RDF.type, OWL.AsymmetricProperty))
 +
g.add((ex.hasPresident, RDF.type, OWL.IrreflexiveProperty))
 +
g.add((ex.hasPresident, RDF.type, OWL.InverseFunctionalProperty)) #countries do not share their president with another
 +
#not functionalproperty as a country (Bosnia) can have more than one president
  
 +
#Closure
 +
owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)
  
 +
#Serialization
 +
print(g.serialize(format="ttl"))
 +
# g.serialize("lab8.xml", format="xml") #serializes to XML file
  
===XML Data for above example===
 
<syntaxhighlight>
 
<data>
 
    <tv_show id="1050">
 
        <title>The_Sopranos</title>
 
        <actor>
 
            <firstname>James</firstname>
 
            <lastname>Gandolfini</lastname>
 
        </actor>
 
    </tv_show>
 
    <tv_show id="1066">
 
        <title>Seinfeld</title>
 
        <actor>
 
            <firstname>Jerry</firstname>
 
            <lastname>Seinfeld</lastname>
 
        </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 - HTML==
+
== OWL 2 (Lab 9)==
 +
'''NOTE: This is an OWL Protégé file'''
 +
 
 
<syntaxhighlight>
 
<syntaxhighlight>
from bs4 import BeautifulSoup as bs, NavigableString
 
from rdflib import Graph, URIRef, Namespace
 
from rdflib.namespace import RDF
 
  
g = Graph()
+
@prefix : <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
ex = Namespace("http://example.org/")
+
@prefix dc: <http://purl.org/dc/terms#> .
g.bind("ex", ex)
+
@prefix io: <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
 +
@prefix dbr: <http://dbpedia.org/resource/> .
 +
@prefix owl: <http://www.w3.org/2002/07/owl#> .
 +
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
 +
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
 +
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
 +
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
 +
@prefix prov: <http://www.w3.org/ns/prov#> .
 +
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 +
@base <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
 +
 
 +
<http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology> rdf:type owl:Ontology .
 +
 
 +
#################################################################
 +
#    Object Properties
 +
#################################################################
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#indictedIn
 +
io:indictedIn rdf:type owl:ObjectProperty ;
 +
              rdfs:subPropertyOf io:involvedIn ;
 +
              rdfs:domain io:InvestigatedPerson ;
 +
              rdfs:range io:Investigation .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#investigating
 +
io:investigating rdf:type owl:ObjectProperty ;
 +
                rdfs:subPropertyOf io:involvedIn ;
 +
                rdfs:domain io:Investigator ;
 +
                rdfs:range io:Investigation .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#involvedIn
 +
io:involvedIn rdf:type owl:ObjectProperty ;
 +
              rdfs:domain foaf:Person ;
 +
              rdfs:range io:Investigation .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#leading
 +
io:leading rdf:type owl:ObjectProperty ;
 +
          rdfs:subPropertyOf io:investigating ;
 +
          rdfs:domain io:InvestigationLeader ;
 +
          rdfs:range io:Investigation .
 +
 
 +
 
 +
#################################################################
 +
#    Data properties
 +
#################################################################
 +
 
 +
###  http://purl.org/dc/elements/1.1/description
 +
<http://purl.org/dc/elements/1.1/description> rdf:type owl:DatatypeProperty ;
 +
                                              rdfs:domain io:Investigation ;
 +
                                              rdfs:range xsd:string .
 +
 
 +
 
 +
###  http://purl.org/dc/elements/1.1/title
 +
<http://purl.org/dc/elements/1.1/title> rdf:type owl:DatatypeProperty ;
 +
                                        rdfs:domain io:Investigation ;
 +
                                        rdfs:range xsd:string .
 +
 
 +
 
 +
###  http://www.w3.org/ns/prov#endedAtTime
 +
prov:endedAtTime rdf:type owl:DatatypeProperty ,
 +
                          owl:FunctionalProperty ;
 +
                rdfs:domain io:Investigation ;
 +
                rdfs:range xsd:dateTime .
 +
 
 +
 
 +
###  http://www.w3.org/ns/prov#startedAtTime
 +
prov:startedAtTime rdf:type owl:DatatypeProperty ,
 +
                            owl:FunctionalProperty ;
 +
                  rdfs:domain io:Investigation ;
 +
                  rdfs:range xsd:dateTime .
 +
 
 +
 
 +
###  http://xmlns.com/foaf/0.1/name
 +
foaf:name rdf:type owl:DatatypeProperty ;
 +
          rdfs:domain foaf:Person ;
 +
          rdfs:range xsd:string .
 +
 
 +
 
 +
#################################################################
 +
#    Classes
 +
#################################################################
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigatedPerson
 +
io:InvestigatedPerson rdf:type owl:Class ;
 +
                      rdfs:subClassOf io:Person ;
 +
                      owl:disjointWith io:Investigator .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation
 +
io:Investigation rdf:type owl:Class .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigationLeader
 +
io:InvestigationLeader rdf:type owl:Class ;
 +
                      rdfs:subClassOf io:Investigator .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigator
 +
io:Investigator rdf:type owl:Class ;
 +
                rdfs:subClassOf io:Person .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Person
 +
io:Person rdf:type owl:Class ;
 +
          rdfs:subClassOf foaf:Person .
 +
 
 +
 
 +
###  http://xmlns.com/foaf/0.1/Person
 +
foaf:Person rdf:type owl:Class .
 +
 
 +
 
 +
#################################################################
 +
#    Individuals
 +
#################################################################
 +
 
 +
###  http://dbpedia.org/resource/Donald_Trump
 +
dbr:Donald_Trump rdf:type owl:NamedIndividual ;
 +
                foaf:name "Donald Trump" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Elizabeth_Prelogar
 +
dbr:Elizabeth_Prelogar rdf:type owl:NamedIndividual ;
 +
                      foaf:name "Elizabeth Prelogar" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Michael_Flynn
 +
dbr:Michael_Flynn rdf:type owl:NamedIndividual ;
 +
                  foaf:name "Michael Flynn" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Paul_Manafort
 +
dbr:Paul_Manafort rdf:type owl:NamedIndividual ;
 +
                  foaf:name "Paul Manafort" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Robert_Mueller
 +
dbr:Robert_Mueller rdf:type owl:NamedIndividual ;
 +
                  foaf:name "Robert Mueller" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Roger_Stone
 +
dbr:Roger_Stone rdf:type owl:NamedIndividual ;
 +
                foaf:name "Roger Stone" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)
 +
<http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> rdf:type owl:NamedIndividual ,
 +
                                                                                io:Investigation ;
 +
                                                                        <http://purl.org/dc/elements/1.1/title> "Mueller Investigation" .
 +
 
  
html = open("tv_shows.html").read()
+
#################################################################
html = bs(html, features="html.parser")
+
#    General axioms
 +
#################################################################
  
shows = html.find_all('li', attrs={'class': 'show'})
+
[ rdf:type owl:AllDifferent ;
for show in shows:
+
  owl:distinctMembers ( dbr:Donald_Trump
    title = show.find("h3").text
+
                        dbr:Elizabeth_Prelogar
    actors = show.find('ul', attrs={'class': 'actor_list'})
+
                        dbr:Michael_Flynn
    for actor in actors:
+
                        dbr:Paul_Manafort
        if isinstance(actor, NavigableString):
+
                        dbr:Robert_Mueller
            continue
+
                        dbr:Roger_Stone
        else:
+
                      )
            actor = actor.text.replace(" ", "_")
+
] .
            g.add((URIRef(ex + title), ex.stars, URIRef(ex + actor)))
 
            g.add((URIRef(ex + actor), RDF.type, ex.Actor))
 
  
    g.add((URIRef(ex + title), RDF.type, ex.TV_Show))
 
  
 +
###  Generated by the OWL API (version 4.5.25.2023-02-15T19:15:49Z) https://github.com/owlcs/owlapi
  
print(g.serialize(format="turtle").decode())
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===HTML code for the example above===
+
== OWL-DL (Lab 10)==
 +
'''NOTE: This is an OWL Protégé file.'''
 +
 
 
<syntaxhighlight>
 
<syntaxhighlight>
<!DOCTYPE html>
+
@prefix : <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
<html>
+
@prefix dc: <http://purl.org/dc/terms#> .
<head>
+
@prefix io: <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
    <meta charset="utf-8">
+
@prefix dbr: <http://dbpedia.org/resource/> .
    <title></title>
+
@prefix owl: <http://www.w3.org/2002/07/owl#> .
</head>
+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<body>
+
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
    <div class="tv_shows">
+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
        <ul>
+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
            <li class="show">
+
@prefix prov: <http://www.w3.org/ns/prov#> .
                <h3>The_Sopranos</h3>
+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
                <div class="irrelevant_data"></div>
+
@base <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
                 <ul class="actor_list">
+
 
                    <li>James Gandolfini</li>
+
<http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology> rdf:type owl:Ontology .
                </ul>
+
 
            </li>
+
#################################################################
            <li class="show">
+
#    Object Properties
                <h3>Seinfeld</h3>
+
#################################################################
                <div class="irrelevant_data"></div>
+
 
                 <ul class="actor_list">
+
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#indictedIn
                    <li >Jerry Seinfeld</li>
+
io:indictedIn rdf:type owl:ObjectProperty ;
                    <li>Jason Alexander</li>
+
              rdfs:subPropertyOf io:involvedIn ;
                    <li>Julia Louis-Dreyfus</li>
+
              rdfs:domain io:InvestigatedPerson ;
                </ul>
+
              rdfs:range io:Investigation .
            </li>
+
 
        </ul>
+
 
    </div>
+
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#investigating
</body>
+
io:investigating rdf:type owl:ObjectProperty ;
</html>
+
                rdfs:subPropertyOf io:involvedIn ;
 +
                rdfs:domain io:Investigator ;
 +
                rdfs:range io:Investigation .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#involvedIn
 +
io:involvedIn rdf:type owl:ObjectProperty ;
 +
              rdfs:domain foaf:Person ;
 +
              rdfs:range io:Investigation .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#leading
 +
io:leading rdf:type owl:ObjectProperty ;
 +
          rdfs:subPropertyOf io:investigating ;
 +
          rdfs:domain io:InvestigationLeader ;
 +
          rdfs:range io:Investigation .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#specialProsecutor
 +
io:specialProsecutor rdf:type owl:ObjectProperty ;
 +
                    rdfs:subPropertyOf io:leading .
 +
 
 +
 
 +
#################################################################
 +
#    Data properties
 +
#################################################################
 +
 
 +
###  http://purl.org/dc/elements/1.1/description
 +
<http://purl.org/dc/elements/1.1/description> rdf:type owl:DatatypeProperty ;
 +
                                              rdfs:domain io:Investigation ;
 +
                                              rdfs:range xsd:string .
 +
 
 +
 
 +
###  http://purl.org/dc/elements/1.1/title
 +
<http://purl.org/dc/elements/1.1/title> rdf:type owl:DatatypeProperty ;
 +
                                        rdfs:range xsd:string .
 +
 
 +
 
 +
###  http://www.w3.org/ns/prov#endedAtTime
 +
prov:endedAtTime rdf:type owl:DatatypeProperty ,
 +
                          owl:FunctionalProperty ;
 +
                rdfs:domain io:Investigation ;
 +
                rdfs:range xsd:dateTime .
 +
 
 +
 
 +
###  http://www.w3.org/ns/prov#startedAtTime
 +
prov:startedAtTime rdf:type owl:DatatypeProperty ,
 +
                            owl:FunctionalProperty ;
 +
                  rdfs:domain io:Investigation ;
 +
                  rdfs:range xsd:dateTime .
 +
 
 +
 
 +
###  http://xmlns.com/foaf/0.1/name
 +
foaf:name rdf:type owl:DatatypeProperty ;
 +
          rdfs:domain foaf:Person ;
 +
          rdfs:range xsd:string .
 +
 
 +
 
 +
#################################################################
 +
#    Classes
 +
#################################################################
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigatedPerson
 +
io:InvestigatedPerson rdf:type owl:Class ;
 +
                      rdfs:subClassOf io:Person ;
 +
                      owl:disjointWith io:Investigator .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation
 +
io:Investigation rdf:type owl:Class .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigationLeader
 +
io:InvestigationLeader rdf:type owl:Class ;
 +
                      rdfs:subClassOf io:Investigator .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigator
 +
io:Investigator rdf:type owl:Class ;
 +
                 rdfs:subClassOf io:Person .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Person
 +
io:Person rdf:type owl:Class ;
 +
          rdfs:subClassOf foaf:Person .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#SpecialCounselInvestigation
 +
io:SpecialCounselInvestigation rdf:type owl:Class ;
 +
                              owl:equivalentClass [ rdf:type owl:Restriction ;
 +
                                                    owl:onProperty [ owl:inverseOf io:specialProsecutor
 +
                                                                    ] ;
 +
                                                    owl:someValuesFrom owl:Thing
 +
                                                  ] ;
 +
                              rdfs:subClassOf io:Investigation .
 +
 
 +
 
 +
###  http://xmlns.com/foaf/0.1/Person
 +
foaf:Person rdf:type owl:Class .
 +
 
 +
 
 +
#################################################################
 +
#    Individuals
 +
#################################################################
 +
 
 +
###  http://dbpedia.org/resource/Donald_Trump
 +
dbr:Donald_Trump rdf:type owl:NamedIndividual ;
 +
                foaf:name "Donald Trump" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Elizabeth_Prelogar
 +
dbr:Elizabeth_Prelogar rdf:type owl:NamedIndividual ,
 +
                                io:Investigator ;
 +
                      io:investigating <http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> ;
 +
                      foaf:name "Elizabeth Prelogar" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Michael_Flynn
 +
dbr:Michael_Flynn rdf:type owl:NamedIndividual ;
 +
                  foaf:name "Michael Flynn" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Paul_Manafort
 +
dbr:Paul_Manafort rdf:type owl:NamedIndividual ;
 +
                  io:indictedIn <http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> ;
 +
                  foaf:name "Paul Manafort" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Robert_Mueller
 +
dbr:Robert_Mueller rdf:type owl:NamedIndividual ;
 +
                  io:leading <http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> ;
 +
                  foaf:name "Robert Mueller" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Roger_Stone
 +
dbr:Roger_Stone rdf:type owl:NamedIndividual ;
 +
                 foaf:name "Roger Stone" .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Jack_Smith_(politician)
 +
<http://dbpedia.org/resource/Jack_Smith_(politician)> rdf:type owl:NamedIndividual ;
 +
                                                      io:specialProsecutor io:Investigation_of_Trumps_Handling_Graded_Documents ,
 +
                                                                          io:Investigation_of_Trumps_Role_US_Capital_Attack .
 +
 
 +
 
 +
###  http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)
 +
<http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> rdf:type owl:NamedIndividual ;
 +
                                                                        <http://purl.org/dc/elements/1.1/title> "Mueller Investigation" .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation_of_Trumps_Handling_Graded_Documents
 +
io:Investigation_of_Trumps_Handling_Graded_Documents rdf:type owl:NamedIndividual .
 +
 
 +
 
 +
###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation_of_Trumps_Role_US_Capital_Attack
 +
io:Investigation_of_Trumps_Role_US_Capital_Attack rdf:type owl:NamedIndividual .
 +
 
 +
 
 +
#################################################################
 +
#    General axioms
 +
#################################################################
 +
 
 +
[ rdf:type owl:AllDifferent ;
 +
  owl:distinctMembers ( dbr:Donald_Trump
 +
                        dbr:Elizabeth_Prelogar
 +
                        dbr:Michael_Flynn
 +
                        dbr:Paul_Manafort
 +
                        dbr:Robert_Mueller
 +
                        dbr:Roger_Stone
 +
                        <http://dbpedia.org/resource/Jack_Smith_(politician)>
 +
                      )
 +
] .
 +
 
 +
 
 +
###  Generated by the OWL API (version 4.5.25.2023-02-15T19:15:49Z) https://github.com/owlcs/owlapi
 +
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==WEB API Calls (In this case JSON)==
+
==Using Graph Embeddings (Lab 11)==
<syntaxhighlight>
+
 
import requests
+
https://colab.research.google.com/drive/1a0lpmpjngXY2zsKRFcRWeXwVFMM5NO_O?usp=sharing
import json
 
import pprint
 
  
# Retrieve JSON data from API service URL. Then load it with the json library as a json object.
+
==Training Graph Embeddings (Lab 12)==
url = "http://api.geonames.org/postalCodeLookupJSON?postalcode=46020&#country=ES&username=demo"
 
data = requests.get(url).content.decode("utf-8")
 
data = json.loads(data)
 
pprint.pprint(data)
 
</syntaxhighlight>
 
  
 +
https://colab.research.google.com/drive/1jKpzlQ7gYTVzgphJsrK5iuMpFhkrY96q
  
==JSON-LD==
+
==Web APIs and JSON-LD (Lab 13)==
  
 
<syntaxhighlight>
 
<syntaxhighlight>
import rdflib
+
import requests
 +
from rdflib import FOAF, Namespace, Literal, RDF, Graph
  
g = rdflib.Graph()
+
r = requests.get('http://api.open-notify.org/astros.json').json()
  
example = """
+
g = Graph()
{
+
ex = Namespace('http://example.org/')
  "@context": {
+
 
    "name": "http://xmlns.com/foaf/0.1/name",
+
g.bind("ex", ex)
    "homepage": {
+
NS = {
      "@id": "http://xmlns.com/foaf/0.1/homepage",
+
     "ex": ex,
      "@type": "@id"
+
    "foaf":FOAF
     }
 
  },
 
  "@id": "http://me.markus-lanthaler.com/",
 
  "name": "Markus Lanthaler",
 
  "homepage": "http://www.markus-lanthaler.com/"
 
 
}
 
}
"""
 
  
# json-ld parsing automatically deals with @contexts
+
#Write a small program that queries the Open Notify Astros API
g.parse(data=example, format='json-ld')
+
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'])))
  
# serialisation does expansion by default
+
res = g.query("""
for line in g.serialize(format='json-ld').decode().splitlines():
+
    CONSTRUCT {?person1 foaf:knows ?person2}
    print(line)
+
    WHERE {
 +
        ?person1 ex:onCraft ?craft .
 +
        ?person2 ex:onCraft ?craft .
 +
        }
 +
""", initNs=NS)
  
# by supplying a context object, serialisation can do compaction
+
for triplet in res:
context = {
+
    # (we don't need to add that they know themselves)
    "foaf": "http://xmlns.com/foaf/0.1/"
+
    if (triplet[0] != triplet[2]):
}
+
        g.add((triplet))
for line in g.serialize(format='json-ld', context=context).decode().splitlines():
 
    print(line)
 
</syntaxhighlight>
 
  
 +
#Serialise the graph to JSON-LD   
 +
print(g.serialize(format="json-ld"))
  
<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>
+
#DBpedia Spotlight was worked on Lab 5 (CSV to RDF).
 +
</syntaxhighlight>

Latest revision as of 11:54, 5 May 2023

This page will be updated with Python examples related to the labs as the course progresses.

Examples from the lectures

Lecture 1: Introduction to KGs

Turtle example:

@prefix ex: <http://example.org/> .
ex:Roger_Stone
    ex:name "Roger Stone" ;
    ex:occupation ex:lobbyist ;
    ex:significant_person ex:Donald_Trump .
ex:Donald_Trump
    ex:name "Donald Trump" .

Lecture 2: RDF

Blank nodes for anonymity, or when we have not decided on a URI:

from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD

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

g = Graph()
g.bind('ex', EX)  # this is why the line '@prefix ex: <http://example.org/> .'
                  # and the 'ex.' prefix are used when we print out Turtle later

robertMueller = BNode()
g.add((robertMueller, RDF.type, EX.Human))
g.add((robertMueller, FOAF.name, Literal('Robert Mueller', lang='en')))
g.add((robertMueller, EX.position_held, Literal('Director of the Federal Bureau of Investigation', lang='en')))

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

Blank nodes used to group related properties:

from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD

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

g = Graph()
g.bind('ex', EX)

# This is a task in Exercise 2

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

Literals:

from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD

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

g = Graph()
g.bind('ex', EX)

g.add((EX.Robert_Mueller, RDF.type, EX.Human))
g.add((EX.Robert_Mueller, FOAF.name, Literal('Robert Mueller', lang='en')))
g.add((EX.Robert_Mueller, FOAF.name, Literal('رابرت مولر', lang='fa')))
g.add((EX.Robert_Mueller, DC.description, Literal('sixth director of the FBI', datatype=XSD.string)))
g.add((EX.Robert_Mueller, EX.start_time, Literal(2001, datatype=XSD.integer)))

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

Alternative container (open):

from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD

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

g = Graph()
g.bind('ex', EX)

muellerReportArchives = BNode()
g.add((muellerReportArchives, RDF.type, RDF.Alt))

archive1 = 'https://archive.org/details/MuellerReportVolume1Searchable/' \
                    'Mueller%20Report%20Volume%201%20Searchable/'
archive2 = 'https://edition.cnn.com/2019/04/18/politics/full-mueller-report-pdf/index.html'
archive3 = 'https://www.politico.com/story/2019/04/18/mueller-report-pdf-download-text-file-1280891'

g.add((muellerReportArchives, RDFS.member, Literal(archive1, datatype=XSD.anyURI)))
g.add((muellerReportArchives, RDFS.member, Literal(archive2, datatype=XSD.anyURI)))
g.add((muellerReportArchives, RDFS.member, Literal(archive3, datatype=XSD.anyURI)))

g.add((EX.Mueller_Report, RDF.type, FOAF.Document))
g.add((EX.Mueller_Report, DC.contributor, EX.Robert_Mueller))
g.add((EX.Mueller_Report, SCHEMA.archivedAt, muellerReportArchives))

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

Sequence container (open):

from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD

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

g = Graph()
g.bind('ex', EX)

donaldTrumpSpouses = BNode()
g.add((donaldTrumpSpouses, RDF.type, RDF.Seq))
g.add((donaldTrumpSpouses, RDF._1, EX.IvanaTrump))
g.add((donaldTrumpSpouses, RDF._2, EX.MarlaMaples))
g.add((donaldTrumpSpouses, RDF._3, EX.MelaniaTrump))

g.add((EX.Donald_Trump, SCHEMA.spouse, donaldTrumpSpouses))

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

Collection (closed list):

from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD

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

g = Graph()
g.bind('ex', EX)

from rdflib.collection import Collection

g = Graph()
g.bind('ex', EX)

donaldTrumpSpouses = BNode()
Collection(g, donaldTrumpSpouses, [
    EX.IvanaTrump, EX.MarlaMaples, EX.MelaniaTrump
])
g.add((EX.Donald_Trump, SCHEMA.spouse, donaldTrumpSpouses))

print(g.serialize(format='turtle'))
g.serialize(destination='s02_Donald_Trump_spouses_list.ttl', format='turtle')

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

Example lab solutions

Getting started (Lab 1)

from rdflib import Graph, Namespace

g = Graph()

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

g.bind("ex", ex)

#The Mueller Investigation was lead by Robert Mueller.
g.add((ex.Mueller_Investigation, ex.leadBy, ex.Robert_Muller))

#It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, and Roger Stone.
g.add((ex.Mueller_Investigation, ex.involved, ex.Paul_Manafort))
g.add((ex.Mueller_Investigation, ex.involved, ex.Rick_Gates))
g.add((ex.Mueller_Investigation, ex.involved, ex.George_Papadopoulos))
g.add((ex.Mueller_Investigation, ex.involved, ex.Michael_Flynn))
g.add((ex.Mueller_Investigation, ex.involved, ex.Michael_Cohen))
g.add((ex.Mueller_Investigation, ex.involved, ex.Roger_Stone))

# --- Paul Manafort ---
#Paul Manafort was business partner of Rick Gates.
g.add((ex.Paul_Manafort, ex.businessManager, ex.Rick_Gates))
# He was campaign chairman for Trump
g.add((ex.Paul_Manafort, ex.campaignChairman, ex.Donald_Trump))

# He was charged with money laundering, tax evasion, and foreign lobbying.
g.add((ex.Paul_Manafort, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.Paul_Manafort, ex.chargedWith, ex.TaxEvasion))
g.add((ex.Paul_Manafort, ex.chargedWith, ex.ForeignLobbying))

# He was convicted for bank and tax fraud.
g.add((ex.Paul_Manafort, ex.convictedFor, ex.BankFraud))
g.add((ex.Paul_Manafort, ex.convictedFor, ex.TaxFraud))

# He pleaded guilty to conspiracy.
g.add((ex.Paul_Manafort, ex.pleadGuiltyTo, ex.Conspiracy))
# He was sentenced to prison.
g.add((ex.Paul_Manafort, ex.sentencedTo, ex.Prison))
# He negotiated a plea agreement.
g.add((ex.Paul_Manafort, ex.negoiated, ex.PleaBargain))

# --- Rick Gates ---
#Rick Gates was charged with money laundering, tax evasion and foreign lobbying.
g.add((ex.Rick_Gates, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.Rick_Gates, ex.chargedWith, ex.TaxEvasion))
g.add((ex.Rick_Gates, ex.chargedWith, ex.ForeignLobbying))

#He pleaded guilty to conspiracy and lying to FBI.
g.add((ex.Rick_Gates, ex.pleadGuiltyTo, ex.Conspiracy))
g.add((ex.Rick_Gates, ex.pleadGuiltyTo, ex.LyingToFBI))

#Use the serialize method to write out the model in different formats on screen
print(g.serialize(format="ttl"))
# g.serialize("lab1.ttl", format="ttl") #or to file

#Loop through the triples in the model to print out all triples that have pleading guilty as predicate
for subject, object in g[ : ex.pleadGuiltyTo : ]:
    print(subject, ex.pleadGuiltyTo, object)

# Michael Cohen, Michael Flynn and the lying is part of lab 2 and therefore the answer is not provided this week 

#Write a method (function) that submits your model for rendering and saves the returned image to file.
import requests
import shutil

def graphToImage(graph):
    data = {"rdf":graph, "from":"ttl", "to":"png"}
    link = "http://www.ldf.fi/service/rdf-grapher"
    response = requests.get(link, params = data, stream=True)
    # print(response.content)
    print(response.raw)
    with open("lab1.png", "wb") as fil:
        shutil.copyfileobj(response.raw, fil)

graph = g.serialize(format="ttl")
graphToImage(graph)

RDF programming with RDFlib (Lab 2)

from rdflib import Graph, URIRef, Namespace, Literal, XSD, BNode
from rdflib.collection import Collection

g = Graph()
g.parse("lab1.ttl", format="ttl") #Retrives the triples from lab 1

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

# --- Michael Cohen ---
#Michael Cohen was Donald Trump's attorney.
g.add((ex.Michael_Cohen, ex.attorneyTo, ex.Donald_Trump))
#He pleaded guilty to lying to the FBI.
g.add((ex.Michael_Cohen, ex.pleadGuiltyTo, ex.LyingToCongress))

# --- Michael Flynn ---
#Michael Flynn was adviser to Trump.
g.add((ex.Michael_Flynn, ex.adviserTo, ex.Donald_Trump))
#He pleaded guilty to lying to the FBI.
g.add((ex.Michael_Flynn, ex.pleadGuiltyTo, ex.LyingToFBI))
# He negotiated a plea agreement.
g.add((ex.Michael_Flynn, ex.negoiated, ex.PleaBargain)) 

#How can you modify your knowledge graph to account for the different lying?
#Remove these to not have duplicates
g.remove((ex.Michael_Flynn, ex.pleadGuiltyTo, ex.LyingToFBI)) 
g.remove((ex.Michael_Flynn, ex.negoiated, ex.PleaBargain))
g.remove((ex.Rick_Gates, ex.pleadGuiltyTo, ex.LyingToFBI))
g.remove((ex.Rick_Gates, ex.pleadGuiltyTo, ex.Conspiracy))
g.remove((ex.Rick_Gates, ex.chargedWith, ex.ForeignLobbying))
g.remove((ex.Rick_Gates, ex.chargedWith, ex.MoneyLaundering))
g.remove((ex.Rick_Gates, ex.chargedWith, ex.TaxEvasion))
g.remove((ex.Michael_Cohen, ex.pleadGuiltyTo, ex.LyingToCongress))

# --- Michael Flynn ---
FlynnLying = BNode() 
g.add((FlynnLying, ex.crime, ex.LyingToFBI))
g.add((FlynnLying, ex.pleadGulityOn, Literal("2017-12-1", datatype=XSD.date)))
g.add((FlynnLying, ex.liedAbout, Literal("His communications with a former Russian ambassador during the presidential transition", datatype=XSD.string)))
g.add((FlynnLying, ex.pleaBargain, Literal("true", datatype=XSD.boolean)))
g.add((ex.Michael_Flynn, ex.pleadGuiltyTo, FlynnLying))

# --- Rick Gates ---
GatesLying = BNode()
Crimes = BNode()
Charged = BNode()
Collection(g, Crimes, [ex.LyingToFBI, ex.Conspiracy])
Collection(g, Charged, [ex.ForeignLobbying, ex.MoneyLaundering, ex.TaxEvasion])
g.add((GatesLying, ex.crime, Crimes))
g.add((GatesLying, ex.chargedWith, Charged))
g.add((GatesLying, ex.pleadGulityOn, Literal("2018-02-23", datatype=XSD.date)))
g.add((GatesLying, ex.pleaBargain, Literal("true", datatype=XSD.boolean)))
g.add((ex.Rick_Gates, ex.pleadGuiltyTo, GatesLying))

# --- Michael Cohen ---
CohenLying = BNode()
g.add((CohenLying, ex.crime, ex.LyingToCongress))
g.add((CohenLying, ex.liedAbout, ex.TrumpRealEstateDeal))
g.add((CohenLying, ex.prosecutorsAlleged, Literal("In an August 2017 letter Cohen sent to congressional committees investigating Russian election interference, he falsely stated that the project ended in January 2016", datatype=XSD.string)))
g.add((CohenLying, ex.mullerInvestigationAlleged, Literal("Cohen falsely stated that he had never agreed to travel to Russia for the real estate deal and that he did not recall any contact with the Russian government about the project", datatype=XSD.string)))
g.add((CohenLying, ex.pleadGulityOn, Literal("2018-11-29", datatype=XSD.date)))
g.add((CohenLying, ex.pleaBargain, Literal("true", datatype=XSD.boolean)))
g.add((ex.Michael_Cohen, ex.pleadGuiltyTo, CohenLying))

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

#Save (serialize) your graph to a Turtle file.
# g.serialize("lab2.ttl", format="ttl")

#Add a few triples to the Turtle file with more information about Donald Trump.
'''
ex:Donald_Trump ex:address [ ex:city ex:Palm_Beach ;
            ex:country ex:United_States ;
            ex:postalCode 33480 ;
            ex:residence ex:Mar_a_Lago ;
            ex:state ex:Florida ;
            ex:streetName "1100 S Ocean Blvd"^^xsd:string ] ;
    ex:previousAddress [ ex:city ex:Washington_DC ;
            ex:country ex:United_States ;
            ex:phoneNumber "1 202 456 1414"^^xsd:integer ;
            ex:postalCode "20500"^^xsd:integer ;
            ex:residence ex:The_White_House ;
            ex:streetName "1600 Pennsylvania Ave."^^xsd:string ];
    ex:marriedTo ex:Melania_Trump;
    ex:fatherTo (ex:Ivanka_Trump ex:Donald_Trump_Jr ex: ex:Tiffany_Trump ex:Eric_Trump ex:Barron_Trump).
'''

#Read (parse) the Turtle file back into a Python program, and check that the new triples are there
def serialize_Graph():
    newGraph = Graph()
    newGraph.parse("lab2.ttl")
    print(newGraph.serialize())

# serialize_Graph() #Don't need this to run until after adding the triples above to the ttl file

#Write a method (function) that starts with Donald Trump prints out a graph depth-first to show how the other graph nodes are connected to him
visited_nodes = set()

def create_Tree(model, nodes):
    #Traverse the model breadth-first to create the tree.
    global visited_nodes
    tree = Graph()
    children = set()
    visited_nodes |= set(nodes)
    for s, p, o in model:
        if s in nodes and o not in visited_nodes:
            tree.add((s, p, o))
            visited_nodes.add(o)
            children.add(o)
        if o in nodes and s not in visited_nodes:
            invp = URIRef(f'{p}_inv') #_inv represents inverse of
            tree.add((o, invp, s))
            visited_nodes.add(s)
            children.add(s)
    if len(children) > 0:
        children_tree = create_Tree(model, children)
        for triple in children_tree:
            tree.add(triple)
    return tree

def print_Tree(tree, root, indent=0):
    #Print the tree depth-first.
    print(str(root))
    for s, p, o in tree:
        if s==root:
            print('    '*indent + '  ' + str(p), end=' ')
            print_Tree(tree, o, indent+1)
    
tree = create_Tree(g, [ex.Donald_Trump])
print_Tree(tree, ex.Donald_Trump)

SPARQL Programming (Lab 4)

NOTE: These tasks were performed on the old dataset, with the new dataset, some of these answers would be different.

from rdflib import Graph, Namespace, RDF, FOAF
from SPARQLWrapper import SPARQLWrapper, JSON, POST, GET, TURTLE

g = Graph()
g.parse("Russia_investigation_kg.ttl")

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

NS = {
    '': ex,
    'rdf': RDF,
    'foaf': FOAF,
}

# Print out a list of all the predicates used in your graph.
task1 = g.query("""
SELECT DISTINCT ?p WHERE{
    ?s ?p ?o .
}
""", initNs=NS)

print(list(task1))

# Print out a sorted list of all the presidents represented in your graph.
task2 = g.query("""
SELECT DISTINCT ?president WHERE{
    ?s :president ?president .
}
ORDER BY ?president
""", initNs=NS)

print(list(task2))

# Create dictionary (Python dict) with all the represented presidents as keys. For each key, the value is a list of names of people indicted under that president.
task3_dic = {}

task3 = g.query("""
SELECT ?president ?person WHERE{
    ?s :president ?president;
       :name ?person;
       :outcome :indictment.
}
""", initNs=NS)

for president, person in task3:
    if president not in task3_dic:
        task3_dic[president] = [person]
    else:
        task3_dic[president].append(person)

print(task3_dic)

# Use an ASK query to investigate whether Donald Trump has pardoned more than 5 people.

# This task is a lot trickier than it needs to be. As far as I'm aware RDFLib has no HAVING support, so a query like this:
task4 = g.query("""
ASK {
  	SELECT (COUNT(?s) as ?count) WHERE{
    	?s :pardoned :true;
   	   :president :Bill_Clinton  .
    }
    HAVING (?count > 5)
}
""", initNs=NS)

print(task4.askAnswer)

# Which works fine in Blazegraph and is a valid SPARQL query will always provide false in RDFLib, cause it uses HAVING. Instead you have to use a nested SELECT query like below, where you use FILTER instead of HAVING. Donald Trump has no pardons, so I have instead chosen Bill Clinton (which has 13 pardons) to check if the query works. 

task4 = g.query("""
    ASK{
        SELECT ?count WHERE{{
  	        SELECT (COUNT(?s) as ?count) WHERE{
    	        ?s :pardoned :true;
                   :president :Bill_Clinton  .
                }}
        FILTER (?count > 5) 
        }
    }
""", initNs=NS)

print(task4.askAnswer)

# Use a DESCRIBE query to create a new graph with information about Donald Trump. Print out the graph in Turtle format.

# By all accounts, it seems DESCRIBE queries are yet to be implemented in RDFLib, but they are attempting to implement it: https://github.com/RDFLib/rdflib/pull/2221 (Issue and proposed solution raised) & https://github.com/RDFLib/rdflib/commit/2325b4a81724c1ccee3a131067db4fbf9b4e2629 (Solution committed to RDFLib). This solution does not work. However, this proposed solution should work if DESCRIBE is implemented in RDFLib

# task5 = g.query(""" 
# DESCRIBE :Donald_Trump
# """, initNs=NS)

# print(task5.serialize())

# ----- SPARQLWrapper -----

namespace = "kb" #Default namespace
sparql = SPARQLWrapper("http://localhost:9999/blazegraph/namespace/"+ namespace + "/sparql") #Replace localhost:9999 with your URL

# The current dates are URIs, we would want to change them to Literals with datatype "date" for task 1 & 2
update_str = """
    PREFIX ns1: <http://example.org#>

    DELETE {
        ?s ns1:cp_date ?cp;
            ns1:investigation_end ?end;
            ns1:investigation_start ?start.
    }
    INSERT{
        ?s ns1:cp_date ?cpDate;
            ns1:investigation_end ?endDate;
            ns1:investigation_start ?startDate.
    }
    WHERE{
        ?s ns1:cp_date ?cp . #Date conviction was recieved
        BIND (replace(str(?cp), str(ns1:), "")  AS ?cpRemoved)
        BIND (STRDT(STR(?cpRemoved), xsd:date) AS ?cpDate)
        
        ?s ns1:investigation_end ?end . #Investigation End
        BIND (replace(str(?end), str(ns1:), "")  AS ?endRemoved)
        BIND (STRDT(STR(?endRemoved), xsd:date) AS ?endDate)
        
        ?s ns1:investigation_start ?start . #Investigation Start
        BIND (replace(str(?start), str(ns1:), "")  AS ?startRemoved)
        BIND (STRDT(STR(?startRemoved), xsd:date) AS ?startDate)
}"""

sparql.setQuery(update_str)
sparql.setMethod(POST)
sparql.query()

# Ask whether there was an ongoing indictment on the date 1990-01-01.
sparql.setQuery("""
    PREFIX ns1: <http://example.org#>
    ASK {
        SELECT ?end ?start
        WHERE{
            ?s ns1:investigation_end ?end;
               ns1:investigation_start ?start;
               ns1:outcome ns1:indictment.
            FILTER(?start <= "1990-01-01"^^xsd:date && ?end >= "1990-01-01"^^xsd:date) 
	    }
    }
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
print(f"Are there any investigation on the 1990-01-01: {results['boolean']}")

# List ongoing indictments on that date 1990-01-01.
sparql.setQuery("""
    PREFIX ns1: <http://example.org#>
    SELECT ?s
    WHERE{
        ?s ns1:investigation_end ?end;
           ns1:investigation_start ?start;
           ns1:outcome ns1:indictment.
        FILTER(?start <= "1990-01-01"^^xsd:date && ?end >= "1990-01-01"^^xsd:date) 
    }
""")

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

print("The ongoing investigations on the 1990-01-01 are:")
for result in results["results"]["bindings"]:
    print(result["s"]["value"])

# Describe investigation number 100 (muellerkg:investigation_100).
sparql.setQuery("""
    PREFIX ns1: <http://example.org#>
    DESCRIBE ns1:investigation_100
""")

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

print(results.serialize())

# Print out a list of all the types used in your graph.
sparql.setQuery("""
    PREFIX ns1: <http://example.org#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

    SELECT DISTINCT ?types
    WHERE{
        ?s rdf:type ?types . 
    }
""")

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

rdf_Types = []

for result in results["results"]["bindings"]:
    rdf_Types.append(result["types"]["value"])

print(rdf_Types)

# Update the graph to that every resource that is an object in a muellerkg:investigation triple has the rdf:type muellerkg:Investigation.
update_str = """
    PREFIX ns1: <http://example.org#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

    INSERT{
        ?invest rdf:type ns1:Investigation .
    }
    WHERE{
        ?s ns1:investigation ?invest .
}"""

sparql.setQuery(update_str)
sparql.setMethod(POST)
sparql.query()

#To Test
sparql.setQuery("""
    prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX ns1: <http://example.org#>

    ASK{
        ns1:watergate rdf:type ns1:Investigation.
    }
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()
print(results['boolean'])

# Update the graph to that every resource that is an object in a muellerkg:person triple has the rdf:type muellerkg:IndictedPerson.
update_str = """
    PREFIX ns1: <http://example.org#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

    INSERT{
        ?person rdf:type ns1:IndictedPerson .
    }
    WHERE{
        ?s ns1:person ?person .
}"""

sparql.setQuery(update_str)
sparql.setMethod(POST)
sparql.query()

#To test, run the query in the above task, replacing the ask query with e.g. ns1:Deborah_Gore_Dean rdf:type ns1:IndictedPerson

# Update the graph so all the investigation nodes (such as muellerkg:watergate) become the subject in a dc:title triple with the corresponding string (watergate) as the literal.
update_str = """
    PREFIX ns1: <http://example.org#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX dc: <http://purl.org/dc/elements/1.1/>

    INSERT{
        ?invest dc:title ?investString.
    }
    WHERE{
        ?s ns1:investigation ?invest .
        BIND (replace(str(?invest), str(ns1:), "")  AS ?investString)
}"""

sparql.setQuery(update_str)
sparql.setMethod(POST)
sparql.query()

#Same test as above, replace it with e.g. ns1:watergate dc:title "watergate"

# Print out a sorted list of all the indicted persons represented in your graph.
sparql.setQuery("""
    PREFIX ns1: <http://example.org#>
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>

    SELECT ?name
    WHERE{
    ?s  ns1:person ?name;
        ns1:outcome ns1:indictment.
    }
    ORDER BY ?name
""")

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

names = []

for result in results["results"]["bindings"]:
    names.append(result["name"]["value"])

print(names)

# Print out the minimum, average and maximum indictment days for all the indictments in the graph.
sparql.setQuery("""
    prefix xsd: <http://www.w3.org/2001/XMLSchema#>
    PREFIX ns1: <http://example.org#>

    SELECT (AVG(?daysRemoved) as ?avg) (MAX(?daysRemoved) as ?max) (MIN(?daysRemoved) as ?min)  WHERE{
        ?s  ns1:indictment_days ?days;
            ns1:outcome ns1:indictment.
    
    BIND (replace(str(?days), str(ns1:), "")  AS ?daysR)
    BIND (STRDT(STR(?daysR), xsd:float) AS ?daysRemoved)
}
""")

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

for result in results["results"]["bindings"]:
    print(f'The longest an investigation lasted was: {result["max"]["value"]}')
    print(f'The shortest an investigation lasted was: {result["min"]["value"]}')
    print(f'The average investigation lasted: {result["avg"]["value"]}')

# Print out the minimum, average and maximum indictment days for all the indictments in the graph per investigation.
sparql.setQuery("""
    prefix xsd: <http://www.w3.org/2001/XMLSchema#>
    PREFIX ns1: <http://example.org#>

    SELECT ?investigation (AVG(?daysRemoved) as ?avg) (MAX(?daysRemoved) as ?max) (MIN(?daysRemoved) as ?min)  WHERE{
    ?s  ns1:indictment_days ?days;
        ns1:outcome ns1:indictment;
        ns1:investigation ?investigation.
    
    BIND (replace(str(?days), str(ns1:), "")  AS ?daysR)
    BIND (STRDT(STR(?daysR), xsd:float) AS ?daysRemoved)
    }
    GROUP BY ?investigation
""")

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

for result in results["results"]["bindings"]:
    print(f'{result["investigation"]["value"]} - min: {result["min"]["value"]}, max: {result["max"]["value"]}, avg: {result["avg"]["value"]}')

CSV To RDF (Lab 5)

#Imports
import re
from pandas import *
from numpy import nan
from rdflib import Graph, Namespace, URIRef, Literal, RDF, XSD, FOAF
from spotlight import SpotlightException, annotate

SERVER = "https://api.dbpedia-spotlight.org/en/annotate"
# Test around with the confidence, and see how many names changes depending on the confidence. However, be aware that anything lower than this (0.83) it will replace James W. McCord and other names that includes James with LeBron James
CONFIDENCE = 0.83 

def annotate_entity(entity, filters={'types': 'DBpedia:Person'}):
	annotations = []
	try:
		annotations = annotate(address=SERVER, text=entity, confidence=CONFIDENCE, filters=filters)
	except SpotlightException as e:
		print(e)
	return annotations

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

#Pandas' read_csv function to load russia-investigation.csv
df = read_csv("russia-investigation.csv")
#Replaces all instances of nan to None type with numpy's nan
df = df.replace(nan, None)

#Function that prepares the values to be added to the graph as a URI or Literal
def prepareValue(row):
	if row == None: #none type
		value = Literal(row)
	elif isinstance(row, str) and re.match(r'\d{4}-\d{2}-\d{2}', row): #date
		value = Literal(row, datatype=XSD.date)
	elif isinstance(row, bool): #boolean value (true / false)
		value = Literal(row, datatype=XSD.boolean)
	elif isinstance(row, int): #integer
		value = Literal(row, datatype=XSD.integer)
	elif isinstance(row, str): #string
		value = URIRef(ex + row.replace('"', '').replace(" ", "_").replace(",","").replace("-", "_"))
	elif isinstance(row, float): #float
		value = Literal(row, datatype=XSD.float)

	return value

#Convert the non-semantic CSV dataset into a semantic RDF 
def csv_to_rdf(df):
	for index, row in df.iterrows():
		id = URIRef(ex + "Investigation_" + str(index))
		investigation = prepareValue(row["investigation"])
		investigation_start = prepareValue(row["investigation-start"])
		investigation_end = prepareValue(row["investigation-end"])
		investigation_days = prepareValue(row["investigation-days"])
		indictment_days = prepareValue(row["indictment-days "])
		cp_date = prepareValue(row["cp-date"])
		cp_days = prepareValue(row["cp-days"])
		overturned = prepareValue(row["overturned"])
		pardoned = prepareValue(row["pardoned"])
		american = prepareValue(row["american"])
		outcome = prepareValue(row["type"])
		name_ex = prepareValue(row["name"])
		president_ex = prepareValue(row["president"])

		#Spotlight Search
		name = annotate_entity(str(row['name']))
                # Removing the period as some presidents won't be found with it
		president = annotate_entity(str(row['president']).replace(".", ""))
		
		#Adds the tripples to the graph
		g.add((id, RDF.type, ex.Investigation))
		g.add((id, ex.investigation, investigation))
		g.add((id, ex.investigation_start, investigation_start))
		g.add((id, ex.investigation_end, investigation_end))
		g.add((id, ex.investigation_days, investigation_days))
		g.add((id, ex.indictment_days, indictment_days))
		g.add((id, ex.cp_date, cp_date))
		g.add((id, ex.cp_days, cp_days))
		g.add((id, ex.overturned, overturned))
		g.add((id, ex.pardoned, pardoned))
		g.add((id, ex.american, american))
		g.add((id, ex.outcome, outcome))

		#Spotlight search
		#Name
		try:
			g.add((id, ex.person, URIRef(name[0]["URI"])))
		except:
			g.add((id, ex.person, name_ex))

		#President
		try:
			g.add((id, ex.president, URIRef(president[0]["URI"])))
		except:
			g.add((id, ex.president, president_ex))

csv_to_rdf(df)
print(g.serialize())

SHACL (Lab 6)

from pyshacl import validate
from rdflib import Graph

data_graph = Graph()
# parses the Turtle examples from the lab
data_graph.parse("data_graph.ttl")

# Remember to test you need to change the rules so they conflict with the data graph (or vice versa). For example, change "exactly one name" to have exactly two, and see the output 
shape_graph = """
@prefix ex: <http://example.org/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ex:LabTasks_Shape
    a sh:NodeShape ;
    sh:targetClass ex:PersonUnderInvestigation ;
    sh:property [
        sh:path foaf:name ;
        sh:minCount 1 ; #Every person under investigation has exactly one name. 
        sh:maxCount 1 ; #Every person under investigation has exactly one name.
        sh:datatype rdf:langString ; #All person names must be language-tagged
    ] ;
    sh:property [
        sh:path ex:chargedWith ;
        sh:nodeKind sh:IRI ; #The object of a charged with property must be a URI.
        sh:class ex:Offense ; #The object of a charged with property must be an offense.
    ] .

# --- If you have more time tasks ---
ex:MoreTime_Shape rdf:type sh:NodeShape;
    sh:targetClass ex:Indictment;
    
    # The only allowed values for ex:american are true, false or unknown.
    sh:property [
        sh:path ex:american;
        sh:pattern "(true|false|unknown)" ;
    ] ;
    
    # The value of a property that counts days must be an integer.
    sh:property [
        sh:path ex:indictment_days;
        sh:datatype xsd:integer;
    ] ;   
    sh:property [
        sh:path ex:investigation_days;
        sh:datatype xsd:integer;
    ] ;
    
    # The value of a property that indicates a start date must be xsd:date.
    sh:property [
        sh:path ex:investigation_start;
        sh:datatype xsd:date;
    ] ;

    # The value of a property that indicates an end date must be xsd:date or unknown (tip: you can use sh:or (...) ).
    sh:property [
        sh:path ex:investigation_end;
        sh:or (
         [ sh:datatype xsd:date ]
         [ sh:hasValue "unknown" ]
    )] ;
    
    # Every indictment must have exactly one FOAF name for the investigated person.
    sh:property [
        sh:path foaf:name;
        sh:minCount 1;
        sh:maxCount 1;
    ] ;
    
    # Every indictment must have exactly one investigated person property, and that person must have the type ex:PersonUnderInvestigation.
    sh:property [
        sh:path ex:investigatedPerson ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
        sh:class ex:PersonUnderInvestigation ;
        sh:nodeKind sh:IRI ;
    ] ;

    # No URI-s can contain hyphens ('-').
    sh:property [
        sh:path ex:outcome ;
        sh:nodeKind sh:IRI ;
        sh:pattern "^[^-]*$" ;
    ] ;

    # Presidents must be identified with URIs.
    sh:property [
        sh:path ex:president ;
        sh:class ex:President ;
        sh:nodeKind sh:IRI ;
    ] .
"""

shacl_graph = Graph()
# parses the contents of a shape_graph made in the tasks
shacl_graph.parse(data=shape_graph)

# uses pySHACL's validate method to apply the shape_graph constraints to the data_graph
results = validate(
    data_graph,
    shacl_graph=shacl_graph,
    inference='both'
)

# prints out the validation result
boolean_value, results_graph, results_text = results

# print(boolean_value)
print(results_graph.serialize(format='ttl'))
# print(results_text)

#Write a SPARQL query to print out each distinct sh:resultMessage in the results_graph
distinct_messages = """
PREFIX sh: <http://www.w3.org/ns/shacl#> 

SELECT DISTINCT ?message WHERE {
    [] sh:result ?errorBlankNode.
    ?errorBlankNode sh:resultMessage ?message.    

    # Alternativ and cleaner solution, look at https://www.w3.org/TR/sparql11-query/#pp-language (9.1 Property Path Syntax)
    # [] sh:result / sh:resultMessage ?message .
}
"""
messages = results_graph.query(distinct_messages)
for row in messages:
    print(row.message)

#each sh:resultMessage in the results_graph once, along with the number of times that message has been repeated in the results
count_messages = """
PREFIX sh: <http://www.w3.org/ns/shacl#> 

SELECT ?message (COUNT(?node) AS ?num_messages) WHERE {
    [] sh:result ?errorBlankNode .
    ?errorBlankNode sh:resultMessage ?message ;
                    sh:focusNode ?node .
}
GROUP BY ?message
ORDER BY DESC(?count) ?message
"""

messages = results_graph.query(count_messages)
for row in messages:
    print(f"COUNT: {row.num_messages} | MESSAGE: {row.message}")

RDFS (Lab 7)

import owlrl
from rdflib import Graph, RDF, Namespace, FOAF, RDFS

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

g.bind("ex", ex)
g.bind("foaf", FOAF)

NS = {
    'ex': ex,
    'rdf': RDF,
    'rdfs': RDFS,
    'foaf': FOAF,
}

#Write a small function that computes the RDFS closure on your graph.
def flush():
    owlrl.DeductiveClosure(owlrl.RDFS_Semantics).expand(g)

#Rick Gates was charged with money laundering and tax evasion.
g.add((ex.Rick_Gates, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.Rick_Gates, ex.chargedWith, ex.TaxEvasion))

#When one thing that is charged with another thing,
g.add((ex.chargedWith, RDFS.domain, ex.PersonUnderInvestigation))  #the first thing is a person under investigation and
g.add((ex.chargedWith, RDFS.range, ex.Offense))  #the second thing is an offense.

#Write a SPARQL query that checks the RDF type(s) of Rick Gates and money laundering in your RDF graph.
print('Is Rick Gates a ex:PersonUnderInvestigation:', g.query('ASK {ex:Rick_Gates rdf:type ex:PersonUnderInvestigation}', initNs=NS).askAnswer)
print('Is Money Laundering a ex:Offense:', g.query('ASK {ex:MoneyLaundering rdf:type ex:Offense}', initNs=NS).askAnswer)
flush()
print('Is Rick Gates a ex:PersonUnderInvestigation:', g.query('ASK {ex:Rick_Gates rdf:type ex:PersonUnderInvestigation}', initNs=NS).askAnswer)
print('Is Money Laundering a ex:Offense:', g.query('ASK {ex:MoneyLaundering rdf:type ex:Offense}', initNs=NS).askAnswer)

#A person under investigation is a FOAF person
g.add((ex.PersonUnderInvestigation, RDFS.subClassOf, FOAF.Person))
print('Is Rick Gates a foaf:Person:', g.query('ASK {ex:Rick_Gates rdf:type foaf:Person}', initNs=NS).askAnswer)
flush()
print('Is Rick Gates a foaf:Person:', g.query('ASK {ex:Rick_Gates rdf:type foaf:Person}', initNs=NS).askAnswer)

#Paul Manafort was convicted for tax evasion.
g.add((ex.Paul_Manafort, ex.convictedFor, ex.TaxEvasion))
#the first thing is also charged with the second thing
g.add((ex.convictedFor, RDFS.subPropertyOf, ex.chargedWith)) 
print('Is Paul Manafort charged with Tax Evasion:', g.query('ASK {ex:Paul_Manafort ex:chargedWith ex:TaxEvasion}', initNs=NS).askAnswer)
flush()
print('Is Paul Manafort charged with Tax Evasion:', g.query('ASK {ex:Paul_Manafort ex:chargedWith ex:TaxEvasion}', initNs=NS).askAnswer)

print(g.serialize())

OWL 1 (Lab 8)

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

g = Graph()
ex = Namespace('http://example.org/')
schema = Namespace('http://schema.org/')
dbr = Namespace('https://dbpedia.org/page/')

g.bind("ex", ex)
# g.bind("schema", schema)
g.bind("foaf", FOAF)

# Donald Trump and Robert Mueller are two different persons.
g.add((ex.Donald_Trump, OWL.differentFrom, ex.Robert_Mueller))

# Actually, all the names mentioned in connection with the Mueller investigation refer to different people.
b1 = BNode()
b2 = BNode()
Collection(g, b2, [ex.Robert_Mueller, ex.Paul_Manafort, ex.Rick_Gates, ex.George_Papadopoulos, ex.Michael_Flynn, ex.Michael_Cohen, ex.Roger_Stone, ex.Donald_Trump])
g.add((b1, RDF.type, OWL.AllDifferent))
g.add((b1, OWL.distinctMembers, b2))

# All these people are foaf:Persons as well as schema:Persons
g.add((FOAF.Person, OWL.equivalentClass, schema.Person))

# Tax evation is a kind of bank and tax fraud.
g.add((ex.TaxEvation, RDFS.subClassOf, ex.BankFraud))
g.add((ex.TaxEvation, RDFS.subClassOf, ex.TaxFraud))

# The Donald Trump involved in the Mueller investigation is dbpedia:Donald_Trump and not dbpedia:Donald_Trump_Jr.
g.add((ex.Donald_Trump, OWL.sameAs, dbr.Donald_Trump))
g.add((ex.Donald_Trump, OWL.differentFrom, URIRef(dbr + "Donald_Trump_Jr.")))

# Congress, FBI and the Mueller investigation are foaf:Organizations.
g.add((ex.Congress, RDF.type, FOAF.Organization))
g.add((ex.FBI, RDF.type, FOAF.Organization))
g.add((ex.Mueller_Investigation, RDF.type, FOAF.Organization))

# Nothing can be both a person and an organization.
g.add((FOAF.Person, OWL.disjointWith, FOAF.Organization))

# Leading an organization is a way of being involved in an organization.
g.add((ex.leading, RDFS.subPropertyOf, ex.involved))

# Being a campaign manager or an advisor for is a way of supporting someone.
g.add((ex.campaignManagerTo, RDFS.subPropertyOf, ex.supports))
g.add((ex.advisorTo, RDFS.subPropertyOf, ex.supports))

# Donald Trump is a politician and a Republican.
g.add((ex.Donald_Trump, RDF.type, ex.Politician))
g.add((ex.Donald_Trump, RDF.type, ex.Republican))

# A Republican politician is both a politician and a Republican.
g.add((ex.RepublicanPolitician, RDFS.subClassOf, ex.Politician))
g.add((ex.RepublicanPolitician, RDFS.subClassOf, ex.Republican))

#hasBusinessPartner
g.add((ex.Paul_Manafort, ex.hasBusinessPartner, ex.Rick_Gates))
g.add((ex.hasBusinessPartner, RDF.type, OWL.SymmetricProperty))
g.add((ex.hasBusinessPartner, RDF.type, OWL.IrreflexiveProperty))

#adviserTo
g.add((ex.Michael_Flynn, ex.adviserTo, ex.Donald_Trump))
g.add((ex.adviserTo, RDF.type, OWL.IrreflexiveProperty))
# Not necessarily asymmetric as it's not a given that they couldn't be advisors to each other  

#wasLyingTo
g.add((ex.Rick_Gates_Lying, ex.wasLyingTo, ex.FBI))
g.add((ex.wasLyingTo, RDF.type, OWL.IrreflexiveProperty))
# Not asymmetric as the subject and object could lie to each other; also in this context, the FBI can lie to you

#presidentOf
g.add((ex.Donald_Trump, ex.presidentOf, ex.USA))
g.add((ex.presidentOf, RDF.type, OWL.AsymmetricProperty))
g.add((ex.presidentOf, RDF.type, OWL.IrreflexiveProperty))
g.add((ex.presidentOf, RDF.type, OWL.FunctionalProperty)) #can only be president of one country
#not inversefunctionalproperty as Bosnia has 3 presidents https://www.culturalworld.org/do-any-countries-have-more-than-one-president.htm

#hasPresident
g.add((ex.USA, ex.hasPresident, ex.Donald_Trump))
g.add((ex.hasPresident, RDF.type, OWL.AsymmetricProperty))
g.add((ex.hasPresident, RDF.type, OWL.IrreflexiveProperty))
g.add((ex.hasPresident, RDF.type, OWL.InverseFunctionalProperty)) #countries do not share their president with another
#not functionalproperty as a country (Bosnia) can have more than one president

#Closure
owlrl.DeductiveClosure(owlrl.OWLRL_Semantics).expand(g)

#Serialization
print(g.serialize(format="ttl"))
# g.serialize("lab8.xml", format="xml") #serializes to XML file

OWL 2 (Lab 9)

NOTE: This is an OWL Protégé file

@prefix : <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
@prefix dc: <http://purl.org/dc/terms#> .
@prefix io: <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
@prefix dbr: <http://dbpedia.org/resource/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .

<http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology> rdf:type owl:Ontology .

#################################################################
#    Object Properties
#################################################################

###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#indictedIn
io:indictedIn rdf:type owl:ObjectProperty ;
              rdfs:subPropertyOf io:involvedIn ;
              rdfs:domain io:InvestigatedPerson ;
              rdfs:range io:Investigation .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#investigating
io:investigating rdf:type owl:ObjectProperty ;
                 rdfs:subPropertyOf io:involvedIn ;
                 rdfs:domain io:Investigator ;
                 rdfs:range io:Investigation .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#involvedIn
io:involvedIn rdf:type owl:ObjectProperty ;
              rdfs:domain foaf:Person ;
              rdfs:range io:Investigation .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#leading
io:leading rdf:type owl:ObjectProperty ;
           rdfs:subPropertyOf io:investigating ;
           rdfs:domain io:InvestigationLeader ;
           rdfs:range io:Investigation .


#################################################################
#    Data properties
#################################################################

###  http://purl.org/dc/elements/1.1/description
<http://purl.org/dc/elements/1.1/description> rdf:type owl:DatatypeProperty ;
                                              rdfs:domain io:Investigation ;
                                              rdfs:range xsd:string .


###  http://purl.org/dc/elements/1.1/title
<http://purl.org/dc/elements/1.1/title> rdf:type owl:DatatypeProperty ;
                                        rdfs:domain io:Investigation ;
                                        rdfs:range xsd:string .


###  http://www.w3.org/ns/prov#endedAtTime
prov:endedAtTime rdf:type owl:DatatypeProperty ,
                          owl:FunctionalProperty ;
                 rdfs:domain io:Investigation ;
                 rdfs:range xsd:dateTime .


###  http://www.w3.org/ns/prov#startedAtTime
prov:startedAtTime rdf:type owl:DatatypeProperty ,
                            owl:FunctionalProperty ;
                   rdfs:domain io:Investigation ;
                   rdfs:range xsd:dateTime .


###  http://xmlns.com/foaf/0.1/name
foaf:name rdf:type owl:DatatypeProperty ;
          rdfs:domain foaf:Person ;
          rdfs:range xsd:string .


#################################################################
#    Classes
#################################################################

###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigatedPerson
io:InvestigatedPerson rdf:type owl:Class ;
                      rdfs:subClassOf io:Person ;
                      owl:disjointWith io:Investigator .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation
io:Investigation rdf:type owl:Class .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigationLeader
io:InvestigationLeader rdf:type owl:Class ;
                       rdfs:subClassOf io:Investigator .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigator
io:Investigator rdf:type owl:Class ;
                rdfs:subClassOf io:Person .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Person
io:Person rdf:type owl:Class ;
          rdfs:subClassOf foaf:Person .


###  http://xmlns.com/foaf/0.1/Person
foaf:Person rdf:type owl:Class .


#################################################################
#    Individuals
#################################################################

###  http://dbpedia.org/resource/Donald_Trump
dbr:Donald_Trump rdf:type owl:NamedIndividual ;
                 foaf:name "Donald Trump" .


###  http://dbpedia.org/resource/Elizabeth_Prelogar
dbr:Elizabeth_Prelogar rdf:type owl:NamedIndividual ;
                       foaf:name "Elizabeth Prelogar" .


###  http://dbpedia.org/resource/Michael_Flynn
dbr:Michael_Flynn rdf:type owl:NamedIndividual ;
                  foaf:name "Michael Flynn" .


###  http://dbpedia.org/resource/Paul_Manafort
dbr:Paul_Manafort rdf:type owl:NamedIndividual ;
                  foaf:name "Paul Manafort" .


###  http://dbpedia.org/resource/Robert_Mueller
dbr:Robert_Mueller rdf:type owl:NamedIndividual ;
                   foaf:name "Robert Mueller" .


###  http://dbpedia.org/resource/Roger_Stone
dbr:Roger_Stone rdf:type owl:NamedIndividual ;
                foaf:name "Roger Stone" .


###  http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)
<http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> rdf:type owl:NamedIndividual ,
                                                                                 io:Investigation ;
                                                                        <http://purl.org/dc/elements/1.1/title> "Mueller Investigation" .


#################################################################
#    General axioms
#################################################################

[ rdf:type owl:AllDifferent ;
  owl:distinctMembers ( dbr:Donald_Trump
                        dbr:Elizabeth_Prelogar
                        dbr:Michael_Flynn
                        dbr:Paul_Manafort
                        dbr:Robert_Mueller
                        dbr:Roger_Stone
                      )
] .


###  Generated by the OWL API (version 4.5.25.2023-02-15T19:15:49Z) https://github.com/owlcs/owlapi

OWL-DL (Lab 10)

NOTE: This is an OWL Protégé file.

@prefix : <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
@prefix dc: <http://purl.org/dc/terms#> .
@prefix io: <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .
@prefix dbr: <http://dbpedia.org/resource/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#> .

<http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology> rdf:type owl:Ontology .

#################################################################
#    Object Properties
#################################################################

###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#indictedIn
io:indictedIn rdf:type owl:ObjectProperty ;
              rdfs:subPropertyOf io:involvedIn ;
              rdfs:domain io:InvestigatedPerson ;
              rdfs:range io:Investigation .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#investigating
io:investigating rdf:type owl:ObjectProperty ;
                 rdfs:subPropertyOf io:involvedIn ;
                 rdfs:domain io:Investigator ;
                 rdfs:range io:Investigation .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#involvedIn
io:involvedIn rdf:type owl:ObjectProperty ;
              rdfs:domain foaf:Person ;
              rdfs:range io:Investigation .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#leading
io:leading rdf:type owl:ObjectProperty ;
           rdfs:subPropertyOf io:investigating ;
           rdfs:domain io:InvestigationLeader ;
           rdfs:range io:Investigation .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#specialProsecutor
io:specialProsecutor rdf:type owl:ObjectProperty ;
                     rdfs:subPropertyOf io:leading .


#################################################################
#    Data properties
#################################################################

###  http://purl.org/dc/elements/1.1/description
<http://purl.org/dc/elements/1.1/description> rdf:type owl:DatatypeProperty ;
                                              rdfs:domain io:Investigation ;
                                              rdfs:range xsd:string .


###  http://purl.org/dc/elements/1.1/title
<http://purl.org/dc/elements/1.1/title> rdf:type owl:DatatypeProperty ;
                                        rdfs:range xsd:string .


###  http://www.w3.org/ns/prov#endedAtTime
prov:endedAtTime rdf:type owl:DatatypeProperty ,
                          owl:FunctionalProperty ;
                 rdfs:domain io:Investigation ;
                 rdfs:range xsd:dateTime .


###  http://www.w3.org/ns/prov#startedAtTime
prov:startedAtTime rdf:type owl:DatatypeProperty ,
                            owl:FunctionalProperty ;
                   rdfs:domain io:Investigation ;
                   rdfs:range xsd:dateTime .


###  http://xmlns.com/foaf/0.1/name
foaf:name rdf:type owl:DatatypeProperty ;
          rdfs:domain foaf:Person ;
          rdfs:range xsd:string .


#################################################################
#    Classes
#################################################################

###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigatedPerson
io:InvestigatedPerson rdf:type owl:Class ;
                      rdfs:subClassOf io:Person ;
                      owl:disjointWith io:Investigator .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation
io:Investigation rdf:type owl:Class .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#InvestigationLeader
io:InvestigationLeader rdf:type owl:Class ;
                       rdfs:subClassOf io:Investigator .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigator
io:Investigator rdf:type owl:Class ;
                rdfs:subClassOf io:Person .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Person
io:Person rdf:type owl:Class ;
          rdfs:subClassOf foaf:Person .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#SpecialCounselInvestigation
io:SpecialCounselInvestigation rdf:type owl:Class ;
                               owl:equivalentClass [ rdf:type owl:Restriction ;
                                                     owl:onProperty [ owl:inverseOf io:specialProsecutor
                                                                    ] ;
                                                     owl:someValuesFrom owl:Thing
                                                   ] ;
                               rdfs:subClassOf io:Investigation .


###  http://xmlns.com/foaf/0.1/Person
foaf:Person rdf:type owl:Class .


#################################################################
#    Individuals
#################################################################

###  http://dbpedia.org/resource/Donald_Trump
dbr:Donald_Trump rdf:type owl:NamedIndividual ;
                 foaf:name "Donald Trump" .


###  http://dbpedia.org/resource/Elizabeth_Prelogar
dbr:Elizabeth_Prelogar rdf:type owl:NamedIndividual ,
                                io:Investigator ;
                       io:investigating <http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> ;
                       foaf:name "Elizabeth Prelogar" .


###  http://dbpedia.org/resource/Michael_Flynn
dbr:Michael_Flynn rdf:type owl:NamedIndividual ;
                  foaf:name "Michael Flynn" .


###  http://dbpedia.org/resource/Paul_Manafort
dbr:Paul_Manafort rdf:type owl:NamedIndividual ;
                  io:indictedIn <http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> ;
                  foaf:name "Paul Manafort" .


###  http://dbpedia.org/resource/Robert_Mueller
dbr:Robert_Mueller rdf:type owl:NamedIndividual ;
                   io:leading <http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> ;
                   foaf:name "Robert Mueller" .


###  http://dbpedia.org/resource/Roger_Stone
dbr:Roger_Stone rdf:type owl:NamedIndividual ;
                foaf:name "Roger Stone" .


###  http://dbpedia.org/resource/Jack_Smith_(politician)
<http://dbpedia.org/resource/Jack_Smith_(politician)> rdf:type owl:NamedIndividual ;
                                                      io:specialProsecutor io:Investigation_of_Trumps_Handling_Graded_Documents ,
                                                                           io:Investigation_of_Trumps_Role_US_Capital_Attack .


###  http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)
<http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> rdf:type owl:NamedIndividual ;
                                                                        <http://purl.org/dc/elements/1.1/title> "Mueller Investigation" .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation_of_Trumps_Handling_Graded_Documents
io:Investigation_of_Trumps_Handling_Graded_Documents rdf:type owl:NamedIndividual .


###  http://www.semanticweb.org/bruker/ontologies/2023/2/InvestigationOntology#Investigation_of_Trumps_Role_US_Capital_Attack
io:Investigation_of_Trumps_Role_US_Capital_Attack rdf:type owl:NamedIndividual .


#################################################################
#    General axioms
#################################################################

[ rdf:type owl:AllDifferent ;
  owl:distinctMembers ( dbr:Donald_Trump
                        dbr:Elizabeth_Prelogar
                        dbr:Michael_Flynn
                        dbr:Paul_Manafort
                        dbr:Robert_Mueller
                        dbr:Roger_Stone
                        <http://dbpedia.org/resource/Jack_Smith_(politician)>
                      )
] .


###  Generated by the OWL API (version 4.5.25.2023-02-15T19:15:49Z) https://github.com/owlcs/owlapi

Using Graph Embeddings (Lab 11)

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

Training Graph Embeddings (Lab 12)

https://colab.research.google.com/drive/1jKpzlQ7gYTVzgphJsrK5iuMpFhkrY96q

Web APIs and JSON-LD (Lab 13)

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

r = requests.get('http://api.open-notify.org/astros.json').json()

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

g.bind("ex", ex)
NS = {
    "ex": ex,
    "foaf":FOAF
}

#Write a small program that queries the Open Notify Astros API
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 .
        }
""", initNs=NS)

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

#Serialise the graph to JSON-LD     
print(g.serialize(format="json-ld"))

#DBpedia Spotlight was worked on Lab 5 (CSV to RDF).