diff --git a/ontoScorer/ontology.py b/ontoScorer/ontology.py index c5769c174058eff8c50e82efb6626db130b6a255..942993dc24ad86c7cd60a30f6099560c9a96ff09 100644 --- a/ontoScorer/ontology.py +++ b/ontoScorer/ontology.py @@ -4,7 +4,7 @@ #============================================================================== # Ontology Analyzer #------------------------------------------------------------------------------ -# Extracts various elements (Classes, Object Properties, Data Properties, +# Extracts various entities (Classes, Object Properties, Data Properties, # Individuals, Annotations) from RDF/OWL ontologies. Enables foundational # comparisons between different ontologies. #============================================================================== @@ -14,7 +14,7 @@ from rdflib import URIRef, BNode from rdflib.namespace import split_uri from ontology_metadata import Metadata -from ontology_element import Element, NamedElement, BlankElement +from ontology_entity import Entity, NamedEntity, BlankEntity #============================================================================== # Class: Ontology @@ -32,7 +32,6 @@ class Ontology: self.graph = self.load_ontology(ontology_path) self.classes = self.get_classes() - #-------------------------------------------------------------------------- # Base Method(s) #-------------------------------------------------------------------------- @@ -44,15 +43,14 @@ class Ontology: return g def _get_elements_of_type(self, rdf_type): - """Extract all elements of a specific RDF type from the ontology.""" - elements = [] + """Extract all entities of a specific RDF type from the ontology.""" + entities = [] for s, _, o in self.graph.triples((None, RDF.type, rdf_type)): if isinstance(s, BNode): - elements.append(BlankElement(s, self.graph)) + entities.append(BlankEntity(s, self.graph)) elif isinstance(s, URIRef): - elements.append(NamedElement(s, self.graph)) - return elements - + entities.append(NamedEntity(s, self.graph)) + return entities #-------------------------------------------------------------------------- # Metadata and Annotation Extraction @@ -68,17 +66,15 @@ class Ontology: pass # TODO return -1 - def get_annotations(self): """Extract all annotation comments from the ontology.""" annotations = set() for _, _, o in self.graph.triples((None, RDFS.label, None)): annotations.add(str(o)) return annotations - #-------------------------------------------------------------------------- - # Element Extraction + # Entity Extraction #-------------------------------------------------------------------------- def get_classes(self): @@ -94,22 +90,19 @@ class Ontology: return self._get_elements_of_type(OWL.DatatypeProperty) def get_individuals(self) -> list: - """Extract all individuals from the ontology, including elements explicitly typed as owl:Individual, + """Extract all individuals from the ontology, including entities explicitly typed as owl:Individual, owl:NamedIndividual or any class.""" - # Getting elements of type owl:NamedIndividual and owl:Individual individuals = self._get_elements_of_type(OWL.NamedIndividual) individuals += self._get_elements_of_type(URIRef("http://www.w3.org/2002/07/owl#Individual")) - # Getting all elements typed as one of the classes all_classes = {cls.reference for cls in self.get_classes()} for cls in all_classes: - individuals += [NamedElement(s, self.graph) if isinstance(s, URIRef) else BlankElement(s, self.graph) + individuals += [NamedEntity(s, self.graph) if isinstance(s, URIRef) else BlankEntity(s, self.graph) for s, _, o in self.graph.triples((None, RDF.type, cls))] return list(set(individuals)) # Ensuring uniqueness - #-------------------------------------------------------------------------- # Taxonomic Relation Extraction (Hierarchical Structure) #-------------------------------------------------------------------------- @@ -125,7 +118,6 @@ class Ontology: """Extract all restrictons from the ontology.""" return self._get_elements_of_type(OWL.Restriction) - #-------------------------------------------------------------------------- # Comparison Method(s) #-------------------------------------------------------------------------- @@ -135,7 +127,6 @@ class Ontology: self_classes = {c.name() for c in self.classes} other_classes = {c.name() for c in other_ontology.classes} - # Pour des comparaisons plus avancées, ajustez ce code unique_to_self = self_classes - other_classes unique_to_other = other_classes - self_classes diff --git a/ontoScorer/ontology_element.py b/ontoScorer/ontology_entity.py similarity index 78% rename from ontoScorer/ontology_element.py rename to ontoScorer/ontology_entity.py index 8e5e80ffff47818529877305256724cf402f39df..2b2ae8e1066c1d7f44ae0f884d612f5f3980d73f 100644 --- a/ontoScorer/ontology_element.py +++ b/ontoScorer/ontology_entity.py @@ -2,28 +2,28 @@ # -*-coding:Utf-8 -* #============================================================================== -# Ontology Elements +# Ontology Entities #------------------------------------------------------------------------------ -# Represents various elements (Classes, Object Properties, Data Properties, +# Represents various entities (Classes, Object Properties, Data Properties, # Individuals) within an ontology. #============================================================================== from rdflib import URIRef, BNode from rdflib.namespace import split_uri -class Element: +class Entity: def __init__(self, reference, graph): self.reference = reference self.graph = graph def properties(self): - """Retrieve properties associated with the element.""" + """Retrieve properties associated with the entity.""" return list(self.graph.predicate_objects(subject=self.reference)) def name(self): raise NotImplementedError("The method name() must be implemented by subclasses.") -class NamedElement(Element): +class NamedEntity(Entity): def __init__(self, uri, graph): super().__init__(uri, graph) self.uri = uri @@ -32,10 +32,10 @@ class NamedElement(Element): return str(self.uri) def name(self): - _, element_name = split_uri(self.uri) - return element_name + _, entity_name = split_uri(self.uri) + return entity_name -class BlankElement(Element): +class BlankEntity(Entity): def __init__(self, bnode, graph): super().__init__(bnode, graph) self.id = str(bnode) @@ -45,4 +45,3 @@ class BlankElement(Element): def name(self): return self.id - diff --git a/tests/context.py b/tests/context.py index c611d6eb47e6caafe8cabd172b8a49d061a56587..3eb163fbe0b14e8a12364c1baffa3da66a952ed7 100644 --- a/tests/context.py +++ b/tests/context.py @@ -3,7 +3,7 @@ import sys CURRENT_DIRPATH = os.path.dirname(os.path.abspath(__file__)) LIB_PATH = os.path.dirname(f'{CURRENT_DIRPATH}/../..') -print(f'Test Context: {LIB_PATH}') +# print(f'Test Context: {LIB_PATH}') sys.path.insert(0, os.path.abspath(LIB_PATH)) import ontoScorer