Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 4 #141

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Assignment4/Raaquel09-b190231/task06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-
"""Task06.ipynb

Automatically generated by Colab.

Original file is located at
https://colab.research.google.com/drive/18oCBCzPclqA8ETm62lUF8wbcfvqWEmMV

**Task 06: Modifying RDF(s)**
"""

!pip install rdflib
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials"

"""Read the RDF file as shown in class"""

from rdflib import Graph, Namespace, Literal
from rdflib.namespace import RDF, RDFS
g = Graph()
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False)
g.parse(github_storage+"/rdf/example5.rdf", format="xml")

"""Create a new class named Researcher"""

ns = Namespace("http://somewhere#")
g.add((ns.Researcher, RDF.type, RDFS.Class))
for s, p, o in g:
print(s,p,o)

"""**TASK 6.1: Create a new class named "University"**

"""

# TO DO
ns = Namespace("http://somewhere#")
g.add((ns.University, RDF.type, RDFS.Class))
# Visualize the results
for s, p, o in g:
print(s,p,o)

"""**TASK 6.2: Add "Researcher" as a subclass of "Person"**"""

# TO DO
ns = Namespace("http://somewhere#")
g.add((ns.Researcher, RDFS.subClassOf, ns.Person))
# Visualize the results
for s, p, o in g:
print(s,p,o)

"""**TASK 6.3: Create a new individual of Researcher named "Jane Smithers"**"""

# TO DO
ns = Namespace("http://somewhere#")
g.add((ns.JaneSmithers, RDF.type, ns.Researcher))
# Visualize the results
for s, p, o in g:
print(s,p,o)

"""**TASK 6.4: Add to the individual JaneSmithers the email address, fullName, given and family names. Use the https://schema.org vocabulary**"""

# TO DO
ns = Namespace("http://somewhere#")
SCHEMA = Namespace("https://schema.org/")
g.add((ns.JaneSmithers, SCHEMA.Email, Literal("[email protected]")))
g.add((ns.JaneSmithers, SCHEMA.FN, Literal("Jane Smithers")))
g.add((ns.JaneSmithers, SCHEMA.Given, Literal("Jane")))
g.add((ns.JaneSmithers, SCHEMA.Family, Literal("Smithers")))
# Visualize the results
for s, p, o in g:
print(s, p, o)

"""**TASK 6.5: Add UPM as the university where John Smith works. Use the "https://example.org/ namespace**"""

# TO DO
ns = Namespace("http://somewhere#")
SCHEMA = Namespace("https://schema.org/")
EX = Namespace("https://example.org/")
g.add((EX.upm, SCHEMA.name, Literal("UPM")))
g.add((ns.JohnSmith, RDF.type, ns.Researcher))
g.add((ns.JohnSmith, SCHEMA.worksFor, EX.upm))
# Visualize the results
for s, p, o in g:
print(s, p, o)

"""**Task 6.6: Add that Jown knows Jane using the FOAF vocabulary. Make sure the relationship exists.**"""

# TO DO
ns = Namespace("http://somewhere#")
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
g.add((ns.JohnSmith, FOAF.knows, ns.JaneSmithers))
# Visualize the results
for s, p, o in g:
print(s, p, o)
171 changes: 171 additions & 0 deletions Assignment4/Raaquel09-b190231/task07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# -*- coding: utf-8 -*-
"""Task07.ipynb

Automatically generated by Colab.

Original file is located at
https://colab.research.google.com/drive/1hzCxRzLLr7tRG1Ql9Qf7xnYVL3-ewL0I

**Task 07: Querying RDF(s)**
"""

!pip install rdflib
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials"

"""First let's read the RDF file"""

from rdflib import Graph, Namespace, Literal
from rdflib.namespace import RDF, RDFS
g = Graph()
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False)
g.parse(github_storage+"/rdf/example6.rdf", format="xml")

"""**TASK 7.1: List all subclasses of "LivingThing" with RDFLib and SPARQL**"""

from rdflib.plugins.sparql import prepareQuery
from rdflib import FOAF

ns = Namespace("http://somewhere#")
VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")

q1 = prepareQuery('''
PREFIX ns: <http://somewhere#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>

SELECT ?subclass WHERE {
?subclass rdfs:subClassOf <http://somewhere#LivingThing> .
}
''',
initNs = {"ns": ns, "foaf" : FOAF, "vcard" : VCARD}
)

# Visualize the results

for r in g.query(q1):
print(r)

"""**TASK 7.2: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)**

"""

q2 = prepareQuery('''
PREFIX ns: <http://somewhere#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>


SELECT ?individuals WHERE {
{
?individuals a ns:Person
} UNION {
?subclass rdfs:subClassOf ns:Person .
?individuals a ?subclass .
}
}
''',
initNs = {"ns": ns, "foaf" : FOAF, "vcard" : VCARD}
)

# Visualize the results

for r in g.query(q2):
print(r)

"""**TASK 7.3: List all individuals of just "Person" or "Animal". You do not need to list the individuals of the subclasses of person (in SPARQL only)**

"""

q3 = prepareQuery('''
PREFIX ns: <http://somewhere#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>

SELECT ?individuals WHERE {
{ ?individuals a ns:Person }
UNION
{ ?individuals a ns:Animal }
}
''',
initNs = {"ns": ns, "foaf" : FOAF, "vcard" : VCARD}
)

# Visualize the results

for r in g.query(q3):
print(r)

"""**TASK 7.4: List the name of the persons who know Rocky (in SPARQL only)**"""

q4 = prepareQuery('''
PREFIX ns: <http://somewhere#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>

SELECT ?name WHERE {
?person a ns:Person .
?person foaf:knows ns:RockySmith .
?person vcard:FN ?name .
}
''',
initNs = {"ns": ns, "foaf" : FOAF, "vcard" : VCARD}
)

# Visualize the results

for r in g.query(q4):
print(r)

"""**Task 7.5: List the name of those animals who know at least another animal in the graph (in SPARQL only)**"""

q5 = prepareQuery('''
PREFIX ns: <http://somewhere#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>

SELECT ?animalName WHERE {
?animal a ns:Animal .
?animal foaf:knows ?otherAnimal .
?otherAnimal a ns:Animal .
?animal vcard:Given ?animalName .
}
''',
initNs = {"ns": ns, "foaf" : FOAF, "vcard" : VCARD}
)

# Visualize the results

for r in g.query(q5):
print(r)

"""**Task 7.6: List the age of all living things in descending order (in SPARQL only)**"""

q6 = prepareQuery('''
PREFIX ns: <http://somewhere#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>

SELECT ?livingThings ?age WHERE {
{
?livingThing a ?subclass .
?subclass rdfs:subClassOf* ns:LivingThing .
}
?livingThing foaf:age ?age .
FILTER(?age > 0)
}
ORDER BY DESC(?age)
''',
initNs = {"ns": ns, "foaf" : FOAF, "vcard" : VCARD}
)

# Visualize the results

for r in g.query(q6):
print(r)