Skip to content

Commit a1ec719

Browse files
authored
Entrega Assignment4 Laura Silva (#85)
* Se creó con Colab * Se creó con Colab * Se creó con Colab * Se creó con Colab * Se creó con Colab * Create Assignment4_LauraSilva_24C024_task06 * Create Assignment4_LauraSilva_24C024_task07 * Create Assignment4_LauraSilva_24C024_validation * Create task06 * Delete Assignment4/Assignment4_LauraSilva_24C024_task06 * Delete Assignment4/Assignment4_LauraSilva_24C024_task07 * Delete Assignment4/Assignment4_LauraSilva_24C024_validation * Create task07 * Create validation * Update task07 * Se creó con Colab * Rename task06 to task06.py * Update task06.py * Update and rename task07 to task07.py * Update task06.py * Rename validation to validation.py * adding materials for assignment 4 * Update task06.py * adding materials for assignment 4 * Create task06.py * Delete Assignment4carpeta directory
1 parent cf5555b commit a1ec719

3 files changed

Lines changed: 579 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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/Laauraaxsc/Curso2025-2026-DataScience/blob/master/Assignment4/Assignment4_LauraSilva_24C024/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+
ontology = Namespace("http://oeg.fi.upm.es/def/ontology#")
39+
40+
g.namespace_manager.bind("person", person, override=True)
41+
g.namespace_manager.bind("ontology", ontology, override=True)
42+
g.namespace_manager.bind("rdf", RDF, override=True)
43+
g.namespace_manager.bind("rdfs", RDFS, override=True)
44+
g.namespace_manager.bind("xsd", XSD, override=True)
45+
46+
"""**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**
47+
48+
"""
49+
50+
# TO DO
51+
clases_y_padres = [("Person", None),("Professor", "Person"),("FullProfessor", "Professor"),("AssociateProfessor", "Professor"),("InterimAssociateProfessor", "AssociateProfessor")]
52+
53+
for nombre, padre in clases_y_padres:
54+
clase = person[nombre]
55+
g.add((clase, RDF.type, RDFS.Class))
56+
g.add((clase, RDFS.label, Literal(nombre, datatype=XSD.string)))
57+
if padre:
58+
g.add((clase, RDFS.subClassOf, person[padre]))
59+
60+
# Visualize the results
61+
for s, p, o in g:
62+
print(s,p,o)
63+
64+
# Validation. Do not remove
65+
r.validate_task_06_01(g)
66+
67+
"""**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)**"""
68+
69+
# TO DO
70+
def agregar_propiedad(nombre, dominio=None, rango=None):
71+
prop = person[nombre]
72+
g.add((prop, RDF.type, RDF.Property))
73+
g.add((prop, RDFS.label, Literal(nombre, datatype=XSD.string)))
74+
if dominio:
75+
g.add((prop, RDFS.domain, dominio))
76+
if rango:
77+
g.add((prop, RDFS.range, rango))
78+
return prop
79+
80+
agregar_propiedad("hasColleague", dominio=person.Person, rango=person.Person)
81+
agregar_propiedad("hasName", dominio=person.Person, rango=RDFS.Literal)
82+
agregar_propiedad("hasHomePage", dominio=person.FullProfessor, rango=RDFS.Literal)
83+
84+
# Visualize the results
85+
for s, p, o in g:
86+
print(s,p,o)
87+
88+
# Validation. Do not remove
89+
r.validate_task_06_02(g)
90+
91+
"""**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""
92+
93+
# TO DO
94+
datos = Namespace("http://oeg.fi.upm.es/resource/person/")
95+
g.namespace_manager.bind("data", datos, True)
96+
97+
def agregar_individuo(nombre, tipo_rdf, etiqueta, propiedades=None):
98+
sujeto = datos[nombre]
99+
g.remove((sujeto, None, None))
100+
g.add((sujeto, RDF.type, tipo_rdf))
101+
g.add((sujeto, RDFS.label, Literal(etiqueta, datatype=XSD.string)))
102+
if propiedades:
103+
for predicado, objeto in propiedades:
104+
g.add((sujeto, predicado, objeto))
105+
return sujeto
106+
107+
oscar = agregar_individuo("Oscar", person.FullProfessor, "Oscar")
108+
asun = agregar_individuo("Asun", person.AssociateProfessor, "Asun")
109+
raul = agregar_individuo("Raul", person.InterimAssociateProfessor, "Raul")
110+
111+
g.add((oscar, person.hasColleague, asun))
112+
g.add((oscar, person.hasName, Literal("Oscar Corcho García", datatype=XSD.string)))
113+
g.add((asun, person.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string)))
114+
g.add((asun, person.hasColleague, raul))
115+
116+
117+
# Visualize the results
118+
for s, p, o in g:
119+
print(s,p,o)
120+
121+
r.validate_task_06_03(g)
122+
123+
"""**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**
124+
125+
"""
126+
127+
# TO DO
128+
VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
129+
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
130+
131+
data = Namespace("http://oeg.fi.upm.es/resource/person/")
132+
oscar = data.Oscar
133+
134+
g.add((oscar, VCARD.Given, Literal("Oscar", datatype=XSD.string)))
135+
g.add((oscar, VCARD.Family, Literal("Corcho", datatype=XSD.string)))
136+
g.add((oscar, FOAF.email, Literal("oscar@oeg-upm.net", datatype=XSD.string)))
137+
# Visualize the results
138+
for s, p, o in g:
139+
print(s,p,o)
140+
141+
# Validation. Do not remove
142+
r.validate_task_06_04(g)
143+
r.save_report("_Task_06")
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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/Laauraaxsc/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+
def listar_clases_y_superclase(grafo):
37+
clases = set(grafo.subjects(RDF.type, RDFS.Class))
38+
clases.update(grafo.subjects(RDFS.subClassOf, None))
39+
clases.update(grafo.objects(None, RDFS.subClassOf))
40+
41+
resultado = []
42+
for c in clases:
43+
sc = grafo.value(subject=c, predicate=RDFS.subClassOf, object=None)
44+
resultado.append((c, sc))
45+
return resultado
46+
47+
# Visualize the results
48+
result = listar_clases_y_superclase(g) #list of tuples
49+
for r in result:
50+
print(r)
51+
52+
## Validation: Do not remove
53+
report.validate_07_1a(result)
54+
55+
"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**"""
56+
57+
query = """
58+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
59+
60+
SELECT DISTINCT ?c ?sc
61+
WHERE {
62+
{
63+
{ ?c a rdfs:Class }
64+
UNION
65+
{ ?c rdfs:subClassOf ?_any }
66+
UNION
67+
{ ?_x rdfs:subClassOf ?c }
68+
}
69+
OPTIONAL { ?c rdfs:subClassOf ?sc }
70+
}
71+
"""
72+
73+
for r in g.query(query):
74+
print(r.c, r.sc)
75+
76+
# Validation: Do not remove
77+
report.validate_07_1b(query,g)
78+
79+
"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"**
80+
81+
"""
82+
83+
ns = Namespace("http://oeg.fi.upm.es/def/people#")
84+
85+
clase_person = ns.Person
86+
clases = [clase_person]
87+
88+
for c in clases:
89+
for sc in g.subjects(RDFS.subClassOf, c):
90+
if sc not in clases:
91+
clases.append(sc)
92+
93+
individuals = []
94+
for c in clases:
95+
for ind in g.subjects(RDF.type, c):
96+
if ind not in individuals:
97+
individuals.append(ind)
98+
99+
for i in individuals:
100+
print(i)
101+
102+
# validation. Do not remove
103+
report.validate_07_02a(individuals)
104+
105+
"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**"""
106+
107+
query = """
108+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
109+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
110+
PREFIX ns: <http://oeg.fi.upm.es/def/people#>
111+
112+
SELECT DISTINCT ?ind
113+
WHERE {
114+
?c rdfs:subClassOf* ns:Person .
115+
?ind rdf:type ?c .
116+
}
117+
"""
118+
119+
for r in g.query(query):
120+
print(r.ind)
121+
# Visualize the results
122+
123+
## Validation: Do not remove
124+
report.validate_07_02b(g, query)
125+
126+
"""**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**"""
127+
128+
query = """
129+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
130+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
131+
PREFIX ns: <http://oeg.fi.upm.es/def/people#>
132+
133+
SELECT DISTINCT ?name ?type
134+
WHERE {
135+
?ind ?p ns:Rocky .
136+
?ind rdfs:label ?name .
137+
?ind rdf:type ?type .
138+
}
139+
"""
140+
# Visualize the results
141+
for r in g.query(query):
142+
print(r.name, r.type)
143+
144+
## Validation: Do not remove
145+
report.validate_07_03(g, query)
146+
147+
"""**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**"""
148+
149+
query = """
150+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
151+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
152+
PREFIX ns: <http://oeg.fi.upm.es/def/people#>
153+
154+
SELECT DISTINCT ?name
155+
WHERE {
156+
?ind rdfs:label ?name .
157+
158+
{
159+
?ind ns:hasColleague ?c .
160+
}
161+
UNION
162+
{
163+
?ind ns:hasColleague/ns:hasColleague ?c .
164+
}
165+
166+
?c ?any ns:Rocky .
167+
}
168+
"""
169+
170+
for r in g.query(query):
171+
print(r.name)
172+
173+
# TO DO
174+
# Visualize the results
175+
176+
## Validation: Do not remove
177+
report.validate_07_04(g,query)
178+
report.save_report("_Task_07")

0 commit comments

Comments
 (0)