Lab Solutions: Difference between revisions

From info216
No edit summary
m (Added lab1 solutions)
Tags: Replaced Visual edit
 
(166 intermediate revisions by 7 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.
Here we will present suggested solutions after each lab. ''The page will be updated as the course progresses''
 
=Getting started (Lab 1)=
 
==Lecture 1: Python, RDFlib, and PyCharm==
 
 
===Printing the triples of the Graph in a readable way===
<syntaxhighlight>
<syntaxhighlight>
# The turtle format has the purpose of being more readable for humans.
print(g.serialize(format="turtle").decode())
</syntaxhighlight>


===Coding Tasks Lab 1===
from rdflib import Graph, Namespace
<syntaxhighlight>
from rdflib import Graph, Namespace, URIRef, BNode, Literal
from rdflib.namespace import RDF, FOAF, XSD


g = Graph()
ex = Namespace('http://example.org/')
ex = Namespace("http://example.org/")
 
g.add((ex.Cade, ex.married, ex.Mary))
g.add((ex.France, ex.capital, ex.Paris))
g.add((ex.Cade, ex.age, Literal("27", datatype=XSD.integer)))
g.add((ex.Mary, ex.age, Literal("26", datatype=XSD.integer)))
g.add((ex.Mary, ex.interest, ex.Hiking))
g.add((ex.Mary, ex.interest, ex.Chocolate))
g.add((ex.Mary, ex.interest, ex.Biology))
g.add((ex.Mary, RDF.type, ex.Student))
g.add((ex.Paris, RDF.type, ex.City))
g.add((ex.Paris, ex.locatedIn, ex.France))
g.add((ex.Cade, ex.characteristic, ex.Kind))
g.add((ex.Mary, ex.characteristic, ex.Kind))
g.add((ex.Mary, RDF.type, FOAF.Person))
g.add((ex.Cade, RDF.type, FOAF.Person))
 
</syntaxhighlight>
 
===Lab 1/2 - Different ways of Making an Address ===
 
<syntaxhighlight>
 
from rdflib import Graph, Namespace, URIRef, BNode, Literal
from rdflib.namespace import RDF, FOAF, XSD


g = Graph()
g = Graph()
ex = Namespace("http://example.org/")
# How to represent the address of Cade Tracey. From probably the worst solution to the best.


# Solution 1 -
g.bind("ex", ex)
# Make the entire address into one Literal. However, Generally we want to seperate each part of an address into their own triples. This is useful for instance if we want to find only the streets where people live.


g.add((ex.Cade_Tracey, ex.livesIn, Literal("1516_Henry_Street, Berkeley, California 94709, USA")))
# 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))


# Solution 2 -
# Paul Manafort was business partner of Rick Gates
# Seperate the different pieces information into their own triples
g.add((ex.PaulManafort, ex.businessPartner, ex.RickGates))


g.add((ex.Cade_tracey, ex.street, Literal("1516_Henry_Street")))
# He was campaign chairman for Donald Trump
g.add((ex.Cade_tracey, ex.city, Literal("Berkeley")))
g.add((ex.PaulManafort, ex.campaignChairman, ex.DonaldTrump))
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")))


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


# Solution 3 - Some parts of the addresses can make more sense to be resources than Literals.
# He was convicted for bank and tax fraud.
# 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.PaulManafort, ex.convictedOf, ex.BankFraud))
g.add((ex.PaulManafort, ex.convictedOf, ex.TaxFraud))


g.add((ex.Cade_tracey, ex.street, Literal("1516_Henry_Street")))
# He pleaded guilty to conspiracy.
g.add((ex.Cade_tracey, ex.city, ex.Berkeley))
g.add((ex.PaulManafort, ex.pleadGuiltyTo, ex.Conspiracy))
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))


# He was sentenced to prison.
g.add((ex.PaulManafort, ex.sentencedTo, ex.Prison))


# Solution 4
# He negotiated a plea agreement.
# 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.PaulManafort, ex.negotiated, ex.PleaAgreement))
# One advantage of this is that we can easily remove the entire address, instead of removing each individual part of the address.  
# Solution 4 or 5 is how I would recommend to make addresses. Here, ex.CadeAddress could also be called something like ex.address1 or so on, if you want to give each address a unique ID.


# Address URI - CadeAdress
# 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))


g.add((ex.Cade_Tracey, ex.address, ex.CadeAddress))
# He pleaded guilty to conspiracy and lying to FBI.
g.add((ex.CadeAddress, RDF.type, ex.Address))
g.add((ex.RickGates, ex.pleadGuiltyTo, ex.Conspiracy))
g.add((ex.CadeAddress, ex.street, Literal("1516 Henry Street")))
g.add((ex.RickGates, ex.pleadGuiltyTo, ex.LyingToFBI))
g.add((ex.CadeAddress, ex.city, ex.Berkeley))
g.add((ex.CadeAddress, ex.state, ex.California))
g.add((ex.CadeAddress, ex.postalCode, Literal("94709")))
g.add((ex.CadeAddress, ex.country, ex.USA))


# OR
# 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


# Blank node for Address. 
# Loop through the triples in the model to print out all triples that have pleading guilty as predicate
address = BNode()
for subject, object in g[ : ex.pleadGuiltyTo :]:
g.add((ex.Cade_Tracey, ex.address, address))
    print(subject, ex.pleadGuiltyTo, object)
g.add((address, RDF.type, ex.Address))
g.add((address, ex.street, Literal("1516 Henry Street", datatype=XSD.string)))
g.add((address, ex.city, ex.Berkeley))
g.add((address, ex.state, ex.California))
g.add((address, ex.postalCode, Literal("94709", datatype=XSD.string)))
g.add((address, ex.country, ex.USA))


# --- IF you have more time tasks ---


# Solution 5 using existing vocabularies for address
# Michael Cohen, Michael Flynn and the lying is part of lab 2 and therefore the answer is not provided this week


# (in this case https://schema.org/PostalAddress from schema.org).  
#Write a method (function) that submits your model for rendering and saves the returned image to file.
# Also using existing ontology for places like California. (like http://dbpedia.org/resource/California from dbpedia.org)
import requests
import shutil


schema = "https://schema.org/"
def graphToImage(graphInput):
dbp = "https://dpbedia.org/resource/"
    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)


g.add((ex.Cade_Tracey, schema.address, ex.CadeAddress))
graph = g.serialize(format="ttl")
g.add((ex.CadeAddress, RDF.type, schema.PostalAddress))
graphToImage(graph)
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))


</syntaxhighlight>
</syntaxhighlight>
===Lab 2 - Collection Example ===
<syntaxhighlight>
from rdflib import Graph, Namespace
from rdflib.collection import Collection
# Sometimes we want to add many objects or subjects for the same predicate at once.
# In these cases we can use Collection() to save some time.
# In this case I want to add all countries that Emma has visited at once.
b = BNode()
g.add((ex.Emma, ex.visit, b))
Collection(g, b,
    [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])
# OR
g.add((ex.Emma, ex.visit, ex.EmmaVisits))
Collection(g, ex.EmmaVisits,
    [ex.Portugal, ex.Italy, ex.France, ex.Germany, ex.Denmark, ex.Sweden])
</syntaxhighlight>
===Lab 3 - Setting a Blazegraph connection and two SELECT examples ===
<syntaxhighlight>
from pymantic import sparql
server = sparql.SPARQLServer("http://127.0.0.1:9999/blazegraph/sparql")
# SELECT and print all triples in the Blazegraph database
query = """SELECT * where { ?s ?p ?o }"""
result = server.query(query)
for uri in result['results']['bindings']:
    print(uri['s']['value'] + " " + uri['p']['value'] + " " + uri['o']['value'])
# SELECT the city and country where Emma lives.
query = """PREFIX ex: <http://example.org/>
SELECT DISTINCT ?city ?country
WHERE {ex:Emma ex:address ?address. ?address ex:city ?city. ?address ex:country ?country}
"""
result = server.query(query)
for uri in result['results']['bindings']:
    print(uri['city']['value'] + " " + uri['country']['value'])
</syntaxhighlight>
<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>

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)