/** * Alignment: turn a `CanonicalRow` (raw + components) into a `LabeledRow` * (raw + tokens + BIO labels) or a `QuarantinedRow` (raw + reason) per the Phase 1 plan. * * Pipeline: * * 1. For each `(tag, value)` in `components`, find the value's character span in `raw`. * First try a verbatim substring match (case-insensitive, whitespace-collapsed). * If that fails, fall back to fuzzy match via `fastest-levenshtein`, * with a tunable edit distance threshold. * 2. If any component cannot be located, reject the row with a human-readable reason and send it to the * quarantine pile (`reason: "component-not-found:"` or `"edit-distance-exceeded::"`). * 3. Tokenize `raw` with the supplied `Tokenizer` (defaults to the whitespace tokenizer). * 4. For each token: walk the list of component spans, pick the one whose span * contains the token's character range. * First token in a component span → `B-`; subsequent tokens → `I-`; no overlap → `O`. * 5. Emit the located char spans verbatim as `span_starts[]` / `span_ends[]` / * `span_tags[]` (the v0.5.0 char-offset format). * * Structural invariants the function preserves (the span ones loudly — a violation throws * rather than quarantines, because it indicates a bug here rather than bad source data): * * - `tokens.length === labels.length` always. * - Each component contributes at most one contiguous BIO run (no `B-tag … O … I-tag` gaps). * This is enforced by greedy first-match span assignment + ordered token iteration. * - The span triple is sorted ascending by start and non-overlapping. * - `raw` is NFC-normalized (asserted per row. a non-NFC raw makes char offsets ambiguous * downstream — NFD `é` occupies two code units where NFC `é` occupies one — and silently so). */ import type { ComponentTag } from "@mailwoman/codex/component"; import type { CanonicalRow, LabeledRow, QuarantinedRow } from "#types"; import { type Tokenizer } from "#utils/tokenize"; /** * Options for `alignRow`. */ export interface AlignOptions { /** * Tokenizer to use. * * @defaultValue `whitespaceTokenizer()` */ tokenizer?: Tokenizer; /** * Max Levenshtein edit distance to accept when a verbatim substring match fails. * * Set `0` to require verbatim matches only. * * Distance is computed against same-length windows in `raw`, so the threshold * scales naturally with the component value length. * * @defaultValue `2` */ maxEditDistance?: number; /** * Case-insensitive comparison for substring search. * * The retained span in `raw` is the original case. * Only matching is case-insensitive. * * @defaultValue `true` */ caseInsensitive?: boolean; } /** * Either a successful labeled row or a quarantined one. */ export type AlignmentResult = { kind: "labeled"; row: LabeledRow; } | { kind: "quarantined"; row: QuarantinedRow; }; /** * One located char-offset label span over a row's `raw` ([start, end) in UTF-16 code units). The element type behind the parallel `span_starts[]`/`span_ends[]`/`span_tags[]` triple on `LabeledRow`. */ export interface ComponentSpan { tag: ComponentTag; start: number; end: number; } /** * The char spans of a row's components in `raw`, sorted by start, or the quarantine * reason when a component cannot be placed. * * `raw` and the component values must already be NFC-normalized. * * {@linkcode alignRow} labels a row from these spans, and the WOF sampler keys a row's * structure cell by their order, so both read a component that occurs inside another * (a region whose name appears inside its locality's name) the same way. */ export declare function locateComponentSpans(raw: string, components: CanonicalRow["components"], opts?: Pick): { spans: ComponentSpan[]; } | { reason: string; }; /** * Align a single row. */ /** * A copy of a row's components with each value in NFC, the form {@linkcode alignRow} writes to a labeled row. * * A caller that decides a row's split before aligning it reads these, * so the decision equals the one made on the aligned row. */ export declare function nfcComponents(components: CanonicalRow["components"]): CanonicalRow["components"]; export declare function alignRow(row: CanonicalRow, opts?: AlignOptions): AlignmentResult; /** * Enforce the span-triple invariants: in-bounds, sorted ascending by start, non-overlapping. * * For `alignRow`: `claimed`-span bookkeeping in `locateSpan` already makes overlap impossible * and the caller sorts, so a violation here is a bug in this file rather than bad source data. * Throw rather than quarantine, so the corruption cannot ride into a corpus. * * Any code that emits the triple without going through `alignRow` must pass its output through this. */ export declare function assertSpanInvariants(spans: readonly ComponentSpan[], row: Pick): void; //# sourceMappingURL=align.d.ts.map