Ontologizer

BaseOntology

class ontolearner.base.ontology.BaseOntology(language: str = 'en', base_dir: str | None = None)[source]

Bases: ABC

Abstract base class for ontology processing and data extraction.

This class provides a standardized interface for loading, processing, and extracting structured data from ontologies across different domains. It supports both local file loading and automatic downloading from Hugging Face repositories.

Initialize the ontology instance.

Parameters:
  • language – Language code for label extraction (default: ‘en’). Used when multiple language labels are available.

  • base_dir – Base directory for resolving relative import paths. Useful when ontology imports other local files.

build_graph() None

Build NetworkX graph from RDF data.

This method should be implemented by each specific ontology class to handle their unique graph structure.

category: str = None
check_if_class(entity)

Check if an entity is a class (i.e., rdf:type rdfs:Class or owl:Class).

contains_imports() bool

Hook: Check if the ontology contains imports.

creator: str = None
domain: str = None
download_url = None
extract(reinforce_extraction: bool = False) OntologyData

Extract structured learning data from the loaded ontology.

This method extracts three types of ontological information needed for machine learning tasks: term typings, taxonomic relations, and non-taxonomic relations. The extraction strategy depends on the data source.

For local ontologies, it performs live extraction from the RDF graph. For Hugging Face ontologies, it downloads pre-processed JSON files for efficiency, with an option to force live extraction.

Parameters:

reinforce_extraction – If True and loaded from Hugging Face, forces live extraction from RDF instead of using pre-processed JSON files. Useful for debugging or when pre-processed data is outdated.

Returns:

  • term_typings: List of term-to-type mappings

  • type_taxonomies: Hierarchical relationships between types

  • type_non_taxonomic_relations: Non-hierarchical relationships

Return type:

OntologyData object containing

Raises:
  • ValueError – If ontology has not been loaded yet.

  • Exception – If extraction fails due to malformed data or network issues.

extract_term_typings() List[TermTyping]

Extract term-to-type mappings (e.g., instances of classes).

extract_type_non_taxonomic_relations() Tuple[List[str], List[str], List[NonTaxonomicRelation]]

Extract non-taxonomic relations from the ontology.

extract_type_taxonomies() Tuple[List[str], List[TaxonomicRelation]]

Extract taxonomy from the ontology

format: str = None
from_huggingface()

Download an ontology file from a Hugging Face repository.

This method constructs the appropriate repository ID and filename based on the OntoLearner naming conventions and downloads the ontology file to a local cache directory.

Parameters:
  • ontology_id – Unique identifier for the ontology (e.g., “wine”).

  • domain – Domain category (e.g., “Food & Beverage”).

  • format – File format extension (e.g., “owl”, “rdf”).

Returns:

Local file path to the downloaded ontology file.

Raises:
  • ValueError – If any required parameter is missing or empty.

  • Exception – If download fails due to network issues, authentication, or file not found in repository.

from_local(path: str)

Validate and return a local ontology file path.

This method checks if the specified local file exists and returns the path if valid. It serves as a validation step for local file loading.

Parameters:

path – File system path to the local ontology file.

Returns:

The same path if the file exists and is accessible.

Raises:

FileNotFoundError – If the specified file does not exist.

get_label(uri: str) str

Extracts the label for a given URI in the specified language from the RDF graph. If no valid label is found, returns None.

is_valid_label(label: str) Any
last_updated: str = None
license: str = None
load(path: str | None = None) None

Load an ontology from a local file or Hugging Face repository.

This method is the main entry point for loading ontologies. It automatically determines the source (local vs. Hugging Face) based on whether a path is provided, handles RDF parsing, and processes any owl:imports statements.

Parameters:

path – Optional local file path. If provided, loads from local file. If None, downloads from Hugging Face using class attributes (ontology_id, domain, format).

Raises:
  • ValueError – If the loaded ontology contains no RDF triples.

  • FileNotFoundError – If local file path is invalid.

  • Exception – If download from Hugging Face fails or RDF parsing fails.

loaded_from_huggingface = False
loaded_from_local = False
ontology_full_name: str = None
ontology_id: str = None
version: str = None

AutoOntology

class ontolearner._ontology.AutoOntology(ontology_id)[source]

Bases: object

Factory class to automatically instantiate an ontology class from the ontolearner.ontology module based on a given ontology ID.

This class scans all classes defined in the ontolearner.ontology module and returns an instance of the first class that:

  • has a callable load method,

  • has an ontology_id attribute,

  • and whose class name matches the given ontology_id (case-insensitive).

If no matching class is found, an instance of BaseOntology is returned by default.

Parameters:

ontology_id (str) – The identifier (name) of the ontology class to instantiate.

Returns:

An instance of the matching ontology class or a BaseOntology fallback.

Return type:

BaseOntology

Example

>>> auto_onto = AutoOntology("AgrO")
>>> print(type(auto_onto))
>>> <class 'ontolearner.ontology.AgrO'>

If no class matches “unknownontology”: >>> auto_onto = AutoOntology(“unknownontology”) >>> print(type(auto_onto)) >>> <class ‘ontolearner.base.BaseOntology’>

class ontolearner._ontology.OntoLearnerMetadataExporter[source]

Bases: object

Generates Dublin Core metadata for ontology classes.

export(path: str = 'DCMI-Metadata.rdf')
get_url(domain, ontology_id)