Skip to content
Snippets Groups Projects
Commit 8e482965 authored by Aurélien Lamercerie's avatar Aurélien Lamercerie
Browse files

Rename class (ontology) Element to Entity

parent a582ae25
Branches
No related tags found
No related merge requests found
......@@ -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,7 +66,6 @@ class Ontology:
pass # TODO
return -1
def get_annotations(self):
"""Extract all annotation comments from the ontology."""
annotations = set()
......@@ -76,9 +73,8 @@ class Ontology:
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
......
......@@ -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
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment