@mailwoman/resolver 10.0.0 → 10.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -7
- package/lib/admin/coherence-passes.ts +19 -160
- package/lib/admin/containment.ts +12 -30
- package/lib/admin/jp-municipality.ts +20 -13
- package/lib/admin/winner.ts +30 -62
- package/lib/backend-capabilities.ts +25 -24
- package/lib/bare-toponym-race.ts +69 -37
- package/lib/decorate-node.ts +17 -41
- package/lib/fold-name.ts +5 -18
- package/lib/index.ts +5 -9
- package/lib/plausibility.ts +35 -62
- package/lib/postcode/country-coherence.ts +64 -284
- package/lib/postcode/prefix.ts +20 -84
- package/lib/postcode/shape-coherence.ts +23 -113
- package/lib/remote-resolver.ts +32 -29
- package/lib/rerank.ts +34 -50
- package/lib/resolve/passes.ts +144 -147
- package/lib/resolve.ts +726 -0
- package/lib/span-rescore.ts +215 -172
- package/lib/street/evidence.ts +65 -73
- package/lib/street/tier.ts +33 -175
- package/lib/toponym-prior.ts +53 -179
- package/out/admin/coherence-passes.d.ts +17 -68
- package/out/admin/coherence-passes.d.ts.map +1 -1
- package/out/admin/coherence-passes.js +18 -160
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/containment.d.ts +11 -29
- package/out/admin/containment.d.ts.map +1 -1
- package/out/admin/containment.js +12 -30
- package/out/admin/containment.js.map +1 -1
- package/out/admin/jp-municipality.d.ts +20 -13
- package/out/admin/jp-municipality.d.ts.map +1 -1
- package/out/admin/jp-municipality.js +18 -12
- package/out/admin/jp-municipality.js.map +1 -1
- package/out/admin/winner.d.ts +30 -62
- package/out/admin/winner.d.ts.map +1 -1
- package/out/admin/winner.js +18 -51
- package/out/admin/winner.js.map +1 -1
- package/out/{admin/index.d.ts → admin.d.ts} +1 -1
- package/out/admin.d.ts.map +1 -0
- package/out/{admin/index.js → admin.js} +1 -1
- package/out/admin.js.map +1 -0
- package/out/backend-capabilities.d.ts +18 -19
- package/out/backend-capabilities.d.ts.map +1 -1
- package/out/backend-capabilities.js +25 -24
- package/out/backend-capabilities.js.map +1 -1
- package/out/bare-toponym-race.d.ts +54 -27
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/bare-toponym-race.js +69 -37
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts +6 -8
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +17 -41
- package/out/decorate-node.js.map +1 -1
- package/out/fold-name.d.ts +5 -18
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +5 -18
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +5 -6
- package/out/index.d.ts.map +1 -1
- package/out/index.js +5 -8
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +27 -57
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +16 -49
- package/out/plausibility.js.map +1 -1
- package/out/postcode/country-coherence.d.ts +48 -162
- package/out/postcode/country-coherence.d.ts.map +1 -1
- package/out/postcode/country-coherence.js +19 -252
- package/out/postcode/country-coherence.js.map +1 -1
- package/out/postcode/prefix.d.ts +17 -68
- package/out/postcode/prefix.d.ts.map +1 -1
- package/out/postcode/prefix.js +8 -77
- package/out/postcode/prefix.js.map +1 -1
- package/out/postcode/shape-coherence.d.ts +17 -84
- package/out/postcode/shape-coherence.d.ts.map +1 -1
- package/out/postcode/shape-coherence.js +9 -105
- package/out/postcode/shape-coherence.js.map +1 -1
- package/out/{postcode/index.d.ts → postcode.d.ts} +1 -1
- package/out/postcode.d.ts.map +1 -0
- package/out/{postcode/index.js → postcode.js} +1 -1
- package/out/postcode.js.map +1 -0
- package/out/remote-resolver.d.ts +27 -28
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +9 -19
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +28 -45
- package/out/rerank.d.ts.map +1 -1
- package/out/rerank.js +13 -38
- package/out/rerank.js.map +1 -1
- package/out/resolve/passes.d.ts +95 -85
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +40 -105
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve.d.ts +15 -0
- package/out/resolve.d.ts.map +1 -0
- package/out/resolve.js +543 -0
- package/out/resolve.js.map +1 -0
- package/out/span-rescore.d.ts +51 -64
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +132 -130
- package/out/span-rescore.js.map +1 -1
- package/out/street/evidence.d.ts +56 -61
- package/out/street/evidence.d.ts.map +1 -1
- package/out/street/evidence.js +22 -46
- package/out/street/evidence.js.map +1 -1
- package/out/street/tier.d.ts +17 -29
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +27 -175
- package/out/street/tier.js.map +1 -1
- package/out/{street/index.d.ts → street.d.ts} +1 -1
- package/out/street.d.ts.map +1 -0
- package/out/{street/index.js → street.js} +1 -1
- package/out/street.js.map +1 -0
- package/out/toponym-prior.d.ts +21 -109
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +41 -170
- package/out/toponym-prior.js.map +1 -1
- package/package.json +44 -147
- package/lib/resolve/index.ts +0 -989
- package/out/admin/index.d.ts.map +0 -1
- package/out/admin/index.js.map +0 -1
- package/out/postcode/index.d.ts.map +0 -1
- package/out/postcode/index.js.map +0 -1
- package/out/resolve/index.d.ts +0 -20
- package/out/resolve/index.d.ts.map +0 -1
- package/out/resolve/index.js +0 -808
- package/out/resolve/index.js.map +0 -1
- package/out/street/index.d.ts.map +0 -1
- package/out/street/index.js.map +0 -1
- package/vitest.config.ts +0 -38
- /package/lib/{admin/index.ts → admin.ts} +0 -0
- /package/lib/{postcode/index.ts → postcode.ts} +0 -0
- /package/lib/{street/index.ts → street.ts} +0 -0
package/lib/postcode/prefix.ts
CHANGED
|
@@ -2,85 +2,28 @@
|
|
|
2
2
|
* @copyright Sister Software
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Postcode-prefix prior (#31, Mechanism 3) — the partial-code prior for postcodes the full-code
|
|
7
|
-
* gazetteer does not carry. #1480's abstention is the defect: a BT unit with no permissive source
|
|
8
|
-
* behind it (the NI half of Code-Point Open) misses the gazetteer, and the tree contributes
|
|
9
|
-
* NOTHING for it — no country scope, no district, no coordinate. This module derives the code's
|
|
10
|
-
* PREFIX and probes the PFX1 index (`postcode-prefix-<cc>.bin`), so the resolve "abstains on the
|
|
11
|
-
* UNIT and still contributes its DISTRICT": the node resolves from the prefix's centroid and/or
|
|
12
|
-
* ancestry, whichever the artifact carries.
|
|
13
|
-
*
|
|
14
|
-
* The index is a structural type (`PostcodePrefixIndexLike`, declared in `core/resolver/types.ts`),
|
|
15
|
-
* injected by the pipeline and never imported from `@mailwoman/neural` — B3-5: the probe touches
|
|
16
|
-
* zero model inputs. The loader lives in `@mailwoman/neural/postcode-prefix-index.ts` (PFX1 format:
|
|
17
|
-
* magic, header, dictionary, quantized i16 coordinates, `radiusP95Km`, ancestry); the resolver
|
|
18
|
-
* side only defines the derivation law and the probe.
|
|
19
|
-
*
|
|
20
|
-
* ## The derivation law (per artifact country)
|
|
21
|
-
*
|
|
22
|
-
* - **GB** — the OUTWARD code: the compact (whitespace-stripped) form minus its trailing 3 unit
|
|
23
|
-
* characters ("SW1A 2AA" → "SW1A", "BT9 5GS" → "BT9"). Never a greedy regex — the unit is
|
|
24
|
-
* exactly 3 chars in the compact form, and `outwardOf` in `mailwoman/gazetteer-pipeline/
|
|
25
|
-
* postcode-prefix.ts` is the build-side twin of this law.
|
|
26
|
-
* - **US** — the 3-digit section ("94043" → "940"). M-3 measured 3 digits as the first length
|
|
27
|
-
* where the ZIP prefix stops being free (145 km p95 median); a 5-digit artifact would be the
|
|
28
|
-
* full code the gazetteer already carries. No US artifact ships yet (B3-4 needs a ZCTA
|
|
29
|
-
* acquisition) — the branch exists so the law is tested before the data.
|
|
30
|
-
* - **Anything else** — abstain (null).
|
|
31
|
-
*
|
|
32
|
-
* ## The probe's country restriction
|
|
33
|
-
*
|
|
34
|
-
* The index is country-specific EVIDENCE: it is only probed when its `country` matches the
|
|
35
|
-
* query's country scope (or the scope is absent). A GB index must not speak under a US scope —
|
|
36
|
-
* the pipeline passes the locale's index, and the walk's country filter is the caller's declared
|
|
37
|
-
* universe.
|
|
38
|
-
*
|
|
39
|
-
* ## Metadata contract (what the node carries afterward)
|
|
40
|
-
*
|
|
41
|
-
* - `postcode_prefix` — the prefix string that resolved it.
|
|
42
|
-
* - `postcode_prefix_ancestors` — the ancestry the prefix asserts (coarsest-first; GB outward
|
|
43
|
-
* nodes carry country → constituent-country → district).
|
|
44
|
-
* - `postcode_prefix_radius_p95_km` — the artifact's measured p95 radius, when the node carries a
|
|
45
|
-
* coordinate (M-3's receipt: a 1-digit US band and a GB outward code differ by 200×; never read
|
|
46
|
-
* a coordinate without its radius).
|
|
47
|
-
* - `coordinate_source: "postcode_prefix"` — ONLY when the node actually carries a coordinate.
|
|
48
|
-
* The ancestry-only tier (NI's 80 BT districts) stays coordinate-free — the meaning-of-zero
|
|
49
|
-
* rule, and the half that decides B3-3: inventing a BT centroid would reproduce the `BT3 9QQ` →
|
|
50
|
-
* Sheffield defect #1480 just fixed.
|
|
51
|
-
*
|
|
52
|
-
* **D-rule: opt-in behind `ResolveOpts.postcodePrefixPrior`, default-OFF** (the PCN1 posture:
|
|
53
|
-
* data + loader + offline probe, no decode wiring; the header ships without `delta` until a
|
|
54
|
-
* calibration measures one). Bars: B3-2 (≥60% of held-out units within 10 km, zero worse than the
|
|
55
|
-
* abstention arm), B3-3 (NI ≥95% country scope GB + NIR ancestry + correct district named, 0%
|
|
56
|
-
* coordinate), B3-5 (structural — no model inputs touched).
|
|
57
5
|
*/
|
|
58
6
|
|
|
59
7
|
import type { PostcodePrefixIndexLike, PostcodePrefixNode, ResolvedPlace } from "@mailwoman/core/resolver"
|
|
60
8
|
|
|
61
9
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
10
|
+
* Describes a resolved place whose coordinate may be absent, such as a
|
|
11
|
+
* postcode-prefix hit with ancestry only.
|
|
12
|
+
*
|
|
13
|
+
* A missing coordinate stays `undefined`.
|
|
14
|
+
* An invented centroid would place the postcode at a point the data does not support.
|
|
67
15
|
*/
|
|
68
16
|
export type CoordinateOptionalPlace = Omit<ResolvedPlace, "lat" | "lon"> & { lat?: number; lon?: number }
|
|
69
17
|
|
|
70
|
-
/**
|
|
71
|
-
* The MINIMUM compact code length for a GB outward derivation — a shorter code has no 3-character unit to strip ("B3"
|
|
72
|
-
* is a GB area, not a unit-bearing code).
|
|
73
|
-
*/
|
|
74
18
|
const MIN_GB_OUTWARD_CODE_LENGTH = 5
|
|
75
19
|
|
|
76
|
-
/**
|
|
77
|
-
* The MINIMUM code length for a US section derivation.
|
|
78
|
-
*/
|
|
79
20
|
const MIN_US_SECTION_CODE_LENGTH = 3
|
|
80
21
|
|
|
81
22
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
23
|
+
* Derives the key a postcode prefix index uses for `code`: the GB outward code
|
|
24
|
+
* or the US three-digit prefix.
|
|
25
|
+
*
|
|
26
|
+
* @returns `null` for a country with no rule or a code too short to contain a prefix.
|
|
84
27
|
*/
|
|
85
28
|
export function derivePostcodePrefix(code: string, country?: string): string | null {
|
|
86
29
|
if (!code || !country) return null
|
|
@@ -88,13 +31,9 @@ export function derivePostcodePrefix(code: string, country?: string): string | n
|
|
|
88
31
|
const compact = code.replaceAll(/\s+/g, "")
|
|
89
32
|
|
|
90
33
|
switch (country.toUpperCase()) {
|
|
91
|
-
// The outward-code law: compact minus the trailing 3 unit characters ("SW1A2AA" → "SW1A",
|
|
92
|
-
// "BT93GS" → "BT9"). Exactly 3 — never a greedy regex, the build-side twin is `outwardOf`.
|
|
93
34
|
case "GB":
|
|
94
35
|
return compact.length >= MIN_GB_OUTWARD_CODE_LENGTH ? compact.slice(0, -3) : null
|
|
95
|
-
|
|
96
|
-
// requires a real ZIP shape: a probe for something that is not a code simply misses, and a reader
|
|
97
|
-
// that re-derived the writer's validation would be a second copy of it.
|
|
36
|
+
|
|
98
37
|
case "US":
|
|
99
38
|
return compact.length >= MIN_US_SECTION_CODE_LENGTH ? compact.slice(0, 3) : null
|
|
100
39
|
default:
|
|
@@ -107,19 +46,21 @@ export function derivePostcodePrefix(code: string, country?: string): string | n
|
|
|
107
46
|
*/
|
|
108
47
|
export interface PostcodePrefixProbeResult {
|
|
109
48
|
/**
|
|
110
|
-
* The prefix
|
|
49
|
+
* The prefix derived from the code that matched an index entry.
|
|
111
50
|
*/
|
|
112
51
|
prefix: string
|
|
52
|
+
|
|
113
53
|
/**
|
|
114
|
-
* The index node
|
|
54
|
+
* The matched index node.
|
|
55
|
+
*
|
|
56
|
+
* Its ancestors and optional centroid and radius supply the prior.
|
|
115
57
|
*/
|
|
116
58
|
node: PostcodePrefixNode
|
|
117
59
|
}
|
|
118
60
|
|
|
119
61
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* abstain — never throws, never guesses.
|
|
62
|
+
* Looks up `code`'s prefix in the index, returning `null` when the query's country
|
|
63
|
+
* differs from the index's or no indexed prefix can be derived.
|
|
123
64
|
*/
|
|
124
65
|
export function probePostcodePrefix(
|
|
125
66
|
code: string,
|
|
@@ -128,8 +69,6 @@ export function probePostcodePrefix(
|
|
|
128
69
|
): PostcodePrefixProbeResult | null {
|
|
129
70
|
const indexCountry = index.country?.toUpperCase()
|
|
130
71
|
|
|
131
|
-
// Country check: the index is evidence FOR its own country only. A GB index under a US scope stays
|
|
132
|
-
// silent — the walk's country filter is the caller's declared universe.
|
|
133
72
|
if (queryCountry && indexCountry && queryCountry.toUpperCase() !== indexCountry) return null
|
|
134
73
|
|
|
135
74
|
const prefix = derivePostcodePrefix(code, indexCountry)
|
|
@@ -142,10 +81,8 @@ export function probePostcodePrefix(
|
|
|
142
81
|
}
|
|
143
82
|
|
|
144
83
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* present ONLY when the node carries one, so an ancestry-only hit stays coordinate-free by construction (B3-3's 0%
|
|
148
|
-
* half).
|
|
84
|
+
* Builds the synthetic `postalcode` place for a prefix hit, with `id: 0` because it is not a gazetteer row.
|
|
85
|
+
* It includes a coordinate only when the prefix node has one.
|
|
149
86
|
*/
|
|
150
87
|
export function postcodePrefixResolvedPlace(
|
|
151
88
|
prefix: string,
|
|
@@ -156,8 +93,7 @@ export function postcodePrefixResolvedPlace(
|
|
|
156
93
|
id: 0,
|
|
157
94
|
name: prefix,
|
|
158
95
|
placetype: "postalcode",
|
|
159
|
-
|
|
160
|
-
// whose country_id resolves to nothing.
|
|
96
|
+
|
|
161
97
|
country: index.country?.toUpperCase() ?? "",
|
|
162
98
|
...(node.lat !== undefined && node.lon !== undefined ? { lat: node.lat, lon: node.lon } : {}),
|
|
163
99
|
score: 0,
|
|
@@ -2,76 +2,6 @@
|
|
|
2
2
|
* @copyright Sister Software
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Postcode-shape coherence (#31, Mechanism 1) — shape as CONFIDENCE and EXCLUSION, downstream of the
|
|
7
|
-
* siblings. The fifth member of the joint-consistency coherence family (after `applyAdminCoherence`
|
|
8
|
-
* #263, `applyExplicitCountryCoherence` #822, `applyRegionCountryCoherence`, and
|
|
9
|
-
* `findPostcodeCountryScope` #42/#1477), and the only one that is pure-sync and backend-free:
|
|
10
|
-
* everything it needs is `candidateSystemsForPostcode` plus the tree.
|
|
11
|
-
*
|
|
12
|
-
* The defect (M-1, `docs/superpowers/plans/2026-08-05-postcode-structure-arc.md`): the decoder
|
|
13
|
-
* sometimes tags a HOUSE NUMBER as `postcode` when its digits form a foreign postcode shape —
|
|
14
|
-
* "1200" in "Twin Peaks Golf Course, 1200 Cornell Dr, Longmont, CO 80503" is accepted only by the
|
|
15
|
-
* AU/NZ 4-digit shape, while every sibling placetype says US. The shape alone cannot say a code is
|
|
16
|
-
* foreign (49/100 Gauntlet codes are accepted by more than one system, 10 by none), so this pass
|
|
17
|
-
* never reads the shape as country evidence by itself — it INTERSECTS each span's candidate systems
|
|
18
|
-
* with the systems the SIBLINGS already assert, and only a confident sibling set can demote.
|
|
19
|
-
*
|
|
20
|
-
* ## The rule (three outcomes, per span)
|
|
21
|
-
*
|
|
22
|
-
* 1. **Intersection non-empty → CONFIRMED.** Stamp `postcode_shape_systems` (the narrowed
|
|
23
|
-
* intersection, upper-case). Additive metadata only — resolution is byte-identical (B1-1). The
|
|
24
|
-
* same intersection narrows `findPostcodeCountryScope`'s candidate list when the caller threads
|
|
25
|
-
* it (a pure subset of codex's shape candidates — safe by construction).
|
|
26
|
-
* 2. **Intersection empty + confident siblings → EXCLUDED.** A digit-only span is demoted to
|
|
27
|
-
* `house_number` (the correct sibling tag — B1-2's "correct sibling tag surviving"); a
|
|
28
|
-
* letter-bearing span keeps its tag and gets `postcode_shape_excluded: true` instead (the
|
|
29
|
-
* compound-split corner — "15 07691" — is #942 postal-compound-recovery territory, out of
|
|
30
|
-
* scope). Either way the span's contribution to the resolve is stripped: `firstPostcodeValue`,
|
|
31
|
-
* the walk's postcode lookup, and the post-walk postcode passes all skip excluded spans.
|
|
32
|
-
* 3. **No confident siblings → ABSTAIN** (the `postcode-country-coherence.ts:269` posture
|
|
33
|
-
* verbatim). A shape no codex system recognizes (the 10/110 slice-less codes) also abstains —
|
|
34
|
-
* an empty candidate set is no evidence either way.
|
|
35
|
-
*
|
|
36
|
-
* ## What counts as a sibling signal
|
|
37
|
-
*
|
|
38
|
-
* - The **country node**, via `matchCountry`, territory-mapped: PR/VI/GU/MP/AS are USPS
|
|
39
|
-
* abbreviations (`codex/us/state.ts`), so a "Puerto Rico" token speaks for `us` — the mapping
|
|
40
|
-
* that protects the true postcode in "Ponce, 00716, Puerto Rico".
|
|
41
|
-
* - The **region node**, via `matchSubdivision` (US states + CA provinces; the US-first tiebreak
|
|
42
|
-
* that makes "CA" mean California is `matchSubdivision`'s own), plus the region's
|
|
43
|
-
* `country_hint` metadata stamp (`mailwoman/region-recognition.ts` writes it on 2-letter US
|
|
44
|
-
* state abbreviations).
|
|
45
|
-
* - Signals are filtered to the codex SystemCode universe BEFORE the intersection test. A country
|
|
46
|
-
* with no codex slice (ES, MX, IE, …) can never appear in any candidate set, so an unfiltered
|
|
47
|
-
* signal would make every intersection empty and every span "foreign" — the slice-less filter
|
|
48
|
-
* is the whole reason the JP-shaped "15 07691" span in the ES Portopetro row ABSTAINS rather
|
|
49
|
-
* than false-excludes.
|
|
50
|
-
*
|
|
51
|
-
* Deliberately NOT a signal: a second postcode span. The census's "cross-span" idea fails on the
|
|
52
|
-
* symmetric case — "2000 Sydney NSW, SW1A 2AA London" would exclude BOTH true codes (their systems
|
|
53
|
-
* are disjoint), a regression worse than the defect. Cross-country multi-postcode strings are
|
|
54
|
-
* pathological, and every M-1 span carries a country/region/`country_hint` signal instead.
|
|
55
|
-
*
|
|
56
|
-
* `defaultCountry` is never a signal: it is a LOCALE DEFAULT, not knowledge. B1-3's confound —
|
|
57
|
-
* "Sydney NSW 2000, Australia" reached with a US default must not have its 2000 excluded, and "10
|
|
58
|
-
* Downing Street, London SW1A 2AA" under a US default must not either. Those rows are exactly what
|
|
59
|
-
* `findPostcodeCountryScope` exists to rescue, and an exclusion pass that trusted the default
|
|
60
|
-
* would delete the evidence the country pass needs.
|
|
61
|
-
*
|
|
62
|
-
* ## Evidence posture
|
|
63
|
-
*
|
|
64
|
-
* M-1's six Gauntlet exclusion spans grade 4 exclusions (US "1600"/"3080"/"1200" via their region,
|
|
65
|
-
* PR "3499" via the territory-mapped country) + 2 documented abstentions (MX "2000" — no country
|
|
66
|
-
* token, "Tabasco" is not a `matchSubdivision` key; ES "15 07691" — no ES slice). Within-country,
|
|
67
|
-
* the exclusion problem is close to empty on the curated board, and a 5-digit house number in a
|
|
68
|
-
* DE/FR address is shape-native — the shape cannot exclude it (M-1 finding #1), so the mechanism
|
|
69
|
-
* CONFIRMS it instead.
|
|
70
|
-
*
|
|
71
|
-
* **D-rule: opt-in behind `ResolveOpts.postcodeShapeCoherence`, default-OFF.** Demotion is the
|
|
72
|
-
* failure mode with teeth, so a default-on promotion needs the full pre-registered eval (B1-1
|
|
73
|
-
* byte-stability, B1-2 exclusion ≥90% with the correct sibling tag surviving, B1-3 confound ≤2%
|
|
74
|
-
* false exclusions; kill on any B1-3 δ).
|
|
75
5
|
*/
|
|
76
6
|
|
|
77
7
|
import { candidateSystemsForPostcode, SYSTEM_CODES } from "@mailwoman/codex"
|
|
@@ -79,45 +9,37 @@ import { matchCountry, matchSubdivision } from "@mailwoman/codex/country"
|
|
|
79
9
|
import { isUSStateAbbreviation } from "@mailwoman/codex/us"
|
|
80
10
|
import { collectNodes, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
81
11
|
|
|
82
|
-
/**
|
|
83
|
-
* The codex address systems a sibling signal can speak for — the universe `candidateSystemsForPostcode` can return, in
|
|
84
|
-
* the upper-case ISO form this module's signals are emitted in (`SystemCode` itself is lower-case). Signals from
|
|
85
|
-
* countries with no codex slice are filtered out BEFORE the intersection test, so a slice-less country can never
|
|
86
|
-
* manufacture an empty intersection (the false-exclusion trap; see the header). Derived from codex's own list so a
|
|
87
|
-
* system added there is admitted here in the same change.
|
|
88
|
-
*/
|
|
89
12
|
const SYSTEM_UNIVERSE: ReadonlySet<string> = new Set<string>(SYSTEM_CODES.map((system) => system.toUpperCase()))
|
|
90
13
|
|
|
91
14
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
15
|
+
* Records which postcode spans {@link applyPostcodeShapeCoherence} confirmed, excluded, or abstained on.
|
|
16
|
+
*
|
|
17
|
+
* It also records the postcode systems of the first confirmed span.
|
|
94
18
|
*/
|
|
95
19
|
export interface PostcodeShapeVerdict {
|
|
96
20
|
/**
|
|
97
|
-
* The
|
|
98
|
-
*
|
|
99
|
-
* confirmed.
|
|
21
|
+
* The upper-case postcode systems of the first confirmed span, passed to
|
|
22
|
+
* `findPostcodeCountryScope` as its candidate systems; `undefined` when no span was confirmed.
|
|
100
23
|
*/
|
|
101
24
|
narrowing?: string[]
|
|
25
|
+
|
|
102
26
|
/**
|
|
103
|
-
*
|
|
27
|
+
* Postcode values whose shape fits a system used by a sibling country or region span.
|
|
104
28
|
*/
|
|
105
29
|
confirmed: string[]
|
|
30
|
+
|
|
106
31
|
/**
|
|
107
|
-
*
|
|
32
|
+
* Postcode values whose shape fits none of the systems the siblings name.
|
|
108
33
|
*/
|
|
109
34
|
excluded: string[]
|
|
35
|
+
|
|
110
36
|
/**
|
|
111
|
-
*
|
|
37
|
+
* Postcode values left undecided because they match no known postcode shape
|
|
38
|
+
* or the tree names no country or region.
|
|
112
39
|
*/
|
|
113
40
|
abstained: string[]
|
|
114
41
|
}
|
|
115
42
|
|
|
116
|
-
/**
|
|
117
|
-
* Collect the sibling country signals: the country node (territory-mapped), the region node (`matchSubdivision` +
|
|
118
|
-
* `country_hint`), each filtered to the codex SystemCode universe. The tree is walked once for ALL postcode spans —
|
|
119
|
-
* sibling evidence is tree-level, not per-span.
|
|
120
|
-
*/
|
|
121
43
|
function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
|
|
122
44
|
const out = new Set<string>()
|
|
123
45
|
|
|
@@ -126,8 +48,7 @@ function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
|
|
|
126
48
|
const matched = matchCountry(n.value)
|
|
127
49
|
|
|
128
50
|
if (!matched) continue
|
|
129
|
-
|
|
130
|
-
// so a US-territory country token ("Puerto Rico", "Guam") is a US-system signal.
|
|
51
|
+
|
|
131
52
|
const system = isUSStateAbbreviation(matched.iso2) ? "US" : matched.iso2.toUpperCase()
|
|
132
53
|
|
|
133
54
|
if (SYSTEM_UNIVERSE.has(system)) {
|
|
@@ -138,9 +59,6 @@ function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
|
|
|
138
59
|
}
|
|
139
60
|
|
|
140
61
|
if (n.tag === "region") {
|
|
141
|
-
// (a) The region's value as a subdivision ("CA" → US, "ON" → CA — the US-wins tiebreak for
|
|
142
|
-
// "CA" is matchSubdivision's own). (b) The `country_hint` stamp annotateUSRegions writes on
|
|
143
|
-
// 2-letter US state abbreviations — the same evidence via the pipeline's other path.
|
|
144
62
|
const sub = matchSubdivision(n.value)
|
|
145
63
|
|
|
146
64
|
if (sub && SYSTEM_UNIVERSE.has(sub.country.toUpperCase())) {
|
|
@@ -159,16 +77,16 @@ function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
|
|
|
159
77
|
}
|
|
160
78
|
|
|
161
79
|
/**
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
80
|
+
* Checks each postcode span's shape against the postcode systems of the countries
|
|
81
|
+
* and regions listed in the same tree, without querying a backend.
|
|
82
|
+
*
|
|
83
|
+
* A span that fits none of those systems is excluded: an all-digit span is retagged
|
|
84
|
+
* `house_number`, and any other span is stamped `postcode_shape_excluded`.
|
|
165
85
|
*/
|
|
166
86
|
export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): PostcodeShapeVerdict {
|
|
167
87
|
const verdict: PostcodeShapeVerdict = { confirmed: [], excluded: [], abstained: [] }
|
|
168
88
|
|
|
169
|
-
|
|
170
|
-
// "first confirmed" is the node `state.postcode` will read.
|
|
171
|
-
const postcodes = collectNodes(roots, (n) => n.tag === "postcode" && n.value.trim().length > 0)
|
|
89
|
+
const postcodes = collectNodes(roots, (n) => n.tag === "postcode" && n.value.trim())
|
|
172
90
|
|
|
173
91
|
if (!postcodes.length) return verdict
|
|
174
92
|
|
|
@@ -178,8 +96,6 @@ export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): Post
|
|
|
178
96
|
const code = node.value.trim()
|
|
179
97
|
const systems = candidateSystemsForPostcode(code)
|
|
180
98
|
|
|
181
|
-
// A shape no codex system recognizes is no evidence either way — abstain (the 10/110
|
|
182
|
-
// slice-less Gauntlet codes: IE Eircode, SI, IM, …).
|
|
183
99
|
if (!systems.length) {
|
|
184
100
|
verdict.abstained.push(code)
|
|
185
101
|
|
|
@@ -190,8 +106,7 @@ export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): Post
|
|
|
190
106
|
|
|
191
107
|
if (intersection.length) {
|
|
192
108
|
verdict.confirmed.push(code)
|
|
193
|
-
|
|
194
|
-
// candidate set the country-scope pass should bound itself to.
|
|
109
|
+
|
|
195
110
|
node.metadata = { ...node.metadata, postcode_shape_systems: intersection }
|
|
196
111
|
|
|
197
112
|
if (verdict.narrowing === undefined) {
|
|
@@ -202,9 +117,6 @@ export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): Post
|
|
|
202
117
|
}
|
|
203
118
|
|
|
204
119
|
if (siblingSystems.size) {
|
|
205
|
-
// Confident siblings, empty intersection → EXCLUDED. A digit-only span is demoted to its
|
|
206
|
-
// correct sibling tag (B1-2); a letter-bearing span (the "15 07691" compound corner) keeps
|
|
207
|
-
// its tag and is stamped instead — either way every resolve consumer skips it.
|
|
208
120
|
verdict.excluded.push(code)
|
|
209
121
|
|
|
210
122
|
if (/^\d+$/.test(code)) {
|
|
@@ -216,8 +128,6 @@ export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): Post
|
|
|
216
128
|
continue
|
|
217
129
|
}
|
|
218
130
|
|
|
219
|
-
// No confident siblings — the same abstention posture postcode-country-coherence.ts:269 takes
|
|
220
|
-
// on zero-or-two coherent countries. Never guess with the shape alone.
|
|
221
131
|
verdict.abstained.push(code)
|
|
222
132
|
}
|
|
223
133
|
|
|
@@ -225,9 +135,9 @@ export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): Post
|
|
|
225
135
|
}
|
|
226
136
|
|
|
227
137
|
/**
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
138
|
+
* Returns true for a postcode span excluded by shape coherence when it could not be retagged.
|
|
139
|
+
*
|
|
140
|
+
* Every postcode consumer in the resolver must skip that span.
|
|
231
141
|
*/
|
|
232
142
|
export function isShapeExcludedPostcode(node: AddressNode): boolean {
|
|
233
143
|
return node.tag === "postcode" && node.metadata?.["postcode_shape_excluded"] === true
|
package/lib/remote-resolver.ts
CHANGED
|
@@ -2,35 +2,22 @@
|
|
|
2
2
|
* @copyright Sister Software
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* `RemoteResolver` — the `Resolver` interface (`resolveTree`) over HTTP. The adapter the interface
|
|
7
|
-
* docstring anticipated (Phase 4.4): a client POSTs a parsed `AddressTree` + the serializable
|
|
8
|
-
* `ResolveOpts` to a resolver service, which owns the gazetteer + situs/interpolation extracts,
|
|
9
|
-
* runs the cascade, and returns the resolved tree. Two payoffs:
|
|
10
|
-
*
|
|
11
|
-
* 1. **Multi-instance** — stateless parser nodes (the ~30 MB ONNX model) talk to ONE resolver service
|
|
12
|
-
* (the multi-GB gazetteer + extracts). `parse` locally, `new RemoteResolver(...).resolveTree`
|
|
13
|
-
* remotely — same interface the in-process `WOFResolver` satisfies, so it's a drop-in.
|
|
14
|
-
* 2. **Canary** — point it at a second resolver build (or an adapter fronting Pelias/Nominatim/BAN)
|
|
15
|
-
* and diff the resolved trees through the identical contract.
|
|
16
|
-
*
|
|
17
|
-
* Pure transport: `fetch` only, no node-specific deps (runs in the browser too). The
|
|
18
|
-
* `addressPoints` / `interpolation` opts are LIVE SQLite handles — not serializable — so they're
|
|
19
|
-
* stripped before the POST; the resolver service supplies its own from the tree's region (the
|
|
20
|
-
* data lives server-side, which is the whole point). All other opts (defaultCountry, calibration,
|
|
21
|
-
* hierarchyCompletion, …) ride along.
|
|
22
5
|
*/
|
|
23
6
|
|
|
24
7
|
import type { AddressTree } from "@mailwoman/core/decoder"
|
|
8
|
+
import { stringifyJSON } from "@mailwoman/core/json"
|
|
25
9
|
import type { ResolveOpts, Resolver } from "@mailwoman/core/resolver"
|
|
26
10
|
|
|
27
11
|
/**
|
|
28
|
-
* `ResolveOpts`
|
|
12
|
+
* Describes the `ResolveOpts` sent over the wire.
|
|
13
|
+
*
|
|
14
|
+
* The options omit live `addressPoints` and `interpolation` lookup handles
|
|
15
|
+
* because they cannot be serialized.
|
|
29
16
|
*/
|
|
30
17
|
export type SerializableResolveOpts = Omit<ResolveOpts, "addressPoints" | "interpolation">
|
|
31
18
|
|
|
32
19
|
/**
|
|
33
|
-
* Strip the live lookup handles from `ResolveOpts` so the rest can be JSON-serialized over
|
|
20
|
+
* Strip the live lookup handles from `ResolveOpts` so the rest can be JSON-serialized over http.
|
|
34
21
|
*/
|
|
35
22
|
export function serializableResolveOpts(opts?: ResolveOpts): SerializableResolveOpts | undefined {
|
|
36
23
|
if (!opts) return undefined
|
|
@@ -39,36 +26,46 @@ export function serializableResolveOpts(opts?: ResolveOpts): SerializableResolve
|
|
|
39
26
|
return rest
|
|
40
27
|
}
|
|
41
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Describes the subset of `fetch` that {@link RemoteResolver} calls,
|
|
31
|
+
* so callers can inject a custom or test implementation.
|
|
32
|
+
*/
|
|
42
33
|
export type RemoteResolverFetch = (
|
|
43
34
|
url: string,
|
|
44
35
|
init: { method: string; headers: Record<string, string>; body: string; signal?: AbortSignal }
|
|
45
36
|
) => Promise<{ ok: boolean; status: number; statusText: string; json(): Promise<unknown> }>
|
|
46
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Configures a {@link RemoteResolver}: the endpoint to POST to, an optional `fetch`,
|
|
40
|
+
* a timeout (10 seconds by default), and extra request headers.
|
|
41
|
+
*/
|
|
47
42
|
export interface RemoteResolverOpts {
|
|
48
43
|
/**
|
|
49
|
-
*
|
|
44
|
+
* Gives the full URL of the resolver service's resolve-tree endpoint,
|
|
45
|
+
* such as `http://resolver:7081/v1/resolve`.
|
|
50
46
|
*/
|
|
51
47
|
endpoint: string
|
|
48
|
+
|
|
52
49
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* Typed by what this module reads — `ok`, `status`, `statusText`, `json()` — rather than the full `fetch`, whose
|
|
56
|
-
* `Response` carries a body stream and headers no code here touches. The global `fetch` satisfies this shape, and a
|
|
57
|
-
* test double can be an object instead of an assertion.
|
|
50
|
+
* Supplies a fetch implementation for tests or custom agents, defaulting to the global `fetch`.
|
|
58
51
|
*/
|
|
59
52
|
fetch?: RemoteResolverFetch
|
|
53
|
+
|
|
60
54
|
/**
|
|
61
|
-
*
|
|
55
|
+
* Sets the per-request timeout in milliseconds, defaulting to 10,000.
|
|
62
56
|
*/
|
|
63
57
|
timeoutMs?: number
|
|
58
|
+
|
|
64
59
|
/**
|
|
65
|
-
*
|
|
60
|
+
* Adds request headers such as auth or tracing.
|
|
61
|
+
*
|
|
62
|
+
* The resolver merges them over the default `Content-Type: application/json`.
|
|
66
63
|
*/
|
|
67
64
|
headers?: Record<string, string>
|
|
68
65
|
}
|
|
69
66
|
|
|
70
67
|
/**
|
|
71
|
-
* The wire request body `
|
|
68
|
+
* The wire request body `post <endpoint>` expects (and the matching server handler parses).
|
|
72
69
|
*/
|
|
73
70
|
export interface ResolveTreeRequest {
|
|
74
71
|
tree: AddressTree
|
|
@@ -82,6 +79,12 @@ export interface ResolveTreeResponse {
|
|
|
82
79
|
tree: AddressTree
|
|
83
80
|
}
|
|
84
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Resolves address trees by POSTing them to a remote resolver endpoint, so a client
|
|
84
|
+
* can resolve without loading gazetteer databases itself.
|
|
85
|
+
*
|
|
86
|
+
* Live lookup handles in `ResolveOpts` are dropped before sending, so the server resolves with its own.
|
|
87
|
+
*/
|
|
85
88
|
export class RemoteResolver implements Resolver {
|
|
86
89
|
readonly #endpoint: string
|
|
87
90
|
readonly #fetch: RemoteResolverFetch
|
|
@@ -106,7 +109,7 @@ export class RemoteResolver implements Resolver {
|
|
|
106
109
|
const res = await this.#fetch(this.#endpoint, {
|
|
107
110
|
method: "POST",
|
|
108
111
|
headers: { "Content-Type": "application/json", ...this.#headers },
|
|
109
|
-
body:
|
|
112
|
+
body: stringifyJSON(body),
|
|
110
113
|
signal: controller.signal,
|
|
111
114
|
})
|
|
112
115
|
|