OntologyPorts
What it is
- Defines abstract “port” interfaces for ontology-based Named Entity Recognition (NER).
- Establishes contracts for:
- An NER adapter that maps text to an ontology (provided as Turtle).
- A service that performs NER using an internal adapter/configured ontology.
Public API
class IOntologyNERPort(ABC)
Interface for an ontology-aware NER adapter.
named_entity_recognition(self, input: str, ontology_str: str) -> rdflib.Graph- Applies NER to
inputand maps recognized entities to ontology concepts defined inontology_str(Turtle). - Returns an
rdflib.Graphwith mapped entities/relationships.
- Applies NER to
class IOntologyService(ABC)
Interface for an ontology NER service.
named_entity_recognition(self, input: str) -> rdflib.Graph- Applies NER to
input. - Returns an
rdflib.Graphwith mapped entities/relationships.
- Applies NER to
Configuration/Dependencies
- Dependencies
rdflib.Graph(return type for both ports)langchain_core.language_models.BaseChatModel(typed private attribute inIOntologyNERPort)
- Notes
- The module defines private typed attributes (
__chat_model,__ner_adaptor) but does not expose constructors or setters; concrete implementations are responsible for wiring these.
- The module defines private typed attributes (
Usage
Minimal example implementing both interfaces:
from rdflib import Graph
from naas_abi_core.services.ontology.OntologyPorts import IOntologyNERPort, IOntologyService
class DummyNER(IOntologyNERPort):
def named_entity_recognition(self, input: str, ontology_str: str) -> Graph:
return Graph() # populate graph in a real implementation
class OntologyService(IOntologyService):
def __init__(self, ner: IOntologyNERPort, ontology_str: str):
self._ner = ner
self._ontology_str = ontology_str
def named_entity_recognition(self, input: str) -> Graph:
return self._ner.named_entity_recognition(input, self._ontology_str)
svc = OntologyService(DummyNER(), ontology_str="") # Turtle ontology string goes here
g = svc.named_entity_recognition("Some text")
print(len(g))
Caveats
- These are abstract interfaces only; no concrete NER logic is provided.
IOntologyNERPort.named_entity_recognitionrequires the ontology as a Turtle string (ontology_str).