generated from FacultadInformatica-LinkedData/Template-Curso
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask07.py
115 lines (90 loc) · 2.66 KB
/
task07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
"""Task07.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/FacultadInformatica-LinkedData/Curso2022-2023-DataScience/blob/master/Assignment4/course_materials/notebooks/Task07.ipynb
**Task 07: Querying 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/example6.rdf", format="xml")
"""**TASK 7.1: List all subclasses of "Person" with RDFLib and SPARQL**"""
# TO DO
# Visualize the results
from rdflib.plugins.sparql import prepareQuery
ns = Namespace("http://somewhere#")
for s1,p1,o1 in g.triples((None, RDFS.subClassOf, ns.Person)):
print(s1)
q1 = prepareQuery('''
SELECT
?x
WHERE {
?x rdfs:subClassOf ns:Person.
}
''',
initNs = { "rdfs": RDFS, "ns":ns}
)
for t1 in g.query(q1):
print(t1)
#for r in g.query(q1):
# print(r)
"""**TASK 7.2: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)**
"""
# TO DO
# Visualize the results
for s1,p1,o1 in g.triples((None, RDF.type, ns.Person)):
print(s1)
for s1,p1,o1 in g.triples((None, RDFS.subClassOf, ns.Person)):
for s2,p2,o2 in g.triples((None, RDF.type, s1)):
print(s2)
q2 = prepareQuery('''
SELECT
?x
WHERE {
{
?x rdf:type ns:Person.
} UNION {
?y rdfs:subClassOf ns:Person.
?x rdf:type ?y
}
}
''',
initNs = { "rdfs": RDFS,"rdf": RDF, "ns": ns}
)
for t2 in g.query(q2):
print(t2)
"""**TASK 7.3: List all individuals of "Person" and all their properties including their class with RDFLib and SPARQL**
"""
# TO DO
# Visualize the results
for s1,p1,o1 in g.triples((None, RDF.type, ns.Person)):
for s2,p2,o2 in g.triples((s1, None, None)):
print(s2,p2,o2)
for s1,p1,o1 in g.triples((None, RDFS.subClassOf, ns.Person)):
for s2,p2,o2 in g.triples((None, RDF.type, s1)):
for s3,p3,o3 in g.triples((s1, None, None)):
print(s3,p3,o3)
q3 = prepareQuery('''
SELECT
?x ?Property ?z
WHERE {
{
?x rdf:type ns:Person.
?x ?Property ?z
} UNION {
?x rdf:type ?y.
?x ?Property ?z.
?y rdfs:subClassOf ns:Person
}
}
''',
initNs = { "rdfs": RDFS, "ns":ns, "rdf":RDF}
)
for t3 in g.query(q3):
print(t3)