import type { SeededRandom } from "@mailwoman/core/math/random"; import { type PathBuilderLike } from "path-ts"; import { type ParquetColumns } from "#parquet/writers"; /** * The columns of a CJK corpus parquet file, in file order. */ export declare const CJK_CORPUS_COLUMNS: ParquetColumns; /** * One labeled CJK corpus row. */ export interface CJKRecord { raw: string; tokens: string[]; labels: string[]; span_starts: number[]; span_ends: number[]; span_tags: string[]; country: string; source: string; register: string; } /** * Writes rows as one corpus parquet file, each `license` set to its source's register license id. * * @throws Before writing, when a row's source has no license id. */ export declare function writeCJKCorpusParquet(rows: readonly CJKRecord[], path: PathBuilderLike, context: string, columns?: ParquetColumns): Promise; /** * Which rows of a split {@linkcode writeCJKSplit} counts coverage over. */ export declare const CJKStatsSample: { /** * The first `n` records of every part, as the label corpora count them. */ readonly FirstOfEachPart: "first-of-each-part"; /** * The first `n` records of the split, as the KR registry corpus counts them. */ readonly FirstOfSplit: "first-of-split"; }; export type CJKStatsSample = (typeof CJKStatsSample)[keyof typeof CJKStatsSample]; /** * Options for {@linkcode writeCJKSplit}. */ export interface WriteCJKSplitOptions { /** * The split's directory, created when absent. */ directory: PathBuilderLike; rows: readonly T[]; rowsPerPart: number; /** * Encodes one part's rows, in order. * * A builder whose encoder draws from a generator encodes the part in one call, * so the draws follow the part order. */ encodePart: (rows: readonly T[]) => CJKRecord[]; /** * The part file name's prefix: `part` writes `part-0000.parquet`. */ partPrefix: string; /** * The context string used in a refused license stamp. */ context: string; statsSample: { kind: CJKStatsSample; rows: number; }; } /** * One written split's row and part counts and the coverage over its sampled records. */ export interface CJKSplitReport { rows: number; parts: number; coverage: CoverageStats; } /** * Writes one split as parquet parts of `rowsPerPart` rows each, and counts its coverage. */ export declare function writeCJKSplit(options: WriteCJKSplitOptions): Promise; /** * The longest field value a renderer keeps. */ export declare const MAX_FIELD_CHARS = 64; /** * The longest rendered row, in code points, the character model reads without truncation. */ export declare const MAX_RENDERED_CHARS = 96; /** * Builds a row's raw text and its spans, one tagged field or untagged glue at a time. */ export declare class RowRenderer { #private; raw: string; starts: number[]; ends: number[]; tags: string[]; /** * Appends a tagged field. * An empty field leaves the row unchanged. */ put(tag: string, text: string): void; /** * Appends untagged text. */ glue(text: string): void; /** * The code-point length of the text so far. */ get length(): number; } /** * The legacy token columns of a span-labeled row: each whitespace token of `raw`, * labeled `B-` with the span its first character falls in, or `O` when none covers it. * * The character path ignores them, and the corpus schema requires them. */ export declare function labelTokensByFirstSpan(raw: string, starts: readonly number[], ends: readonly number[], tags: readonly string[]): { tokens: string[]; labels: string[]; }; /** * The separator between the parts of a composite key, written as an escape * so no NUL byte enters the source. * * A space cannot serve, because a 시군구 is written with one (`수원시 장안구`), * and `unitKey("A", "B C")` would collide with `unitKey("A B", "C")`. */ export declare const UNIT_SEPARATOR = "\0"; /** * The parts of a composite key, joined so no two different part lists produce the same key. */ export declare function unitKey(...parts: readonly string[]): string; /** * Adds `value` to the set a key set holds under `key`, starting the set when the key is new. */ export declare function addToKeySet(map: Map>, key: string, value: string): void; /** * A unit's hold-out bucket in [0, 100): its normalized name's MD5, read as an integer, modulo 100. */ export declare function muniBucket(unit: string): number; /** * Yields one decision per item of a pass over `count` items, taking exactly `min(quota, count)` of them. * * Each item is taken with probability `remaining quota / remaining items`, drawn from `rng`. * No draw is made once the quota is filled, so the draw count depends on where the quota fills. */ export declare function selectExact(count: number, quota: number, rng: SeededRandom): Generator; /** * Takes the next decision from a selector. * * @throws When the selector is exhausted, because a second pass met more items than the first counted. */ export declare function nextSelection(selector: Iterator): boolean; /** * The largest per-group cap whose capped total stays within `target`. */ export declare function waterFill(counts: ReadonlyMap, target: number): number; /** * Each group's quota under a water-filled cap, with the remainder up to `target` * granted to the groups with the most headroom first, so the quotas total `target` * whenever the pool has at least `target` rows. */ export declare function waterFillQuotas(poolCounts: ReadonlyMap, target: number): { cap: number; quotas: Map; }; /** * The per-character BIO array of `raw` from code-point spans. * * @throws When the span arrays differ in length, or a span is out of bounds, unsorted or overlapping. */ export declare function charLabelArrayFromSpans(raw: string, starts: readonly number[], ends: readonly number[], tags: readonly string[]): string[]; /** * Options for {@linkcode verifyRecord}. */ export interface VerifyRecordOptions { labelSetName: string; /** * @defaultValue {@linkcode MAX_RENDERED_CHARS} */ maxRenderedChars?: number; /** * @defaultValue true */ forbidWhitespace?: boolean; } /** * A record {@linkcode verifyRecord} refuses for one of its own checks. * * Its message begins with the defect's kind, before the first colon, so a builder * that counts refusals groups them by that prefix. * Spans the label array refuses throw a plain `Error` instead. */ export declare class RecordCheckError extends Error { name: string; } /** * Refuses a record that would train badly: no span, too long to read whole, a tag outside the label set, * an empty span, whitespace inside a span when forbidden, or spans the label array refuses. * * Each message quotes text as Python's `repr` does, the messages the Python builders wrote. * * @throws {@linkcode RecordCheckError} On the first such defect, naming it. */ export declare function verifyRecord(record: Pick, tagSet: ReadonlySet, options: VerifyRecordOptions): void; /** * The label set every Korean, Taiwanese and CJK-overlay row is checked against. */ export declare const CJK_LABEL_SET_NAME = "stage3-cjk"; /** * {@linkcode verifyRecord} under {@linkcode CJK_LABEL_SET_NAME}, with whitespace allowed inside a span. */ export declare function verifyCJKRecord(record: Pick, tagSet: ReadonlySet): void; /** * The tags of a label set, read from the label schema `@mailwoman/neural` emits. * * The schema is read as data, because `@mailwoman/neural` depends on `@mailwoman/gazetteer`, * which depends on this package. * * @throws When the schema identifies no such label set. */ export declare function readLabelSetTags(name: string): Promise>; /** * A corpus split's coverage report, in the key order the Python builders wrote. */ export interface CoverageStats { rows: number; bio_char_coverage_all: number; bio_char_coverage_significant: number; raw_len_min_median_max: [number, number, number] | null; registers: Map; per_tag_rows: Map; per_tag_spans: Map; per_tag_chars: Map; per_label_chars: Map; } /** * Rows, labeled-character coverage, lengths and per-tag and per-label counts over `records`. * * `per_tag_rows` counts each tag once per row, in the order the tag first appears in the row. * The Python report counted from a set, so its tie order followed `PYTHONHASHSEED`. */ export declare function coverageStats(records: Iterable>): CoverageStats; /** * The id of the padding character. */ export declare const PAD_CHAR_ID = 0; /** * The id of an unknown character. */ export declare const UNK_CHAR_ID = 1; /** * A character vocabulary: `` and ``, then every character seen at least * `minCount` times in code-point order, numbered from 2. */ export declare function buildCharVocab(texts: Iterable, minCount?: number): Map; /** * The vocabulary of characters seen at least twice in a corpus's train split, written as `name` beside it. * * Each corpus seals its own from its own train parts, read in code-point order of their paths. */ export declare function sealCharVocab(corpusDirectory: PathBuilderLike, name: string): Promise>; /** * Reads a character vocabulary a builder wrote. * * The object's key order isn't the vocabulary's order, because JSON parsing * lists integer-like keys such as `"1"` first. * A caller reads its characters and size, and orders them itself. * * @throws When the file is missing, or holds anything other than an object of integer ids. */ export declare function loadCharVocab(path: PathBuilderLike): Promise>; /** * A vocabulary of ``, `` and `characters` in code-point order, * numbered from 2, the seal {@linkcode buildCharVocab} writes. */ export declare function sealCharacters(characters: Iterable): Map; /** * Writes a vocabulary as tab-indented JSON with one entry per line and a trailing newline, * the bytes the Python builders wrote and the model card pins. */ export declare function saveCharVocab(vocab: ReadonlyMap, path: PathBuilderLike): Promise; //# sourceMappingURL=builder.d.ts.map