Datasets:
Tasks:
Text Classification
Formats:
parquet
Languages:
Ancient Greek (to 1453)
Size:
100K - 1M
License:
| """Public representation of sentence-aligned metrical lines. | |
| The build uses richer Hypotactic records internally for alignment and | |
| provenance. Only metrical content may enter the model-facing | |
| ``metrical_lines`` JSON column. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import unicodedata | |
| from typing import Any | |
| PUBLIC_METRICAL_LINE_FIELDS = ("text", "metre", "syllables") | |
| PUBLIC_METRICAL_LINE_FIELD_SET = frozenset(PUBLIC_METRICAL_LINE_FIELDS) | |
| def _normalize(text: str) -> str: | |
| text = text.lower().replace("ς", "σ") | |
| return "".join( | |
| character | |
| for character in unicodedata.normalize("NFD", text) | |
| if unicodedata.category(character) != "Mn" and character.isalpha() | |
| ) | |
| def _normalized_slice(text: str, start: int, end: int) -> str: | |
| """Slice by normalized alphabetic offsets while retaining source spelling.""" | |
| if not 0 <= start < end <= len(_normalize(text)): | |
| raise ValueError(f"invalid normalized slice {start}:{end}") | |
| positions = [] | |
| for index, character in enumerate(text): | |
| positions.extend([index] * len(_normalize(character))) | |
| if len(positions) != len(_normalize(text)): | |
| raise ValueError("could not map normalized metrical text to source text") | |
| return text[positions[start]:positions[end - 1] + 1].strip() | |
| def public_metrical_line(line: dict[str, Any]) -> dict[str, Any]: | |
| """Return the strict, model-facing allowlist for one metrical line.""" | |
| missing = PUBLIC_METRICAL_LINE_FIELD_SET - line.keys() | |
| if missing: | |
| raise ValueError(f"metrical line lacks public fields: {sorted(missing)}") | |
| public = {field: line[field] for field in PUBLIC_METRICAL_LINE_FIELDS} | |
| if not isinstance(public["text"], str) or not public["text"].strip(): | |
| raise ValueError("metrical line text must be a non-empty string") | |
| if not isinstance(public["metre"], str) or not public["metre"].strip(): | |
| raise ValueError("metrical line metre must be a non-empty string") | |
| if not isinstance(public["syllables"], list) or not public["syllables"]: | |
| raise ValueError("metrical line syllables must be a non-empty list") | |
| return public | |
| def cropped_public_metrical_line( | |
| line: dict[str, Any], start: int, end: int, | |
| ) -> dict[str, Any]: | |
| """Publish only the syllables and text inside a normalized character span.""" | |
| normalized_text = _normalize(line["text"]) | |
| if not 0 <= start < end <= len(normalized_text): | |
| raise ValueError(f"invalid metrical crop {start}:{end}/{len(normalized_text)}") | |
| selected = [] | |
| cursor = 0 | |
| for syllable in line["syllables"]: | |
| syllable_length = len(_normalize(syllable["text"])) | |
| syllable_start, syllable_end = cursor, cursor + syllable_length | |
| cursor = syllable_end | |
| overlaps = max(start, syllable_start) < min(end, syllable_end) | |
| if overlaps: | |
| selected_syllable = dict(syllable) | |
| if syllable_start < start or syllable_end > end: | |
| selected_syllable["text"] = _normalized_slice( | |
| syllable["text"], | |
| max(start, syllable_start) - syllable_start, | |
| min(end, syllable_end) - syllable_start, | |
| ) | |
| selected.append(selected_syllable) | |
| if cursor != len(normalized_text): | |
| raise ValueError( | |
| "metrical syllables do not cover line text exactly: " | |
| f"{cursor} != {len(normalized_text)}" | |
| ) | |
| cropped = { | |
| "text": _normalized_slice(line["text"], start, end), | |
| "metre": line["metre"], | |
| "syllables": selected, | |
| } | |
| if _normalize(cropped["text"]) != normalized_text[start:end]: | |
| raise ValueError("cropped metrical text does not match requested span") | |
| if _normalize("".join(item["text"] for item in selected)) != normalized_text[start:end]: | |
| raise ValueError("cropped syllables do not match requested span") | |
| return public_metrical_line(cropped) | |
| def sanitize_metrical_lines(encoded: str) -> str: | |
| """Strip every non-allowlisted field from serialized metrical lines.""" | |
| lines = json.loads(encoded) if isinstance(encoded, str) else encoded | |
| if not isinstance(lines, list) or not lines: | |
| raise ValueError("metrical_lines must be a non-empty list") | |
| return [public_metrical_line(line) for line in lines] | |
| def load_public_metrical_lines(encoded: Any) -> list[dict[str, Any]]: | |
| """Strictly validate already-public metrical lines.""" | |
| lines = json.loads(encoded) if isinstance(encoded, str) else encoded | |
| if not isinstance(lines, list) or not lines: | |
| raise ValueError("metrical_lines must be a non-empty list") | |
| for line in lines: | |
| if not isinstance(line, dict): | |
| raise ValueError("each metrical line must be a JSON object") | |
| if set(line) != PUBLIC_METRICAL_LINE_FIELD_SET: | |
| raise ValueError( | |
| "metrical line fields are not the public allowlist: " | |
| f"{sorted(line)}" | |
| ) | |
| public_metrical_line(line) | |
| return lines | |