Skip to content

ontocast.tool.chunk.embedding

Embedding helpers shared by the heading tiers: the embedder type and cosine.

Attributes

__all__ = ['TextEmbedder', 'cosine', 'unit_vector'] module-attribute

Classes

TextEmbedder

Bases: Protocol

Embeds short texts, or returns None when no model is available.

Source code in ontocast/tool/chunk/embedding.py
class TextEmbedder(Protocol):
    """Embeds short texts, or returns ``None`` when no model is available."""

    def __call__(self, texts: list[str]) -> list[list[float]] | None: ...

Methods:

__call__(texts)
Source code in ontocast/tool/chunk/embedding.py
def __call__(self, texts: list[str]) -> list[list[float]] | None: ...

Functions:

cosine(left, right)

Cosine similarity of two unit vectors.

Source code in ontocast/tool/chunk/embedding.py
def cosine(left: list[float], right: list[float]) -> float:
    """Cosine similarity of two unit vectors."""
    return sum(a * b for a, b in zip(left, right))

unit_vector(vector)

vector scaled to length one (a zero vector is returned unchanged).

Source code in ontocast/tool/chunk/embedding.py
def unit_vector(vector: list[float]) -> list[float]:
    """``vector`` scaled to length one (a zero vector is returned unchanged)."""
    norm = math.sqrt(sum(value * value for value in vector)) or 1.0
    return [value / norm for value in vector]