Diane Trout (alienghic) wrote,
Diane Trout
alienghic

  • Mood:

RDF modeling with Python

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.

henly.n3
# 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 .
infer.py
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
  smart_model.setRules(rules.allRules())
  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():                                   
    model.addStatement(it.current())                 

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
  runQuery(model)

if __name__ == "__main__":
  main()
Tags: kde, python, semantic web
Subscribe

  • Dragonblink woke up

    She's finally out of the recovery room and is awake and talking. She sounds a little horse from the breathing tube, and is currently feeling "not bad…

  • Dragonblink Post Surgery Update

    The doctor just stopped by to tell me that everything went really well. The protrusion into her spinal column was larger than what was apparent on…

  • Dragonblink Surgery Update

    They called me a few minutes ago to tell me that the surgery started 30 minutes ago and is going well. (Don't you like this nearly-live operational…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments