Skip to content

Commit

Permalink
added neural ontology manager
Browse files Browse the repository at this point in the history
  • Loading branch information
LckyLke committed Dec 8, 2024
1 parent b293d15 commit bb2fa35
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
9 changes: 7 additions & 2 deletions examples/neural_reasoner_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"""
from owlapy.owl_neural_reasoners.owl_neural_reasoner import OWLNeuralReasoner
from owlapy.owl_neural_reasoners.neural_ontology_manager import NeuralOntologyManager
from owlapy.owl_reasoner import StructuralReasoner
from owlapy.owl_ontology_manager import OntologyManager
from owlapy.utils import concept_reducer, concept_reducer_properties, jaccard_similarity, f1_set_similarity
Expand Down Expand Up @@ -77,9 +78,13 @@ def execute(args):
symbolic_kb = StructuralReasoner(ontology=OntologyManager().load_ontology(path=args.path_kg))
# (2) Initialize Neural OWL Reasoner.
if args.path_kge_model:
neural_owl_reasoner = OWLNeuralReasoner(path_neural_embedding=args.path_kge_model, gamma=args.gamma)
neural_manager = NeuralOntologyManager()
neural_manager.load_neural_embedding(path=args.path_kge_model)
neural_owl_reasoner = OWLNeuralReasoner(ontology=neural_manager, gamma=args.gamma)
else:
neural_owl_reasoner = OWLNeuralReasoner(path_of_kb=args.path_kg, gamma=args.gamma)
neural_manager = NeuralOntologyManager()
neural_manager.load_ontology(path=args.path_kg)
neural_owl_reasoner = OWLNeuralReasoner(ontology=neural_manager, gamma=args.gamma)
# Fix the random seed.
random.seed(args.seed)
###################################################################
Expand Down
10 changes: 8 additions & 2 deletions owlapy/owl_neural_reasoners/abstract.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from abc import ABC, abstractmethod
from typing import List, Tuple, Generator
from typing import List, Tuple, Generator, Union
from owlapy.class_expression import OWLClassExpression
from owlapy.owl_neural_reasoners.neural_ontology_manager import NeuralOntologyManager
from owlapy.owl_ontology import AbstractOWLOntology

class AbstractNeuralReasoner(ABC):
"""Abstract class for Neural Reasoners that operate on OWL Class Expressions using embeddings."""
@abstractmethod
def __init__(self, ontology: Union[NeuralOntologyManager, AbstractOWLOntology], **kwargs):
pass

@abstractmethod
def predict(self, h: str = None, r: str = None, t: str = None) -> List[Tuple[str, float]]:
Expand Down Expand Up @@ -33,4 +38,5 @@ def object_properties_in_signature(self) -> List:
@abstractmethod
def data_properties_in_signature(self) -> List:
"""Retrieve all data properties in the knowledge base."""
pass
pass
#TODO LF: Maybe we can define more methods or remove some of them -> depending on concrete implementations.
28 changes: 28 additions & 0 deletions owlapy/owl_neural_reasoners/neural_ontology_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from owlapy.owl_ontology import Ontology
from owlapy.owl_ontology_manager import OntologyManager
from dicee.knowledge_graph_embeddings import KGE

# LF: for now functions same as normal OntologyManager (+ can return path)
class NeuralOntologyManager(OntologyManager):
def __init__(self, world_store=None):
super().__init__(world_store)

def load_ontology(self, path: str = None) -> Ontology:
self._path = path
return super().load_ontology(path)

def load_neural_embedding(self, path: str = None):
self._path_neural_embedding = path
return KGE(path)

def get_path(self) -> str:
if hasattr(self, '_path'):
return self._path
else:
return None

def get_path_neural_embedding(self) -> str:
if hasattr(self, '_path_neural_embedding'):
return self._path_neural_embedding
else:
return None
6 changes: 5 additions & 1 deletion owlapy/owl_neural_reasoners/owl_neural_reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
from collections import Counter, OrderedDict
from functools import lru_cache
from owlapy.owl_neural_reasoners.abstract import AbstractNeuralReasoner
from owlapy.owl_ontology import Ontology
from owlapy.owl_neural_reasoners.neural_ontology_manager import NeuralOntologyManager

# TODO:
def is_valid_entity(text_input: str):
return True if "/" in text_input else False

class OWLNeuralReasoner(AbstractNeuralReasoner):
""" OWL Neural Reasoner uses a neural link predictor to retrieve instances of an OWL Class Expression"""
def __init__(self, path_of_kb: str = None, path_neural_embedding: str = None, gamma: float = 0.25, max_cache_size: int = 2**20):
def __init__(self, ontology: NeuralOntologyManager, gamma: float = 0.25, max_cache_size: int = 2**20):
assert gamma is None or 0 <= gamma <= 1, "Confidence threshold (gamma) must be in the range [0, 1]."
path_of_kb = ontology.get_path()
path_neural_embedding = ontology.get_path_neural_embedding( )
self.gamma = gamma
self._prediction_cache = OrderedDict()
self._max_cache_size = max_cache_size
Expand Down
12 changes: 8 additions & 4 deletions tests/test_neural_reasoner_accuracy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from owlapy.owl_neural_reasoners.owl_neural_reasoner import OWLNeuralReasoner
from owlapy.owl_neural_reasoners.neural_ontology_manager import NeuralOntologyManager
from owlapy.owl_reasoner import StructuralReasoner
from owlapy.owl_ontology_manager import OntologyManager
from owlapy.utils import concept_reducer, concept_reducer_properties, jaccard_similarity, f1_set_similarity
Expand Down Expand Up @@ -31,12 +32,15 @@ def test_retrieval_performance():
# (1) Initialize knowledge base.
symbolic_kb = StructuralReasoner(ontology=OntologyManager().load_ontology(path=path_kg))

# (2) Initialize Neural OWL Reasoner.
# (2) Initialize Neural OWL Reasoner.
if path_kge_model:
neural_owl_reasoner = OWLNeuralReasoner(path_neural_embedding=path_kge_model, gamma=gamma)
neural_manager = NeuralOntologyManager()
neural_manager.load_neural_embedding(path=path_kge_model)
neural_owl_reasoner = OWLNeuralReasoner(ontology=neural_manager, gamma=gamma)
else:
neural_owl_reasoner = OWLNeuralReasoner(path_of_kb=path_kg, gamma=gamma)

neural_manager = NeuralOntologyManager()
neural_manager.load_ontology(path=path_kg)
neural_owl_reasoner = OWLNeuralReasoner(ontology=neural_manager, gamma=gamma)
# Fix the random seed.
random.seed(seed)

Expand Down

0 comments on commit bb2fa35

Please sign in to comment.