Skip to main content

OntoLearner: A Modular Python Library for Ontology Learning with LLMs.

Project description

OntoLearner Logo

OntoLearner: A Modular Python Library for Ontology Learning with LLMs

PyPI version PyPI Downloads License: MIT Hugging Face Collection pre-commit Documentation Status Maintenance DOI

OntoLearner is a modular and extensible architecture designed to support ontology learning and reuse. The conceptual and functional architecture of OntoLearner is shown as following. The framework comprises three core components—Ontologizers, Learning Tasks, and Learner Models—structured to enable reusable and customizable ontology engineering workflows.

🧪 Installation

OntoLearner is available on PyPI and you can install using pip:

pip install ontolearner

Next, verify the installation:

import ontolearner

print(ontolearner.__version__)

Please refer to Installation page for further options.

🔗 Essential Resources

Resource Info
📚 OntoLearner Documentation OntoLearner's extensive documentation website.
🤗 Datasets on Hugging Face Access curated, machine-readable ontologies.
Quick Tour on OntoLearner Open In Colab version=1.2.1 OntoLearner hands-on Colab tutorials.
🚀 Quickstart Get started quickly with OntoLearner’s main features and workflow.
🕸️ Learning Tasks Explore supported ontology learning tasks like LLMs4OL Paradigm tasks and Text2Onto.
🧠 Learner Models Browse and configure various learner models, including LLMs, Retrieval, or RAG approaches.
📚 Ontologies Documentations Review benchmark ontologies and datasets used for evaluation and training.
🧩 How to work with Ontologizer? Learn how to modularize and preprocess ontologies using the Ontologizer module.

🚀 Quick Tour

Get started with OntoLearner in just a few lines of code. This guide demonstrates how to initialize ontologies, load datasets, and train an LLM-assisted learner for ontology engineering tasks.

Basic Usage - Automatic Download from Hugging Face:

from ontolearner import Wine

# 1. Initialize an ontologizer from OntoLearner
ontology = Wine()

# 2. Load the ontology automatically from HuggingFace
ontology.load()

# 3. Extract the learning task dataset
data = ontology.extract()

To see the ontology metadata you can print the ontology:

print(ontology)

Now, explore 150+ ready-to-use ontologies or read on how to work with ontologizers.

Learner Models:

from ontolearner import AutoRetrieverLearner, AgrO, train_test_split, evaluation_report

# 1. Programmatic import of an ontology
ontology = AgrO()
ontology.load()

# 2. Load tasks datasets
ontological_data = ontology.extract()

# 3. Split into train and test sets
train_data, test_data = train_test_split(ontological_data, test_size=0.2, random_state=42)

# 4. Initialize Learner
task = 'non-taxonomic-re'
ret_learner = AutoRetrieverLearner(top_k=5)
ret_learner.load(model_id='sentence-transformers/all-MiniLM-L6-v2')

# 5. Fit the model to training data and do the predict
ret_learner.fit(train_data, task=task)
predicts = ret_learner.predict(test_data, task=task)

# 6. Evaluation
truth = ret_learner.tasks_ground_truth_former(data=test_data, task=task)
metrics = evaluation_report(y_true=truth, y_pred=predicts, task=task)
print(metrics)

Other learners:

LearnerPipeline: The OntoLearner also offers a streamlined LearnerPipeline class that simplifies the entire process of initializing, training, predicting, and evaluating a RAG setup into a single call.

# Import core components from the OntoLearner library
from ontolearner import LearnerPipeline, AgrO, train_test_split

# Load the AgrO ontology, which includes structured agricultural knowledge
ontology = AgrO()
ontology.load()  # Load ontology data (e.g., entities, relations, metadata)

# Extract relation instances from the ontology and split them into training and test sets
train_data, test_data = train_test_split(
    ontology.extract(),      # Extract annotated (head, tail, relation) triples
    test_size=0.2,           # 20% for evaluation
    random_state=42          # Ensures reproducible splits
)

# Initialize the learning pipeline using a dense retriever
pipeline = LearnerPipeline(
    retriever_id='sentence-transformers/all-MiniLM-L6-v2',  # Hugging Face model ID for retrieval
    batch_size=10,       # Number of samples to process per batch (if batching is enabled internally)
    top_k=5              # Retrieve top-5 most relevant support instance per query
)

# Run the pipeline on the training and test data
# The pipeline performs: fit() → predict() → evaluate() in sequence
outputs = pipeline(
    train_data=train_data,
    test_data=test_data,
    evaluate=True,           # If True, computes precision, recall, and F1-score
    task='non-taxonomic-re'  # Specifies that we are doing non-taxonomic relation prediction
)

# Print the evaluation metrics (precision, recall, F1)
print("Metrics:", outputs['metrics'])

# Print the total elapsed time for training and evaluation
print("Elapsed time:", outputs['elapsed_time'])

# Print the full output dictionary (includes predictions)
print(outputs)

⭐ Contribution

We welcome contributions to enhance OntoLearner and make it even better! Please review our contribution guidelines in CONTRIBUTING.md before getting started. You are also welcome to assist with the ongoing maintenance by referring to MAINTENANCE.md. Your support is greatly appreciated.

If you encounter any issues or have questions, please submit them in the GitHub issues tracker.

💡 Acknowledgements

If you find this repository helpful or use OntoLearner in your work or research, feel free to cite our publication:

@inproceedings{babaei2023llms4ol,
  title={LLMs4OL: Large language models for ontology learning},
  author={Babaei Giglou, Hamed and D’Souza, Jennifer and Auer, S{\"o}ren},
  booktitle={International Semantic Web Conference},
  pages={408--427},
  year={2023},
  organization={Springer}
}

or:

@software{babaei_giglou_2025_15399783,
  author       = {Babaei Giglou, Hamed and D'Souza, Jennifer and Aioanei, Andrei and Mihindukulasooriya, Nandana and Auer, Sören},
  title        = {OntoLearner: A Modular Python Library for Ontology Learning with LLMs},
  month        = may,
  year         = 2025,
  publisher    = {Zenodo},
  version      = {v1.3.0},
  doi          = {10.5281/zenodo.15399783},
  url          = {https://doi.org/10.5281/zenodo.15399783},
}

This software is archived in Zenodo under the DOI DOI and is licensed under License: MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ontolearner-1.4.5.tar.gz (421.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ontolearner-1.4.5-py3-none-any.whl (134.7 kB view details)

Uploaded Python 3

File details

Details for the file ontolearner-1.4.5.tar.gz.

File metadata

  • Download URL: ontolearner-1.4.5.tar.gz
  • Upload date:
  • Size: 421.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.10.18 Linux/6.11.0-1018-azure

File hashes

Hashes for ontolearner-1.4.5.tar.gz
Algorithm Hash digest
SHA256 0a95b8fb69b06ec8a8c243907130ff288b95103ce72ddcbdc28dcf8cd16f543b
MD5 c764c820a521927e0c28db843314ef7d
BLAKE2b-256 4128a2b7fd98043612cfd75e039d20eb2ffbfb9b235263427e35865dec008494

See more details on using hashes here.

File details

Details for the file ontolearner-1.4.5-py3-none-any.whl.

File metadata

  • Download URL: ontolearner-1.4.5-py3-none-any.whl
  • Upload date:
  • Size: 134.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.10.18 Linux/6.11.0-1018-azure

File hashes

Hashes for ontolearner-1.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c51c1f3101e2d119db881174b5f4629efd732726bd4f2532a75fb82d56b1812a
MD5 e2d7bf6b5b4130313b630b1ffbf2a7fa
BLAKE2b-256 7030e4851a0d01bdd5cda41145439f86cb5c4f23af591528763660ea11544207

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page