Lab Solutions: Difference between revisions
From info216
Added proposed solution for Lab 8 - JSON-LD |
No edit summary |
||
| (21 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
Here we will present suggested solutions after each lab. ''The page will be updated as the course progresses'' | Here we will present suggested solutions after each lab. ''The page will be updated as the course progresses'' | ||
=Getting started (Lab 1)= | =Getting started (Lab 1)= | ||
| Line 86: | Line 89: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<!-- | |||
=RDF programming with RDFlib (Lab 2)= | =RDF programming with RDFlib (Lab 2)= | ||
| Line 1,338: | Line 1,341: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=SHACL (Lab 9)= | |||
<syntaxhighlight lang="Python"> | |||
from pyshacl import validate | |||
from rdflib import Graph | |||
data_graph = Graph() | |||
# parses the Turtle example from the task | |||
data_graph.parse("data_graph.ttl") | |||
prefixes = """ | |||
@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#> . | |||
""" | |||
shape_graph = """ | |||
ex:PUI_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:User_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:minCount 1 ; | |||
sh:class ex:President ; | |||
sh:nodeKind sh:IRI ; | |||
] . | |||
""" | |||
shacl_graph = Graph() | |||
# parses the contents of a shape_graph you made in the previous task | |||
shacl_graph.parse(data=prefixes+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 / 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 ?result . | |||
?result 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("COUNT MESSAGE") | |||
print(row.num_messages, " ", row.message) | |||
</syntaxhighlight> | |||
=RDFS (Lab 10)= | |||
<syntaxhighlight lang="Python"> | |||
import owlrl | |||
from rdflib import Graph, RDF, Namespace, Literal, XSD, FOAF, RDFS | |||
from rdflib.collection import Collection | |||
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(): | |||
engine = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False) | |||
engine.closure() | |||
engine.flush_stored_triples() | |||
#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 (subject) is a person under investigation and | |||
g.add((ex.chargedWith, RDFS.range, ex.Offense)) #the second thing (object) is an offense. | |||
#Write a SPARQL query that checks the RDF type(s) of Rick Gates and money laundering in your RDF graph. | |||
print(g.query('ASK {ex:Rick_Gates rdf:type ex:PersonUnderInvestigation}', initNs=NS).askAnswer) | |||
print(g.query('ASK {ex:MoneyLaundering rdf:type ex:Offense}', initNs=NS).askAnswer) | |||
flush() | |||
print(g.query('ASK {ex:Rick_Gates rdf:type ex:PersonUnderInvestigation}', initNs=NS).askAnswer) | |||
print(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(g.query('ASK {ex:Rick_Gates rdf:type foaf:Person}', initNs=NS).askAnswer) | |||
flush() | |||
print(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)) | |||
flush() | |||
print(g.query('ASK {ex:Paul_Manafort ex:chargedWith ex:TaxEvasion}', initNs=NS).askAnswer) | |||
print(g.serialize()) | |||
</syntaxhighlight> | |||
=OWL 1 (Lab 11)= | |||
<syntaxhighlight lang="Python"> | |||
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 | |||
</syntaxhighlight> | |||
=OWL 2 (Lab 12)= | |||
<syntaxhighlight lang="Python"> | |||
@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://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 . | |||
### http://xmlns.com/foaf/0.1/title | |||
foaf:title rdf:type owl:DatatypeProperty ; | |||
rdfs:domain io:Investigation ; | |||
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 ; | |||
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/Special_Counsel_investigation_(2017–2019) | |||
<http://dbpedia.org/resource/Special_Counsel_investigation_(2017–2019)> rdf:type owl:NamedIndividual ; | |||
foaf: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 | |||
</syntaxhighlight> | |||
=Using Graph Embeddings (Lab 13)= | |||
https://colab.research.google.com/drive/1WkRJUeUBVF5yVv7o0pOKfsd4pqG6369k | |||
=Training Graph Embeddings (Lab 14)= | |||
https://colab.research.google.com/drive/1jKpzlQ7gYTVzgphJsrK5iuMpFhkrY96q | |||
--> | |||
Latest revision as of 16:35, 14 January 2026
Here we will present suggested solutions after each lab. The page will be updated as the course progresses
Getting started (Lab 1)
from rdflib import Graph, Namespace
ex = Namespace('http://example.org/')
g = Graph()
g.bind("ex", ex)
# The Mueller Investigation was lead by Robert Mueller
g.add((ex.MuellerInvestigation, ex.leadBy, ex.RobertMueller))
# It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, Michael Cohen, and Roger Stone.
g.add((ex.MuellerInvestigation, ex.involved, ex.PaulManafort))
g.add((ex.MuellerInvestigation, ex.involved, ex.RickGates))
g.add((ex.MuellerInvestigation, ex.involved, ex.GeorgePapadopoulos))
g.add((ex.MuellerInvestigation, ex.involved, ex.MichaelFlynn))
g.add((ex.MuellerInvestigation, ex.involved, ex.MichaelCohen))
g.add((ex.MuellerInvestigation, ex.involved, ex.RogerStone))
# Paul Manafort was business partner of Rick Gates
g.add((ex.PaulManafort, ex.businessPartner, ex.RickGates))
# He was campaign chairman for Donald Trump
g.add((ex.PaulManafort, ex.campaignChairman, ex.DonaldTrump))
# He was charged with money laundering, tax evasion, and foreign lobbying.
g.add((ex.PaulManafort, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.PaulManafort, ex.chargedWith, ex.TaxEvasion))
g.add((ex.PaulManafort, ex.chargedWith, ex.ForeignLobbying))
# He was convicted for bank and tax fraud.
g.add((ex.PaulManafort, ex.convictedOf, ex.BankFraud))
g.add((ex.PaulManafort, ex.convictedOf, ex.TaxFraud))
# He pleaded guilty to conspiracy.
g.add((ex.PaulManafort, ex.pleadGuiltyTo, ex.Conspiracy))
# He was sentenced to prison.
g.add((ex.PaulManafort, ex.sentencedTo, ex.Prison))
# He negotiated a plea agreement.
g.add((ex.PaulManafort, ex.negotiated, ex.PleaAgreement))
# Rick Gates was charged with money laundering, tax evasion and foreign lobbying.
g.add((ex.RickGates, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.RickGates, ex.chargedWith, ex.TaxEvasion))
g.add((ex.RickGates, ex.chargedWith, ex.ForeignLobbying))
# He pleaded guilty to conspiracy and lying to FBI.
g.add((ex.RickGates, ex.pleadGuiltyTo, ex.Conspiracy))
g.add((ex.RickGates, ex.pleadGuiltyTo, ex.LyingToFBI))
# Use the serialize method of rdflib.Graph to write out the model in different formats (on screen or to file)
print(g.serialize(format="ttl")) # To screen
#g.serialize("lab1.ttl", format="ttl") # 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)
# --- IF you have more time tasks ---
# 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(graphInput):
data = {"rdf":graphInput, "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 file:
shutil.copyfileobj(response.raw, file)
graph = g.serialize(format="ttl")
graphToImage(graph)