2/7/2013 - 9:27 PM

Python: Query an RDF graph

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=""> 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:  <>
                          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()))