sphragis / scripts /text_units.py
Urdatorn's picture
Claude Opus 5
Publish structured columns instead of JSON inside strings
31e2783
Raw History Blame Contribute Delete
1.16 kB
"""Model-facing text units.
``text`` is a list of strings with one entry per sentence or metrical line, in
the same order as ``conllu``. It used to be a JSON-encoded string; it is now a
plain list, so nothing needs decoding before use.
"""
from __future__ import annotations
from typing import Any
def load_text_units(units: Any) -> list[str]:
"""Validate a published text column and return it unchanged."""
if not isinstance(units, list) or not units:
raise ValueError("text must be a non-empty list")
if not all(isinstance(unit, str) and unit.strip() for unit in units):
raise ValueError("every text unit must be a non-empty string")
return units
def source_text_units(value: Any) -> list[str]:
"""Accept either one internal string or an already-published list."""
if isinstance(value, str):
if not value.strip():
raise ValueError("source text must be a non-empty string")
return [value]
return load_text_units(value)
def encode_text_units(units: list[str]) -> list[str]:
"""Validate text units for publication without changing them."""
return load_text_units(units)