dice-group / owlapy

OWLAPY is a Python Framework for creating and manipulating OWL Ontologies.
MIT License
16 stars 1 forks source link

Get rdf:labels of a owl class #78

Closed renespeck closed 5 days ago

renespeck commented 6 days ago

Hi, is it possible to get the labels of a class defined in an ontology?

<owl:Class rdf:ID="AAV">
  <rdfs:isDefinedBy rdf:resource="http://www.ontologyportal.org/SUMO.owl"/>
  <rdfs:subClassOf rdf:resource="#MilitaryVehicle"/>
  <rdfs:subClassOf rdf:resource="#AmphibiousVehicle"/>
  <rdfs:subClassOf rdf:resource="#PassengerVehicle"/>
  <rdfs:label xml:lang="en">AA v</rdfs:label>
  <rdfs:label xml:lang="en">Amphibious Assault Vehicle</rdfs:label>
</owl:Class>

Best and thanks.

alkidbaci commented 6 days ago

Yes you can achieve that by searching for values of rdfs:label data property:

from owlapy.class_expression import OWLClass
from owlapy.iri import IRI
from owlapy.owl_ontology_manager import OntologyManager
from owlapy.owl_property import OWLDataProperty
from owlapy.owl_reasoner import FastInstanceCheckerReasoner, OntologyReasoner

ontology = OntologyManager().load_ontology("path/to/some/ontology.owl")
reasoner = FastInstanceCheckerReasoner(ontology, OntologyReasoner(ontology))

c = OWLClass(IRI('http://your_ontology_namespace#', 'AAV'))
p = OWLDataProperty("http://www.w3.org/2000/01/rdf-schema#label")

[print(label.get_literal()) for label in reasoner.data_property_values(c, p)]

# ===Results===

# AA v
# Amphibious Assault Vehicle

Note: Reasoners that support this are only FastInstanceCheckerReasoner and OntologyReasoner.

renespeck commented 6 days ago

Thank you very much also for your quick reply.