Log in

No account? Create an account

RDF modeling with Python

« previous entry | next entry »
Sep. 23rd, 2008 | 11:54 pm
mood: accomplishedaccomplished

I've been trying to figure out how to get an RDF model with inference working as that's one more step toward creating intelligence assistance software. (Inference is a way of providing simple deductive logic to a set of statements.)

Python has a basic rdf library, rdflib, but it lacked the inference rules. KDE4 has an rdf library Soprano, for which there's a python interface. Unfortunately, it's just a light wrapper around the C++ API, so it's not the easiest thing to make work.

# This is a basic example from
# Semantic Web for the Working Ontologist
# illustrating subClassOf inference

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix shop: <http://shop.com/d#> .

shop:Henleys rdfs:subClassOf shop:Shirts .
shop:ChamoisHenly rdf:type shop:Henleys .
from PyKDE4.soprano import Soprano       
from PyQt4.QtCore import QUrl            

def createInferenceModel(model):
  # it appears that you have to wrap a storage model
  # with an inference model.                        
  smart_model = Soprano.Inference.InferenceModel(model)

  # The apidox on the kde site suggest there's a class that
  # will return a model with the RDFS rules loaded         
  # but I couldn't find a way to get to it with            
  # the beta kde4 4.1.1 python package                           
  # so the following code adds the RDFS rules to the inference model
  # by hand                                                         

  # get the RDFS rules
  RuleSet = Soprano.Inference.RuleSet
  rules = RuleSet.standardRuleSet(Soprano.Inference.RDFS)

  # add the rules to the inference model
  return smart_model                    

def loadModelFromFile(model, n3file):
  # get the plugin manager           
  plugin_manager = Soprano.PluginManager.instance()
  # SerializationTurtle is an enum                 
  turtle_id= Soprano.SerializationTurtle           
  turtle_parser = plugin_manager.discoverParserForSerialization(turtle_id)

  # Turtle is a subset of N3, so my simple N3 file loaded using
  # the turtle parser, there didn't seem to be an N3 parser available.
  it = turtle_parser.parseFile(n3file, QUrl(''), turtle_id)           

  # add all the statements from the file to the model
  while it.next():                                   

def dumpStatements(model):
  # some debugging code   
  it = model.listStatements()
  while it.next():           
    s = it.current()         
    print s.subject().toString(),
    print s.predicate().toString(),
    print s.object().toString()    

def runQuery(model):
  # exmple from Semantic Web for the working Ontologist
  # SparQL query showing the type inference for subClassOf
  query = """PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX shop: <http://shop/#>
    SELECT ?x
    WHERE {
      ?x rdf:type shop:Shirts .
  # report anything interesting
  print "---- results ---"
  result = model.executeQuery(query, Soprano.Query.QueryLanguageSparql)
  while result.next():
     print result.binding('x').toString()

def main():
  # create default model
  model = Soprano.createModel()
  # wrap it so it can do inference
  smart_model = createInferenceModel(model)
  # load data into it
  loadModelFromFile(smart_model, 'henly.n3')
  # see if it works

if __name__ == "__main__":

Link | Leave a comment | Share

Comments {0}