Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an Ontology in Python

I have an Ontology written in OWL. Does anyone know I can load it into python? any packages or even manually? rdflib which is mentioned in other questions is not suitable for me because it mainly concerns with RDF and "Seth" which is a very nice library doesn't work, because it requires "Pellet" library for which the website seems to be down and it(seth) also only works with JVM 1.4!

like image 910
Hossein Avatar asked Feb 23 '11 15:02

Hossein


4 Answers

I've not fully tried this, but you could look at loading your ontology into a Sesame database and then query it using a Python wrapper. I've played with RDFAlchemy and pySesame a little, but I still don't have a feel for how good they are. RDF/OWL databases feel very immature to me in general, so expect to encounter some significant technical hurtles.

like image 108
Cerin Avatar answered Sep 29 '22 12:09

Cerin


Within rdfAlchemy in the commands.py file there is commands section that was started for just this purpose. Haven't used it in a while but it is the beginings of exactly what you asked about i.e. creating the python skel from an ontology.

In fact, it uses paster "commands". If you go to the rdfalchemy subdir and type:

paster rdfSubject -h

you should see:

Usage: /home/phil/venv/some_path/bin/paster rdfSubject [options] 
paster rdfalchemy.commands
Create an rdfSubject subclass with descriptors from an RDF Schema

    will set the rdf_type
    Descriptors will be created

      1.  rdfs:domain and rdfs:range are respected
      2.  rdfSingle is used for properties that are
            * owl:InverseFunctionalProperty
            * owl:FunctionalProperty   
      3.  rdfList or rdfContainer is used if the proper range is set
      4.  rdfMultiple is used for all others

    The resulting .py file is ment to be a skeleton for the developers confvience.  
    Do not expect to be able to use the raw results.

Create an rdfSubject subclass with descriptors from an RDF Schema

Options:
  -h, --help            show this help message and exit
  -v, --verbose         
  -n, --simulate        
  -s SCHEMA, --schema=SCHEMA
                        file name or url of rdfSchema for this class
  -o FOUT, --fout=FOUT  output file name default: stdout (e.g.
                        ../MyRdfModel.py)
  -l, --list            list valid instances of `owl:Class` in the schema file
like image 30
Phil Cooper Avatar answered Sep 29 '22 12:09

Phil Cooper


Right, RDFLib works at the RDF semantics, so you'd need a toolkit for managing OWL semantics.

What I usually do it to first use another tool to get all the triples (OWLAPI or OWLRL, for instance) and then load it in RDFLib.

like image 24
wikier Avatar answered Sep 29 '22 12:09

wikier


In case anyone stumbles across this question again, this package is now available for interacting with OWL ontologies in Python: https://pypi.org/project/Owlready2/

From the project description, it has these capabilities:

  1. Import OWL 2.0 ontologies in NTriples, RDF/XML or OWL/XML format.
  2. Export OWL 2.0 ontologies to NTriples or RDF/XML.
  3. Manipulates ontology classes, instances and properties transparently, as if they were normal Python objects.
  4. Add Python methods to ontology classes.
  5. Perform automatic classification of classes and instances, using the HermiT reasoner.
  6. Tested up to 100 millions of RDF triples (but can potentially support more).
  7. In addition, the quadstore is compatible with the RDFlib Pyton module, which can be used to perform SPARQL queries.
like image 45
rosemcc Avatar answered Sep 29 '22 12:09

rosemcc