@mailwoman/resolver 9.2.0 → 9.3.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 +5 -5
- package/{admin-coherence-passes.ts → lib/admin/coherence-passes.ts} +8 -8
- package/{admin-containment.ts → lib/admin/containment.ts} +5 -14
- package/lib/admin/index.ts +10 -0
- package/lib/admin/jp-municipality.ts +97 -0
- package/{admin-winner.ts → lib/admin/winner.ts} +13 -2
- package/{backend-capabilities.ts → lib/backend-capabilities.ts} +3 -3
- package/{bare-toponym-race.ts → lib/bare-toponym-race.ts} +1 -1
- package/{decorate-node.ts → lib/decorate-node.ts} +2 -2
- package/{fold-name.ts → lib/fold-name.ts} +2 -2
- package/{index.ts → lib/index.ts} +16 -21
- package/{plausibility.ts → lib/plausibility.ts} +5 -4
- package/{postcode-country-coherence.ts → lib/postcode/country-coherence.ts} +27 -32
- package/lib/postcode/index.ts +9 -0
- package/{postcode-prefix.ts → lib/postcode/prefix.ts} +4 -4
- package/{postcode-shape-coherence.ts → lib/postcode/shape-coherence.ts} +10 -24
- package/{remote-resolver.ts → lib/remote-resolver.ts} +13 -4
- package/{rerank.ts → lib/rerank.ts} +2 -2
- package/{resolve.ts → lib/resolve/index.ts} +120 -602
- package/lib/resolve/passes.ts +544 -0
- package/{span-rescore.ts → lib/span-rescore.ts} +63 -97
- package/{street-evidence.ts → lib/street/evidence.ts} +39 -6
- package/lib/street/index.ts +8 -0
- package/{street-tier.ts → lib/street/tier.ts} +125 -110
- package/{toponym-prior.ts → lib/toponym-prior.ts} +8 -8
- package/out/{admin-coherence-passes.d.ts → admin/coherence-passes.d.ts} +6 -6
- package/out/admin/coherence-passes.d.ts.map +1 -0
- package/out/{admin-coherence-passes.js → admin/coherence-passes.js} +9 -9
- package/out/admin/coherence-passes.js.map +1 -0
- package/out/{admin-containment.d.ts → admin/containment.d.ts} +5 -5
- package/out/admin/containment.d.ts.map +1 -0
- package/out/{admin-containment.js → admin/containment.js} +6 -12
- package/out/admin/containment.js.map +1 -0
- package/out/admin/index.d.ts +10 -0
- package/out/admin/index.d.ts.map +1 -0
- package/out/admin/index.js +10 -0
- package/out/admin/index.js.map +1 -0
- package/out/admin/jp-municipality.d.ts +50 -0
- package/out/admin/jp-municipality.d.ts.map +1 -0
- package/out/admin/jp-municipality.js +54 -0
- package/out/admin/jp-municipality.js.map +1 -0
- package/out/{admin-winner.d.ts → admin/winner.d.ts} +8 -3
- package/out/admin/winner.d.ts.map +1 -0
- package/out/{admin-winner.js → admin/winner.js} +14 -3
- package/out/admin/winner.js.map +1 -0
- package/out/backend-capabilities.d.ts.map +1 -1
- package/out/backend-capabilities.js +3 -3
- package/out/backend-capabilities.js.map +1 -1
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/bare-toponym-race.js +1 -1
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts +1 -1
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +1 -1
- package/out/decorate-node.js.map +1 -1
- package/out/fold-name.d.ts +2 -2
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +2 -2
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +15 -15
- package/out/index.d.ts.map +1 -1
- package/out/index.js +9 -9
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +5 -4
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +5 -4
- package/out/plausibility.js.map +1 -1
- package/out/{postcode-country-coherence.d.ts → postcode/country-coherence.d.ts} +16 -16
- package/out/postcode/country-coherence.d.ts.map +1 -0
- package/out/{postcode-country-coherence.js → postcode/country-coherence.js} +24 -25
- package/out/postcode/country-coherence.js.map +1 -0
- package/out/postcode/index.d.ts +9 -0
- package/out/postcode/index.d.ts.map +1 -0
- package/out/postcode/index.js +9 -0
- package/out/postcode/index.js.map +1 -0
- package/out/{postcode-prefix.d.ts → postcode/prefix.d.ts} +4 -4
- package/out/postcode/prefix.d.ts.map +1 -0
- package/out/{postcode-prefix.js → postcode/prefix.js} +4 -4
- package/out/postcode/prefix.js.map +1 -0
- package/out/{postcode-shape-coherence.d.ts → postcode/shape-coherence.d.ts} +3 -3
- package/out/postcode/shape-coherence.d.ts.map +1 -0
- package/out/{postcode-shape-coherence.js → postcode/shape-coherence.js} +11 -20
- package/out/postcode/shape-coherence.js.map +1 -0
- package/out/remote-resolver.d.ts +18 -3
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +2 -2
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +1 -1
- package/out/rerank.d.ts.map +1 -1
- package/out/rerank.js +2 -2
- package/out/rerank.js.map +1 -1
- package/out/{resolve.d.ts → resolve/index.d.ts} +1 -1
- package/out/resolve/index.d.ts.map +1 -0
- package/out/{resolve.js → resolve/index.js} +88 -387
- package/out/resolve/index.js.map +1 -0
- package/out/resolve/passes.d.ts +218 -0
- package/out/resolve/passes.d.ts.map +1 -0
- package/out/resolve/passes.js +333 -0
- package/out/resolve/passes.js.map +1 -0
- package/out/span-rescore.d.ts +16 -16
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +43 -69
- package/out/span-rescore.js.map +1 -1
- package/out/{street-evidence.d.ts → street/evidence.d.ts} +18 -3
- package/out/street/evidence.d.ts.map +1 -0
- package/out/{street-evidence.js → street/evidence.js} +22 -6
- package/out/street/evidence.js.map +1 -0
- package/out/street/index.d.ts +8 -0
- package/out/street/index.d.ts.map +1 -0
- package/out/street/index.js +8 -0
- package/out/street/index.js.map +1 -0
- package/out/{street-tier.d.ts → street/tier.d.ts} +14 -3
- package/out/street/tier.d.ts.map +1 -0
- package/out/{street-tier.js → street/tier.js} +115 -99
- package/out/street/tier.js.map +1 -0
- package/out/toponym-prior.d.ts +8 -8
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +8 -8
- package/out/toponym-prior.js.map +1 -1
- package/package.json +116 -59
- package/vitest.config.ts +5 -6
- package/out/admin-coherence-passes.d.ts.map +0 -1
- package/out/admin-coherence-passes.js.map +0 -1
- package/out/admin-containment.d.ts.map +0 -1
- package/out/admin-containment.js.map +0 -1
- package/out/admin-winner.d.ts.map +0 -1
- package/out/admin-winner.js.map +0 -1
- package/out/postcode-country-coherence.d.ts.map +0 -1
- package/out/postcode-country-coherence.js.map +0 -1
- package/out/postcode-prefix.d.ts.map +0 -1
- package/out/postcode-prefix.js.map +0 -1
- package/out/postcode-shape-coherence.d.ts.map +0 -1
- package/out/postcode-shape-coherence.js.map +0 -1
- package/out/resolve.d.ts.map +0 -1
- package/out/resolve.js.map +0 -1
- package/out/street-evidence.d.ts.map +0 -1
- package/out/street-evidence.js.map +0 -1
- package/out/street-tier.d.ts.map +0 -1
- package/out/street-tier.js.map +0 -1
- package/out/vitest.config.d.ts +0 -16
- package/out/vitest.config.d.ts.map +0 -1
- package/out/vitest.config.js +0 -36
- package/out/vitest.config.js.map +0 -1
|
@@ -12,20 +12,20 @@
|
|
|
12
12
|
* not the MA one.
|
|
13
13
|
*/
|
|
14
14
|
import { matchCountry, matchSubdivision } from "@mailwoman/codex/country";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import { describeCapabilityGaps, reportCapabilityGaps } from "
|
|
21
|
-
import { BARE_REGION_DOMINANCE_LOG10, bareCountryCandidate, bareRegionCandidate, logPopulation, loneBareLocalityNode, pickLargerAdmin, } from "
|
|
22
|
-
import { decorateNode
|
|
23
|
-
import { findPostcodeCountryScope, stampPostcodeCountryScope, } from "
|
|
24
|
-
import { postcodePrefixResolvedPlace, probePostcodePrefix } from "
|
|
25
|
-
import { applyPostcodeShapeCoherence, isShapeExcludedPostcode } from "
|
|
26
|
-
import {
|
|
27
|
-
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "
|
|
28
|
-
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, promoteCapitals, rankByCountryPrior, rankByImportance } from "
|
|
15
|
+
import { collectNodes, } from "@mailwoman/core/decoder";
|
|
16
|
+
import { DEFAULT_PLACETYPE_MAP, isPlacetypeFallback, countriesFromPostcodeFormat, } from "@mailwoman/core/resolver";
|
|
17
|
+
import { applyAdminCoherence, applyExplicitCountryCoherence, applyRegionCountryCoherence, } from "#admin/coherence-passes";
|
|
18
|
+
import { adminContainmentVerdict, firstRegionQualifier, partitionByContainment } from "#admin/containment";
|
|
19
|
+
import { resolveCompoundMunicipality } from "#admin/jp-municipality";
|
|
20
|
+
import { describeCapabilityGaps, reportCapabilityGaps } from "#backend-capabilities";
|
|
21
|
+
import { BARE_REGION_DOMINANCE_LOG10, bareCountryCandidate, bareRegionCandidate, logPopulation, loneBareLocalityNode, pickLargerAdmin, } from "#bare-toponym-race";
|
|
22
|
+
import { decorateNode } from "#decorate-node";
|
|
23
|
+
import { findPostcodeCountryScope, stampPostcodeCountryScope, } from "#postcode/country-coherence";
|
|
24
|
+
import { postcodePrefixResolvedPlace, probePostcodePrefix } from "#postcode/prefix";
|
|
25
|
+
import { applyPostcodeShapeCoherence, isShapeExcludedPostcode } from "#postcode/shape-coherence";
|
|
26
|
+
import { applyPostcodeConsistency, applySpanRescore, createNodeTraceRecorder, DIAGNOSTIC_BANDS, firstPostcodeValue, NOOP_TRACE_RECORDER, pickCompletion, } from "#resolve/passes";
|
|
27
|
+
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "#street/tier";
|
|
28
|
+
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, promoteCapitals, rankByCountryPrior, rankByImportance, } from "#toponym-prior";
|
|
29
29
|
/**
|
|
30
30
|
* Build a `Resolver` backed by a `ResolverBackend`. The backend can be any concrete impl structurally compatible with
|
|
31
31
|
* `PlaceLookup` — e.g. `new WOFSQLitePlaceLookup({ databasePath }).asResolverBackend()` or a fake for tests.
|
|
@@ -46,15 +46,7 @@ export function createWOFResolver(backend) {
|
|
|
46
46
|
* existing coherence passes keep handling it). Returns the alpha-2 or null.
|
|
47
47
|
*/
|
|
48
48
|
function explicitCountryScope(roots) {
|
|
49
|
-
const countryNodes =
|
|
50
|
-
const stack = [...roots];
|
|
51
|
-
while (stack.length) {
|
|
52
|
-
const node = stack.pop();
|
|
53
|
-
if (node.tag === "country" && node.value.trim()) {
|
|
54
|
-
countryNodes.push(node);
|
|
55
|
-
}
|
|
56
|
-
stack.push(...node.children);
|
|
57
|
-
}
|
|
49
|
+
const countryNodes = collectNodes(roots, (node) => node.tag === "country" && node.value.trim().length > 0);
|
|
58
50
|
if (countryNodes.length !== 1)
|
|
59
51
|
return null;
|
|
60
52
|
const value = countryNodes[0].value.trim();
|
|
@@ -65,333 +57,6 @@ function explicitCountryScope(roots) {
|
|
|
65
57
|
return null;
|
|
66
58
|
return matched.iso2;
|
|
67
59
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Cap on candidates recorded per {@link ResolveNodeTrace} — the trace is a record, not a dump. The count past the cap is
|
|
70
|
-
* reported in `candidatesTruncated`, so absence of a row is never silent.
|
|
71
|
-
*/
|
|
72
|
-
const TRACE_CANDIDATE_CAP = 10;
|
|
73
|
-
/**
|
|
74
|
-
* The fine end of the probe window: `microhood` and no finer.
|
|
75
|
-
*
|
|
76
|
-
* Everything above it — `postalcode`, `venue`, `campus`, `building`, `address` — is excluded because a mislabeled ADMIN
|
|
77
|
-
* span lands on another admin band, and probing every venue in a country per miss buys a long tail of coincidental name
|
|
78
|
-
* matches for a diagnostic that is meant to be read.
|
|
79
|
-
*/
|
|
80
|
-
const FINEST_DIAGNOSTIC_BAND = PLACETYPE_SPECIFICITY["microhood"];
|
|
81
|
-
/**
|
|
82
|
-
* The coarse end of the probe window. `country` and no coarser: a name matching at `continent` or `planet` says nothing
|
|
83
|
-
* about a mislabeled admin span, and the walk resolves the country from its own node anyway.
|
|
84
|
-
*/
|
|
85
|
-
const COARSEST_DIAGNOSTIC_BAND = PLACETYPE_SPECIFICITY["country"];
|
|
86
|
-
/**
|
|
87
|
-
* The admin bands `ResolveOpts.diagnoseUnreachable` re-probes, coarse to fine.
|
|
88
|
-
*
|
|
89
|
-
* Derived from `PLACETYPE_SPECIFICITY` rather than typed out, so a placetype added there is probed here without anyone
|
|
90
|
-
* remembering to.
|
|
91
|
-
*/
|
|
92
|
-
const DIAGNOSTIC_BANDS = Object.entries(PLACETYPE_SPECIFICITY)
|
|
93
|
-
.filter(([, rank]) => rank !== undefined && rank <= FINEST_DIAGNOSTIC_BAND && rank >= COARSEST_DIAGNOSTIC_BAND)
|
|
94
|
-
.toSorted((a, b) => a[1] - b[1])
|
|
95
|
-
.map(([placetype]) => placetype);
|
|
96
|
-
const NOOP_TRACE_RECORDER = Object.freeze({
|
|
97
|
-
bind() { },
|
|
98
|
-
gate() { },
|
|
99
|
-
stage() { },
|
|
100
|
-
reachable() { },
|
|
101
|
-
emit() { },
|
|
102
|
-
});
|
|
103
|
-
function createNodeTraceRecorder(sink) {
|
|
104
|
-
const gates = [];
|
|
105
|
-
const stageOrders = [];
|
|
106
|
-
let reachableIn;
|
|
107
|
-
let ctx = null;
|
|
108
|
-
return {
|
|
109
|
-
bind(node, placetype, query, defaultLimit) {
|
|
110
|
-
ctx = { node, placetype, query, defaultLimit };
|
|
111
|
-
},
|
|
112
|
-
gate(name) {
|
|
113
|
-
gates.push(name);
|
|
114
|
-
},
|
|
115
|
-
stage(name, order) {
|
|
116
|
-
stageOrders.push([name, order]);
|
|
117
|
-
},
|
|
118
|
-
reachable(bands) {
|
|
119
|
-
reachableIn = bands;
|
|
120
|
-
},
|
|
121
|
-
emit(picked) {
|
|
122
|
-
if (!ctx)
|
|
123
|
-
return;
|
|
124
|
-
const rankMap = new Map();
|
|
125
|
-
for (const [stage, order] of stageOrders) {
|
|
126
|
-
order.forEach((candidate, index) => {
|
|
127
|
-
let ranks = rankMap.get(candidate);
|
|
128
|
-
if (!ranks) {
|
|
129
|
-
ranks = {};
|
|
130
|
-
rankMap.set(candidate, ranks);
|
|
131
|
-
}
|
|
132
|
-
ranks[stage] = index + 1;
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
const finalOrder = stageOrders.at(-1)?.[1] ?? [];
|
|
136
|
-
const rows = finalOrder.slice(0, TRACE_CANDIDATE_CAP).map((c) => ({
|
|
137
|
-
id: c.id,
|
|
138
|
-
name: c.name,
|
|
139
|
-
country: c.country,
|
|
140
|
-
placetype: c.placetype,
|
|
141
|
-
score: c.score,
|
|
142
|
-
...(c.prominence !== undefined ? { prominence: c.prominence } : {}),
|
|
143
|
-
...(c.importance !== undefined ? { importance: c.importance } : {}),
|
|
144
|
-
...(c.population !== undefined ? { population: c.population } : {}),
|
|
145
|
-
...(c.exactMatch !== undefined ? { exactMatch: c.exactMatch } : {}),
|
|
146
|
-
...(c.containedByQualifier !== undefined ? { containedByQualifier: c.containedByQualifier } : {}),
|
|
147
|
-
ranks: rankMap.get(c) ?? {},
|
|
148
|
-
}));
|
|
149
|
-
sink({
|
|
150
|
-
tag: ctx.node.tag,
|
|
151
|
-
value: ctx.node.value,
|
|
152
|
-
placetype: ctx.placetype,
|
|
153
|
-
query: {
|
|
154
|
-
...(ctx.query.country ? { country: ctx.query.country } : {}),
|
|
155
|
-
...(ctx.query.parentID !== undefined ? { parentID: ctx.query.parentID } : {}),
|
|
156
|
-
...(ctx.query.postcode ? { postcode: ctx.query.postcode } : {}),
|
|
157
|
-
...(ctx.query.regionQualifier ? { regionQualifier: ctx.query.regionQualifier } : {}),
|
|
158
|
-
limit: ctx.query.limit ?? ctx.defaultLimit,
|
|
159
|
-
},
|
|
160
|
-
gates,
|
|
161
|
-
...(reachableIn ? { reachableIn } : {}),
|
|
162
|
-
candidates: rows,
|
|
163
|
-
candidatesTruncated: Math.max(0, finalOrder.length - TRACE_CANDIDATE_CAP),
|
|
164
|
-
picked,
|
|
165
|
-
});
|
|
166
|
-
},
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Pick the completion locality when an admin maps to several coincident same-name candidates (#405). REFERENTIAL
|
|
171
|
-
* likelihood is the PRIMARY signal — the principal city is the populous one, and it can sit FARTHER from the admin
|
|
172
|
-
* centroid than a tiny same-name hamlet (the Niigata case from #403). Nearest centroid breaks a referential tie; a
|
|
173
|
-
* genuine tie (same population AND distance) ABSTAINS rather than guess.
|
|
174
|
-
*
|
|
175
|
-
* ROAD_TO_V9 §2: `compareReferential` is referential DESC with raw population as its own tiebreak, which is the SAME
|
|
176
|
-
* ORDER as the plain `b.population - a.population` it replaced — referential is strictly increasing in population below
|
|
177
|
-
* saturation and constant above it. The abstention check below still reads raw population, deliberately: two megacities
|
|
178
|
-
* that saturate to the same referential score are not tied, and abstaining there would be a new behavior.
|
|
179
|
-
*/
|
|
180
|
-
function pickCompletion(candidates) {
|
|
181
|
-
if (!candidates.length)
|
|
182
|
-
return null;
|
|
183
|
-
if (candidates.length === 1)
|
|
184
|
-
return candidates[0];
|
|
185
|
-
const ranked = [...candidates]
|
|
186
|
-
.map((c) => ({ c, referential: referentialFromPopulation(c.population), population: c.population }))
|
|
187
|
-
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
|
|
188
|
-
.sort((a, b) => compareReferential(a, b) || a.c.distanceKm - b.c.distanceKm)
|
|
189
|
-
.map((x) => x.c);
|
|
190
|
-
const [first, second] = ranked;
|
|
191
|
-
if (first.population === second.population && first.distanceKm === second.distanceKm)
|
|
192
|
-
return null;
|
|
193
|
-
return first;
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Find the first postcode value anywhere in the tree (a one-shot pre-scan; postcode and locality are siblings, so the
|
|
197
|
-
* top-down walk wouldn't otherwise let the locality lookup see it).
|
|
198
|
-
*/
|
|
199
|
-
function firstPostcodeValue(roots) {
|
|
200
|
-
const stack = [...roots];
|
|
201
|
-
while (stack.length) {
|
|
202
|
-
const n = stack.pop();
|
|
203
|
-
// #31 Mechanism 1: a shape-excluded span keeps its tag but contributes nothing — its code
|
|
204
|
-
// must never become the address's postcode (it would poison the country-scope pass's anchor).
|
|
205
|
-
if (n.tag === "postcode" && !isShapeExcludedPostcode(n) && n.value.trim().length)
|
|
206
|
-
return n.value.trim();
|
|
207
|
-
stack.push(...n.children);
|
|
208
|
-
}
|
|
209
|
-
return undefined;
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Span-rescore tier (#370): opt-in last-resort locality recovery. Runs ONLY when the tree resolved NOTHING (the #685
|
|
213
|
-
* brake — never disturb a working coordinate). Enumerates raw-token spans, exact- matches the same-country gazetteer
|
|
214
|
-
* (longest-wins + postcode-consistency gate; see `span-rescore.ts`), and on a hit INJECTS a resolved `locality` node
|
|
215
|
-
* decorated exactly like a normally-resolved one. Default-ON (#370, promoted 2026-06-25); byte-stable opt-out via
|
|
216
|
-
* `opts.spanRescore: false`. Async (it queries the backend), so it's awaited.
|
|
217
|
-
*/
|
|
218
|
-
async function applySpanRescore(roots, raw, backend, opts) {
|
|
219
|
-
if (hasResolvedPlace(roots))
|
|
220
|
-
return; // already resolved — never second-guess a working coordinate
|
|
221
|
-
// Default-ON since 2026-06-25, so this runs on every unresolved tree — a backend hiccup here must
|
|
222
|
-
// degrade to no-rescore, never crash the resolve (the same fall-through the main walk gives).
|
|
223
|
-
let hit;
|
|
224
|
-
try {
|
|
225
|
-
hit = await findRescoreCandidate(raw, roots, backend, {
|
|
226
|
-
country: opts.defaultCountry,
|
|
227
|
-
postcode: firstPostcodeValue(roots),
|
|
228
|
-
gateKm: opts.spanRescoreGateKm,
|
|
229
|
-
// Default-ON (promoted 2026-07-03); explicit `false` opts out — the spanRescore idiom.
|
|
230
|
-
postalCompoundRecovery: opts.postalCompoundRecovery !== false,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
catch {
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
// #942 postal-compound recovery, part 2: when NO city span matched, decorate the FAILED postcode
|
|
237
|
-
// node from its code-shaped token subset ("1382 Kožljek" → the bare "1382" row) — a postcode-tier
|
|
238
|
-
// coordinate FLOOR, strictly subordinate to a recovered locality. Only-on-miss matters: a GeoNames
|
|
239
|
-
// medoid postcode centroid is COARSER than the exact village centroid, and consumers that rank
|
|
240
|
-
// postcode above locality (the eval harness does) would otherwise trade a 0.2 km village pin for a
|
|
241
|
-
// 5 km area centroid. Same unresolved tree, so the #685 brake semantics hold.
|
|
242
|
-
if (!hit && opts.postalCompoundRecovery !== false) {
|
|
243
|
-
try {
|
|
244
|
-
await recoverPostcodeNode(roots, backend, opts.defaultCountry, opts.traceSink);
|
|
245
|
-
}
|
|
246
|
-
catch {
|
|
247
|
-
// degrade to no-recovery, never crash the resolve
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
if (!hit)
|
|
251
|
-
return;
|
|
252
|
-
const node = {
|
|
253
|
-
tag: "locality",
|
|
254
|
-
value: hit.text,
|
|
255
|
-
start: hit.start,
|
|
256
|
-
end: hit.end,
|
|
257
|
-
// No model confidence for a post-hoc recovery; a mid-tier value marks it as recovered, not asserted.
|
|
258
|
-
confidence: 0.5,
|
|
259
|
-
children: [],
|
|
260
|
-
};
|
|
261
|
-
// #1537: the same-span namesake runner-ups, not an empty list. A name the model reads as a `street`
|
|
262
|
-
// ("Springfield", "Berlin", "Moscow") never reaches the admin walk, so this tier is the ONLY thing that
|
|
263
|
-
// resolves it — and decorating with `[]` meant the geocode path's `candidates` held one entry and the
|
|
264
|
-
// dominance margin `declared_ambiguity` reads was uncomputable for exactly the famous-homonym class.
|
|
265
|
-
// The winner is unchanged (see findRescoreCandidate); this is additive.
|
|
266
|
-
decorateNode(node, hit.place, hit.alternatives);
|
|
267
|
-
// `rescore_gated` carries the gate's precision signal as an EXPLICIT handle — NOT folded into the
|
|
268
|
-
// calibrated `confidence`, which would break the isotonic guarantee (a true calibrated 0.83 must not
|
|
269
|
-
// be confused with a rescore plug-in estimate; DeepSeek 2026-06-23). true = postcode gate fired
|
|
270
|
-
// (high-precision); false = ungated (no postcode→point coverage for this country, ~83%-precision).
|
|
271
|
-
node.metadata = { ...node.metadata, span_rescore: true, rescore_gated: hit.gated };
|
|
272
|
-
roots.push(node);
|
|
273
|
-
// #1721 follow-up: this tier answers OFF the walk, and it used to answer off the record too — the famous-name
|
|
274
|
-
// class ("Frankfurt") returned a coordinate beside an empty resolver trace, blinding every retrieval account.
|
|
275
|
-
// One record per rescue keeps the trace's promise: no resolved coordinate without a lookup record.
|
|
276
|
-
if (opts.traceSink) {
|
|
277
|
-
const rec = createNodeTraceRecorder(opts.traceSink);
|
|
278
|
-
rec.bind(node, "locality", opts.defaultCountry ? { country: opts.defaultCountry } : {}, 1);
|
|
279
|
-
rec.gate("span_rescore");
|
|
280
|
-
rec.gate(hit.gated ? "rescore_gated" : "rescore_ungated");
|
|
281
|
-
rec.stage("rescore", [hit.place, ...hit.alternatives]);
|
|
282
|
-
rec.emit({ id: hit.place.id, name: hit.place.name, source: "span_rescore" });
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* #942: find the first confident-but-UNRESOLVED postcode node whose value is a polluted compound ("1382 Kožljek"),
|
|
287
|
-
* resolve its code-shaped token subset as a `postalcode`, and decorate the node from that hit
|
|
288
|
-
* (`postal_compound_recovered` metadata marks the provenance). No-op when every postcode node resolved, the value has
|
|
289
|
-
* no digit-bearing tokens, or the subset equals the full value (then the walk already tried it).
|
|
290
|
-
*/
|
|
291
|
-
async function recoverPostcodeNode(roots, backend, country, traceSink) {
|
|
292
|
-
const stack = [...roots];
|
|
293
|
-
while (stack.length) {
|
|
294
|
-
const n = stack.pop();
|
|
295
|
-
if (n.tag === "postcode" && !n.placeID && n.value.trim()) {
|
|
296
|
-
const code = postcodeCodeSubset(n.value);
|
|
297
|
-
if (!code || code === n.value.trim())
|
|
298
|
-
continue;
|
|
299
|
-
const hits = await backend.findPlace({ text: code, placetype: "postalcode", country, limit: 1 });
|
|
300
|
-
const top = hits.find((h) => h.lat !== 0 || h.lon !== 0);
|
|
301
|
-
if (top) {
|
|
302
|
-
decorateNode(n, top, []);
|
|
303
|
-
n.metadata = { ...n.metadata, postal_compound_recovered: true };
|
|
304
|
-
// The same off-the-record hole the span-rescore record closes (#1721 follow-up).
|
|
305
|
-
if (traceSink) {
|
|
306
|
-
const rec = createNodeTraceRecorder(traceSink);
|
|
307
|
-
rec.bind(n, "postalcode", { ...(country ? { country } : {}), limit: 1 }, 1);
|
|
308
|
-
rec.gate("postal_compound_recovery");
|
|
309
|
-
rec.stage("recovery", hits);
|
|
310
|
-
rec.emit({ id: top.id, name: top.name, source: "postal_compound_recovery" });
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
return; // first postcode node only — one recovery per tree
|
|
314
|
-
}
|
|
315
|
-
if (n.children?.length) {
|
|
316
|
-
stack.push(...n.children);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Postcode-disambiguated locality selection (#370 "Lever A"). The single biggest miss on the EU/AU panel is a
|
|
322
|
-
* same-named town resolved to the WRONG instance — "06260 Saint-Pierre" lands 617 km off — while the postcode that
|
|
323
|
-
* would disambiguate it (06260 → Alpes-Maritimes) sits resolved in the same tree, discarded because the
|
|
324
|
-
* coordinate-picker prefers the (wrong) locality node and never cross- checks it. This post-walk pass closes that loop,
|
|
325
|
-
* backend-agnostically and with no extra query:
|
|
326
|
-
*
|
|
327
|
-
* 1. Find the resolved postcode's coordinate (the trustworthy anchor — a postcode is unambiguous within a country in a way
|
|
328
|
-
* a town name is not).
|
|
329
|
-
* 2. For each resolved locality node farther than `gateKm` from it: re-pick the same-named candidate from the node's
|
|
330
|
-
* already-captured `alternatives` that is NEAREST the postcode and within the gate. This keeps locality granularity
|
|
331
|
-
* at the CORRECT instance.
|
|
332
|
-
* 3. If no alternative reconciles, the locality instance is unreliable — fall its coordinate back to the postcode point
|
|
333
|
-
* (right area, the safe answer) and flag `postcode_city_mismatch`.
|
|
334
|
-
*
|
|
335
|
-
* Only fires where the postcode resolved to a point, so it composes with postcode coverage (#193) — add a country's
|
|
336
|
-
* postcodes and this immediately disambiguates its same-named towns. **Default-ON** since the #370 operator promotion
|
|
337
|
-
* (2026-07-04, commit `0010bb8c`) — `opts.postcodeConsistency: false` opts out, and the pass is byte-stable on every
|
|
338
|
-
* tree with no resolved postcode point (the `!anchor` early return below).
|
|
339
|
-
*/
|
|
340
|
-
function applyPostcodeConsistency(roots, gateKm) {
|
|
341
|
-
// The resolved postcode anchor (first one with a real coordinate).
|
|
342
|
-
let anchor = null;
|
|
343
|
-
const findAnchor = [...roots];
|
|
344
|
-
while (findAnchor.length) {
|
|
345
|
-
const n = findAnchor.pop();
|
|
346
|
-
// #31 Mechanism 1: a shape-excluded span never anchors the consistency re-pick either.
|
|
347
|
-
if (n.tag === "postcode" && !isShapeExcludedPostcode(n) && isResolvedWithCoord(n)) {
|
|
348
|
-
anchor = { lat: n.lat, lon: n.lon };
|
|
349
|
-
break;
|
|
350
|
-
}
|
|
351
|
-
findAnchor.push(...n.children);
|
|
352
|
-
}
|
|
353
|
-
if (!anchor)
|
|
354
|
-
return; // no postcode→point — nothing to disambiguate against (gate can't fire)
|
|
355
|
-
const stack = [...roots];
|
|
356
|
-
while (stack.length) {
|
|
357
|
-
const node = stack.pop();
|
|
358
|
-
stack.push(...node.children);
|
|
359
|
-
if ((node.tag !== "locality" && node.tag !== "dependent_locality") || !isResolvedWithCoord(node))
|
|
360
|
-
continue;
|
|
361
|
-
if (haversineKm(anchor.lat, anchor.lon, node.lat, node.lon) <= gateKm)
|
|
362
|
-
continue; // already consistent
|
|
363
|
-
// Re-pick: the same-named candidate nearest the postcode, within the gate. `alternatives` is
|
|
364
|
-
// typed `unknown[]` on the node (decoder/types.ts can't import resolver types) — they ARE the
|
|
365
|
-
// `ResolvedPlace` runner-ups decorateNode attached, so the cast is sound.
|
|
366
|
-
const alts = node.alternatives ?? [];
|
|
367
|
-
const reconciling = alts
|
|
368
|
-
.filter((a) => a.lat !== 0 || a.lon !== 0)
|
|
369
|
-
.map((a) => ({ a, d: haversineKm(anchor.lat, anchor.lon, a.lat, a.lon) }))
|
|
370
|
-
.filter((x) => x.d <= gateKm)
|
|
371
|
-
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
|
|
372
|
-
.sort((x, y) => x.d - y.d)[0];
|
|
373
|
-
if (reconciling) {
|
|
374
|
-
// Swap to the consistent instance; the displaced winner becomes an alternative.
|
|
375
|
-
const displaced = {
|
|
376
|
-
id: 0,
|
|
377
|
-
name: String(node.metadata?.["resolver_name"] ?? node.value),
|
|
378
|
-
placetype: "locality",
|
|
379
|
-
country: reconciling.a.country,
|
|
380
|
-
lat: node.lat,
|
|
381
|
-
lon: node.lon,
|
|
382
|
-
score: 0,
|
|
383
|
-
};
|
|
384
|
-
const rest = alts.filter((a) => a !== reconciling.a);
|
|
385
|
-
decorateNode(node, reconciling.a, [displaced, ...rest]);
|
|
386
|
-
node.metadata = { ...node.metadata, postcode_repicked: true };
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
|
-
// No same-named instance near the postcode → the town is unreliable; trust the postcode's area.
|
|
390
|
-
node.lat = anchor.lat;
|
|
391
|
-
node.lon = anchor.lon;
|
|
392
|
-
node.metadata = { ...node.metadata, postcode_city_mismatch: true, coordinate_source: "postcode_fallback" };
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
60
|
class WOFResolver {
|
|
396
61
|
#backend;
|
|
397
62
|
/**
|
|
@@ -508,7 +173,9 @@ class WOFResolver {
|
|
|
508
173
|
postcode: state.postcode,
|
|
509
174
|
defaultCountry: state.defaultCountry,
|
|
510
175
|
...(shapeVerdict?.narrowing !== undefined ? { candidateSystems: shapeVerdict.narrowing } : {}),
|
|
511
|
-
...(opts.
|
|
176
|
+
...(opts.postcodeCountryCoherenceThresholdKm !== undefined
|
|
177
|
+
? { thresholdKm: opts.postcodeCountryCoherenceThresholdKm }
|
|
178
|
+
: {}),
|
|
512
179
|
});
|
|
513
180
|
if (postcodeScope) {
|
|
514
181
|
// The override. `hardCountry` is not cleared because it is never consulted while a
|
|
@@ -526,9 +193,9 @@ class WOFResolver {
|
|
|
526
193
|
stampPostcodeCountryScope(newRoots, postcodeScope);
|
|
527
194
|
}
|
|
528
195
|
// The pre-scope's receipt (#1735): consumers that select country-scoped artifacts AFTER the walk
|
|
529
|
-
// (geocode-core's rooftop-
|
|
196
|
+
// (geocode-core's rooftop-extract second pass) key on a scope stamp. Without this, an address whose
|
|
530
197
|
// country was right FROM THE TREE never writes one — the #42 pass stays silent because the default
|
|
531
|
-
// is already coherent — and the FR rooftop
|
|
198
|
+
// is already coherent — and the FR rooftop extract silently stops loading for "…, France" inputs
|
|
532
199
|
// under a non-FR locale (six board venue rows dropped from rooftop to city centroid; the battery
|
|
533
200
|
// caught it).
|
|
534
201
|
if (explicitScope) {
|
|
@@ -560,15 +227,15 @@ class WOFResolver {
|
|
|
560
227
|
// resolve (a foreign subdivision — "Montreal QC" under a US locale). The default filter discarded "QC" and
|
|
561
228
|
// force-matched the locality to the US namesake; this re-resolves the subdivision + its same-named locality
|
|
562
229
|
// under the subdivision's OWN country. Disjoint from the two passes above (unresolved region + resolved
|
|
563
|
-
// locality); evidence-
|
|
230
|
+
// locality); evidence-conditional + byte-stable on the domestic path (a US region resolves under `US`).
|
|
564
231
|
await applyRegionCountryCoherence(newRoots, this.#backend, state.defaultCountry);
|
|
565
232
|
}
|
|
566
|
-
// Postcode-consistency (#370 "
|
|
233
|
+
// Postcode-consistency (#370 "Change A"): default-ON (promoted 2026-07-04 — the corrected check:
|
|
567
234
|
// FI 231/0, SI 37/6, CZ 47/2, US byte-flat; see the ResolveOpts docstring). After the admin walk
|
|
568
235
|
// (needs both the locality and the postcode resolved) and before the street tiers (which key off
|
|
569
236
|
// the postcode/street, not the locality coordinate this adjusts). `false` opts out, byte-stable.
|
|
570
237
|
if (opts.postcodeConsistency !== false) {
|
|
571
|
-
applyPostcodeConsistency(newRoots, opts.
|
|
238
|
+
applyPostcodeConsistency(newRoots, opts.postcodeConsistencyThresholdKm ?? 50);
|
|
572
239
|
}
|
|
573
240
|
// Address-point tier (#476): opt-in street-level exact match. After the admin walk so the
|
|
574
241
|
// tier can never disturb admin attribution — it only ADDS the precise coordinate. Byte-stable
|
|
@@ -577,7 +244,7 @@ class WOFResolver {
|
|
|
577
244
|
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback);
|
|
578
245
|
}
|
|
579
246
|
// Interpolation tier (#483): strictly AFTER the exact-point block so an estimate can never
|
|
580
|
-
// override a real situs point (applyInterpolation also
|
|
247
|
+
// override a real situs point (applyInterpolation also checks on resolution_tier). Opt-in;
|
|
581
248
|
// byte-stable when opts.interpolation is absent.
|
|
582
249
|
if (opts.interpolation) {
|
|
583
250
|
applyInterpolation(newRoots, opts.interpolation, opts.interpolationRadiusCalibration);
|
|
@@ -600,7 +267,7 @@ class WOFResolver {
|
|
|
600
267
|
// Street-centroid tier (#1042): LAST, after span-rescore, so it can (a) union the span-rescore-recovered
|
|
601
268
|
// country into its FR/national country hints (a placer-misrouted street — "Rue Sainte-Catherine" → IT — leaves
|
|
602
269
|
// admin unresolved, and only span-rescore recovers the FR country signal) and (b) override a coarse recovered
|
|
603
|
-
// locality with the exact street centroid.
|
|
270
|
+
// locality with the exact street centroid. Applies only when there is no house number + no existing street-level tier, so a
|
|
604
271
|
// rooftop query is untouched; byte-stable when opts.streetCentroids absent.
|
|
605
272
|
if (opts.streetCentroids) {
|
|
606
273
|
applyStreetCentroid(newRoots, tree.raw, opts.streetCentroids, opts.streetCountryHints ?? []);
|
|
@@ -647,7 +314,20 @@ class WOFResolver {
|
|
|
647
314
|
// stripped of their resolve contribution — the walk does not look them up. Digit-only excluded
|
|
648
315
|
// spans were retagged to `house_number` and flow through their correct sibling placetype.
|
|
649
316
|
if (placetype && state.lookupsRemaining > 0 && node.value.trim().length && !isShapeExcludedPostcode(node)) {
|
|
650
|
-
|
|
317
|
+
let picked = await this.#lookupAndPick(node, placetype, parentResolved, state);
|
|
318
|
+
// A compound JP municipality as a scoped pair: the parent fallback is withheld on the tail probe only.
|
|
319
|
+
if (!picked && node.tag === "municipality") {
|
|
320
|
+
picked = await resolveCompoundMunicipality(node.value, parentResolved, () => state.lookupsRemaining > 0, async (value, scope, parentFallback) => {
|
|
321
|
+
const prior = state.parentFallback;
|
|
322
|
+
state.parentFallback = prior && parentFallback;
|
|
323
|
+
try {
|
|
324
|
+
return await this.#lookupAndPick({ ...node, value }, placetype, scope, state);
|
|
325
|
+
}
|
|
326
|
+
finally {
|
|
327
|
+
state.parentFallback = prior;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
651
331
|
if (picked) {
|
|
652
332
|
resolved = picked.top;
|
|
653
333
|
decorateNode(decorated, picked.top, picked.alternatives);
|
|
@@ -738,7 +418,7 @@ class WOFResolver {
|
|
|
738
418
|
query.parentID = parentResolved.id;
|
|
739
419
|
}
|
|
740
420
|
// #194: a resolved parent's country wins, then the caller's `defaultCountry`, then the confident
|
|
741
|
-
// placer `hardCountry`. All three are a HARD candidate filter. The placer's `hardCountry` is
|
|
421
|
+
// placer `hardCountry`. All three are a HARD candidate filter. The placer's `hardCountry` is conditional
|
|
742
422
|
// upstream on high confidence (so it only fires when the model is sure), and on a miss the node is
|
|
743
423
|
// left UNRESOLVED rather than re-resolved globally: the off-continent rows are precisely the ones
|
|
744
424
|
// whose locality isn't in the country's gazetteer slice, so a global retry would just re-admit the
|
|
@@ -813,7 +493,7 @@ class WOFResolver {
|
|
|
813
493
|
// set — its index is country-scoped by construction, so it is not the fold this branch
|
|
814
494
|
// exists to avoid (the B3 tests hold that path).
|
|
815
495
|
if (placetype === "postalcode" && !query.country && state.postcodeFormatCountries?.length) {
|
|
816
|
-
rec.
|
|
496
|
+
rec.check("postcode_format_probe");
|
|
817
497
|
let best;
|
|
818
498
|
for (const impliedCountry of state.postcodeFormatCountries) {
|
|
819
499
|
try {
|
|
@@ -839,12 +519,12 @@ class WOFResolver {
|
|
|
839
519
|
try {
|
|
840
520
|
candidates = await this.#backend.findPlace(query);
|
|
841
521
|
rec.stage("initial", candidates);
|
|
842
|
-
// #1731: the backend's interior region-scope fallback, surfaced as a
|
|
522
|
+
// #1731: the backend's interior region-scope fallback, surfaced as a check — the resolver
|
|
843
523
|
// never sees the scoped probe miss, only the stamp the re-admitted rows carry.
|
|
844
524
|
if (candidates[0]?.regionScopeMiss) {
|
|
845
|
-
rec.
|
|
525
|
+
rec.check("region_scope_miss");
|
|
846
526
|
}
|
|
847
|
-
// Parent soft-
|
|
527
|
+
// Parent soft-filtering: `parentID` is a HARD descendant filter in the backend, which wrongly
|
|
848
528
|
// zeroes the result when the parent resolved wrong OR the gazetteer hierarchy is incomplete
|
|
849
529
|
// (a real locality whose `ancestors` chain is missing its region). Rather than turn a
|
|
850
530
|
// resolvable node into an unresolved one, retry once WITHOUT the parent constraint — we
|
|
@@ -852,7 +532,7 @@ class WOFResolver {
|
|
|
852
532
|
// this still can't wander to a foreign place. Same logical resolution → no extra budget.
|
|
853
533
|
if (!candidates.length && state.parentFallback && query.parentID !== undefined) {
|
|
854
534
|
delete query.parentID;
|
|
855
|
-
rec.
|
|
535
|
+
rec.check("parent_fallback_retry");
|
|
856
536
|
candidates = await this.#backend.findPlace(query);
|
|
857
537
|
rec.stage("parent_fallback", candidates);
|
|
858
538
|
}
|
|
@@ -869,10 +549,10 @@ class WOFResolver {
|
|
|
869
549
|
// Defensive: a backend failure should not abort the whole tree walk. Leave the node with
|
|
870
550
|
// its classifier attribution intact.
|
|
871
551
|
//
|
|
872
|
-
// The reason rides the
|
|
552
|
+
// The reason rides the check name. A bare `backend_error` cannot tell a closed database from a
|
|
873
553
|
// finalized statement from a genuine query fault, which is what made three full-board constraint
|
|
874
554
|
// runs unreadable — 64 of 591 rows errored and the census could not say why.
|
|
875
|
-
rec.
|
|
555
|
+
rec.check(`backend_error: ${error.message}`);
|
|
876
556
|
rec.emit(null);
|
|
877
557
|
return null;
|
|
878
558
|
}
|
|
@@ -894,7 +574,7 @@ class WOFResolver {
|
|
|
894
574
|
? { coordinate_source: "postcode_prefix" }
|
|
895
575
|
: {}),
|
|
896
576
|
};
|
|
897
|
-
rec.
|
|
577
|
+
rec.check("postcode_prefix_prior");
|
|
898
578
|
const prefixPlace = postcodePrefixResolvedPlace(probe.prefix, probe.node, state.postcodePrefixIndex);
|
|
899
579
|
rec.emit({ id: prefixPlace.id, name: prefixPlace.name, source: "postcode_prefix" });
|
|
900
580
|
return {
|
|
@@ -912,7 +592,7 @@ class WOFResolver {
|
|
|
912
592
|
// with no locality candidates at all, a country hit resolves the span outright.
|
|
913
593
|
const isBareRace = placetype === "locality" && node === state.bareLocalityNode;
|
|
914
594
|
if (isBareRace) {
|
|
915
|
-
rec.
|
|
595
|
+
rec.check("bare_race");
|
|
916
596
|
}
|
|
917
597
|
const bareCountry = isBareRace ? await bareCountryCandidate(this.#backend, node.value, query.country) : null;
|
|
918
598
|
const bareRegion = isBareRace ? await bareRegionCandidate(this.#backend, node.value, query.country) : null;
|
|
@@ -921,7 +601,7 @@ class WOFResolver {
|
|
|
921
601
|
// outright (the same precedence the dominance rule below applies).
|
|
922
602
|
const admin = pickLargerAdmin(bareCountry, bareRegion);
|
|
923
603
|
if (admin) {
|
|
924
|
-
rec.
|
|
604
|
+
rec.check("empty_admin_pick");
|
|
925
605
|
rec.emit({ id: admin.id, name: admin.name, source: "empty_admin" });
|
|
926
606
|
return {
|
|
927
607
|
top: admin,
|
|
@@ -973,13 +653,15 @@ class WOFResolver {
|
|
|
973
653
|
const tier = Number(b.exactMatch ?? false) - Number(a.exactMatch ?? false);
|
|
974
654
|
if (tier !== 0)
|
|
975
655
|
return tier;
|
|
976
|
-
|
|
977
|
-
|
|
656
|
+
// A candidate with no country gets no posterior mass rather than an index by `undefined`, which reads
|
|
657
|
+
// every unknown-country row as the same key.
|
|
658
|
+
const aKey = (a.prominence ?? a.score) + w * (a.country === undefined ? 0 : (post[a.country] ?? 0));
|
|
659
|
+
const bKey = (b.prominence ?? b.score) + w * (b.country === undefined ? 0 : (post[b.country] ?? 0));
|
|
978
660
|
return bKey - aKey || b.score - a.score;
|
|
979
661
|
});
|
|
980
662
|
rec.stage("anchor", ranked);
|
|
981
663
|
}
|
|
982
|
-
// Locale-country soft prior (#27) — the #912
|
|
664
|
+
// Locale-country soft prior (#27) — the #912 change's other half, at the tier that decides a bare
|
|
983
665
|
// toponym the model tagged `locality`. `--locale en-GB "Whitby"` answers Whitby, Ontario while
|
|
984
666
|
// `--default-country GB "Whitby"` answers the gold, because the CLI DROPS the locale-inferred
|
|
985
667
|
// country for this shape rather than choosing between "hard filter" and "nothing". This is the
|
|
@@ -1016,15 +698,15 @@ class WOFResolver {
|
|
|
1016
698
|
// containment partition (the qualifier is the address's own text; evidence outranks a prior).
|
|
1017
699
|
// Same stand-down as fame: an anchor posterior is postcode evidence and silences the prior.
|
|
1018
700
|
if (state.capitalLevel) {
|
|
1019
|
-
ranked =
|
|
701
|
+
ranked = promoteCapitalsWithReceipt(ranked, state.capitalLevel, node);
|
|
1020
702
|
rec.stage("capital", ranked);
|
|
1021
703
|
}
|
|
1022
704
|
}
|
|
1023
705
|
// Admin-containment partition (#1717 stage 2): the LAST soft re-rank, after the anchor/fame keys
|
|
1024
706
|
// above, because the qualifier is the address's OWN text — evidence, which outranks a prior. The
|
|
1025
|
-
// backend already put contained rows first; this second partition is
|
|
707
|
+
// backend already put contained rows first; this second partition is required, not belt-and-
|
|
1026
708
|
// braces: `rankByImportance` just re-ordered the exact tier by fame, and Richmond, Virginia
|
|
1027
|
-
// outscores Richmond, North Yorkshire on importance — without this the
|
|
709
|
+
// outscores Richmond, North Yorkshire on importance — without this the change loses exactly where
|
|
1028
710
|
// fame disagrees with the qualifier (the shared-function partition, tier-safe + stable, so it can
|
|
1029
711
|
// never promote a contained partial match over an exact one). No stamps → identity → byte-stable
|
|
1030
712
|
// on any backend that ignored `regionQualifier`.
|
|
@@ -1050,7 +732,7 @@ class WOFResolver {
|
|
|
1050
732
|
}
|
|
1051
733
|
const top = ranked[0];
|
|
1052
734
|
if (top.score < state.minWinningScore) {
|
|
1053
|
-
rec.
|
|
735
|
+
rec.check("min_score_reject");
|
|
1054
736
|
rec.emit(null);
|
|
1055
737
|
return null;
|
|
1056
738
|
}
|
|
@@ -1059,16 +741,16 @@ class WOFResolver {
|
|
|
1059
741
|
// DOMINATE the locality winner by {@link BARE_REGION_DOMINANCE_LOG10} in log-population:
|
|
1060
742
|
// prominence saturates at the backend's populationBoost cap, so the margin is measured on the
|
|
1061
743
|
// raw populations, and the margin is what keeps bare "New York" on the city (state 19.6M vs
|
|
1062
|
-
// city 8.8M = 0.35, under the
|
|
744
|
+
// city 8.8M = 0.35, under the threshold) while bare "Georgia" promotes to the 11M state over the
|
|
1063
745
|
// Vermont hamlet (margin 3.4). A bare name with no admin namesake never reaches these lines,
|
|
1064
746
|
// and the displaced locality stays first among the alternatives either way.
|
|
1065
747
|
if (bareCountry && (bareCountry.prominence ?? bareCountry.score) > (top.prominence ?? top.score)) {
|
|
1066
|
-
rec.
|
|
748
|
+
rec.check("bare_country_repick");
|
|
1067
749
|
rec.emit({ id: bareCountry.id, name: bareCountry.name, source: "bare_country" });
|
|
1068
750
|
return { top: bareCountry, alternatives: ranked, metadata: { bare_country_repick: true } };
|
|
1069
751
|
}
|
|
1070
752
|
if (bareRegion && logPopulation(bareRegion) >= logPopulation(top) + BARE_REGION_DOMINANCE_LOG10) {
|
|
1071
|
-
rec.
|
|
753
|
+
rec.check("bare_region_repick");
|
|
1072
754
|
rec.emit({ id: bareRegion.id, name: bareRegion.name, source: "bare_region" });
|
|
1073
755
|
return { top: bareRegion, alternatives: ranked, metadata: { bare_region_repick: true } };
|
|
1074
756
|
}
|
|
@@ -1077,18 +759,37 @@ class WOFResolver {
|
|
|
1077
759
|
// identity/coordinate are unchanged; only `metadata.resolution_quality` is stamped downstream.
|
|
1078
760
|
if (isPlacetypeFallback(placetype, top.placetype)) {
|
|
1079
761
|
top.resolutionQuality = "fallback";
|
|
1080
|
-
rec.
|
|
762
|
+
rec.check("placetype_fallback");
|
|
1081
763
|
}
|
|
1082
764
|
rec.emit({ id: top.id, name: top.name, source: "ranked" });
|
|
1083
|
-
// The
|
|
1084
|
-
//
|
|
1085
|
-
//
|
|
1086
|
-
//
|
|
765
|
+
// The trace stamps (#1717 stage 2 / #1719's rule): an opted-in mechanism that cannot fire — a
|
|
766
|
+
// pre-sidecar artifact, an incapable backend — must say so in the result, not degrade silently.
|
|
767
|
+
// The parse-side census cannot see resolver mechanisms, so these stamps are its census surface.
|
|
768
|
+
// admin_containment asserts a question was asked; variant_alias_exemption (#1893) asserts the
|
|
769
|
+
// winning candidate reached the top BECAUSE the exemption spared it the cross-country penalty —
|
|
770
|
+
// the winner-level firing receipt, same posture as capital_promotion.
|
|
771
|
+
const pickMetadata = {
|
|
772
|
+
...(containmentEligible ? { admin_containment: adminContainmentVerdict(ranked) } : {}),
|
|
773
|
+
...(top.variantAliasExempted === true ? { variant_alias_exemption: true } : {}),
|
|
774
|
+
};
|
|
1087
775
|
return {
|
|
1088
776
|
top,
|
|
1089
777
|
alternatives: ranked.slice(1),
|
|
1090
|
-
...(
|
|
778
|
+
...(Object.keys(pickMetadata).length ? { metadata: pickMetadata } : {}),
|
|
1091
779
|
};
|
|
1092
780
|
}
|
|
1093
781
|
}
|
|
1094
|
-
|
|
782
|
+
/**
|
|
783
|
+
* #1880's promotion plus its firing receipt, in one place: when the promotion changes the race's leading candidate, the
|
|
784
|
+
* node is stamped `capital_promotion` with the promoted candidate's country — the same posture as
|
|
785
|
+
* `postcode_country_scope`, a mechanism reporting that it SPOKE apart from whether the outcome moved, so an unchanged
|
|
786
|
+
* verdict downstream can never mean either "harmless" or "never ran". Metadata-only; nothing reads it to rank.
|
|
787
|
+
*/
|
|
788
|
+
function promoteCapitalsWithReceipt(ranked, level, node) {
|
|
789
|
+
const promoted = promoteCapitals(ranked, level);
|
|
790
|
+
if (promoted[0] !== ranked[0]) {
|
|
791
|
+
node.metadata = { ...node.metadata, capital_promotion: promoted[0].country ?? true };
|
|
792
|
+
}
|
|
793
|
+
return promoted;
|
|
794
|
+
}
|
|
795
|
+
//# sourceMappingURL=index.js.map
|