# `@mailwoman/postcode` Postcode evidence: what a postcode's form admits, and readers for the binary postcode artifacts. The package depends on `@mailwoman/codex` and `@mailwoman/core`. It reads no file: the binary readers take a `Uint8Array`, and the JSON tables are imported as modules. ## Root export `@mailwoman/postcode` exports the form checks: - `findPostcodeSpans` (`@mailwoman/postcode/postal-spans`) finds the spans of a line that have a postcode's form in any country, from the `postal` family. It matches either case, and the training encoder reads the same artifact. `POSTCODE_SHAPES`, `WHOLE_POSTCODE_SHAPES` and `wholePostcodeShape` are the frozen legacy table in `frozen-shapes.json`, kept for the published model's `shaped` span mode and postcode repair until the first model that declares `span_mode: "postal"` ships. - `acceptPostcode`, `countriesAcceptingPostcode` and `hasPostcodeForm` check a whole value against a country's postcode syntax. The `postal` family's artifact answers them. The generator at `packages/mailwoman/tools/dev-tools/labels/postcode/country-formats.ts` reads Google's libaddressinput tables. - `subregionsForPostcode` and `statesSubregionPrefixes` read libaddressinput's `sub_zips` prefixes from `subregions.json`, generated by the same tool. - `countryFromPostcodeFormat` and `countriesFromPostcodeFormat` return the countries a whole postcode's form implies. - `candidateSystemsForPostcode` returns the codex address systems whose own postcode normalizer accepts a value. `isUnitGradePostcodeHit` and `areaPostcodeLeadsLocality` sit in the same module. - `normalizePostcodeDisplay` collapses whitespace and uppercases a value to the single-space display form. ```ts import { countriesFromPostcodeFormat, subregionsForPostcode } from "@mailwoman/postcode" countriesFromPostcodeFormat("110 00") // ["CZ", "SK", "SE", "GR"] subregionsForPostcode("35203", "US") // [{ key: "AL", isoid: "AL" }] ``` ## Subpath exports Every module under `lib/` is importable as `@mailwoman/postcode/`. Three of them are not in the root export: - `@mailwoman/postcode/binary-resolver` holds `PostcodeBinaryLookup`, which reads a PCB1 postcode binary from bytes and looks up the places recorded for a postcode, and `serializePostcodeBinary`, which writes one. - `@mailwoman/postcode/prefix-index` holds `PostcodePrefixIndexLookup`, which reads a PFX1 prefix index from bytes and probes a prefix for its weighted ancestry, and `serializePostcodePrefixIndex`, which writes one. - `@mailwoman/postcode/recognizer/legacy` holds `postcodeLegacy`, a family of `@mailwoman/recognizer` that fills the `knownFormats` field with the postcode and PO box formats the parse pipeline reads: the thirteen legacy names, their confidences, spans and order. The `structure` family supplies the tokens that `postcodeLegacy` reads. Compose the two families. A composition without `structure` throws a `RecognizerCompositionError` at construction. The family reports no recognitions of its own. `@mailwoman/postcode/recognizer/query-shape` composes the two once and exports `computeQueryShape`, which the pipeline's stages call. ```ts const { fields } = createRecognizer([structure, postcodeLegacy]).recognize("SW1A 1AA") fields.knownFormats // [{ format: "uk_postcode", span: { start: 0, end: 8, body: "SW1A 1AA" }, confidence: 0.9 }] ``` The table is `lib/legacy-formats/table.ts`: each legacy format with its country, pattern, token count and confidence, and the PO box leaders of each language. `node packages/postcode/tools/generate.run.ts` compiles it with `recognizer-build --definition`, and `--check` fails when a committed file differs. The patterns are the thirteen legacy patterns, so wider recognition from every country's syntax is a separate change. `isPostcodeFormat` from `@mailwoman/core/pipeline/types` is the predicate for these names. `@mailwoman/postcode/recognizer/postal` holds `postal`, a second family, in a module of its own so that a bundle of one family holds no byte of the other's artifact. The family is generated from the syntax of every country that `postcodeSyntaxCountries` lists. A recognition of `postal` reports that a string has the form of a postcode in some countries. The recognition leaves open whether the code is allocated, and PCB1 and PFX1 membership stay with the dictionary readers. There's one shape for each distinct language, so every country that writes four digits shares one shape, and the shape's metadata lists the countries. A country whose layout prints a prefix, such as `〒` for Japan, `L-` for Luxembourg and `AZ ` for Azerbaijan, has a second `printed.` shape for the prefix, any white space and the code, and `splitPrintedPostcode` separates the two. ```ts const text = "12345 Main Street" const candidates = postalCandidates(createRecognizer([postal]).recognize(text).recognitions) candidates[0].countries // every country that writes five digits, as a CountrySet ``` `acceptingCountries(recognizer, text)` equals `countriesAcceptingPostcode(text)`. Each shape declares `boundary: "token"`. The engine reports an embedded match only when no ASCII letter, ASCII digit or connector touches it. `lib/recognizer/postal/readers.ts` states the rule. `postalCountries` returns a `CountrySet`, which cannot hold Ascension Island, Tristan da Cunha or Kosovo, so `postalCountryCodes` returns every code. The generator asks the compiler which countries share a language and which shapes overlap, and the build fails on an overlap that no shape declares. ## Three kinds of evidence The package answers three different questions about a postcode, and each answer rests on its own kind of evidence: - **Syntax:** does a country's postcode grammar accept this text? `acceptPostcode` answers from libaddressinput's patterns plus `lib/country-formats/written-forms.ts`. That file holds the written forms the generated data leaves out, such as the US ZIP+4 written without its dash. A syntax match means the text could be a postcode. Whether the code is allocated is a membership question. `canComplete` asks the same question of a partial postcode: can some suffix make the text a whole postcode, and in which candidate countries? The engine's prefix status of the country's `postal` shapes answers it, from the same grammar that matches a whole value. - **Membership:** is this code, or a code beginning with this prefix, one the data has seen? PCB1 answers for exact codes and PFX1 for prefixes, each with its own coverage. An unobserved code inside a country's coverage is different from a code outside it. Given PFX1 indexes, `canComplete` reports membership in its own `membership` field (`observed`, `unobserved` or `out-of-coverage`, with each index's scope and coverage note), and that field never changes the syntax answer. - **Geography:** which places does the code point to? PCB1 records candidate places for a code, and PFX1 records weighted ancestors for a prefix. A character prefix isn't geographic containment, so a prefix match is evidence about the code's region only, and the place a full code names needs an exact PCB1 match. A consumer keeps these apart. A five-digit token is syntactically valid in several countries at once (`110 00` matches CZ, SK, SE and GR), so a syntax answer is a set of countries, never one country. ## Where postcode evidence enters a parse The neural model decides which tokens are the postcode. The package never overrides that decision at decode time. A decode-time bias toward syntax-accepted spans was measured on the regression board and held back (#2681). A bias small enough to change no other board row also left every row it was meant to fix with its old label, because the model's margin on those tokens exceeds it. Larger biases regressed rows, for example by reading Ireland as an Eircode or by pushing a road number away from `13 Mile Road`. A postcode's position is learned instead from training data, including the training-time move that copies a row's postcode across its admin tail (#2750). The package's evidence enters after the parse, in the resolver. `applyPostcodeShapeCoherence`, switched by `postcodeShapeCoherence` and on by default, checks the postcode span against the syntax of every country or region the parse names: - A span one of those countries accepts is confirmed, and the accepting countries narrow the postcode lookup. - A span none of them accepts is excluded. It keeps its tag and its text, and the postcode slot in the result stays empty. It never becomes a house number, and it never overwrites a house number the parse found. - A parse that identifies no country leaves the span as it is. The check removes a wrong postcode. It never invents one, and it never relabels a span as something else. No autocomplete path calls `canComplete` yet. The FST autocomplete in `packages/mailwoman/lib/autocomplete/core.ts` completes place names and never asks whether a postcode prefix can complete.