Skip to content

ontocast.tool.ontology_validation.label_repair

LLM-free labels for newly minted ontology terms.

A minted term the model left unlabelled almost always has a readable local name (ResearchProject, hasHTTPHeader). Its label is that name split into words, so deriving it costs nothing and spares the critic a call whose only fix would be the label. An opaque local name (Q42, a hash) carries no words; it is left unlabelled and the missing_label finding still fires.

Attributes

__all__ = ['derive_missing_labels', 'label_from_local_name'] module-attribute

Classes

Functions:

derive_missing_labels(inserts, snapshot_graph, known_terms=frozenset())

The rdfs:label triples the delta's unlabelled minted terms are missing.

Covers every class or property the delta declares that has no label in the delta or the snapshot and whose local name has words. A term of a well-known vocabulary or of the catalog (known_terms) is never minted here: an axiom on it is a reuse, and its labels are its owner's.

Returns:

Type Description
RDFGraph

A graph of the label triples to insert, empty when none is missing.

Source code in ontocast/tool/ontology_validation/label_repair.py
def derive_missing_labels(
    inserts: RDFGraph,
    snapshot_graph: RDFGraph | None,
    known_terms: Set[str] = frozenset(),
) -> RDFGraph:
    """The ``rdfs:label`` triples the delta's unlabelled minted terms are missing.

    Covers every class or property the delta declares that has no label in the
    delta or the snapshot and whose local name has words. A term of a
    well-known vocabulary or of the catalog (``known_terms``) is never minted
    here: an axiom on it is a reuse, and its labels are its owner's.

    Returns:
        A graph of the label triples to insert, empty when none is missing.
    """
    labels = RDFGraph()
    for subject in sorted(_declared_class_or_property_subjects(inserts), key=str):
        iri = str(subject)
        if iri in known_terms or is_well_known_term(iri) is not None:
            continue
        if _has_label(subject, inserts, snapshot_graph):
            continue
        label = label_from_local_name(_local_name(iri))
        if label is not None:
            labels.add((subject, RDFS.label, Literal(label, lang="en")))
    return labels

label_from_local_name(local)

A readable label for a local name, or None when it has no words.

Acronyms keep their capitals, other words are lower-cased, and a name that starts with a capital (a class) keeps a capital first letter: ResearchProject gives "Research project", hasHTTPHeader gives "has HTTP header".

Source code in ontocast/tool/ontology_validation/label_repair.py
def label_from_local_name(local: str) -> str | None:
    """A readable label for a local name, or ``None`` when it has no words.

    Acronyms keep their capitals, other words are lower-cased, and a name that
    starts with a capital (a class) keeps a capital first letter:
    ``ResearchProject`` gives "Research project", ``hasHTTPHeader`` gives
    "has HTTP header".
    """
    if not local or is_opaque_local_name(local):
        return None
    words: list[str] = []
    for chunk in re.split(r"[_\-\s]+", local):
        words.extend(_WORD_RE.findall(chunk))
    if not words or not any(word.isalpha() for word in words):
        return None
    text = " ".join(
        word if word.isupper() and len(word) > 1 else word.lower() for word in words
    )
    if local[0].isupper():
        text = text[0].upper() + text[1:]
    return text