I have an ontology in an 'owl' file (nif.owl). I am familiar with Java, but it kept crashing; therefore, I tried using Python. However, since I have not used Python before, I am not sure if I am loading the ontology correctly!
Here is the part that I believe is related to loading the ontology:
g = rdflib.Graph()
g.parse ('nif.owl', format='xml')
nif = rdflib.Namespace('http://purl.org/nif/ontology/nif.owl')
g.bind('nif', nif)
I believe the problem is where the g.parse sets the format to 'xml'. I think maybe the 'xml' is wrong.
I have also attached the header of the ontology file as an image.
The reason I think there is a mistake with the code is the result I get which I showed in the image below:
Thanks!
PS: Below is the full code in case there is something wrong with it:
import logging
import rdflib
import time
logging.basicConfig()
logger = logging.getLogger('logger')
logger.warning('The system may break down')
start_time = time.time()
g = rdflib.Graph()
g.parse ('nif.owl', format='xml')
nif = rdflib.Namespace('http://purl.org/nif/ontology/nif.owl')
g.bind('nif', nif)
query = """
select distinct ?p
where { ?s ?p ?o}
LIMIT 5
"""
result = g.query(query)
print(result.serialize(format='csv'))
print("--- %s seconds ---" % (time.time() - start_time))
rdflib
is the appropriate API here? I might be wrong, but your ontology basically contains just a set ofowl:import
statements - this, in fact is a feature of OWL 2 - I doubt thatrdflib
will handle those imports is intended given thatrdflib
is designed for RDF. I'm pretty sure it will not load all imports into the graph. Honestly, if you don't need SPARQL, a dedicate OWL lib likeowlready2
would be the better way to work on OWL ontologies. – Opsis5.5.0
- whether the ontology fits into memory, I don't know. – Opsisowl:import
statements, i.e. use the RDF/SPARQL capabilities of rdflib and get the import URLs from the ontology document. (this indeed has to be done transitively if the imported ontologies do import other ontologies) – Opsisowl:import
statements which in fact do provide the URLs to all the ontologies. How you parse those URLs from the file doesn't matter. It's RDF/XML, so either you use SPARQL here or you use any other XML lib for Python. – Opsis