diff --git a/Assignment3/GuillermoDiaz.txt b/Assignment3/GuillermoDiaz.txt new file mode 100644 index 0000000..18404e0 --- /dev/null +++ b/Assignment3/GuillermoDiaz.txt @@ -0,0 +1,47 @@ + +#1 + +select distinct ?prop +where { +?politician rdf:type . +?politician ?prop ?values + +} + LIMIT 100 + +#2 + +select distinct ?prop +where { +?politician rdf:type . +?politician ?prop ?values. +FILTER(?prop != rdf:type) +} + LIMIT 100 + +#3 +select distinct ?values +where { +?politician rdf:type . +?politician ?prop ?values. +FILTER(?prop != rdf:type) +} + LIMIT 100 + + #4 + select distinct ?prop ?values +where { +?politician rdf:type . +?politician ?prop ?values. +FILTER(?prop != rdf:type) +} + LIMIT 100 + +#5 +select distinct ?prop COUNT(?values) +where { +?politician rdf:type . +?politician ?prop ?values. +FILTER(?prop != rdf:type) +} + LIMIT 100 \ No newline at end of file diff --git a/Assignment4/gdiazbenito-20c018/task06.py b/Assignment4/gdiazbenito-20c018/task06.py new file mode 100644 index 0000000..bc89bee --- /dev/null +++ b/Assignment4/gdiazbenito-20c018/task06.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +"""Task06.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/drive/1hSv61ejXESrp34CUz1PqZCsoA43tzc8y + +**Task 06: Modifying RDF(s)** +""" + +#pip install rdflib +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2020-2021/master/Assignment4" + +"""Leemos el fichero RDF de la forma que lo hemos venido haciendo""" + +from rdflib import Graph, Namespace, Literal, XSD +from rdflib.namespace import RDF, RDFS + +with open('example5.rdf', 'r') as f: + 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(f, 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"** + +**TASK 6.2: Add "Researcher" as a subclass of "Person"** + +**TASK 6.3: Create a new individual of Researcher named "Jane Smith"** + +**TASK 6.4: Add to the individual JaneSmith the fullName, given and family names** + +**TASK 6.5: Add UPM as the university where John Smith works** +""" + +"TASK 6.1:" +g.add((ns.University, RDF.type, RDFS.Class)) +#for s,p,o in g: + # print(s,p,o) + +"TASK 6.2:" +g.add((ns.Researcher, RDFS.subClassOf, ns.Person)) +#for s,p,o in g: + #print(s,p,o) + +"TASK 6.3:" +g.add((ns.JaneSmith,RDF.type,ns.Researcher)) +#for s,p,o in g: + #print(s,p,o) + +"TASK 6.4:" +VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#") +g.add((ns.JaneSmith, VCARD.FN, Literal('Jane Smith', datatype=XSD.string))) +g.add((ns.JaneSmith, VCARD.Family, Literal('Smith', datatype=XSD.string))) +g.add((ns.JaneSmith, VCARD.Given, Literal('Jane', datatype=XSD.string))) +#for s,p,o in g: + #print(s,p,o) + +"TASK 6.5:" +g.add((ns.UPM, RDF.type, ns.University)) +g.add((ns.JohnSmith, VCARD.Works, ns.UPM)) +for s,p,o in g: + print(s,p,o) \ No newline at end of file diff --git a/Assignment4/gdiazbenito-20c018/task07.py b/Assignment4/gdiazbenito-20c018/task07.py new file mode 100644 index 0000000..c734928 --- /dev/null +++ b/Assignment4/gdiazbenito-20c018/task07.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- +"""Task07.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/drive/1tV5j-DRcpPtoJGoMj8v2DSqR_9wyXeiE + +**Task 07: Querying RDF(s)** +""" + +#pip install rdflib +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2020-2021/master/Assignment4" + +"""Leemos el fichero RDF de la forma que lo hemos venido haciendo""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +from rdflib.plugins.sparql import prepareQuery + +with open('../python/example6.rdf', 'r') as f: + 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(f, format="xml") +"""**TASK 7.1: List all subclasses of "Person" with RDFLib and SPARQL** + +**TASK 7.2: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)** + +**TASK 7.3: List all individuals of "Person" and all their properties including their class with RDFLib and SPARQL** +""" + +"TASK 7.1:" +ns = Namespace("http://somewhere#") +#Con RDFLib +for s,p,o in g.triples((None, RDFS.subClassOf, ns.Person)): + print(s,p,o) +#Con SPRQL +q1 = prepareQuery(''' + SELECT + ?Subclass + WHERE { + ?Subclass RDFS:subClassOf ns:Person. + } + ''', + initNs={"RDFS": RDFS, "ns": ns} +) +for r in g.query(q1): + print(r.Subclass) + + +"TASK 7.2:" +#Con RDFLib +for s,p,o in g.triples((None, RDF.type , ns.Person)): + print(s,p,o) +for s,p,o in g.triples((None, RDFS.subClassOf, ns.Person)): + for s2,p2,o2 in g.triples((None,RDF.type,s)): + print(s2,p2,o2) +#Con SPRQL +q1= prepareQuery(''' + SELECT + ?Persons + WHERE { + ?Persons RDF:type ns:Person. + } + ''', + initNs={"RDF": RDF, "ns": ns} +) +#subclases +q2 = prepareQuery(''' + SELECT + ?Persons + WHERE { + ?Subclass RDFS:subClassOf ns:Person. + ?Persons RDF:type ns:Person. + } + ''', + initNs={"RDFS": RDFS, "ns": ns, "RDF":RDF} +) +for r in g.query(q1): + print(r.Persons) + +for r in g.query(q2): + print(r.Persons) \ No newline at end of file