@mailwoman/resolver 8.6.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/index.ts +8 -0
- package/out/index.d.ts +2 -0
- package/out/index.d.ts.map +1 -1
- package/out/index.js +1 -0
- package/out/index.js.map +1 -1
- package/out/postcode-country-coherence.d.ts +211 -0
- package/out/postcode-country-coherence.d.ts.map +1 -0
- package/out/postcode-country-coherence.js +369 -0
- package/out/postcode-country-coherence.js.map +1 -0
- 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 +183 -17
- 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 +12 -4
- package/postcode-country-coherence.ts +509 -0
- package/postcode-prefix.ts +165 -0
- package/postcode-shape-coherence.ts +248 -0
- package/resolve.ts +247 -23
- package/span-rescore.ts +144 -6
- package/street-tier.ts +2 -0
- package/toponym-prior.ts +278 -0
package/resolve.ts
CHANGED
|
@@ -18,10 +18,13 @@ import type { AddressNode, AddressTree, ComponentTag, Interpretation } from "@ma
|
|
|
18
18
|
import {
|
|
19
19
|
type AddressPointLookup,
|
|
20
20
|
type CoincidentLocality,
|
|
21
|
+
compareReferential,
|
|
21
22
|
DEFAULT_PLACETYPE_MAP,
|
|
22
23
|
type InterpolationLookup,
|
|
23
24
|
isPlacetypeFallback,
|
|
24
25
|
type PlacetypeMap,
|
|
26
|
+
type PostcodePrefixIndexLike,
|
|
27
|
+
referentialFromPopulation,
|
|
25
28
|
type ResolvedPlace,
|
|
26
29
|
type ResolveOpts,
|
|
27
30
|
type Resolver,
|
|
@@ -31,8 +34,16 @@ import {
|
|
|
31
34
|
import { haversineKm } from "@mailwoman/spatial"
|
|
32
35
|
|
|
33
36
|
import { foldName } from "./fold-name.ts"
|
|
37
|
+
import {
|
|
38
|
+
findPostcodeCountryScope,
|
|
39
|
+
type PostcodeCountryScope,
|
|
40
|
+
stampPostcodeCountryScope,
|
|
41
|
+
} from "./postcode-country-coherence.ts"
|
|
42
|
+
import { type CoordinateOptionalPlace, postcodePrefixResolvedPlace, probePostcodePrefix } from "./postcode-prefix.ts"
|
|
43
|
+
import { applyPostcodeShapeCoherence, isShapeExcludedPostcode } from "./postcode-shape-coherence.ts"
|
|
34
44
|
import { findRescoreCandidate, hasResolvedPlace, postcodeCodeSubset } from "./span-rescore.ts"
|
|
35
45
|
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "./street-tier.ts"
|
|
46
|
+
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, rankByCountryPrior, rankByImportance } from "./toponym-prior.ts"
|
|
36
47
|
|
|
37
48
|
/**
|
|
38
49
|
* Build a `Resolver` backed by a `ResolverBackend`. The backend can be any concrete impl structurally compatible with
|
|
@@ -54,6 +65,21 @@ interface ResolutionState {
|
|
|
54
65
|
* can inject postcode-proximal locality candidates.
|
|
55
66
|
*/
|
|
56
67
|
postcode?: string
|
|
68
|
+
/**
|
|
69
|
+
* Postcode-containment coherence (#31, Mechanism 2) — forwarded to locality lookups so a coordinate-first backend can
|
|
70
|
+
* re-rank name candidates by proximity to the postcode's own centroid. Opt-in; OFF by default.
|
|
71
|
+
*/
|
|
72
|
+
postcodeContainmentCoherence: boolean
|
|
73
|
+
/**
|
|
74
|
+
* Postcode-prefix prior (#31, Mechanism 3) — on a `postalcode` miss, derive the code's prefix and probe
|
|
75
|
+
* `postcodePrefixIndex`. Opt-in; OFF by default.
|
|
76
|
+
*/
|
|
77
|
+
postcodePrefixPrior: boolean
|
|
78
|
+
/**
|
|
79
|
+
* The injected PFX1 index (structural — `PostcodePrefixIndexLike`, core/resolver/types.ts). Absent = the prior cannot
|
|
80
|
+
* fire.
|
|
81
|
+
*/
|
|
82
|
+
postcodePrefixIndex?: PostcodePrefixIndexLike
|
|
57
83
|
/**
|
|
58
84
|
* Proximity-bias points (viewport, user location) — forwarded to every primary lookup.
|
|
59
85
|
*/
|
|
@@ -66,6 +92,15 @@ interface ResolutionState {
|
|
|
66
92
|
* Weight on the posterior in the locality re-rank. Only used when `anchorPosterior` is set.
|
|
67
93
|
*/
|
|
68
94
|
anchorWeight: number
|
|
95
|
+
/**
|
|
96
|
+
* #27 locale-country SOFT prior for the bare-toponym admin walk. Undefined = no prior (the shipped default) →
|
|
97
|
+
* byte-stable. See `ResolveOpts.localeCountryPrior` for the calibration and why it ships opt-in.
|
|
98
|
+
*/
|
|
99
|
+
localeCountryPrior?: string
|
|
100
|
+
/**
|
|
101
|
+
* Weight of that prior in log10-population units. Only consulted when `localeCountryPrior` is set.
|
|
102
|
+
*/
|
|
103
|
+
localeCountryPriorWeight: number
|
|
69
104
|
/**
|
|
70
105
|
* #743/#194 confident-placer country as a HARD filter (empty→unresolved, no global retry). Off = undefined.
|
|
71
106
|
*/
|
|
@@ -86,7 +121,7 @@ interface ResolutionState {
|
|
|
86
121
|
/**
|
|
87
122
|
* The first region that resolved (its place — for the coincident-roles lookup).
|
|
88
123
|
*/
|
|
89
|
-
resolvedRegion:
|
|
124
|
+
resolvedRegion: CoordinateOptionalPlace | null
|
|
90
125
|
/**
|
|
91
126
|
* The decorated region NODE that produced {@link resolvedRegion} — completion pushes the locality interpretation onto
|
|
92
127
|
* it in place (no synthesized sibling).
|
|
@@ -95,17 +130,27 @@ interface ResolutionState {
|
|
|
95
130
|
}
|
|
96
131
|
|
|
97
132
|
/**
|
|
98
|
-
* Pick the completion locality when an admin maps to several coincident same-name candidates (#405).
|
|
99
|
-
* PRIMARY signal — the principal city is the populous one, and it can sit FARTHER from the admin
|
|
100
|
-
* same-name hamlet (the Niigata case from #403). Nearest centroid breaks a
|
|
101
|
-
* population AND distance) ABSTAINS rather than guess.
|
|
133
|
+
* Pick the completion locality when an admin maps to several coincident same-name candidates (#405). REFERENTIAL
|
|
134
|
+
* likelihood is the PRIMARY signal — the principal city is the populous one, and it can sit FARTHER from the admin
|
|
135
|
+
* centroid than a tiny same-name hamlet (the Niigata case from #403). Nearest centroid breaks a referential tie; a
|
|
136
|
+
* genuine tie (same population AND distance) ABSTAINS rather than guess.
|
|
137
|
+
*
|
|
138
|
+
* ROAD_TO_V9 §2: `compareReferential` is referential DESC with raw population as its own tiebreak, which is the SAME
|
|
139
|
+
* ORDER as the plain `b.population - a.population` it replaced — referential is strictly increasing in population below
|
|
140
|
+
* saturation and constant above it. The abstention check below still reads raw population, deliberately: two megacities
|
|
141
|
+
* that saturate to the same referential score are not tied, and abstaining there would be a new behavior.
|
|
102
142
|
*/
|
|
103
143
|
function pickCompletion(candidates: readonly CoincidentLocality[]): CoincidentLocality | null {
|
|
104
144
|
if (!candidates.length) return null
|
|
105
145
|
|
|
106
146
|
if (candidates.length === 1) return candidates[0]!
|
|
107
|
-
|
|
108
|
-
const ranked = [...candidates]
|
|
147
|
+
|
|
148
|
+
const ranked = [...candidates]
|
|
149
|
+
.map((c) => ({ c, referential: referentialFromPopulation(c.population), population: c.population }))
|
|
150
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
|
|
151
|
+
.sort((a, b) => compareReferential(a, b) || a.c.distanceKm - b.c.distanceKm)
|
|
152
|
+
.map((x) => x.c)
|
|
153
|
+
|
|
109
154
|
const [first, second] = ranked
|
|
110
155
|
|
|
111
156
|
if (first!.population === second!.population && first!.distanceKm === second!.distanceKm) return null
|
|
@@ -123,7 +168,9 @@ function firstPostcodeValue(roots: readonly AddressNode[]): string | undefined {
|
|
|
123
168
|
while (stack.length) {
|
|
124
169
|
const n = stack.pop()!
|
|
125
170
|
|
|
126
|
-
|
|
171
|
+
// #31 Mechanism 1: a shape-excluded span keeps its tag but contributes nothing — its code
|
|
172
|
+
// must never become the address's postcode (it would poison the country-scope pass's anchor).
|
|
173
|
+
if (n.tag === "postcode" && !isShapeExcludedPostcode(n) && n.value.trim().length) return n.value.trim()
|
|
127
174
|
stack.push(...n.children)
|
|
128
175
|
}
|
|
129
176
|
|
|
@@ -186,7 +233,12 @@ async function applySpanRescore(
|
|
|
186
233
|
children: [],
|
|
187
234
|
}
|
|
188
235
|
|
|
189
|
-
|
|
236
|
+
// #1537: the same-span namesake runner-ups, not an empty list. A name the model reads as a `street`
|
|
237
|
+
// ("Springfield", "Berlin", "Moscow") never reaches the admin walk, so this tier is the ONLY thing that
|
|
238
|
+
// resolves it — and decorating with `[]` meant the geocode path's `candidates` held one entry and the
|
|
239
|
+
// dominance margin `declared_ambiguity` reads was uncomputable for exactly the famous-homonym class.
|
|
240
|
+
// The winner is unchanged (see findRescoreCandidate); this is additive.
|
|
241
|
+
decorateNode(node, hit.place, hit.alternatives)
|
|
190
242
|
// `rescore_gated` carries the gate's precision signal as an EXPLICIT handle — NOT folded into the
|
|
191
243
|
// calibrated `confidence`, which would break the isotonic guarantee (a true calibrated 0.83 must not
|
|
192
244
|
// be confused with a rescore plug-in estimate; DeepSeek 2026-06-23). true = postcode gate fired
|
|
@@ -255,8 +307,9 @@ function isResolvedWithCoord(n: AddressNode): boolean {
|
|
|
255
307
|
* (right area, the safe answer) and flag `postcode_city_mismatch`.
|
|
256
308
|
*
|
|
257
309
|
* Only fires where the postcode resolved to a point, so it composes with postcode coverage (#193) — add a country's
|
|
258
|
-
* postcodes and this immediately disambiguates its same-named towns. Default-
|
|
259
|
-
* byte-stable
|
|
310
|
+
* postcodes and this immediately disambiguates its same-named towns. **Default-ON** since the #370 operator promotion
|
|
311
|
+
* (2026-07-04, commit `0010bb8c`) — `opts.postcodeConsistency: false` opts out, and the pass is byte-stable on every
|
|
312
|
+
* tree with no resolved postcode point (the `!anchor` early return below).
|
|
260
313
|
*/
|
|
261
314
|
function applyPostcodeConsistency(roots: readonly AddressNode[], gateKm: number): void {
|
|
262
315
|
// The resolved postcode anchor (first one with a real coordinate).
|
|
@@ -266,7 +319,8 @@ function applyPostcodeConsistency(roots: readonly AddressNode[], gateKm: number)
|
|
|
266
319
|
while (findAnchor.length) {
|
|
267
320
|
const n = findAnchor.pop()!
|
|
268
321
|
|
|
269
|
-
|
|
322
|
+
// #31 Mechanism 1: a shape-excluded span never anchors the consistency re-pick either.
|
|
323
|
+
if (n.tag === "postcode" && !isShapeExcludedPostcode(n) && isResolvedWithCoord(n)) {
|
|
270
324
|
anchor = { lat: n.lat!, lon: n.lon! }
|
|
271
325
|
|
|
272
326
|
break
|
|
@@ -505,6 +559,7 @@ function revertResolverDecoration(node: AddressNode): void {
|
|
|
505
559
|
"classifier_source",
|
|
506
560
|
"classifier_source_id",
|
|
507
561
|
"resolver_score",
|
|
562
|
+
"resolver_prominence",
|
|
508
563
|
"resolver_name",
|
|
509
564
|
"resolver_country",
|
|
510
565
|
"resolution_quality",
|
|
@@ -749,6 +804,15 @@ class WOFResolver implements Resolver {
|
|
|
749
804
|
}
|
|
750
805
|
|
|
751
806
|
async resolveTree(tree: AddressTree, opts: ResolveOpts = {}): Promise<AddressTree> {
|
|
807
|
+
// Postcode-shape coherence (#31, Mechanism 1): the earliest pass in the tree — pure-sync and
|
|
808
|
+
// backend-free, run BEFORE `state.postcode` is read so an excluded span can never become the
|
|
809
|
+
// address's postcode. Opt-in (`postcodeShapeCoherence: true`), OFF by default (D-rule: demotion
|
|
810
|
+
// is the failure mode with teeth). A confirmed span's narrowed systems — the intersection of
|
|
811
|
+
// its codex shape with the tree's confident sibling systems — bound the country-scope pass
|
|
812
|
+
// below; a pure subset of codex's own candidate list, so it can only narrow, never widen.
|
|
813
|
+
// See postcode-shape-coherence.ts.
|
|
814
|
+
const shapeVerdict = opts.postcodeShapeCoherence === true ? applyPostcodeShapeCoherence(tree.roots) : null
|
|
815
|
+
|
|
752
816
|
const state: ResolutionState = {
|
|
753
817
|
lookupsRemaining: opts.maxLookups ?? 10,
|
|
754
818
|
// Full replacement when `placetypeMap` is supplied — callers that want to extend rather
|
|
@@ -759,9 +823,16 @@ class WOFResolver implements Resolver {
|
|
|
759
823
|
defaultCountry: opts.defaultCountry,
|
|
760
824
|
parentFallback: opts.parentFallback ?? true,
|
|
761
825
|
postcode: firstPostcodeValue(tree.roots),
|
|
826
|
+
// #31 Mechanism 2 — forwarded to locality lookups (see #lookupAndPick). Opt-in, OFF by default.
|
|
827
|
+
postcodeContainmentCoherence: opts.postcodeContainmentCoherence === true,
|
|
828
|
+
// #31 Mechanism 3 — the prefix prior + its injected index (see #lookupAndPick). Opt-in, OFF by default.
|
|
829
|
+
postcodePrefixPrior: opts.postcodePrefixPrior === true,
|
|
830
|
+
postcodePrefixIndex: opts.postcodePrefixIndex,
|
|
762
831
|
bias: opts.bias,
|
|
763
832
|
anchorPosterior: opts.anchorPosterior,
|
|
764
833
|
anchorWeight: opts.anchorWeight ?? 2,
|
|
834
|
+
localeCountryPrior: opts.localeCountryPrior,
|
|
835
|
+
localeCountryPriorWeight: opts.localeCountryPriorWeight ?? DEFAULT_COUNTRY_PRIOR_WEIGHT,
|
|
765
836
|
hardCountry: opts.hardCountry,
|
|
766
837
|
// Default-ON (#402): completion only fires for a dual-role region whose locality the parser
|
|
767
838
|
// dropped, and no-ops entirely when the backend has no relation (the browser WASM resolver, or
|
|
@@ -773,12 +844,45 @@ class WOFResolver implements Resolver {
|
|
|
773
844
|
resolvedRegionNode: null,
|
|
774
845
|
}
|
|
775
846
|
|
|
847
|
+
// Postcode-country coherence (#42): the one pre-walk LOOKUP pass (the shape verdict above is
|
|
848
|
+
// pure-sync, backend-free), and the only mechanism allowed to override `defaultCountry`. Its three
|
|
849
|
+
// sibling coherence passes re-pick nodes AFTER the walk; that shape cannot work here, because what
|
|
850
|
+
// needs correcting is the walk's country SCOPE — which poisons the postcode node's own resolution,
|
|
851
|
+
// the postcode-consistency fallback that then drags the locality onto it, and the hard `country`
|
|
852
|
+
// filter on every admin lookup. So the verdict is taken once, up front, and the walk runs under the
|
|
853
|
+
// corrected country. Default-ON (operator-promoted 2026-08-05; `false` opts out) — needs a default
|
|
854
|
+
// country to correct and both a postcode and a locality to be coherent about; abstains unless
|
|
855
|
+
// EXACTLY one country (never the already-coherent default) makes the pair consistent. See
|
|
856
|
+
// postcode-country-coherence.ts.
|
|
857
|
+
let postcodeScope: PostcodeCountryScope | null = null
|
|
858
|
+
|
|
859
|
+
if (opts.postcodeCountryCoherence !== false && state.defaultCountry && state.postcode) {
|
|
860
|
+
postcodeScope = await findPostcodeCountryScope(tree.roots, this.#backend, {
|
|
861
|
+
postcode: state.postcode,
|
|
862
|
+
defaultCountry: state.defaultCountry,
|
|
863
|
+
...(shapeVerdict?.narrowing !== undefined ? { candidateSystems: shapeVerdict.narrowing } : {}),
|
|
864
|
+
...(opts.postcodeCountryCoherenceGateKm !== undefined ? { gateKm: opts.postcodeCountryCoherenceGateKm } : {}),
|
|
865
|
+
})
|
|
866
|
+
|
|
867
|
+
if (postcodeScope) {
|
|
868
|
+
// The override. `hardCountry` is not cleared because it is never consulted while a
|
|
869
|
+
// `defaultCountry` is set (`#lookupAndPick`'s precedence chain reaches it only as the last
|
|
870
|
+
// fallback), so replacing the default is sufficient to re-scope the entire walk.
|
|
871
|
+
state.defaultCountry = postcodeScope.country
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
776
875
|
const newRoots: AddressNode[] = []
|
|
777
876
|
|
|
778
877
|
for (const root of tree.roots) {
|
|
779
878
|
newRoots.push(await this.#walk(root, /* parentResolved */ null, state))
|
|
780
879
|
}
|
|
781
880
|
|
|
881
|
+
// Attribution for the override, stamped on the two nodes that bought it. Additive metadata only.
|
|
882
|
+
if (postcodeScope) {
|
|
883
|
+
stampPostcodeCountryScope(newRoots, postcodeScope)
|
|
884
|
+
}
|
|
885
|
+
|
|
782
886
|
// Dual-role hierarchy completion (#405/#415). Only when enabled, a region resolved, and the parser
|
|
783
887
|
// emitted NO locality — record the dropped locality as a SECONDARY ROLE (an interpretation) on the
|
|
784
888
|
// resolved region node, from the backend's precomputed coincident-roles relation (#403). One node,
|
|
@@ -836,6 +940,15 @@ class WOFResolver implements Resolver {
|
|
|
836
940
|
await applySpanRescore(newRoots, tree.raw, this.#backend, opts)
|
|
837
941
|
}
|
|
838
942
|
|
|
943
|
+
// A failed admin walk can leave the exact-point tier without a locality even though span-rescore
|
|
944
|
+
// recovers one immediately afterward (for example, a locality the model tagged as `region`). Give
|
|
945
|
+
// the exact register one bounded retry with that newly established scope. An existing situs hit is
|
|
946
|
+
// returned early by applyAddressPoint, so the normal path performs no duplicate lookup; interpolation
|
|
947
|
+
// remains subordinate because an exact point is allowed to replace its estimate.
|
|
948
|
+
if (opts.addressPoints) {
|
|
949
|
+
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback)
|
|
950
|
+
}
|
|
951
|
+
|
|
839
952
|
// Street-centroid tier (#1042): LAST, after span-rescore, so it can (a) union the span-rescore-recovered
|
|
840
953
|
// country into its FR/national country hints (a placer-misrouted street — "Rue Sainte-Catherine" → IT — leaves
|
|
841
954
|
// admin unresolved, and only span-rescore recovers the FR country signal) and (b) override a coarse recovered
|
|
@@ -856,7 +969,7 @@ class WOFResolver implements Resolver {
|
|
|
856
969
|
* relation, the region isn't a dual-role place, or it abstains. The region node's primary role stays `region`; the
|
|
857
970
|
* locality rides alongside.
|
|
858
971
|
*/
|
|
859
|
-
#completeRegionRole(region:
|
|
972
|
+
#completeRegionRole(region: CoordinateOptionalPlace, regionNode: AddressNode): void {
|
|
860
973
|
if (typeof region.id !== "number" || !this.#backend.coincidentLocalitiesFor) return
|
|
861
974
|
const loc = pickCompletion(this.#backend.coincidentLocalitiesFor(region.id))
|
|
862
975
|
|
|
@@ -875,7 +988,11 @@ class WOFResolver implements Resolver {
|
|
|
875
988
|
regionNode.interpretations = [...(regionNode.interpretations ?? []), interpretation]
|
|
876
989
|
}
|
|
877
990
|
|
|
878
|
-
async #walk(
|
|
991
|
+
async #walk(
|
|
992
|
+
node: AddressNode,
|
|
993
|
+
parentResolved: CoordinateOptionalPlace | null,
|
|
994
|
+
state: ResolutionState
|
|
995
|
+
): Promise<AddressNode> {
|
|
879
996
|
// Always clone — never mutate input nodes.
|
|
880
997
|
const decorated: AddressNode = { ...node, children: [] }
|
|
881
998
|
|
|
@@ -888,15 +1005,22 @@ class WOFResolver implements Resolver {
|
|
|
888
1005
|
state.localityNodePresent = true
|
|
889
1006
|
}
|
|
890
1007
|
|
|
891
|
-
let resolved:
|
|
1008
|
+
let resolved: CoordinateOptionalPlace | null = null
|
|
892
1009
|
|
|
893
|
-
|
|
1010
|
+
// Shape-excluded postcode spans (letter-bearing ones keep their tag; #31 Mechanism 1) are
|
|
1011
|
+
// stripped of their resolve contribution — the walk does not look them up. Digit-only excluded
|
|
1012
|
+
// spans were retagged to `house_number` and flow through their correct sibling placetype.
|
|
1013
|
+
if (placetype && state.lookupsRemaining > 0 && node.value.trim().length && !isShapeExcludedPostcode(node)) {
|
|
894
1014
|
const picked = await this.#lookupAndPick(node, placetype, parentResolved, state)
|
|
895
1015
|
|
|
896
1016
|
if (picked) {
|
|
897
1017
|
resolved = picked.top
|
|
898
1018
|
decorateNode(decorated, picked.top, picked.alternatives)
|
|
899
1019
|
|
|
1020
|
+
if (picked.metadata) {
|
|
1021
|
+
decorated.metadata = { ...decorated.metadata, ...picked.metadata }
|
|
1022
|
+
}
|
|
1023
|
+
|
|
900
1024
|
// Lineage attachment (#404): stamp the resolved place's ancestor chain onto metadata. Opt-in
|
|
901
1025
|
// + only when the backend supplies it, so the default stays byte-identical (no extra query).
|
|
902
1026
|
if (state.includeAncestors && this.#backend.ancestors) {
|
|
@@ -924,9 +1048,13 @@ class WOFResolver implements Resolver {
|
|
|
924
1048
|
async #lookupAndPick(
|
|
925
1049
|
node: AddressNode,
|
|
926
1050
|
placetype: string,
|
|
927
|
-
parentResolved:
|
|
1051
|
+
parentResolved: CoordinateOptionalPlace | null,
|
|
928
1052
|
state: ResolutionState
|
|
929
|
-
): Promise<{
|
|
1053
|
+
): Promise<{
|
|
1054
|
+
top: CoordinateOptionalPlace
|
|
1055
|
+
alternatives: ResolvedPlace[]
|
|
1056
|
+
metadata?: Record<string, unknown>
|
|
1057
|
+
} | null> {
|
|
930
1058
|
state.lookupsRemaining--
|
|
931
1059
|
|
|
932
1060
|
const query: Parameters<ResolverBackend["findPlace"]>[0] = {
|
|
@@ -961,14 +1089,16 @@ class WOFResolver implements Resolver {
|
|
|
961
1089
|
// collapses the tail (FI 18 km, PL p99 8172→494) at a coverage-bounded recall cost.
|
|
962
1090
|
// #833 forward linkage: a node's own `country_hint` (an address-system recognizer's derived country —
|
|
963
1091
|
// today `recognizeUSRegions` stamping "US" on a recognized closed-set US state) constrains THIS node's
|
|
964
|
-
// lookup
|
|
965
|
-
//
|
|
1092
|
+
// lookup when the caller supplied no locale/default country. A caller scope outranks this derived hint:
|
|
1093
|
+
// `WA` is both Washington and Western Australia, so en-AU must reach the AU candidate instead of being
|
|
1094
|
+
// overwritten by the US-only recognizer. A genuine foreign subdivision under the wrong locale is recovered
|
|
1095
|
+
// later by region-country coherence after the scoped lookup abstains.
|
|
966
1096
|
const countryHint = node.metadata?.["country_hint"]
|
|
967
1097
|
|
|
968
1098
|
const country =
|
|
969
1099
|
parentResolved?.country ??
|
|
970
|
-
(typeof countryHint === "string" ? countryHint : undefined) ??
|
|
971
1100
|
state.defaultCountry ??
|
|
1101
|
+
(typeof countryHint === "string" ? countryHint : undefined) ??
|
|
972
1102
|
state.hardCountry
|
|
973
1103
|
|
|
974
1104
|
if (country) {
|
|
@@ -977,9 +1107,15 @@ class WOFResolver implements Resolver {
|
|
|
977
1107
|
|
|
978
1108
|
// Coordinate-first: hand the sibling postcode to locality lookups so the backend can inject
|
|
979
1109
|
// postcode-proximal candidates the name-match would miss. Only for locality (the placetype both
|
|
980
|
-
// `locality` and `dependent_locality` map to); other placetypes ignore it.
|
|
1110
|
+
// `locality` and `dependent_locality` map to); other placetypes ignore it. The containment flag
|
|
1111
|
+
// (#31, Mechanism 2) asks a coordinate-first backend to re-rank the candidates by proximity to
|
|
1112
|
+
// the postcode's own centroid — opt-in, OFF by default.
|
|
981
1113
|
if (placetype === "locality" && state.postcode) {
|
|
982
1114
|
query.postcode = state.postcode
|
|
1115
|
+
|
|
1116
|
+
if (state.postcodeContainmentCoherence) {
|
|
1117
|
+
query.postcodeContainmentCoherence = true
|
|
1118
|
+
}
|
|
983
1119
|
}
|
|
984
1120
|
|
|
985
1121
|
let candidates: ResolvedPlace[]
|
|
@@ -1003,6 +1139,33 @@ class WOFResolver implements Resolver {
|
|
|
1003
1139
|
return null
|
|
1004
1140
|
}
|
|
1005
1141
|
|
|
1142
|
+
// Postcode-prefix prior (#31, Mechanism 3): when a `postalcode` node misses the gazetteer, derive
|
|
1143
|
+
// the code's prefix and probe the injected PFX1 index (structural — never a model input). A hit
|
|
1144
|
+
// resolves the node to a synthetic place (id 0), carrying a coordinate ONLY when the index node
|
|
1145
|
+
// does — the ancestry-only tier stays coordinate-free (B3-3's 0% half, meaning-of-zero). The
|
|
1146
|
+
// prior's payload is returned as node metadata for #walk to stamp. Opt-in via `postcodePrefixPrior`
|
|
1147
|
+
// + `postcodePrefixIndex`; OFF by default (D-rule: the PCN1 posture).
|
|
1148
|
+
if (!candidates.length && placetype === "postalcode" && state.postcodePrefixPrior && state.postcodePrefixIndex) {
|
|
1149
|
+
const probe = probePostcodePrefix(node.value, state.postcodePrefixIndex, query.country)
|
|
1150
|
+
|
|
1151
|
+
if (probe) {
|
|
1152
|
+
const metadata: Record<string, unknown> = {
|
|
1153
|
+
postcode_prefix: probe.prefix,
|
|
1154
|
+
postcode_prefix_ancestors: probe.node.ancestors,
|
|
1155
|
+
...(probe.node.radiusP95Km !== undefined ? { postcode_prefix_radius_p95_km: probe.node.radiusP95Km } : {}),
|
|
1156
|
+
...(probe.node.lat !== undefined && probe.node.lon !== undefined
|
|
1157
|
+
? { coordinate_source: "postcode_prefix" }
|
|
1158
|
+
: {}),
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
return {
|
|
1162
|
+
top: postcodePrefixResolvedPlace(probe.prefix, probe.node, state.postcodePrefixIndex),
|
|
1163
|
+
alternatives: [],
|
|
1164
|
+
metadata,
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1006
1169
|
if (!candidates.length) return null
|
|
1007
1170
|
// Postcode-anchor re-rank (#369): when a country posterior is supplied (from the address's
|
|
1008
1171
|
// postcode), boost candidates by `anchorWeight * posterior[candidate.country]` and re-sort, so a
|
|
@@ -1054,6 +1217,40 @@ class WOFResolver implements Resolver {
|
|
|
1054
1217
|
})
|
|
1055
1218
|
}
|
|
1056
1219
|
|
|
1220
|
+
// Locale-country soft prior (#27) — the #912 lever's other half, at the tier that decides a bare
|
|
1221
|
+
// toponym the model tagged `locality`. `--locale en-GB "Whitby"` answers Whitby, Ontario while
|
|
1222
|
+
// `--default-country GB "Whitby"` answers the gold, because the CLI DROPS the locale-inferred
|
|
1223
|
+
// country for this shape rather than choosing between "hard filter" and "nothing". This is the
|
|
1224
|
+
// third option: an additive bonus inside the exact tier, never a filter.
|
|
1225
|
+
//
|
|
1226
|
+
// Three stand-downs, in the order they matter. A `defaultCountry` makes the prior a no-op by
|
|
1227
|
+
// construction (every candidate is already in it), so we skip the work rather than pretend. An
|
|
1228
|
+
// `anchorPosterior` is derived from the address's OWN postcode — evidence, which outranks a guess
|
|
1229
|
+
// about where the user is sitting. And an absent prior is the SHIPPED default: `ResolveOpts`
|
|
1230
|
+
// documents why (the weight that flips the four bare GB rows and the weight that holds the en-US
|
|
1231
|
+
// board are disjoint intervals), so this is inert unless a caller opts in.
|
|
1232
|
+
//
|
|
1233
|
+
// Runs BEFORE the importance key deliberately, matching span-rescore: fame is the stronger signal
|
|
1234
|
+
// wherever it has been measured, and it leaves an unscored candidate exactly where the prior put it.
|
|
1235
|
+
if (state.localeCountryPrior && !state.defaultCountry && !state.anchorPosterior && anchorEligible) {
|
|
1236
|
+
ranked = rankByCountryPrior(ranked, state.localeCountryPrior, state.localeCountryPriorWeight)
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// Importance-first (#17/#28). Before this key a bare famous name was decided on POPULATION
|
|
1240
|
+
// alone, which is measurably the wrong prior for the class: `Whitby` answered Whitby, Ontario
|
|
1241
|
+
// (128,377) over Whitby, North Yorkshire (13,130), 5,508 km from where the person meant. Fame is
|
|
1242
|
+
// what a bare toponym is asking about, and the candidate build's blended `importance` column
|
|
1243
|
+
// measures it. Tier-safe and positive-evidence-only (an UNSCORED candidate keeps the rank
|
|
1244
|
+
// population gave it; only the scored ones permute), so it abstains byte-stably on an artifact
|
|
1245
|
+
// that predates the column.
|
|
1246
|
+
//
|
|
1247
|
+
// Skipped outright when an anchor posterior is in force. Fame is the prior of LAST resort: it answers
|
|
1248
|
+
// "which one did you probably mean" only when nothing in the query answered it, and a #369 posterior is
|
|
1249
|
+
// derived from the address's OWN postcode. Evidence outranks a prior. See `toponym-prior.ts`.
|
|
1250
|
+
if (!state.anchorPosterior) {
|
|
1251
|
+
ranked = rankByImportance(ranked)
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1057
1254
|
// Exact-type preference (#718): when the placetype-equivalence group let a broader admin tier
|
|
1058
1255
|
// (`macroregion`/`macrocounty`) into the candidate pool, prefer a candidate of the EXACT
|
|
1059
1256
|
// requested type over the macro fallback — a real `region` (US state, DE Bundesland, ES
|
|
@@ -1092,7 +1289,7 @@ class WOFResolver implements Resolver {
|
|
|
1092
1289
|
* assertion. Surfaces the runner-up candidates on `alternatives` so callers can disambiguate (Springfield-class
|
|
1093
1290
|
* failures, [#8 in the failure catalogue]).
|
|
1094
1291
|
*/
|
|
1095
|
-
function decorateNode(node: AddressNode, resolved:
|
|
1292
|
+
function decorateNode(node: AddressNode, resolved: CoordinateOptionalPlace, alternatives: ResolvedPlace[]): void {
|
|
1096
1293
|
if (node.source !== undefined || node.sourceID !== undefined) {
|
|
1097
1294
|
const meta = { ...node.metadata }
|
|
1098
1295
|
|
|
@@ -1118,6 +1315,15 @@ function decorateNode(node: AddressNode, resolved: ResolvedPlace, alternatives:
|
|
|
1118
1315
|
// the right PLACE (gazetteer-name vs ground-truth) rather than merely echoing the parser's text.
|
|
1119
1316
|
node.metadata = { ...node.metadata, resolver_score: resolved.score, resolver_name: resolved.name }
|
|
1120
1317
|
|
|
1318
|
+
// The winner's PROMINENCE, when the backend computed one. `alternatives` below are full `ResolvedPlace`s and
|
|
1319
|
+
// already carry theirs; without this stamp the WINNER's is the one value in the ranked list that gets dropped,
|
|
1320
|
+
// which makes a top-1-vs-top-2 margin uncomputable from the tree — and that margin is what
|
|
1321
|
+
// `mailwoman/query-intent.ts` reads to decide whether a bare-toponym answer was a clear win. Additive metadata
|
|
1322
|
+
// only; nothing in the resolve reads it back.
|
|
1323
|
+
if (resolved.prominence !== undefined) {
|
|
1324
|
+
node.metadata["resolver_prominence"] = resolved.prominence
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1121
1327
|
// The resolved place's ISO-3166 alpha-2 country (from the gazetteer/candidate row), when known. #1014: lets a
|
|
1122
1328
|
// forward consumer fill country/countrycode without an ancestry walk — the candidate backend carries this even
|
|
1123
1329
|
// though it has no `ancestors()` table.
|
|
@@ -1125,6 +1331,24 @@ function decorateNode(node: AddressNode, resolved: ResolvedPlace, alternatives:
|
|
|
1125
1331
|
node.metadata["resolver_country"] = resolved.country
|
|
1126
1332
|
}
|
|
1127
1333
|
|
|
1334
|
+
// The score-channel carries (ROAD_TO_V9 §2 + #28). Written ONLY when the backend actually has a
|
|
1335
|
+
// value: an absent score means "unmeasured" or "pre-split gazetteer", and a `resolver_*: 0` on the
|
|
1336
|
+
// node would assert a measurement nobody made. Nothing in the resolve path reads these keys back —
|
|
1337
|
+
// they exist for annotation / API surfaces downstream. `resolver_importance` is the blended #28
|
|
1338
|
+
// prior (the value the ranking consulted); `resolver_encyclopedic` is the strict channel, reserved
|
|
1339
|
+
// until a strict-channel source ships.
|
|
1340
|
+
if (resolved.referential !== undefined) {
|
|
1341
|
+
node.metadata["resolver_referential"] = resolved.referential
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
if (resolved.encyclopedic !== undefined) {
|
|
1345
|
+
node.metadata["resolver_encyclopedic"] = resolved.encyclopedic
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
if (resolved.importance !== undefined) {
|
|
1349
|
+
node.metadata["resolver_importance"] = resolved.importance
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1128
1352
|
// The postcode/locality conflict flag (the falsehood differentiator): the postcode pointed to a
|
|
1129
1353
|
// geographically different place than the parsed city name. Surface it so callers can warn rather
|
|
1130
1354
|
// than silently trust the resolved point.
|