Lab Solutions: Difference between revisions

From info216
m (Added lab1 solutions)
Tags: Replaced Visual edit
 
(45 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This page will be updated with Python examples related to the labs 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)=
=Examples from the lectures=
 
==Lecture 1: Introduction to KGs==
Turtle example:
<syntaxhighlight lang="Turtle">
@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>
<syntaxhighlight>
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD


EX = Namespace('http://example.org/')
from rdflib import Graph, Namespace


g = Graph()
ex = Namespace('http://example.org/')
g.bind('ex', EX)
 
# This is a task in Exercise 2
 
print(g.serialize(format='turtle'))
</syntaxhighlight>
 
Literals:
<syntaxhighlight>
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD
 
EX = Namespace('http://example.org/')


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


g.add((EX.Robert_Mueller, RDF.type, EX.Human))
g.bind("ex", ex)
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>
 
Alternative container (open):
<syntaxhighlight>
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'))
</syntaxhighlight>
 
Sequence container (open):
<syntaxhighlight>
from rdflib import Graph, Namespace, Literal, BNode, RDF, RDFS, DC, FOAF, XSD


EX = Namespace('http://example.org/')
# The Mueller Investigation was lead by Robert Mueller
g.add((ex.MuellerInvestigation, ex.leadBy, ex.RobertMueller))


g = Graph()
# It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, Michael Cohen, and Roger Stone.
g.bind('ex', EX)
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))


donaldTrumpSpouses = BNode()
# Paul Manafort was business partner of Rick Gates
g.add((donaldTrumpSpouses, RDF.type, RDF.Seq))
g.add((ex.PaulManafort, ex.businessPartner, ex.RickGates))
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))
# He was campaign chairman for Donald Trump
 
g.add((ex.PaulManafort, ex.campaignChairman, ex.DonaldTrump))
print(g.serialize(format='turtle'))
</syntaxhighlight>
 
Collection (closed list):
<syntaxhighlight>
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'))
</syntaxhighlight>
 
=Example lab solutions=
 
==Getting started (Lab 1)==
 
<syntaxhighlight>
 
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.
# He was charged with money laundering, tax evasion, and foreign lobbying.
g.add((ex.Paul_Manafort, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.PaulManafort, ex.chargedWith, ex.MoneyLaundering))
g.add((ex.Paul_Manafort, ex.chargedWith, ex.TaxEvasion))
g.add((ex.PaulManafort, ex.chargedWith, ex.TaxEvasion))
g.add((ex.Paul_Manafort, ex.chargedWith, ex.ForeignLobbying))
g.add((ex.PaulManafort, ex.chargedWith, ex.ForeignLobbying))


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


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


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


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


#Use the serialize method to write out the model in different formats on screen
# 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"))
print(g.serialize(format="ttl")) # To screen
# g.serialize("lab1.ttl", format="ttl") #or to file
#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
# 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 : ]:
for subject, object in g[ : ex.pleadGuiltyTo :]:
     print(subject, ex.pleadGuiltyTo, object)
     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  
# Michael Cohen, Michael Flynn and the lying is part of lab 2 and therefore the answer is not provided this week  
Line 211: Line 71:
import shutil
import shutil


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


graph = g.serialize(format="ttl")
graph = g.serialize(format="ttl")
graphToImage(graph)
graphToImage(graph)
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 09:10, 3 February 2025

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)