Skip to content

ontocast.onto.util

derive_ontology_id(iri)

Derive a short ontology handle from an absolute IRI.

Prefers rdflib conventional namespace prefixes (SKOS → skos). Falls back to the last non-opaque path segment. Returns None instead of inventing garbage ids (e.g. FOAF 0.1 → would-be 01).

Source code in ontocast/onto/util.py
def derive_ontology_id(iri: str) -> str | None:
    """Derive a short ontology handle from an absolute IRI.

    Prefers rdflib conventional namespace prefixes (SKOS → ``skos``). Falls back
    to the last non-opaque path segment. Returns ``None`` instead of inventing
    garbage ids (e.g. FOAF ``0.1`` → would-be ``01``).
    """
    if not isinstance(iri, str) or not iri.strip():
        return None

    raw = strip_iri_brackets(iri.strip())
    conventional = _conventional_prefix_for(raw)
    if conventional:
        return conventional

    normalized_iri = normalize_ontology_iri(raw)
    parsed = urlparse(normalized_iri)

    candidate = (
        parsed.path.rsplit("/", 1)[-1]
        if parsed.path and "/" in parsed.path
        else parsed.netloc.split(".")[0]
        if parsed.netloc
        else normalized_iri
    )

    return _clean_derived_id(candidate)

is_rdflib_default_namespace(namespace)

Return True when namespace is one of rdflib's built-in bindings.

Source code in ontocast/onto/util.py
def is_rdflib_default_namespace(namespace: str) -> bool:
    """Return True when *namespace* is one of rdflib's built-in bindings."""
    return namespace in RDFLIB_DEFAULT_NAMESPACE_URIS

normalize_ontology_iri(iri)

Normalize an ontology IRI for catalog lookup and alias resolution.

Strips angle brackets and trailing / or # so that http://example.org/ont, …/ont/, and …/ont# compare equal as lookup keys. Author namespace delimiters for prefix bindings remain the responsibility of :func:normalize_namespace_iri.

Source code in ontocast/onto/util.py
def normalize_ontology_iri(iri: str) -> str:
    """Normalize an ontology IRI for catalog lookup and alias resolution.

    Strips angle brackets and trailing ``/`` or ``#`` so that
    ``http://example.org/ont``, ``…/ont/``, and ``…/ont#`` compare equal as
    lookup keys. Author namespace delimiters for prefix bindings remain the
    responsibility of :func:`normalize_namespace_iri`.
    """
    text = strip_iri_brackets(iri)
    return text.rstrip("/#")