Skip to content

Commit e991d89

Browse files
authored
Merge pull request #24 from CarlosCumbrado/master
Assignment 4
2 parents 4070d76 + a0b78f1 commit e991d89

3 files changed

Lines changed: 553 additions & 0 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# -*- coding: utf-8 -*-
2+
"""Task06.ipynb
3+
4+
Automatically generated by Colab.
5+
6+
Original file is located at
7+
https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task06.ipynb
8+
9+
**Task 06: Modifying RDF(s)**
10+
"""
11+
12+
#!pip install rdflib
13+
import urllib.request
14+
url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'
15+
urllib.request.urlretrieve(url, 'validation.py')
16+
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials"
17+
18+
"""Import RDFLib main methods"""
19+
20+
from rdflib import Graph, Namespace, Literal, XSD
21+
from rdflib.namespace import RDF, RDFS
22+
from validation import Report
23+
g = Graph()
24+
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
25+
r = Report()
26+
27+
"""Create a new class named Researcher"""
28+
29+
ns = Namespace("http://mydomain.org#")
30+
g.add((ns.Researcher, RDF.type, RDFS.Class))
31+
for s, p, o in g:
32+
print(s,p,o)
33+
34+
"""**Task 6.0: Create new prefixes for "ontology" and "person" as shown in slide 14 of the Slidedeck 01a.RDF(s)-SPARQL shown in class.**"""
35+
36+
# this task is validated in the next step
37+
person = Namespace("http://oeg.fi.upm.es/def/people#")
38+
ontologia = Namespace("http://oeg.fi.upm.es/def/ontology#")
39+
40+
"""**TASK 6.1: Reproduce the taxonomy of classes shown in slide 34 in class (all the classes under "Vocabulario", Slidedeck: 01a.RDF(s)-SPARQL). Add labels for each of them as they are in the diagram (exactly) with no language tags. Remember adding the correct datatype (xsd:String) when appropriate**
41+
42+
"""
43+
44+
# TO DO
45+
46+
g.add((person.Person, RDF.type, RDFS.Class))
47+
g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string)))
48+
49+
g.add((person.Professor, RDF.type, RDFS.Class))
50+
g.add((person.Professor, RDFS.subClassOf, person.Person))
51+
g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string)))
52+
53+
g.add((person.FullProfessor, RDF.type, RDFS.Class))
54+
g.add((person.FullProfessor, RDFS.subClassOf, person.Professor))
55+
g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string)))
56+
57+
g.add((person.AssociateProfessor, RDF.type, RDFS.Class))
58+
g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor))
59+
g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string)))
60+
61+
g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class))
62+
g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor))
63+
g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string)))
64+
65+
66+
# Visualize the results
67+
for s, p, o in g:
68+
print(s,p,o)
69+
70+
# Validation. Do not remove
71+
r.validate_task_06_01(g)
72+
73+
"""**TASK 6.2: Add the 3 properties shown in slide 36. Add labels for each of them (exactly as they are in the slide, with no language tags), and their corresponding domains and ranges using RDFS. Remember adding the correct datatype (xsd:String) when appropriate. If a property has no range, make it a literal (string)**"""
74+
75+
# TO DO
76+
77+
g.add((person.hasHomePage, RDF.type, RDF.Property))
78+
g.add((person.hasName, RDF.type, RDF.Property))
79+
g.add((person.hasColleague, RDF.type, RDF.Property))
80+
81+
g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
82+
g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
83+
g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
84+
85+
g.add((person.hasHomePage, RDFS.domain, person.FullProfessor))
86+
g.add((person.hasName, RDFS.domain, person.Person))
87+
g.add((person.hasColleague, RDFS.domain, person.Person))
88+
89+
g.add((person.hasHomePage, RDFS.range, RDFS.Literal))
90+
g.add((person.hasName, RDFS.range, RDFS.Literal))
91+
g.add((person.hasColleague, RDFS.range, person.Person))
92+
93+
# Visualize the results
94+
for s, p, o in g:
95+
print(s,p,o)
96+
97+
# Validation. Do not remove
98+
r.validate_task_06_02(g)
99+
100+
""":**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""
101+
102+
# TO DO
103+
104+
person_ind = Namespace("http://oeg.fi.upm.es/resource/person/")
105+
106+
g.add((person_ind.Oscar, RDF.type, person.FullProfessor))
107+
g.add((person_ind.Asun, RDF.type, person.FullProfessor))
108+
g.add((person_ind.Raul, RDF.type, person.InterimAssociateProfessor))
109+
110+
g.add((person_ind.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
111+
g.add((person_ind.Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
112+
g.add((person_ind.Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
113+
114+
g.add((person_ind.Oscar, person.hasColleague, person_ind.Asun))
115+
g.add((person_ind.Asun, person.hasColleague, person_ind.Raul))
116+
117+
g.add((person_ind.Oscar, person.hasName, Literal("Óscar Corcho García", datatype=XSD.string)))
118+
g.add((person_ind.Asun, person.hasHomePage, Literal("http://oeg.fi.upm.es/", datatype=XSD.string)))
119+
120+
# Visualize the results
121+
for s, p, o in g:
122+
print(s,p,o)
123+
124+
r.validate_task_06_03(g)
125+
126+
"""**TASK 6.4: Add to the individual person:Oscar the email address, given and family names. Use the properties already included in example 4 to describe Jane and John (https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials/rdf/example4.rdf). Do not import the namespaces, add them manually**
127+
128+
"""
129+
130+
# TO DO
131+
132+
foaf = Namespace("http://xmlns.com/foaf/0.1/")
133+
vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
134+
135+
g.add((person_ind.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string)))
136+
g.add((person_ind.Oscar, vcard.Given, Literal("Óscar", datatype=XSD.string)))
137+
g.add((person_ind.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string)))
138+
139+
# Visualize the results
140+
for s, p, o in g:
141+
print(s,p,o)
142+
143+
# Validation. Do not remove
144+
r.validate_task_06_04(g)
145+
r.save_report("_Task_06")
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# -*- coding: utf-8 -*-
2+
"""Task07.ipynb
3+
4+
Automatically generated by Colab.
5+
6+
Original file is located at
7+
https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task07.ipynb
8+
9+
**Task 07: Querying RDF(s)**
10+
"""
11+
12+
#!pip install rdflib
13+
import urllib.request
14+
url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'
15+
urllib.request.urlretrieve(url, 'validation.py')
16+
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials"
17+
18+
from validation import Report
19+
20+
"""First let's read the RDF file"""
21+
22+
from rdflib import Graph, Namespace, Literal
23+
from rdflib.namespace import RDF, RDFS
24+
# Do not change the name of the variables
25+
g = Graph()
26+
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
27+
g.parse(github_storage+"/rdf/data06.ttl", format="TTL")
28+
report = Report()
29+
30+
"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**
31+
**Do the exercise in RDFLib returning a list of Tuples: (class, superclass) called "result". If a class does not have a super class, then return None as the superclass**
32+
"""
33+
34+
# TO DO
35+
36+
result = []
37+
for s, p, o in g.triples((None, RDF.type, RDFS.Class)):
38+
superclass = None
39+
for _, _, sup in g.triples((s, RDFS.subClassOf, None)):
40+
superclass = sup
41+
result.append((s, superclass))
42+
43+
# Visualize the results
44+
45+
for r in result:
46+
print(r)
47+
48+
## Validation: Do not remove
49+
report.validate_07_1a(result)
50+
51+
"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**"""
52+
53+
query = """
54+
SELECT ?c ?sc
55+
WHERE {
56+
?c rdf:type rdfs:Class .
57+
OPTIONAL { ?c rdfs:subClassOf ?sc . }
58+
}
59+
"""
60+
61+
for r in g.query(query):
62+
print(r.c, r.sc)
63+
64+
## Validation: Do not remove
65+
report.validate_07_1b(query,g)
66+
67+
"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"**
68+
69+
"""
70+
71+
ns = Namespace("http://oeg.fi.upm.es/def/people#")
72+
73+
# variable a devolver
74+
individuals = []
75+
76+
subclasses = set()
77+
to_visit = [ns.Person]
78+
while to_visit:
79+
cls = to_visit.pop()
80+
if cls in subclasses:
81+
continue
82+
subclasses.add(cls)
83+
for sub, _, _ in g.triples((None, RDFS.subClassOf, cls)):
84+
if sub not in subclasses:
85+
to_visit.append(sub)
86+
87+
for subclass in subclasses:
88+
for ind, _, _ in g.triples((None, RDF.type, subclass)):
89+
individuals.append(ind)
90+
91+
# visualize results
92+
for i in individuals:
93+
print(i)
94+
95+
# validation. Do not remove
96+
report.validate_07_02a(individuals)
97+
98+
"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**"""
99+
100+
query = """
101+
SELECT DISTINCT ?ind
102+
WHERE {
103+
?ind rdf:type ?class .
104+
?class rdfs:subClassOf* <http://oeg.fi.upm.es/def/people#Person> .
105+
}
106+
"""
107+
108+
for r in g.query(query):
109+
print(r.ind)
110+
# Visualize the results
111+
112+
## Validation: Do not remove
113+
report.validate_07_02b(g, query)
114+
115+
"""**TASK 7.3: List the name and type of those who know Rocky (in SPARQL only). Use name and type as variables in the query**"""
116+
117+
query = """
118+
SELECT ?name ?type WHERE{
119+
?name <http://oeg.fi.upm.es/def/people#knows> <http://oeg.fi.upm.es/def/people#Rocky>.
120+
?name a ?type
121+
}
122+
"""
123+
# Visualize the results
124+
for r in g.query(query):
125+
print(r.name, r.type)
126+
127+
## Validation: Do not remove
128+
report.validate_07_03(g, query)
129+
130+
"""**Task 7.4: List the name of those entities who have a colleague with a dog, or that have a collegue who has a colleague who has a dog (in SPARQL). Return the results in a variable called name**"""
131+
132+
query = """
133+
SELECT ?name WHERE {
134+
?p <http://oeg.fi.upm.es/def/people#ownsPet> ?m.
135+
{?name <http://oeg.fi.upm.es/def/people#hasColleague> ?p}
136+
UNION
137+
{?p2 <http://oeg.fi.upm.es/def/people#hasColleague> ?p. ?name <http://oeg.fi.upm.es/def/people#hasColleague> ?p2.}
138+
}
139+
"""
140+
141+
142+
for r in g.query(query):
143+
print(r.name)
144+
145+
# TO DO
146+
# Visualize the results
147+
148+
## Validation: Do not remove
149+
report.validate_07_04(g,query)
150+
report.save_report("_Task_07")

0 commit comments

Comments
 (0)