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: | ||
Here we will present suggested solutions after each lab. ''The page will be updated as the course progresses'' | |||
=Getting started (Lab 1)= | |||
<syntaxhighlight> | <syntaxhighlight> | ||
from rdflib import Graph, Namespace | |||
ex = Namespace('http://example.org/') | |||
g = Graph() | g = Graph() | ||
g.bind("ex", ex) | |||
g.bind( | |||
# The Mueller Investigation was lead by Robert Mueller | |||
g.add((ex.MuellerInvestigation, ex.leadBy, ex.RobertMueller)) | |||
g | # It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, Michael Cohen, and Roger Stone. | ||
g. | 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)) | |||
g.add(( | |||
# He was campaign chairman for Donald Trump | |||
g.add((ex.PaulManafort, ex.campaignChairman, ex.DonaldTrump)) | |||
# He was campaign chairman for Trump | |||
g.add((ex. | |||
# 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. | g.add((ex.PaulManafort, ex.chargedWith, ex.MoneyLaundering)) | ||
g.add((ex. | g.add((ex.PaulManafort, ex.chargedWith, ex.TaxEvasion)) | ||
g.add((ex. | 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. | g.add((ex.PaulManafort, ex.convictedOf, ex.BankFraud)) | ||
g.add((ex. | g.add((ex.PaulManafort, ex.convictedOf, ex.TaxFraud)) | ||
# He pleaded guilty to conspiracy. | # He pleaded guilty to conspiracy. | ||
g.add((ex. | g.add((ex.PaulManafort, ex.pleadGuiltyTo, ex.Conspiracy)) | ||
# He was sentenced to prison. | # He was sentenced to prison. | ||
g.add((ex. | g.add((ex.PaulManafort, ex.sentencedTo, ex.Prison)) | ||
# He negotiated a plea agreement. | # He negotiated a plea agreement. | ||
g.add((ex. | g.add((ex.PaulManafort, ex.negotiated, ex.PleaAgreement)) | ||
# 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. | g.add((ex.RickGates, ex.chargedWith, ex.TaxEvasion)) | ||
g.add((ex. | g.add((ex.RickGates, ex.chargedWith, ex.ForeignLobbying)) | ||
g.add((ex. | |||
#He pleaded guilty to conspiracy and lying to FBI. | # He pleaded guilty to conspiracy and lying to FBI. | ||
g.add((ex. | g.add((ex.RickGates, ex.pleadGuiltyTo, ex.Conspiracy)) | ||
g.add((ex. | 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") # | #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( | def graphToImage(graphInput): | ||
data = {"rdf": | 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 | with open("lab1.png", "wb") as file: | ||
shutil.copyfileobj(response.raw, | 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)