@mailwoman/resolver 9.0.0 → 9.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/out/postcode-country-coherence.d.ts +62 -6
- package/out/postcode-country-coherence.d.ts.map +1 -1
- package/out/postcode-country-coherence.js +176 -20
- package/out/postcode-country-coherence.js.map +1 -1
- package/out/postcode-prefix.d.ts +101 -0
- package/out/postcode-prefix.d.ts.map +1 -0
- package/out/postcode-prefix.js +124 -0
- package/out/postcode-prefix.js.map +1 -0
- package/out/postcode-shape-coherence.d.ts +113 -0
- package/out/postcode-shape-coherence.d.ts.map +1 -0
- package/out/postcode-shape-coherence.js +193 -0
- package/out/postcode-shape-coherence.js.map +1 -0
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +160 -23
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts +28 -0
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +98 -6
- package/out/span-rescore.js.map +1 -1
- package/out/street-tier.d.ts.map +1 -1
- package/out/street-tier.js +2 -0
- package/out/street-tier.js.map +1 -1
- package/out/toponym-prior.d.ts +115 -0
- package/out/toponym-prior.d.ts.map +1 -0
- package/out/toponym-prior.js +240 -0
- package/out/toponym-prior.js.map +1 -0
- package/package.json +4 -4
- package/postcode-country-coherence.ts +239 -26
- package/postcode-prefix.ts +165 -0
- package/postcode-shape-coherence.ts +248 -0
- package/resolve.ts +216 -29
- package/span-rescore.ts +144 -6
- package/street-tier.ts +2 -0
- package/toponym-prior.ts +278 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
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 gate (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
|
+
*/
|
|
76
|
+
|
|
77
|
+
import { candidateSystemsForPostcode, type SystemCode } from "@mailwoman/codex"
|
|
78
|
+
import { matchCountry, matchSubdivision } from "@mailwoman/codex/country"
|
|
79
|
+
import { isUSStateAbbreviation } from "@mailwoman/codex/us"
|
|
80
|
+
import type { AddressNode } from "@mailwoman/core/decoder"
|
|
81
|
+
|
|
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).
|
|
87
|
+
*/
|
|
88
|
+
const SYSTEM_UNIVERSE: ReadonlySet<string> = new Set<string>(["US", "DE", "FR", "CA", "GB", "JP", "AU", "NZ"])
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The pass's per-tree verdict — the caller (and the B1 board tests) can see exactly which spans were confirmed,
|
|
92
|
+
* excluded, or abstained, and which confirmed span's narrowed systems should bound the country-scope pass.
|
|
93
|
+
*/
|
|
94
|
+
export interface PostcodeShapeVerdict {
|
|
95
|
+
/**
|
|
96
|
+
* The narrowed candidate systems (upper-case) of the FIRST confirmed postcode node — the one `firstPostcodeValue`
|
|
97
|
+
* will pick — to thread into `findPostcodeCountryScope` as its candidate list. Undefined when no postcode node was
|
|
98
|
+
* confirmed.
|
|
99
|
+
*/
|
|
100
|
+
narrowing?: string[]
|
|
101
|
+
/**
|
|
102
|
+
* The postcode strings the mechanism confirmed (shape ∩ confident siblings ≠ ∅).
|
|
103
|
+
*/
|
|
104
|
+
confirmed: string[]
|
|
105
|
+
/**
|
|
106
|
+
* The postcode strings the mechanism excluded (shape ∩ confident siblings = ∅).
|
|
107
|
+
*/
|
|
108
|
+
excluded: string[]
|
|
109
|
+
/**
|
|
110
|
+
* The postcode strings the mechanism abstained on (no confident siblings, or no codex shape).
|
|
111
|
+
*/
|
|
112
|
+
abstained: string[]
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Collect the sibling country signals: the country node (territory-mapped), the region node (`matchSubdivision` +
|
|
117
|
+
* `country_hint`), each filtered to the codex SystemCode universe. The tree is walked once for ALL postcode spans —
|
|
118
|
+
* sibling evidence is tree-level, not per-span.
|
|
119
|
+
*/
|
|
120
|
+
function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
|
|
121
|
+
const out = new Set<string>()
|
|
122
|
+
const stack: AddressNode[] = [...roots]
|
|
123
|
+
|
|
124
|
+
while (stack.length) {
|
|
125
|
+
const n = stack.pop()!
|
|
126
|
+
stack.push(...n.children)
|
|
127
|
+
|
|
128
|
+
if (n.tag === "country") {
|
|
129
|
+
const matched = matchCountry(n.value)
|
|
130
|
+
|
|
131
|
+
if (!matched) continue
|
|
132
|
+
// Territory map: PR/VI/GU/MP/AS are USPS state-or-territory abbreviations (codex/us/state.ts),
|
|
133
|
+
// so a US-territory country token ("Puerto Rico", "Guam") is a US-system signal.
|
|
134
|
+
const system = isUSStateAbbreviation(matched.iso2) ? "US" : matched.iso2.toUpperCase()
|
|
135
|
+
|
|
136
|
+
if (SYSTEM_UNIVERSE.has(system)) {
|
|
137
|
+
out.add(system)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (n.tag === "region") {
|
|
144
|
+
// (a) The region's value as a subdivision ("CA" → US, "ON" → CA — the US-wins tiebreak for
|
|
145
|
+
// "CA" is matchSubdivision's own). (b) The `country_hint` stamp annotateUSRegions writes on
|
|
146
|
+
// 2-letter US state abbreviations — the same evidence via the pipeline's other seam.
|
|
147
|
+
const sub = matchSubdivision(n.value)
|
|
148
|
+
|
|
149
|
+
if (sub && SYSTEM_UNIVERSE.has(sub.country.toUpperCase())) {
|
|
150
|
+
out.add(sub.country.toUpperCase())
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const hint = n.metadata?.["country_hint"]
|
|
154
|
+
|
|
155
|
+
if (typeof hint === "string" && SYSTEM_UNIVERSE.has(hint.toUpperCase())) {
|
|
156
|
+
out.add(hint.toUpperCase())
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return out
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Run the shape-coherence verdict over every `postcode` span in the tree, mutating the EXCLUDED spans in place (the
|
|
166
|
+
* retag / exclusion stamp) and stamping CONFIRMED spans' narrowed systems. Pure-sync: no backend, no queries. See the
|
|
167
|
+
* header for the full rule.
|
|
168
|
+
*/
|
|
169
|
+
export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): PostcodeShapeVerdict {
|
|
170
|
+
const verdict: PostcodeShapeVerdict = { confirmed: [], excluded: [], abstained: [] }
|
|
171
|
+
|
|
172
|
+
// The postcode spans in tree order — the same DFS `firstPostcodeValue` uses, so the verdict's
|
|
173
|
+
// "first confirmed" is the node `state.postcode` will read.
|
|
174
|
+
const postcodes: AddressNode[] = []
|
|
175
|
+
const stack: AddressNode[] = [...roots]
|
|
176
|
+
|
|
177
|
+
while (stack.length) {
|
|
178
|
+
const n = stack.pop()!
|
|
179
|
+
|
|
180
|
+
if (n.tag === "postcode" && n.value.trim().length) {
|
|
181
|
+
postcodes.push(n)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
stack.push(...n.children)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!postcodes.length) return verdict
|
|
188
|
+
|
|
189
|
+
const siblingSystems = collectSiblingSystems(roots)
|
|
190
|
+
|
|
191
|
+
for (const node of postcodes) {
|
|
192
|
+
const code = node.value.trim()
|
|
193
|
+
const systems = candidateSystemsForPostcode(code)
|
|
194
|
+
|
|
195
|
+
// A shape no codex system recognizes is no evidence either way — abstain (the 10/110
|
|
196
|
+
// slice-less Gauntlet codes: IE Eircode, SI, IM, …).
|
|
197
|
+
if (!systems.length) {
|
|
198
|
+
verdict.abstained.push(code)
|
|
199
|
+
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const intersection = systems.map((system) => system.toUpperCase()).filter((system) => siblingSystems.has(system))
|
|
204
|
+
|
|
205
|
+
if (intersection.length) {
|
|
206
|
+
verdict.confirmed.push(code)
|
|
207
|
+
// Additive only — resolution stays byte-identical (B1-1). The narrowed intersection is the
|
|
208
|
+
// candidate set the country-scope pass should bound itself to.
|
|
209
|
+
node.metadata = { ...node.metadata, postcode_shape_systems: intersection }
|
|
210
|
+
|
|
211
|
+
if (verdict.narrowing === undefined) {
|
|
212
|
+
verdict.narrowing = intersection
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
continue
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (siblingSystems.size) {
|
|
219
|
+
// Confident siblings, empty intersection → EXCLUDED. A digit-only span is demoted to its
|
|
220
|
+
// correct sibling tag (B1-2); a letter-bearing span (the "15 07691" compound corner) keeps
|
|
221
|
+
// its tag and is stamped instead — either way every resolve consumer skips it.
|
|
222
|
+
verdict.excluded.push(code)
|
|
223
|
+
|
|
224
|
+
if (/^\d+$/.test(code)) {
|
|
225
|
+
node.tag = "house_number"
|
|
226
|
+
} else {
|
|
227
|
+
node.metadata = { ...node.metadata, postcode_shape_excluded: true }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
continue
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// No confident siblings — the same abstention posture postcode-country-coherence.ts:269 takes
|
|
234
|
+
// on zero-or-two coherent countries. Never guess with the shape alone.
|
|
235
|
+
verdict.abstained.push(code)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return verdict
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* True when a node is a postcode span this pass excluded but could not retag (the letter-bearing compound corner). The
|
|
243
|
+
* resolve's postcode consumers (`firstPostcodeValue`, the walk's postcode lookup, the post-walk postcode passes) all
|
|
244
|
+
* skip these, which is what "strip the postcode tag's contribution" means for a span that keeps its tag.
|
|
245
|
+
*/
|
|
246
|
+
export function isShapeExcludedPostcode(node: AddressNode): boolean {
|
|
247
|
+
return node.tag === "postcode" && node.metadata?.["postcode_shape_excluded"] === true
|
|
248
|
+
}
|