-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparql.py
34 lines (28 loc) · 946 Bytes
/
sparql.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
'''
Script to perform a SPARQL Query. Example from Programming the Semantic Web,
page 92-94
'''
import rdflib
from rdflib.graph import ConjunctiveGraph, Namespace
from rdflib import plugin
# Initialize the SPARQL plugins
plugin.register(
"sparql", rdflib.query.Processor,
"rdfextras.sparql.processor", "Processor")
plugin.register(
"sparql", rdflib.query.Result,
"rdfextras.sparql.query", "SPARQLQueryResult")
# Initialize Namespace Objects
DBPEDIA = Namespace("http://dbpedia.org/")
INPHO = Namespace("http://inpho.cogs.indiana.edu/")
THINKER = Namespace("http://inpho.cogs.indiana.edu/thinker/")
g = ConjunctiveGraph()
g.parse("inpho_sample.n3.rdf", format="n3")
# this is the SPARQL Query
results = g.query("""
SELECT ?teacher ?student
WHERE { ?teacher inpho:teacher ?student }
""", initNs={'inpho': INPHO, 'thinker': THINKER})
# print results, see p92-93 for XML serializing
for triple in results:
print triple