dice-group / Ontolearn

Ontolearn is an open-source software library for explainable structured machine learning in Python. It learns OWL class expressions from positive and negative examples.
https://ontolearn-docs-dice-group.netlify.app/index.html
MIT License
39 stars 9 forks source link

Verbalization through LLM service #359

Closed Demirrr closed 7 months ago

Demirrr commented 7 months ago
from ontolearn.learners import Drill
from ontolearn.knowledge_base import KnowledgeBase
from ontolearn.learning_problem import PosNegLPStandard
from ontolearn.verbalizer import LLMVerbalizer
from owlapy.model import OWLNamedIndividual, IRI
from owlapy.render import DLSyntaxObjectRenderer

# (1) Load a knowledge graph.
kb = KnowledgeBase(path='KGs/father.owl')
# (2) Initialize a learner.
model = Drill(knowledge_base=kb)
# (3) Define a description logic concept learning problem.
lp = PosNegLPStandard(pos={OWLNamedIndividual(IRI.create("http://example.com/father#stefan"))},
                      neg={OWLNamedIndividual(IRI.create("http://example.com/father#heinz")),
                           OWLNamedIndividual(IRI.create("http://example.com/father#anna")),
                           OWLNamedIndividual(IRI.create("http://example.com/father#michelle"))})

verbalizer = LLMVerbalizer()
render = DLSyntaxObjectRenderer()
# (4) Learn description logic concepts best fitting (3).
for h in model.fit(learning_problem=lp).best_hypotheses(10):
    str_concept = render.render(h.concept)
    print("Concept:", str_concept)
    print("Verbalization: ", verbalizer(text=str_concept))

where verbalizer object takes an OWL object and generates a natural language task that ideally represents the verbalized class expression

Concept: ≥ 1 hasChild.{markus}
Verbalization:   The concept "≥ 1 hasChild.{markus}" in Description Logic represents that an individual belongs to the class of things that have at least one child named "markus". This is a shorthand notation for "hasChild exactly 1 Markus or hasChild 2 Markus or ...", where "Markus" is an individual name and "hasChild" is a role representing the parent-child relationship.

Concept: person ⊓ (¬female)
Verbalization:   The concept "person ⊓ (¬female)" refers to individuals that belong to the class of persons and are not female. In other words, it describes male persons only, excluding any possibility of females or intersex individuals.

Concept: (¬person) ⊔ (¬female)
Verbalization:   The expression (¬person) ⊔ (¬female) can be read as "the union of non-persons and non-females." In other words, it represents everything that is not a person or not female, which includes all male individuals and any objects or concepts that are neither persons nor female.

Concept: ∀ hasChild⁻.{michelle}
Verbalization:   The expression "∀ hasChild−.{michelle}" in Description Logics (DL) states that, for any individual in the domain of discourse, if it has a child, then that child must be michelle. In other words, this DL axiom asserts that michelle is the only possible value for the 'hasChild' role for all individuals in the domain.

Concept: ∀ hasChild⁻.{stefan}
Verbalization:   The expression "∀ hasChild−.{stefan}" in Description Logic (DL) states that, for any individual in the domain, if it has a child then that child must be equivalent to the individual stefan. In other words, everyone in the domain is a child of stefan and stefan is the only child they have.

Concept: ≥ 1 hasChild.person
Verbalization:   The concept "≥ 1 hasChild.Person" in Description Logic represents that an individual of the current concept must have at least one direct child who is an individual of the "Person" concept. This is often used to describe a relationship between individuals, where the current concept represents a parent or a similar role, and "Person" represents any person or individual.

Concept: = 1 hasChild.person
Verbalization:   The expression "1 hasChild.person" in Description Logics is a role inclusion assertion that specifies that any individual of the current concept (implicitly existential) must have exactly one direct child who is an instance of the concept "person". This can be used to define more specific concepts or constraints on roles in ontologies.

Concept: ≤ 1 hasChild⁻.person
Verbalization:   The concept ≤ 1 hasChild^- . Person in Description Logics represents the set of individuals that have at most one child, where "hasChild" is an object property representing the parent-child relationship and "-" denotes its inverse. This concept can be used to define roles or properties of individuals in a knowledge representation system, such as an ontology.

Concept: ⊤
Verbalization:   In Description Logics, ⊤ (top) is a special concept representing the most general concept in a knowledge representation system. It includes all individuals in the domain of discourse, and serves as the root of the concept hierarchy.

Concept: ≤ 6 hasChild.person
Verbalization:   The concept "≤ 6 hasChild.Person" represents the set of individuals that have no more than six direct or indirect children, where each child is an individual of type Person. This can be used to define a family tree structure, where each parent node has at most six child nodes that are instances of the Person class.
Demirrr commented 7 months ago

Merged with #360