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

Individual Assignment 2a and 2b #58

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
71 changes: 71 additions & 0 deletions Assignment2/IsamShCh-fromJSONtoJSON-LD.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"@context": {
"schema": "http://schema.org/",
"name": "schema:name",
"universidad": "http://example.org/University",
"person": "http://example.org/person",
"home_town": "schema:homeLocation",
"age": "schema:age",
"alma_mater": "schema:alumniOf",
"parents": {
"@id": "schema:parent",
"@container": "@set"
},
"father": "schema:father",
"mother": "schema:mother",
"subjects_this_year": {
"@id": "schema:Course",
"@container": "@list"
},

"teachers": "schema:instructor",
"brief_description": "schema:description"
},
"@id": "person:Isam",
"@type": "schema:Person",
"name": "Isam, Saha Chaham",
"home_town": "Azlef",
"age": 21,

"alma_mater": {
"@id": "universidad:UPM",
"@type": "schema:CollegeOrUniversity",
"name": "Universida Politecnica de Madrid"
},

"parents": [
{
"@type": "schema:Person",
"father": "Mohamed Saha",
"@id": "person:Mohamed"
},
{
"@type": "schema:Person",
"mother": "Farida",
"@id": "person:Farida"
}
],


"subjects_this_year": [
{
"@type": "schema:Course",
"name": "Semantic Web, Linked Data and Knowledge Graphs",
"teachers": "Oscar Corcho",
"brief_description": "Fundamentos de la Web Semántica, datos enlazados y grafos de conocimiento."
},
{
"@type": "schema:Course",
"name": "Algoritmos de procesado y análisis de imagen. Aplicación a teledetección",
"teachers": "Angel Mario García Pedrero",
"brief_description": "Conocimientos básicos del procesado digital de imágenes aplicado a la teledetección."
},
{
"@type": "schema:Course",
"name": "EPAC Informáticos",
"teachers": "Hanane Benali Taouis",
"brief_description": "Desarrollo de habilidades de comunicación efectiva en entornos académicos y profesionales de la ingeniería informática."
}
]

}
41 changes: 41 additions & 0 deletions Assignment2/IsamShCh-fromRDFtoJSON-LD.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{

"@context": {
"schema": "http://schema.org/",
"contains": "schema:contains",
"hasMeasurement": "schema:hasMeasurement",
"hasTemperature": "schema:hasTemperature",

"atTime": "schema:atTime",
"hasOwner": "schema:hasOwner",
"hasName": "schema:name"

},

"@id": "http://example.org/Class01",

"@type": "schema:Classroom",
"contains": [
{
"@id": "http://example.org/Sensor029",
"@type": "schema:Sensor",
"hasMeasurement": {
"@id": "http://example.org/Measurement8401",
"@type": "schema:Measurement",
"hasTemperature": 29,
"atTime": "2010-06-12T12:00:12"
}
},
{
"@id": "http://example.org/Computer101",
"@type": "schema:Computer",
"hasOwner": {
"@id": "http://example.org/User10A",
"@type": "schema:Person",
"hasName": "Pedro"
}
}
]


}
Binary file added Assignment2/IsamShCh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Assignment2/IsamShch.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@prefix : <http://upm-temperatures.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:ClassRoom03 :contains :Sensor347 ;
:contains :Table322 .

:Sensor347 :hasMeasurement :Measurement8432 .

:Measurement8432 :hasTemperature "29"^^xsd:integer ;
:atTime "2022-09-12T12:01:12"^^xsd:dateTime .

:Table322 :hasOwner :ETSIInformaticos .

:ETSIInformaticos :hasName "ETSI Informáticos" .
1 change: 1 addition & 0 deletions Assignment4/IsamShCh-210198/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Placeholder to generate the folder
105 changes: 105 additions & 0 deletions Assignment4/IsamShCh-210198/task06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

# -*- coding: utf-8 -*-
"""Task06.ipynb

Automatically generated by Colaboratory.

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

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


"""**TASK 6.1: Create new classes for "School" and "University. Add an rdfs:label in Spanish"**

"""

# TO DO
g.add((ns.School, RDF.type, RDFS.Class))
g.add((ns.School, RDFS.label, Literal("Escuela", lang="es")))
g.add((ns.University, RDF.type, RDFS.Class))
g.add((ns.University, RDFS.label, Literal("Universidad", lang="es")))

# Visualize the results
print("TASK 6.1:")
for s, p, o in g:
print(s,p,o)

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

# TO DO
g.add((ns.Researcher, RDFS.subClassOf, ns.Person))

# Visualize the results
print("\nTASK 6.2:")
for s, p, o in g:
print(s,p,o)

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

# TO DO
g.add((ns.JaneSmithers, RDF.type, ns.Researcher))

# Visualize the results
print("\nTASK 6.3:")
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
SCHEMA = Namespace("https://schema.org/")
g.add((ns.JaneSmithers, SCHEMA.email, Literal("jane.smithers@example.org")))
g.add((ns.JaneSmithers, SCHEMA.name, Literal("Jane Smithers")))
g.add((ns.JaneSmithers, SCHEMA.givenName, Literal("Jane")))
g.add((ns.JaneSmithers, SCHEMA.familyName, Literal("Smithers")))

# Visualize the results
print("\nTASK 6.4:")
for s, p, o in g.triples((ns.JaneSmithers, None, None)): # more efficient
print(s,p,o)



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

# TO DO
EX = Namespace("https://example.org/")
g.add((EX.UPM, RDF.type, ns.University))
g.add((ns.JohnSmith, EX.worksAt, EX.UPM))


# Visualize the results
print("\nTASK 6.5:")
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
from rdflib import FOAF
g.add((ns.JohnSmith, FOAF.knows, ns.JaneSmith)) # Assuming "Jown" is a typo for "John"

# Visualize the results
print("\nTASK 6.6:")
for s, p, o in g:
print(s,p,o)
136 changes: 136 additions & 0 deletions Assignment4/IsamShCh-210198/task07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# -*- coding: utf-8 -*-
"""Task07.ipynb

Automatically generated by Colaboratory.

Original file is located at
https://colab.research.google.com/drive/1yPniC8cCIRK0ofpUQK_h0bcNOa4CroCF

**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**"""

# TO DO
ns = Namespace("http://somewhere#")
# RDFLib
print("RDFLib - TASK 7.1: List all subclasses of \"LivingThing\" with RDFLib and SPARQL")
for s, p, o in g.triples((None, RDFS.subClassOf, ns.LivingThing)):
print(s)

# SPARQL
q1 = """
SELECT ?subClass
WHERE {
?subClass rdfs:subClassOf ns:LivingThing .
}
"""
print("\nSPARQL - TASK 7.1: List all subclasses of \"LivingThing\" with RDFLib and SPARQL")
for r in g.query(q1, initNs={"ns": ns}):
print(r.subClass)


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

"""

# TO DO
# RDFLib
print("\nRDF - TASK 7.2: List all individuals of \"Person\" with RDFLib and SPARQL (remember the subClasses)")
for subclass in g.triples((None, RDFS.subClassOf, ns.Person)):
for individual in g.triples((None, RDF.type, subclass[0])):
print(individual[0])
for individual in g.triples((None, RDF.type, ns.Person)):
print(individual[0])


# SPARQL
q2 = """
SELECT ?individual
WHERE {
?individual rdf:type/rdfs:subClassOf* ns:Person .
}
"""
print("\nSPARQL - TASK 7.2: List all individuals of \"Person\" with RDFLib and SPARQL (remember the subClasses):")
for r in g.query(q2, initNs={"ns": ns, "rdf": RDF, "rdfs": RDFS}):
print(r.individual)

"""**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)**

"""

# TO DO
q3 = """
SELECT ?individual
WHERE {
{ ?individual rdf:type ns:Person } UNION { ?individual rdf:type ns:Animal }
}
"""
print("\nTASK 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):")
for r in g.query(q3, initNs={"ns": ns, "rdf": RDF}):
print(r.individual)


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

# TO DO
q4 = """

SELECT ?name
WHERE {
?person a ns:Person .
?person foaf:knows ns:RockySmith .
?person vcard:Given ?name .
}
"""
from rdflib import FOAF
print("\nTASK 7.4: List the name of the persons who know Rocky (in SPARQL only)")
for r in g.query(q4):
print(r.name)


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

# TO DO
q5 = """
SELECT ?name
WHERE {
?animal a ns:Animal .
?otherAnimal a ns:Animal .
?animal foaf:knows ?otherAnimal .
?animal vcard:Given ?name .
}
"""
VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")
print("\nTask 7.5: List the name of those animals who know at least another animal in the graph (in SPARQL only)")
for r in g.query(q5):
print(r.name)

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

# TO DO
q6 = """
SELECT ?livingThing ?age
WHERE {
?livingThing a ?subclass .
?subclass rdfs:subClassOf* ns:LivingThing .
?livingThing foaf:age ?age .
}
ORDER BY DESC(?age)
"""

print("\nTask 7.6: List the age of all living things in descending order (in SPARQL only)")
for r in g.query(q6):
print(r.age)