Python: Query an RDF graph
#### notes:
## in order to make the sparql query work with the literals, I modified all the rdf source files!!!
## they had the <rdf:datatype="http://www.w3.org/2001/XMLSchema#string"> datatype, I just replaced it with an unnamed literal
import rdflib
from rdflib.Graph import Graph
from rdflib import Namespace
g = Graph()
myfile ='/Users/mac/Documents/Ontologies/sentence_instances_8_12_08.owl'
g.parse(myfile, format="xml")
print "the lenght of the graph is: ", len(g)
result = g.query("""PREFIX phil: <http://myontology.owl/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?x ?p
WHERE { ?s phil:HAS-NUMBER-REFERENCE ?p . ?s phil:HAS-STRING-CONTENT ?x . } """
)
print "Query result: ", len(result)
dict = {}
#serialize the results
for r in result.serialize('python'):
dict[r[1].__str__()] = r[0].__str__()
print "The dictionary has collapsed %s total sentences into %s elements" % (len(result), len(dict.keys()))