-
Notifications
You must be signed in to change notification settings - Fork 52
Karites2006-assignment 4 #18
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
Merged
dgarijo
merged 14 commits into
FacultadInformatica-LinkedData:master
from
Karites2006:master
Oct 21, 2025
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1774c77
Create Estaciones-de-carga-rapida
Karites2006 8e4cdab
Delete Assignment1/Estaciones-de-carga-rapida
Karites2006 c2f2229
DatasetDescriptions.csv
Karites2006 774f7fe
Merge branch 'FacultadInformatica-LinkedData:master' into master
Karites2006 fbf45d0
Merge branch 'FacultadInformatica-LinkedData:master' into master
Karites2006 161844a
Create Karites2006-24C002
Karites2006 0a0d652
Update Karites2006-24C002
Karites2006 27a6e08
Rename Karites2006-24C002 to borrar
Karites2006 a9743ab
Karites2006 assignment 4
Karites2006 308205a
Merge pull request #4 from Karites2006/Karites2006-assignment-4
Karites2006 eb073d7
Update borrar
Karites2006 be78631
Update task07.py
Karites2006 d66b838
Delete Assignment4/borrar
Karites2006 9f73460
Merge branch 'master' into master
dgarijo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| Your name; Your GitHub user; Dataset name; Dataset URL; Dataset brief description | ||
| Carlos Herranz Bascuñan; 24C002; Karites2006; Estaciones de recarga rápida de acceso público para vehículos eléctricos; https://geoportal.madrid.es/fsdescargas/IDEAM_WBGEOPORTAL/MOVILIDAD/VEHICULOS_ELECTRICOS/CSV/PUNTOS_PUBLICOS_RECARGA_VEHICULOS_ELECTRICOS.csv; Dataset con los datos de los puntos de recarga rápida de acceso público para vehículos eléctricos con ubicacion en la via publica; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Task06.ipynb | ||
|
|
||
| Automatically generated by Colab. | ||
|
|
||
| Original file is located at | ||
| https://colab.research.google.com/drive/1fIpfkOvGe3lONh9AhERH2KFNIgP51gI4 | ||
|
|
||
| **Task 06: Modifying RDF(s)** | ||
| """ | ||
|
|
||
| #!pip install rdflib | ||
| import urllib.request | ||
| url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' | ||
| urllib.request.urlretrieve(url, 'validation.py') | ||
| github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" | ||
|
|
||
| """Import RDFLib main methods""" | ||
|
|
||
| from rdflib import Graph, Namespace, Literal, XSD | ||
| from rdflib.namespace import RDF, RDFS | ||
| from validation import Report | ||
| g = Graph() | ||
| g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) | ||
| r = Report() | ||
|
|
||
| """Create a new class named Researcher""" | ||
|
|
||
| ns = Namespace("http://mydomain.org#") | ||
| g.add((ns.Researcher, RDF.type, RDFS.Class)) | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| """**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.**""" | ||
|
|
||
| persona = Namespace("http://oeg.fi.upm.es/def/people#") | ||
| ontologia = Namespace("http://oeg.fi.upm.es/def/ontology#") | ||
|
|
||
| """**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** | ||
|
|
||
| """ | ||
|
|
||
| g.add((persona.Person, RDFS.label, Literal("Person", datatype=XSD.string))) | ||
| g.add((persona.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) | ||
| g.add((persona.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) | ||
| g.add((persona.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) | ||
| g.add((persona.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) | ||
|
|
||
| g.add((persona.Professor, RDFS.subClassOf, persona.Person)) | ||
| g.add((persona.FullProfessor, RDFS.subClassOf, persona.Professor)) | ||
| g.add((persona.AssociateProfessor, RDFS.subClassOf, persona.Professor)) | ||
| g.add((persona.InterimAssociateProfessor, RDFS.subClassOf, persona.AssociateProfessor)) | ||
|
|
||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| # Validation. Do not remove | ||
| r.validate_task_06_01(g) | ||
|
|
||
| """**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)**""" | ||
|
|
||
| g.add((persona.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) | ||
| g.add((persona.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) | ||
| g.add((persona.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) | ||
|
|
||
| g.add((persona.hasName, RDFS.domain, persona.Person)) | ||
| g.add((persona.hasName, RDFS.range, RDFS.Literal)) | ||
| g.add((persona.hasColleague, RDFS.domain, persona.Person)) | ||
| g.add((persona.hasColleague, RDFS.range, persona.Person)) | ||
| g.add((persona.hasHomePage, RDFS.domain, persona.FullProfessor)) | ||
| g.add((persona.hasHomePage, RDFS.range, RDFS.Literal)) | ||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| # Validation. Do not remove | ||
| r.validate_task_06_02(g) | ||
|
|
||
| """**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**""" | ||
|
|
||
| data = Namespace("http://oeg.fi.upm.es/resource/person/") | ||
|
|
||
| g.add((data.oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) | ||
| g.add((data.asun, RDFS.label, Literal("Asun", datatype=XSD.string))) | ||
| g.add((data.raul, RDFS.label, Literal("Raul", datatype=XSD.string))) | ||
|
|
||
| g.add((data.oscar, RDF.type, persona.AssociateProfessor)) | ||
| g.add((data.asun, RDF.type, persona.FullProfessor)) | ||
| g.add((data.raul, RDF.type, persona.InterimAssociateProfessor)) | ||
|
|
||
| g.add((data.oscar, persona.hasColleague, data.asun)) | ||
| g.add((data.asun, persona.hasColleague, data.raul)) | ||
|
|
||
| g.add((data.oscar, persona.hasName, Literal("Óscar Corcho García", datatype=XSD.string))) | ||
| g.add((data.asun, persona.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string))) | ||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| r.validate_task_06_03(g) | ||
|
|
||
| """**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** | ||
|
|
||
| """ | ||
|
|
||
| FOAF = Namespace("http://xmlns.com/foaf/0.1/") | ||
| VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") | ||
|
|
||
|
|
||
| g.add((data.oscar, VCARD.Given, Literal("Oscar", datatype=XSD.string))) | ||
| g.add((data.oscar, VCARD.Family, Literal("Martinez", datatype=XSD.string))) | ||
| g.add((data.oscar, FOAF.email, Literal("oscar@somewhere.com", datatype=XSD.string))) | ||
|
|
||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| # Validation. Do not remove | ||
| r.validate_task_06_04(g) | ||
| r.save_report("_Task_06") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Task07.ipynb | ||
|
|
||
| Automatically generated by Colab. | ||
|
|
||
| Original file is located at | ||
| https://colab.research.google.com/drive/1OHHm-Ils1yPevE4T2vhpUaKLPF3MTCDy | ||
|
|
||
| **Task 07: Querying RDF(s)** | ||
| """ | ||
|
|
||
| #!pip install rdflib | ||
| import urllib.request | ||
| url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' | ||
| urllib.request.urlretrieve(url, 'validation.py') | ||
| github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" | ||
|
|
||
| from validation import Report | ||
|
|
||
| """First let's read the RDF file""" | ||
|
|
||
| from rdflib import Graph, Namespace, Literal | ||
| from rdflib.namespace import RDF, RDFS | ||
| # Do not change the name of the variables | ||
| g = Graph() | ||
| g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) | ||
| g.parse(github_storage+"/rdf/data06.ttl", format="TTL") | ||
| report = Report() | ||
|
|
||
| """**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** | ||
| **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** | ||
| """ | ||
|
|
||
| result = [] #list of tuples | ||
| resultado_aux=[] | ||
| clase="http://www.w3.org/2000/01/rdf-schema#Class" | ||
| subclase="http://www.w3.org/2000/01/rdf-schema#subClassOf" | ||
| for s, p, o in g: | ||
| if clase in o: | ||
| superclases=g.value(subject=s, predicate=RDFS.subClassOf) | ||
| if superclases is not None: | ||
| result.append((s,g.objects(subject=s, predicate=RDFS.subClassOf))) | ||
| else: | ||
| result.append((s,None)) | ||
| # Visualize the results | ||
| for r in result: | ||
| print(r) | ||
|
|
||
| ## Validation: Do not remove | ||
| report.validate_07_1a(result) | ||
|
|
||
| """**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" | ||
|
|
||
| query = """ | ||
| PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
|
|
||
| SELECT ?c ?sc | ||
| WHERE { | ||
| ?c a rdfs:Class . | ||
| OPTIONAL { ?c rdfs:subClassOf ?sc } | ||
| } | ||
| """ | ||
|
|
||
| for r in g.query(query): | ||
| print(r.c, r.sc) | ||
|
|
||
| ## Validation: Do not remove | ||
| report.validate_07_1b(query,g) | ||
|
|
||
| """**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"** | ||
|
|
||
| """ | ||
|
|
||
| ns = Namespace("http://oeg.fi.upm.es/def/people#") | ||
| tipo="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" | ||
| individuals = [] # variable to return | ||
|
|
||
|
|
||
| for s, p, o in g: | ||
| if ns in s and tipo in p and "http://www.w3.org/2000/01/rdf-schema#Class" not in o and "http://oeg.fi.upm.es/def/people#Animal" not in o: | ||
| individuals.append(s) | ||
| # visualize results | ||
| for i in individuals: | ||
| print(i) | ||
|
|
||
| # validation. Do not remove | ||
| report.validate_07_02a(individuals) | ||
|
|
||
| """**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**""" | ||
|
|
||
| query = """ | ||
| PREFIX people: <http://oeg.fi.upm.es/def/people#> | ||
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | ||
| PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
|
|
||
| SELECT DISTINCT ?ind | ||
| WHERE { | ||
| ?ind rdf:type ?tipo . | ||
|
|
||
| FILTER(?tipo != rdfs:Class && ?tipo != people:Animal) | ||
| } | ||
| """ | ||
|
|
||
| for r in g.query(query): | ||
| print(r.ind) | ||
| # Visualize the results | ||
|
|
||
| ## Validation: Do not remove | ||
| report.validate_07_02b(g, query) | ||
|
|
||
| """**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**""" | ||
|
|
||
| query = """ | ||
| PREFIX people: <http://oeg.fi.upm.es/def/people#> | ||
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | ||
| PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
|
|
||
| SELECT DISTINCT ?name ?type | ||
| WHERE { | ||
| ?name rdf:type ?type . | ||
| ?name people:knows people:Rocky . | ||
| } | ||
| """ | ||
| # TO DO | ||
| # Visualize the results | ||
| for r in g.query(query): | ||
| print(r.name, r.type) | ||
|
|
||
| ## Validation: Do not remove | ||
| report.validate_07_03(g, query) | ||
|
|
||
| """**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**""" | ||
|
|
||
| query = """ | ||
| PREFIX people: <http://oeg.fi.upm.es/def/people#> | ||
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | ||
|
|
||
| SELECT ?name | ||
| WHERE { | ||
| { | ||
| ?name people:hasColleague ?colleague . | ||
| ?colleague people:ownsPet ?dog . | ||
| } | ||
| UNION | ||
| { | ||
| ?name people:hasColleague ?c1 . | ||
| ?c1 people:hasColleague ?c2 . | ||
| ?c2 people:ownsPet ?dog2 . | ||
| } | ||
| } | ||
|
|
||
| """ | ||
| # Visualize the results | ||
| for r in g.query(query): | ||
| print(r.name) | ||
|
|
||
| ## Validation: Do not remove | ||
| report.validate_07_04(g,query) | ||
| report.save_report("_Task_07") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Task08.ipynb | ||
|
|
||
| Automatically generated by Colab. | ||
|
|
||
| Original file is located at | ||
| https://colab.research.google.com/drive/169M_xv5ftBiErN_KfIMMZIae0xG0oWZj | ||
|
|
||
| **Task 08: Completing missing data** | ||
| """ | ||
|
|
||
| #!pip install rdflib | ||
| github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2021-2022/master/Assignment4/course_materials" | ||
|
|
||
| from rdflib import Graph, Namespace, Literal, URIRef | ||
| g1 = Graph() | ||
| g2 = Graph() | ||
| g1.parse(github_storage+"/rdf/data01.rdf", format="xml") | ||
| g2.parse(github_storage+"/rdf/data02.rdf", format="xml") | ||
|
|
||
| """Tarea: lista todos los elementos de la clase Person en el primer grafo (data01.rdf) y completa los campos (given name, family name y email) que puedan faltar con los datos del segundo grafo (data02.rdf). Puedes usar consultas SPARQL o iterar el grafo, o ambas cosas.""" | ||
|
|
||
| #Grado 1 sin cambios | ||
| for s,p,o in g1: | ||
| print(s,p,o) | ||
|
|
||
| #Solucion del problema | ||
| clase_persona="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" | ||
| clase="http://data.org" | ||
| clase_persona_g1=[] | ||
| for s,p,o in g1: | ||
| if clase in o: | ||
| clase_persona_g1.append(s) | ||
|
|
||
| FOAF = Namespace("http://xmlns.com/foaf/0.1/") | ||
| VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") | ||
|
|
||
| for s,p,o in g2: | ||
| if s in clase_persona_g1: | ||
| g1.add((s,p,o)) | ||
|
|
||
| #Solucion | ||
| print("\tGrafo 1:") | ||
| for s,p,o in g1: | ||
| print(s,p,o) | ||
|
|
||
| print("\tGrafo 2:") | ||
| for s,p,o in g2: | ||
| print(s,p,o) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Task09.ipynb | ||
|
|
||
| Automatically generated by Colab. | ||
|
|
||
| Original file is located at | ||
| https://colab.research.google.com/drive/1VcS4mqWxP5AxKgNfuVSnZidFW1FJTsg7 | ||
|
|
||
| **Task 09: Data linking** | ||
| """ | ||
|
|
||
| #!pip install rdflib | ||
| github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2021-2022/master/Assignment4/course_materials/" | ||
|
|
||
| from rdflib import Graph, Namespace, Literal, URIRef | ||
| g1 = Graph() | ||
| g2 = Graph() | ||
| g3 = Graph() | ||
| g1.parse(github_storage+"rdf/data03.rdf", format="xml") | ||
| g2.parse(github_storage+"rdf/data04.rdf", format="xml") | ||
|
|
||
| """Busca individuos en los dos grafos y enlázalos mediante la propiedad OWL:sameAs, inserta estas coincidencias en g3. Consideramos dos individuos iguales si tienen el mismo apodo y nombre de familia. Ten en cuenta que las URI no tienen por qué ser iguales para un mismo individuo en los dos grafos.""" | ||
|
|
||
| from rdflib.namespace import OWL | ||
| clase1="http://data.three.org" | ||
| clase2="http://data.four.org" | ||
|
|
||
| individuos1=[] | ||
| for s,p,o in g1: | ||
| if clase1 in o: | ||
| individuos1.append(s) | ||
|
|
||
| individuos2=[] | ||
| for s,p,o in g2: | ||
| if clase2 in o: | ||
| individuos2.append(s) | ||
|
|
||
| for individuo in individuos1: | ||
| for individuo2 in individuos2: | ||
| familiaURI=URIRef("http://www.w3.org/2001/vcard-rdf/3.0#Family") | ||
| givenURI=URIRef("http://www.w3.org/2001/vcard-rdf/3.0#Given") | ||
| igual_name_familia=g1.value(subject=individuo,predicate=familiaURI) == g2.value(subject=individuo2,predicate=familiaURI) | ||
| igual_name_given=g1.value(subject=individuo,predicate=givenURI) == g2.value(subject=individuo2,predicate=givenURI) | ||
| if igual_name_familia and igual_name_given: | ||
| g3.add((individuo,OWL.sameAs,individuo2)) | ||
|
|
||
| #Solucion del problema | ||
| for s,p,o in g3: | ||
| print(s,p,o) | ||
|
|
||
| print("\tGrado 1:") | ||
| for s,p,o in g1: | ||
| print(s,p,o) | ||
| print("\tGrado 2:") | ||
| for s,p,o in g2: | ||
| print(s,p,o) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.