import { type PathBuilderLike } from "path-ts"; import type { CanonicalRow, LabeledRow } from "#types"; import { type AddressKeyedRow, type FrozenGroups } from "#utils/evaluation-groups"; import { type SplitRetention } from "#utils/retention"; import type { ValidationGroups } from "#utils/validation-groups"; /** * The name of one corpus split. */ export type SplitName = "train" | "val" | "test"; /** * The three splits, in the order a build writes them. */ export declare const SPLIT_NAMES: readonly SplitName[]; /** * The component values that identify the places one country holds out. * * Sources represent a place in different components: `usgov-nad` and `tiger` emit `region`, * but BAN emits none, so a French holdout must also match on postcode. * A row is held out when any declared matcher fires. * A policy with no matchers holds out no entry. */ export interface HoldoutPolicy { regions?: readonly string[]; /** * Prefixes matched against `row.components.postcode`; use one only where it identifies * a single place by itself, such as the first two digits of a French postcode. */ postcodePrefixes?: readonly string[]; localities?: readonly string[]; } /** * One country's holdout as a bare region list or a {@link HoldoutPolicy}. * * Older committed `SPLIT_MANIFEST.json` files use a bare array of region values. */ export type CountryHoldout = readonly string[] | HoldoutPolicy; /** * Options for {@link splitRows}. */ export interface SplitOptions { /** * The holdout policy keyed by ISO 3166-1 alpha-2 country code. */ holdouts?: Record; /** * The corpus version the manifest records. * * @defaultValue `""` */ corpusVersion?: string; /** * The retention table applied to every row, when one was registered. */ retention?: SplitRetention; } /** * The `source_id` lists for each split. */ export interface SplitManifest { train: string[]; val: string[]; test: string[]; holdouts: Record; /** * The retention table applied, when one was registered. */ retention?: { floor: number; protectedShapes: number; }; /** * The corpus version the caller supplied. */ corpus_version: string; counts: { train: number; val: number; test: number; total: number; }; } /** * Returns the default holdouts: places below the first-level region in each country, * keyed only by open-vocabulary values, a postcode district or a locality. * A change here takes effect at the next split of the corpus. * * No entry keys on a region. * A region's name is a region span only in rows from that region, so holding a * region out removes it as a label from training, and the model then relabels it * (#2777: v0.8.0 train held 0 region spans for VT, ND, WY, Asturias, Kerala and Cornwall). * * The split holds out places inside regions. * It removes a small share of each affected region and leaves the test split * measuring unseen places under labels the model has seen. * * A selection script chose the district and locality lists below from the v0.8.0 rows of each country: * - Candidates are postcode districts of a fixed length (US 3, AU 3, RO 4, LV and the * French overseas jurisdictions the full postcode), or, where most rows have no postcode, * localities grouped by spelling with accents and punctuation removed. * A locality whose spelling matches a region surface isn't a candidate. * Each candidate accounts for at least 1% of the country's v0.8.0 held-out rows. * - They're walked in the order of `hashBucket(\`${country}|${candidate}\`, 2 ** 32)` * and taken while the country's held-out rows stay at or under its v0.8.0 held-out rows, * because a borderline row goes to training. * A country whose candidates under that bound number fewer than two takes its smallest * candidates up to two, so its held-out set has groups to grade (YT, PF and NC). * Taking also requires that every region with at least {@link HOLDOUT_CAP_MIN_ROWS} * rows loses at most {@link heldOutRegionCap}`(share)` of its rows, and of its rows * with no postcode, where `share` is the country's v0.8.0 held-out share. * - A locality group is held out by every spelling the v0.8.0 rows write. * `held-out-spans.full.test.ts` checks a built corpus against this table. */ export declare function defaultHoldouts(): Record; /** * The component tags {@link splitForRow} reads. * * A reader that rebuilds components from spans needs only these. */ export declare const HOLDOUT_COMPONENT_TAGS: ReadonlySet; /** * One place in this number is held out in a country without a {@link defaultHoldouts} entry. */ export declare const HASHED_PLACE_HOLDOUT_DIVISOR = 50; /** * The held-out share the hashed holdout aims at: one place in {@link HASHED_PLACE_HOLDOUT_DIVISOR}. */ export declare const HASHED_PLACE_HOLDOUT_SHARE: number; /** * {@link heldOutRegionCap} applies to a region with at least {@link HOLDOUT_CAP_MIN_ROWS} rows. * * The same minimum applies to a country when its rows have no region. */ export declare const HOLDOUT_CAP_MIN_ROWS = 100; /** * The largest share of one region's rows a holdout may take, given the country's * held-out share: `min(0.25, max(0.05, 3 × share))`. * * The declared holdouts in {@link defaultHoldouts} were chosen under this bound, and the * hashed holdout trains on a place that exceeds it (see {@link hashedHoldoutTrainedPlaces}). */ export declare function heldOutRegionCap(share: number): number; /** * The rows one hash-selected place contributes inside one region, or inside its country * when those rows have no region. */ export interface HashedPlaceRegionCount { country: string; /** * The key {@link hashedHoldoutPlace} returns. */ place: string; /** * The region under `foldNFKCWhitespace`, or null for rows with no region. */ region: string | null; rows: number; } /** * Returns, per country, the hash-selected places that exceed {@link heldOutRegionCap} * of {@link HASHED_PLACE_HOLDOUT_SHARE} in some region, sorted. * * A place's rows in a region are divided by that region's rows, and its rows with * no region by the country's rows with no region. * A denominator under {@link HOLDOUT_CAP_MIN_ROWS} isn't checked. * * @param denominators Rows per `${country}|${region}`, with an empty region for rows with no region. */ export declare function hashedPlacesOverCap(counts: Iterable, denominators: ReadonlyMap): Record; /** * The hash-selected places trained on rather than held out, because each one exceeds * {@link heldOutRegionCap} of {@link HASHED_PLACE_HOLDOUT_SHARE} of a region of at least * {@link HOLDOUT_CAP_MIN_ROWS} rows (or of its country's rows with no region). * * {@link hashedPlacesOverCap} chose this table from the v0.8.1 rows. * A place that leaves the holdout isn't replaced by another, so the country's held-out * share falls: a borderline row goes to training, as in {@link defaultHoldouts}. * * The cap bounds a place's share of a region. * It leaves a place that dominates one source's held-out rows in the holdout: v0.8.1's * `IN|locality:new delhi` was 0.036% of IN's rows with no region while holding every osm-in New Delhi row. */ export declare function hashedHoldoutTrainedPlaces(): Record; /** * Returns the place the hashed holdout selects for a row of a country without a * {@link defaultHoldouts} entry, before {@link hashedHoldoutTrainedPlaces} is applied, * or null when the hash doesn't select its place. */ export declare function hashSelectedPlace(country: string, components: Partial>): string | null; /** * The key a row is held out by under the hashed holdout: its venue, else its locality, * else the first two characters of its postcode, else its region. * * A venue name keys before its place because the venue recipes write one name both with a locality and bare. * A locality key would hold out the first form and train on the second. * * Names are compared under {@link foldHoldoutName} and postcodes under `compactPostcode`. * The case, comma and spacing augmentations rewrite a row's components as well as its text, * and an unfolded key sends `La Planada` to a different place than `La Planada`. * * Returns null for a row with none of the four. * That row stays in train. */ export declare function hashedHoldoutPlace(components: Partial>): string | null; /** * Returns the matchers for one country, reading a bare array as a region list. */ export declare function holdoutPolicyFor(holdout: CountryHoldout | null): HoldoutPolicy; /** * Whether a country's declared holdout reads postcode prefixes and no region or locality. * * Under such a policy a row's split reads only its record and its postcode digits. * The corpus augmentations change case, punctuation and abbreviations, and drop a ZIP+4 dash. * None of them changes the compacted postcode digits. * * A variant of a training row therefore trains, and a caller may draw a variant's * training share before making it. */ export declare function holdoutReadsOnlyPostcode(holdout: CountryHoldout | null | undefined): boolean; type SplitInputRow = Pick & { /** * The row's ordered span tags, when the caller holds a labeled row. * * Retention keys on them. * A caller without them gets no retention lookup. */ span_tags?: readonly string[] | null; /** * The id of the base row an augmented row was derived from. * * The evaluation-group policy keys on it, as the overlap audit does. */ base_source_id?: string | null; /** * The source, text and spans a keyed source's address key reads (`GROUP_ADDRESS_KEY_SOURCES`). */ address?: AddressKeyedRow | null; }; /** * Returns the split for one row. * * Both `splitRows` and the streaming `buildCorpus` loop call this, so every * caller assigns a row to the same split. * * A row of a combination the retention table covers below the floor returns `train` * before the holdout is consulted (HOLDOUT_AND_EVALUATION section 5.1: training * retention takes precedence over geographic integrity). * * With `evaluationGroups`, the split is the evaluation-group policy * (HOLDOUT_AND_EVALUATION section 4.6), decided by the row's group and no other field: * a frozen group returns `test`, a validation group `val`, and every other row `train`. * The holdout, the trained places and the retention table are then not consulted, * so no retained shape returns a frozen group's variant to train. * * A row of a keyed source whose address key a frozen row holds is frozen too, * so a re-ingested address under a new record id stays out of train (#3125). * * The overlap audit reads `val` as training data, so `evaluationGroupPolicy` * refuses a validation list that holds a frozen group. */ export declare function splitForRow(row: Pick, holdouts?: Record, trainedPlaces?: Record, retention?: SplitRetention, evaluationGroups?: { frozen: FrozenGroups; validation: Pick; }): SplitName; /** * Builds a `SplitManifest` in memory from labeled or canonical rows. * * Tests and small fixtures use this, while `buildCorpus` uses `splitForRow` * and `writeSplitManifestsFromLabeledFiles` so it never holds every row's split in memory. */ export declare function splitRows(rows: Iterable, opts?: SplitOptions): SplitManifest; /** * Returns a deterministic bucket in `0..n-1` for an id, from the first four bytes * of the SHA-256 digest read as a big-endian uint32. */ export declare function hashBucket(id: string, n: number): number; /** * The first four bytes of an id's SHA-256 digest read as a big-endian uint32, * the value {@link hashBucket} reduces and the validation quota orders groups by. */ export declare function hashUint32(id: string): number; /** * Writes a `SplitManifest` to `/{train,val,test}.txt` (sorted `source_id` values, one per line) * and `SPLIT_MANIFEST.json` (the corpus version, holdouts and counts). */ export declare function writeSplitManifests(manifest: SplitManifest, outputDir: PathBuilderLike): Promise; /** * The `LabeledRow` fields that the split functions read. */ export type SplitInputLabeledRow = Pick; /** * Writes the same files as `writeSplitManifests` from the rows' `source_id` values. * * `buildCorpus` calls this after its align loop partitions rows with `splitForRow`. * The caller passes the counts to avoid rescanning the files. * `sort(1)` spills to disk, so memory stays constant. * * Each split's ids come from the first of these the caller supplies: * * - `sortedSourceIDPaths`: lists already sorted by byte, merged by `sort -m`. * A build that aligned its sources apart passes each source's sorted list. * - `sourceIDPaths`: one id per line in the order align wrote them, read by `sort` directly. * - `labeledPaths`, the labeled JSONL itself, parsed for each row's `source_id`. * An align phase resumed from a checkpoint written before align recorded ids leaves only this. * * The three write the same bytes from the same rows, because each feeds `sort` the same multiset of ids. */ export declare function writeSplitManifestsFromLabeledFiles(opts: { labeledPaths: Record; sourceIDPaths?: Record | null; sortedSourceIDPaths?: Record | null; outputDir: PathBuilderLike; corpusVersion: string; counts: Record; holdouts?: Record; retention?: SplitRetention; }): Promise; /** * Sort the lines of `inputs` by byte into `outPath` with `sort(1)`, which spills to disk. * * With `merge`, each input must already be sorted by byte, and `sort -m` merges them without sorting again. * Duplicate lines are kept either way, and zero inputs write an empty file. */ export declare function sortLines(inputs: readonly PathBuilderLike[], outPath: PathBuilderLike, options?: { merge?: boolean; }): Promise; export {}; //# sourceMappingURL=split.d.ts.map