-
Notifications
You must be signed in to change notification settings - Fork 23
Assignment4 Ignacio Garcia Luengo #99
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #QUERY 1 | ||
|
|
||
| select distinct ?prop ?values | ||
|
|
||
| where { | ||
|
|
||
| ?politico rdf:type <http://dbpedia.org/ontology/Politician>. | ||
| ?politico ?prop ?values | ||
|
|
||
| } | ||
|
|
||
| LIMIT 100 | ||
|
|
||
| #QUERY 2 | ||
|
|
||
| select distinct ?prop ?values | ||
|
|
||
| where { | ||
|
|
||
| ?politico rdf:type <http://dbpedia.org/ontology/Politician>. | ||
| ?politico ?prop ?values. | ||
|
|
||
| FILTER(?prop != rdf:type) | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| #QUERY 3 | ||
|
|
||
|
|
||
| select distinct ?prop ?values | ||
|
|
||
| where { | ||
|
|
||
| ?politico rdf:type <http://dbpedia.org/ontology/Politician>. | ||
| ?politico ?prop ?values. | ||
|
|
||
| FILTER(?prop != rdf:type) | ||
|
|
||
|
|
||
| } | ||
|
|
||
| #QUERY 4 | ||
|
|
||
| select distinct ?propValue | ||
| where | ||
| { | ||
| ?name a <http://dbpedia.org/ontology/Politician> . | ||
| ?name ?prop ?value. | ||
| FILTER(?prop != rdf:type) | ||
| } | ||
|
|
||
|
|
||
| #QUERY 5 | ||
|
|
||
| select distinct ?prop, | ||
| COUNT(DISTINCT ?values) as ?posibleValues | ||
| where{ | ||
| ?name a <http://dbpedia.org/ontology/Politician>. | ||
| ?name ?prop ?values. | ||
| FILTER(?prop != rdf:type). | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Task06.ipynb | ||
|
|
||
| Automatically generated by Colaboratory. | ||
|
|
||
| Original file is located at | ||
| https://colab.research.google.com/drive/1RGn053zTemcIJaB_3_TsDsp9H3ALnaIA | ||
|
|
||
| **Task 06: Modifying RDF(s)** | ||
| """ | ||
|
|
||
| !pip install rdflib | ||
| github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2021-2022/master/Assignment4/course_materials" | ||
|
|
||
| """Leemos el fichero RDF de la forma que lo hemos venido haciendo""" | ||
|
|
||
| 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)) | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| """**TASK 6.1: Create a new class named "University"** | ||
|
|
||
| """ | ||
|
|
||
| # TO DO | ||
| g.add((ns.University, RDF.type, RDFS.Class)) | ||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| university = Namespace("http://University#") | ||
| g.add((university.University, RDF.type, RDFS.Class)) | ||
|
|
||
| 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 | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| g | ||
|
|
||
| """**TASK 6.3: Create a new individual of Researcher named "Jane Smith"**""" | ||
|
|
||
| # TO DO | ||
| g.add((ns.JaneSmith, RDF.type, ns.Researcher)) | ||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| """**TASK 6.4: Add to the individual JaneSmith the fullName, given and family names**""" | ||
|
|
||
| # TO DO | ||
| vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") | ||
| g.add((ns.JaneSmith, vcard.FN, Literal('Jane Smith'))) | ||
| g.add((ns.JaneSmith, vcard.Given, Literal('Jane'))) | ||
| g.add((ns.JaneSmith, vcard.Family, Literal('Smith'))) | ||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
|
|
||
| """**TASK 6.5: Add UPM as the university where John Smith works**""" | ||
|
|
||
| # TO DO | ||
| g.add((ns.UPM, RDF.type, ns.University)) | ||
| g.add((ns.JohnSmith, vcard.worksat, ns.UPM)) | ||
| # Visualize the results | ||
| for s, p, o in g: | ||
| print(s,p,o) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Task07.ipynb | ||
|
|
||
| Automatically generated by Colaboratory. | ||
|
|
||
| Original file is located at | ||
| https://colab.research.google.com/drive/1FlWj0ZqWT8Jc-Mf3FS4WWGCZmYEuKj5m | ||
|
|
||
| **Task 07: Querying RDF(s)** | ||
| """ | ||
|
|
||
| !pip install rdflib | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work in Python, you need to comment it out |
||
| github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2021-2022/master/Assignment4/course_materials" | ||
|
|
||
| """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 | ||
| 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 "Person" with RDFLib and SPARQL**""" | ||
|
|
||
| # TO DO | ||
| ns = Namespace("http://somewhere#") | ||
| VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#") | ||
|
|
||
| #Usando RDFlib | ||
| lista_subclases = [] | ||
| for s, p, o in g.triples((None, RDFS.subClassOf, ns.Person)): | ||
| print(s) | ||
| lista_subclases += [s] | ||
|
|
||
| #SPARQL | ||
| 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: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)** | ||
|
|
||
| """ | ||
|
|
||
| # TO DO | ||
| personas = [] | ||
| for s, p, o in g.triples((None, RDF.type, ns.Person)): | ||
| print(s) | ||
| for subclase in lista_subclases: | ||
| for s1, p1, o1 in g.triples((None, RDF.type, subclase)): | ||
| print(s1) | ||
| personas += [s1] | ||
|
|
||
|
|
||
| individuals = prepareQuery(''' | ||
| SELECT DISTINCT ?Persons WHERE { | ||
| ?Persons RDF:type ns:Person. | ||
| } | ||
| ''', | ||
| initNs={"ns": ns, "RDF": RDF} | ||
| ) | ||
|
|
||
| individuals_subclases = prepareQuery(''' | ||
| SELECT DISTINCT ?Persons WHERE { | ||
| ?Subclass RDFS:subClassOf ns:Person. | ||
| ?Persons RDF:type ?Subclass | ||
| } | ||
| ''', | ||
| initNs={"RDFS": RDFS, "ns": ns, "RDF": RDF} | ||
| ) | ||
| for r in g.query(individuals): | ||
| print(r.Persons) | ||
| for r in g.query(individuals_subclases): | ||
| print(r.Persons) | ||
|
|
||
| """**TASK 7.3: List all individuals of "Person" and all their properties including their class with RDFLib and SPARQL** | ||
|
|
||
| """ | ||
|
|
||
| # TO DO | ||
|
|
||
| for person in personas: | ||
| for s, p, o in g.triples((person, None, None)): | ||
| print(p, o) | ||
|
|
||
|
|
||
| propiedades_person = prepareQuery(''' | ||
| SELECT DISTINCT ?Persons ?Property ?Object WHERE { | ||
| ?Persons RDF:type ns:Person. | ||
| ?Persons ?Property ?Object | ||
| } | ||
| ''', | ||
| initNs={"ns": ns, "RDF": RDF} | ||
| ) | ||
|
|
||
| propiedades_subclass = prepareQuery(''' | ||
| SELECT DISTINCT ?Persons ?Property ?Object WHERE { | ||
| ?Subclass RDFS:subClassOf ns:Person. | ||
| ?Persons RDF:type ?Subclass. | ||
| ?Persons ?Property ?Object. | ||
| } | ||
| ''', | ||
| initNs={"RDFS": RDFS, "ns": ns, "RDF": RDF} | ||
| ) | ||
|
|
||
| for person in personas: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop is quite confusing. You are already getting the right results from the query. Why combine it with rdf lib here? the rdf lib and the query parts of the exercise should be separate |
||
| for r in g.query(propiedades_person): | ||
| if r.Persons == person: | ||
| print(r.Property, r.Object) | ||
| for r in g.query(propiedades_subclass): | ||
| if r.Persons == person: | ||
| print(r.Property, r.Object) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you add it twice?