@mailwoman/resolver 8.0.0 → 8.2.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/fold-name.ts +20 -0
- package/index.ts +13 -2
- package/out/fold-name.d.ts +13 -0
- package/out/fold-name.d.ts.map +1 -0
- package/out/fold-name.js +20 -0
- package/out/fold-name.js.map +1 -0
- package/out/index.d.ts +2 -2
- package/out/index.d.ts.map +1 -1
- package/out/index.js +1 -1
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +40 -6
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +24 -5
- package/out/plausibility.js.map +1 -1
- package/out/remote-resolver.d.ts +21 -7
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +3 -1
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +30 -10
- 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.map +1 -1
- package/out/resolve.js +29 -410
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts +18 -6
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +13 -6
- package/out/span-rescore.js.map +1 -1
- package/out/street-evidence.d.ts +30 -10
- package/out/street-evidence.d.ts.map +1 -1
- package/out/street-evidence.js +8 -6
- package/out/street-evidence.js.map +1 -1
- package/out/street-tier.d.ts +39 -0
- package/out/street-tier.d.ts.map +1 -0
- package/out/street-tier.js +415 -0
- package/out/street-tier.js.map +1 -0
- package/package.json +4 -4
- package/plausibility.ts +51 -9
- package/remote-resolver.ts +23 -7
- package/rerank.ts +37 -12
- package/resolve.ts +80 -481
- package/span-rescore.ts +33 -11
- package/street-evidence.ts +36 -15
- package/street-tier.ts +498 -0
package/out/resolve.js
CHANGED
|
@@ -15,7 +15,9 @@ import { matchCountry, matchSubdivision } from "@mailwoman/codex/country";
|
|
|
15
15
|
import { isStreetDirectionalToken } from "@mailwoman/codex/us";
|
|
16
16
|
import { DEFAULT_PLACETYPE_MAP, isPlacetypeFallback, } from "@mailwoman/core/resolver";
|
|
17
17
|
import { haversineKm } from "@mailwoman/spatial";
|
|
18
|
+
import { foldName } from "./fold-name.js";
|
|
18
19
|
import { findRescoreCandidate, hasResolvedPlace, postcodeCodeSubset } from "./span-rescore.js";
|
|
20
|
+
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "./street-tier.js";
|
|
19
21
|
/**
|
|
20
22
|
* Build a `Resolver` backed by a `ResolverBackend`. The backend can be any concrete impl structurally compatible with
|
|
21
23
|
* `PlaceLookup` — e.g. `new WOFSqlitePlaceLookup({ databasePath }).asResolverBackend()` or a fake for tests.
|
|
@@ -30,10 +32,11 @@ export function createWOFResolver(backend) {
|
|
|
30
32
|
* population AND distance) ABSTAINS rather than guess.
|
|
31
33
|
*/
|
|
32
34
|
function pickCompletion(candidates) {
|
|
33
|
-
if (candidates.length
|
|
35
|
+
if (!candidates.length)
|
|
34
36
|
return null;
|
|
35
37
|
if (candidates.length === 1)
|
|
36
38
|
return candidates[0];
|
|
39
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
|
|
37
40
|
const ranked = [...candidates].sort((a, b) => b.population - a.population || a.distanceKm - b.distanceKm);
|
|
38
41
|
const [first, second] = ranked;
|
|
39
42
|
if (first.population === second.population && first.distanceKm === second.distanceKm)
|
|
@@ -46,409 +49,14 @@ function pickCompletion(candidates) {
|
|
|
46
49
|
*/
|
|
47
50
|
function firstPostcodeValue(roots) {
|
|
48
51
|
const stack = [...roots];
|
|
49
|
-
while (stack.length
|
|
52
|
+
while (stack.length) {
|
|
50
53
|
const n = stack.pop();
|
|
51
|
-
if (n.tag === "postcode" && n.value.trim().length
|
|
54
|
+
if (n.tag === "postcode" && n.value.trim().length)
|
|
52
55
|
return n.value.trim();
|
|
53
56
|
stack.push(...n.children);
|
|
54
57
|
}
|
|
55
58
|
return undefined;
|
|
56
59
|
}
|
|
57
|
-
/** Street-name component tags that, with the street node itself, reconstruct the full street string. */
|
|
58
|
-
const STREET_NAME_TAGS = new Set(["street", "street_prefix", "street_prefix_particle", "street_suffix"]);
|
|
59
|
-
/**
|
|
60
|
-
* Reassemble the full street string from the street node's subtree (#483 coverage fix). The parser nests the
|
|
61
|
-
* directional/suffix as `street_prefix`/`street_suffix` CHILDREN of `street` (containment.ts), so `street.value` alone
|
|
62
|
-
* is the bare base name ("Sheldon" for "East Sheldon Rd") — which misses the coordinate shards keyed on the FULL
|
|
63
|
-
* normalized name. Collect street + its prefix/particle/suffix descendants (NOT house_number/unit, which also nest
|
|
64
|
-
* under street), order by span offset, and join.
|
|
65
|
-
*/
|
|
66
|
-
function assembleStreetValue(streetNode, directionalUnit) {
|
|
67
|
-
const parts = [];
|
|
68
|
-
const stack = [streetNode];
|
|
69
|
-
while (stack.length > 0) {
|
|
70
|
-
const n = stack.pop();
|
|
71
|
-
if (STREET_NAME_TAGS.has(n.tag) && n.value.trim()) {
|
|
72
|
-
parts.push(n);
|
|
73
|
-
}
|
|
74
|
-
stack.push(...n.children);
|
|
75
|
-
}
|
|
76
|
-
// #718 admin-tail: a directional quadrant the model mis-tagged `unit` ("1532 Taylor Street NE" →
|
|
77
|
-
// [unit] "NE") folds back into the street key by span order, so the situs/interp lookup matches the
|
|
78
|
-
// shard's "taylor street northeast" (the lookup normalizer expands the abbreviation). Lookup-key
|
|
79
|
-
// only — the parse output and admin resolution are untouched. Byte-stable when absent (undefined).
|
|
80
|
-
if (directionalUnit && directionalUnit.value.trim()) {
|
|
81
|
-
parts.push(directionalUnit);
|
|
82
|
-
}
|
|
83
|
-
parts.sort((a, b) => a.start - b.start);
|
|
84
|
-
return parts.map((n) => n.value.trim()).join(" ");
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Directional quadrant values the model sometimes emits as a `unit` node instead of inside the street subtree (#718
|
|
88
|
-
* admin-tail diagnostic: ~19% of the admin-fallback tail, 83% of DC). Folded into the street lookup key by
|
|
89
|
-
* {@link assembleStreetValue}; the situs/interp lookup normalizer expands the abbreviation ("ne" → "northeast") so the
|
|
90
|
-
* shard's full street name matches.
|
|
91
|
-
*/
|
|
92
|
-
// The 8 USPS cardinals/intercardinals (abbrev or name) — @codex/us owns the canonical table (#215).
|
|
93
|
-
const isDirectionalUnit = (value) => isStreetDirectionalToken(value.replace(/\./g, ""));
|
|
94
|
-
/**
|
|
95
|
-
* Address-point tier (#476): find `street` + `house_number` in the tree (first occurrence, depth-first), scope by the
|
|
96
|
-
* tree's postcode/locality values, and on an exact hit stamp the point onto the STREET node's metadata. Additive only —
|
|
97
|
-
* admin resolution is never altered.
|
|
98
|
-
*/
|
|
99
|
-
/**
|
|
100
|
-
* Half-width (degrees) of the bbox derived from a resolved locality centroid for the #247 OSM bbox fall-through. ~0.25°
|
|
101
|
-
* ≈ 28 km N–S — generous enough for a large metro whose centroid sits off the queried point, while the EXACT `(street,
|
|
102
|
-
* number)` match keeps a cross-commune collision rare. The proper fix is per-point scope backfill (the OSM association
|
|
103
|
-
* / point-in-polygon pass, #250); this is the coverage stopgap until then.
|
|
104
|
-
*/
|
|
105
|
-
const LOCALITY_BBOX_RADIUS_DEG = 0.25;
|
|
106
|
-
function applyAddressPoint(roots, lookup, bboxFallback) {
|
|
107
|
-
let street;
|
|
108
|
-
let houseNumber;
|
|
109
|
-
let directionalUnit;
|
|
110
|
-
let localityNode;
|
|
111
|
-
let locality;
|
|
112
|
-
let postcode;
|
|
113
|
-
const stack = [...roots];
|
|
114
|
-
while (stack.length > 0) {
|
|
115
|
-
const n = stack.pop();
|
|
116
|
-
if (n.tag === "street" && !street) {
|
|
117
|
-
street = n;
|
|
118
|
-
}
|
|
119
|
-
if (n.tag === "house_number" && !houseNumber) {
|
|
120
|
-
houseNumber = n;
|
|
121
|
-
}
|
|
122
|
-
if (n.tag === "unit" && !directionalUnit && isDirectionalUnit(n.value)) {
|
|
123
|
-
directionalUnit = n;
|
|
124
|
-
}
|
|
125
|
-
if (n.tag === "locality" && !localityNode && n.value.trim()) {
|
|
126
|
-
localityNode = n;
|
|
127
|
-
locality = n.value.trim();
|
|
128
|
-
}
|
|
129
|
-
if (n.tag === "postcode" && !postcode && n.value.trim()) {
|
|
130
|
-
postcode = n.value.trim();
|
|
131
|
-
}
|
|
132
|
-
stack.push(...n.children);
|
|
133
|
-
}
|
|
134
|
-
if (!street || !houseNumber)
|
|
135
|
-
return;
|
|
136
|
-
// #247 OSM bbox fall-through: when enabled (an OSM shard is wired) and the locality resolved to a
|
|
137
|
-
// coordinate, scope a final `(street, number)` probe by the locality's box — recovering OSM points that
|
|
138
|
-
// carry no postcode/locality tag of their own. US situs never enables it, so its probes are byte-identical.
|
|
139
|
-
let bbox;
|
|
140
|
-
if (bboxFallback && localityNode?.lat != null && localityNode.lon != null) {
|
|
141
|
-
bbox = {
|
|
142
|
-
minLat: localityNode.lat - LOCALITY_BBOX_RADIUS_DEG,
|
|
143
|
-
maxLat: localityNode.lat + LOCALITY_BBOX_RADIUS_DEG,
|
|
144
|
-
minLon: localityNode.lon - LOCALITY_BBOX_RADIUS_DEG,
|
|
145
|
-
maxLon: localityNode.lon + LOCALITY_BBOX_RADIUS_DEG,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
const hit = lookup.find({
|
|
149
|
-
street: assembleStreetValue(street, directionalUnit),
|
|
150
|
-
number: houseNumber.value,
|
|
151
|
-
postcode,
|
|
152
|
-
locality,
|
|
153
|
-
bbox,
|
|
154
|
-
});
|
|
155
|
-
if (!hit)
|
|
156
|
-
return;
|
|
157
|
-
street.metadata = {
|
|
158
|
-
...street.metadata,
|
|
159
|
-
address_point: { lat: hit.lat, lon: hit.lon, source: hit.source, release: hit.release },
|
|
160
|
-
resolution_tier: "address_point",
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* House-number interpolation tier (#483): the third rung, consulted ONLY when the exact address-point tier
|
|
165
|
-
* ({@link applyAddressPoint}) did NOT already stamp the street node (`resolution_tier === "address_point"`). That gate
|
|
166
|
-
* IS the "after the exact-point fall-through" — an estimate never overwrites a real situs point. Postcode-scoped (no
|
|
167
|
-
* locality — the interpolators abstain statewide without a postcode). Stamps a DISTINCT metadata key
|
|
168
|
-
* (`interpolated_point`, never `address_point`). Additive only — admin resolution is untouched.
|
|
169
|
-
*/
|
|
170
|
-
function applyInterpolation(roots, lookup, radiusCalibration) {
|
|
171
|
-
let street;
|
|
172
|
-
let houseNumber;
|
|
173
|
-
let directionalUnit;
|
|
174
|
-
let postcode;
|
|
175
|
-
const stack = [...roots];
|
|
176
|
-
while (stack.length > 0) {
|
|
177
|
-
const n = stack.pop();
|
|
178
|
-
if (n.tag === "street" && !street) {
|
|
179
|
-
street = n;
|
|
180
|
-
}
|
|
181
|
-
if (n.tag === "house_number" && !houseNumber) {
|
|
182
|
-
houseNumber = n;
|
|
183
|
-
}
|
|
184
|
-
if (n.tag === "unit" && !directionalUnit && isDirectionalUnit(n.value)) {
|
|
185
|
-
directionalUnit = n;
|
|
186
|
-
}
|
|
187
|
-
if (n.tag === "postcode" && !postcode && n.value.trim()) {
|
|
188
|
-
postcode = n.value.trim();
|
|
189
|
-
}
|
|
190
|
-
stack.push(...n.children);
|
|
191
|
-
}
|
|
192
|
-
if (!street || !houseNumber)
|
|
193
|
-
return;
|
|
194
|
-
// The fall-through gate: an exact situs point already won — never override it with an estimate.
|
|
195
|
-
if (street.metadata?.["resolution_tier"] === "address_point")
|
|
196
|
-
return;
|
|
197
|
-
const hit = lookup.find({ street: assembleStreetValue(street, directionalUnit), number: houseNumber.value, postcode });
|
|
198
|
-
if (!hit)
|
|
199
|
-
return;
|
|
200
|
-
// Conformal-calibrated radius when the caller supplies a multiplier (#374): the raw half-segment
|
|
201
|
-
// heuristic underestimates the true spread (~72% coverage on Travis); ×1.70 → a 90% bound. Default
|
|
202
|
-
// (no multiplier) keeps the raw value, byte-stable. Preserve the raw radius for transparency.
|
|
203
|
-
const calibrated = radiusCalibration ? Math.round(hit.uncertaintyM * radiusCalibration) : hit.uncertaintyM;
|
|
204
|
-
street.metadata = {
|
|
205
|
-
...street.metadata,
|
|
206
|
-
interpolated_point: { lat: hit.lat, lon: hit.lon, source: hit.source, release: hit.release },
|
|
207
|
-
resolution_tier: "interpolated",
|
|
208
|
-
uncertainty_m: calibrated,
|
|
209
|
-
...(radiusCalibration ? { uncertainty_raw_m: hit.uncertaintyM, uncertainty_calibration: radiusCalibration } : {}),
|
|
210
|
-
interpolation_method: hit.method,
|
|
211
|
-
...(hit.parityMatched !== undefined ? { parity_matched: hit.parityMatched } : {}),
|
|
212
|
-
...(hit.bracket !== undefined ? { interpolation_bracket: hit.bracket } : {}),
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* French thoroughfare (voie) type tokens — the leading word that marks a street-only span as a THOROUGHFARE rather than
|
|
217
|
-
* a place name ("Place Bellecour", "Cours de l'Intendance", "Quai des Bateliers"). Used by the #1042 street-centroid
|
|
218
|
-
* tier to recognize a thoroughfare that the model mis-parsed as a `locality` (the FR no-street class, #901).
|
|
219
|
-
* Deliberately generous — a false positive simply misses the exact street-centroid lookup and no-ops; the lookup is the
|
|
220
|
-
* real gate.
|
|
221
|
-
*/
|
|
222
|
-
const FR_VOIE_TYPES = new Set([
|
|
223
|
-
"rue",
|
|
224
|
-
"ruelle",
|
|
225
|
-
"venelle",
|
|
226
|
-
"avenue",
|
|
227
|
-
"av",
|
|
228
|
-
"ave",
|
|
229
|
-
"boulevard",
|
|
230
|
-
"bd",
|
|
231
|
-
"bld",
|
|
232
|
-
"bvd",
|
|
233
|
-
"boul",
|
|
234
|
-
"place",
|
|
235
|
-
"pl",
|
|
236
|
-
"cours",
|
|
237
|
-
"quai",
|
|
238
|
-
"impasse",
|
|
239
|
-
"imp",
|
|
240
|
-
"allee",
|
|
241
|
-
"all",
|
|
242
|
-
"chemin",
|
|
243
|
-
"ch",
|
|
244
|
-
"che",
|
|
245
|
-
"passage",
|
|
246
|
-
"pas",
|
|
247
|
-
"square",
|
|
248
|
-
"sq",
|
|
249
|
-
"faubourg",
|
|
250
|
-
"fg",
|
|
251
|
-
"fbg",
|
|
252
|
-
"route",
|
|
253
|
-
"rte",
|
|
254
|
-
"esplanade",
|
|
255
|
-
"promenade",
|
|
256
|
-
"sentier",
|
|
257
|
-
"sente",
|
|
258
|
-
"villa",
|
|
259
|
-
"cite",
|
|
260
|
-
"hameau",
|
|
261
|
-
"montee",
|
|
262
|
-
"chaussee",
|
|
263
|
-
"traverse",
|
|
264
|
-
"mail",
|
|
265
|
-
"clos",
|
|
266
|
-
"voie",
|
|
267
|
-
"quartier",
|
|
268
|
-
"lotissement",
|
|
269
|
-
"residence",
|
|
270
|
-
"rond",
|
|
271
|
-
]);
|
|
272
|
-
/** Fold to lower-case, diacritic-stripped, punctuation-free tokens — mirrors `street-normalize.ts`'s `fold`. */
|
|
273
|
-
function foldVoieTokens(s) {
|
|
274
|
-
return s
|
|
275
|
-
.normalize("NFKD")
|
|
276
|
-
.replace(/[̀-ͯ]/g, "")
|
|
277
|
-
.toLowerCase()
|
|
278
|
-
.replace(/[.,'’]/g, "")
|
|
279
|
-
.replace(/-/g, " ")
|
|
280
|
-
.split(/\s+/)
|
|
281
|
-
.filter(Boolean);
|
|
282
|
-
}
|
|
283
|
-
/** Does a string START with a French thoroughfare type token ("Rue …", "Place …")? */
|
|
284
|
-
function isVoieShaped(s) {
|
|
285
|
-
const first = foldVoieTokens(s)[0];
|
|
286
|
-
return first !== undefined && FR_VOIE_TYPES.has(first);
|
|
287
|
-
}
|
|
288
|
-
/** Push `v` (trimmed, non-empty, deduped, capped) onto `list`. */
|
|
289
|
-
function pushCandidate(list, v, cap) {
|
|
290
|
-
const t = v?.trim();
|
|
291
|
-
if (t && list.length < cap && !list.includes(t)) {
|
|
292
|
-
list.push(t);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Street-centroid tier (#1042): the street-level rung BELOW the exact/interpolation tiers and ABOVE admin-centroid
|
|
297
|
-
* resolution. For a STREET-ONLY query (a thoroughfare with NO house number), stamp the street's centroid onto a
|
|
298
|
-
* `street` node so a consumer gets a street-level coordinate instead of the commune centroid (or a wrong namesake).
|
|
299
|
-
*
|
|
300
|
-
* The FR no-street class mis-parses the thoroughfare — "Place Bellecour, Lyon" parses `region=Lyon`, `locality="Place
|
|
301
|
-
* Bellecour"`; "Avenue des Champs-Élysées" truncates — so this recovers the thoroughfare + commune RAW-TEXT-first (the
|
|
302
|
-
* same substrate as span-rescore), preferring parsed nodes and falling back to the comma-split raw query. A
|
|
303
|
-
* thoroughfare is recognized by its leading voie type ({@link isVoieShaped}); the commune is any non-voie span. Every
|
|
304
|
-
* (thoroughfare, commune) pair is probed against the exact street-centroid lookup, first hit wins — a false candidate
|
|
305
|
-
* simply misses. Additive only: fires ONLY when no house number is present (rooftop tiers untouched) and no
|
|
306
|
-
* street-level coordinate already resolved, and never alters admin resolution.
|
|
307
|
-
*/
|
|
308
|
-
function applyStreetCentroid(roots, raw, provider, hints) {
|
|
309
|
-
let streetNode;
|
|
310
|
-
let houseNumber = false;
|
|
311
|
-
let postcode;
|
|
312
|
-
const adminValues = []; // region / locality values, in tree order (thoroughfare and commune both hide here)
|
|
313
|
-
const resolvedCountries = []; // countries the tree actually resolved to — a post-resolution country hint
|
|
314
|
-
const stack = [...roots];
|
|
315
|
-
while (stack.length > 0) {
|
|
316
|
-
const n = stack.pop();
|
|
317
|
-
if (n.tag === "house_number") {
|
|
318
|
-
houseNumber = true;
|
|
319
|
-
}
|
|
320
|
-
if (n.tag === "street" && !streetNode) {
|
|
321
|
-
streetNode = n;
|
|
322
|
-
}
|
|
323
|
-
// Never shadow a real street-level coordinate the exact/interp tiers already stamped.
|
|
324
|
-
if (n.metadata?.["resolution_tier"] === "address_point" || n.metadata?.["resolution_tier"] === "interpolated") {
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
if (!postcode && n.tag === "postcode" && n.value.trim()) {
|
|
328
|
-
postcode = n.value.trim();
|
|
329
|
-
}
|
|
330
|
-
if ((n.tag === "region" || n.tag === "locality" || n.tag === "dependent_locality") && n.value.trim()) {
|
|
331
|
-
adminValues.push(n.value.trim());
|
|
332
|
-
}
|
|
333
|
-
const rc = n.metadata?.["resolver_country"]?.trim().toLowerCase();
|
|
334
|
-
if (rc && !resolvedCountries.includes(rc)) {
|
|
335
|
-
resolvedCountries.push(rc);
|
|
336
|
-
}
|
|
337
|
-
stack.push(...n.children);
|
|
338
|
-
}
|
|
339
|
-
if (houseNumber)
|
|
340
|
-
return; // street-only tier — a numbered address is the rooftop tiers' job
|
|
341
|
-
// Candidate countries: pre-resolution hints (defaultCountry + ungated placer) then the resolved countries. BAN is
|
|
342
|
-
// FR-only, so a non-FR candidate simply yields no lookup; the exact (street, base-commune) match is the real filter.
|
|
343
|
-
const countries = [];
|
|
344
|
-
for (const c of [...hints, ...resolvedCountries]) {
|
|
345
|
-
const cc = c?.trim().toLowerCase();
|
|
346
|
-
if (cc && !countries.includes(cc)) {
|
|
347
|
-
countries.push(cc);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
const lookups = countries.map((c) => provider(c)).filter((l) => l != null);
|
|
351
|
-
if (lookups.length === 0)
|
|
352
|
-
return;
|
|
353
|
-
const rawSegments = raw
|
|
354
|
-
.split(",")
|
|
355
|
-
.map((s) => s.trim())
|
|
356
|
-
.filter(Boolean);
|
|
357
|
-
const CAP = 5;
|
|
358
|
-
// Thoroughfare candidates (parsed-first, then raw): the assembled street node, any voie-shaped parsed value, any
|
|
359
|
-
// voie-shaped raw comma-segment. The parse often truncates these (Champs-Élysées → "Avenue des Champs"), so the raw
|
|
360
|
-
// segment is the recovery — a candidate that misses just advances to the next.
|
|
361
|
-
const thoroughfares = [];
|
|
362
|
-
if (streetNode) {
|
|
363
|
-
pushCandidate(thoroughfares, assembleStreetValue(streetNode), CAP);
|
|
364
|
-
}
|
|
365
|
-
for (const v of adminValues) {
|
|
366
|
-
if (isVoieShaped(v)) {
|
|
367
|
-
pushCandidate(thoroughfares, v, CAP);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
for (const s of rawSegments) {
|
|
371
|
-
if (isVoieShaped(s)) {
|
|
372
|
-
pushCandidate(thoroughfares, s, CAP);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
if (thoroughfares.length === 0)
|
|
376
|
-
return;
|
|
377
|
-
// Commune candidates: non-voie parsed admin values, then non-voie raw segments (a truncated/garbled parse loses the
|
|
378
|
-
// commune — "Rue de la République, Marseille" parses `locality="e"` — so the raw "Marseille" is the recovery).
|
|
379
|
-
const communes = [];
|
|
380
|
-
for (const v of adminValues) {
|
|
381
|
-
if (!isVoieShaped(v)) {
|
|
382
|
-
pushCandidate(communes, v, CAP);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
for (const s of rawSegments) {
|
|
386
|
-
if (!isVoieShaped(s) && !thoroughfares.includes(s)) {
|
|
387
|
-
pushCandidate(communes, s, CAP);
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
for (const lookup of lookups) {
|
|
391
|
-
for (const street of thoroughfares) {
|
|
392
|
-
let hit = postcode ? lookup.find({ street, postcode }) : null;
|
|
393
|
-
let matchedCommune;
|
|
394
|
-
for (let i = 0; !hit && i < communes.length; i++) {
|
|
395
|
-
hit = lookup.find({ street, locality: communes[i] });
|
|
396
|
-
if (hit) {
|
|
397
|
-
matchedCommune = communes[i];
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
if (!hit)
|
|
401
|
-
continue;
|
|
402
|
-
const target = streetNode ??
|
|
403
|
-
(() => {
|
|
404
|
-
const injected = {
|
|
405
|
-
tag: "street",
|
|
406
|
-
value: street,
|
|
407
|
-
start: 0,
|
|
408
|
-
end: 0,
|
|
409
|
-
confidence: 0.5,
|
|
410
|
-
children: [],
|
|
411
|
-
};
|
|
412
|
-
roots.push(injected);
|
|
413
|
-
return injected;
|
|
414
|
-
})();
|
|
415
|
-
target.metadata = {
|
|
416
|
-
...target.metadata,
|
|
417
|
-
street_centroid: { lat: hit.lat, lon: hit.lon, source: hit.source, release: hit.release },
|
|
418
|
-
resolution_tier: "street",
|
|
419
|
-
uncertainty_m: hit.uncertaintyM,
|
|
420
|
-
};
|
|
421
|
-
// #1058: a commune-scoped hit is REGISTER evidence of the street's locality — record it for
|
|
422
|
-
// the geocode layer's locality/city decoration, and drop any span-rescored locality that
|
|
423
|
-
// contradicts it. Span-rescore injects SPECULATIVELY (a low-confidence street prefix like
|
|
424
|
-
// "Rue" exact-matches the commune Rue in Somme); the register's exact (street, commune)
|
|
425
|
-
// match is strictly stronger, so the injected token-of-the-street must not survive as the
|
|
426
|
-
// result's city.
|
|
427
|
-
if (matchedCommune) {
|
|
428
|
-
target.metadata = { ...target.metadata, street_locality: matchedCommune };
|
|
429
|
-
for (let i = roots.length - 1; i >= 0; i--) {
|
|
430
|
-
const n = roots[i];
|
|
431
|
-
if (n.tag !== "locality" || n.metadata?.["span_rescore"] !== true)
|
|
432
|
-
continue;
|
|
433
|
-
const names = [n.value, n.metadata?.["resolver_name"] ?? ""];
|
|
434
|
-
if (!names.some((name) => foldName(name) === foldName(matchedCommune))) {
|
|
435
|
-
roots.splice(i, 1);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
/** Case/diacritic-insensitive fold for commune-name comparison (#1058) — mirrors span-rescore's `norm`. */
|
|
444
|
-
function foldName(s) {
|
|
445
|
-
return s
|
|
446
|
-
.toLowerCase()
|
|
447
|
-
.normalize("NFD")
|
|
448
|
-
.replace(/[^a-z0-9 ]/g, " ")
|
|
449
|
-
.replace(/\s+/g, " ")
|
|
450
|
-
.trim();
|
|
451
|
-
}
|
|
452
60
|
/**
|
|
453
61
|
* Span-rescore tier (#370): opt-in last-resort locality recovery. Runs ONLY when the tree resolved NOTHING (the #685
|
|
454
62
|
* brake — never disturb a working coordinate). Enumerates raw-token spans, exact- matches the same-country gazetteer
|
|
@@ -534,7 +142,9 @@ async function recoverPostcodeNode(roots, backend, country) {
|
|
|
534
142
|
}
|
|
535
143
|
}
|
|
536
144
|
}
|
|
537
|
-
/**
|
|
145
|
+
/**
|
|
146
|
+
* A resolved node carries a real coordinate (placeID set + non-zero lat/lon).
|
|
147
|
+
*/
|
|
538
148
|
function isResolvedWithCoord(n) {
|
|
539
149
|
return !!(n.placeID && typeof n.lat === "number" && typeof n.lon === "number" && (n.lat !== 0 || n.lon !== 0));
|
|
540
150
|
}
|
|
@@ -561,7 +171,7 @@ function applyPostcodeConsistency(roots, gateKm) {
|
|
|
561
171
|
// The resolved postcode anchor (first one with a real coordinate).
|
|
562
172
|
let anchor = null;
|
|
563
173
|
const findAnchor = [...roots];
|
|
564
|
-
while (findAnchor.length
|
|
174
|
+
while (findAnchor.length) {
|
|
565
175
|
const n = findAnchor.pop();
|
|
566
176
|
if (n.tag === "postcode" && isResolvedWithCoord(n)) {
|
|
567
177
|
anchor = { lat: n.lat, lon: n.lon };
|
|
@@ -572,7 +182,7 @@ function applyPostcodeConsistency(roots, gateKm) {
|
|
|
572
182
|
if (!anchor)
|
|
573
183
|
return; // no postcode→point — nothing to disambiguate against (gate can't fire)
|
|
574
184
|
const stack = [...roots];
|
|
575
|
-
while (stack.length
|
|
185
|
+
while (stack.length) {
|
|
576
186
|
const node = stack.pop();
|
|
577
187
|
stack.push(...node.children);
|
|
578
188
|
if ((node.tag !== "locality" && node.tag !== "dependent_locality") || !isResolvedWithCoord(node))
|
|
@@ -587,6 +197,7 @@ function applyPostcodeConsistency(roots, gateKm) {
|
|
|
587
197
|
.filter((a) => a.lat !== 0 || a.lon !== 0)
|
|
588
198
|
.map((a) => ({ a, d: haversineKm(anchor.lat, anchor.lon, a.lat, a.lon) }))
|
|
589
199
|
.filter((x) => x.d <= gateKm)
|
|
200
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
|
|
590
201
|
.sort((x, y) => x.d - y.d)[0];
|
|
591
202
|
if (reconciling) {
|
|
592
203
|
// Swap to the consistent instance; the displaced winner becomes an alternative.
|
|
@@ -628,7 +239,7 @@ async function applyAdminCoherence(roots, backend) {
|
|
|
628
239
|
if (regionHere &&
|
|
629
240
|
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
630
241
|
!isResolvedWithCoord(node) &&
|
|
631
|
-
node.value.trim().length
|
|
242
|
+
node.value.trim().length) {
|
|
632
243
|
await reconcileAdminPair(regionHere, node, backend);
|
|
633
244
|
}
|
|
634
245
|
for (const child of node.children) {
|
|
@@ -746,6 +357,7 @@ function revertResolverDecoration(node) {
|
|
|
746
357
|
"resolution_quality",
|
|
747
358
|
"postcode_city_mismatch",
|
|
748
359
|
]) {
|
|
360
|
+
// oxlint-disable-next-line typescript/no-dynamic-delete -- removing one key from a plain record; the object is not on a hot path
|
|
749
361
|
delete meta[key];
|
|
750
362
|
}
|
|
751
363
|
node.metadata = meta;
|
|
@@ -774,7 +386,7 @@ function revertResolverDecoration(node) {
|
|
|
774
386
|
*/
|
|
775
387
|
async function applyExplicitCountryCoherence(roots, backend) {
|
|
776
388
|
const visit = async (node, countryToken, regionAbove) => {
|
|
777
|
-
const countryHere = node.tag === "country" && node.value.trim().length
|
|
389
|
+
const countryHere = node.tag === "country" && node.value.trim().length ? node : countryToken;
|
|
778
390
|
const regionHere = regionAbove || node.tag === "region" || node.tag === "subregion";
|
|
779
391
|
// Fire only when the explicit country is the locality's NEAREST admin context (no region/subregion between).
|
|
780
392
|
// When a region IS present, applyAdminCoherence + the region's `parentID` scope already disambiguate the
|
|
@@ -859,7 +471,7 @@ async function applyRegionCountryCoherence(roots, backend, defaultCountry) {
|
|
|
859
471
|
if (regionHere &&
|
|
860
472
|
!isResolvedWithCoord(regionHere) &&
|
|
861
473
|
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
862
|
-
node.value.trim().length
|
|
474
|
+
node.value.trim().length) {
|
|
863
475
|
await reconcileRegionCountry(regionHere, node, backend, defaultCountry);
|
|
864
476
|
}
|
|
865
477
|
for (const child of node.children) {
|
|
@@ -920,8 +532,14 @@ async function reconcileRegionCountry(regionNode, localityNode, backend, default
|
|
|
920
532
|
}
|
|
921
533
|
class WOFResolver {
|
|
922
534
|
#backend;
|
|
535
|
+
/**
|
|
536
|
+
* The gazetteer artifact's self-declared coverage facts, passed through from the backend so pipeline-level consumers
|
|
537
|
+
* read them from the resolver handle they already hold. Absent on artifacts predating the coverage manifest.
|
|
538
|
+
*/
|
|
539
|
+
artifactCoverage;
|
|
923
540
|
constructor(backend) {
|
|
924
541
|
this.#backend = backend;
|
|
542
|
+
this.artifactCoverage = backend.artifactCoverage;
|
|
925
543
|
}
|
|
926
544
|
async resolveTree(tree, opts = {}) {
|
|
927
545
|
const state = {
|
|
@@ -936,7 +554,7 @@ class WOFResolver {
|
|
|
936
554
|
postcode: firstPostcodeValue(tree.roots),
|
|
937
555
|
bias: opts.bias,
|
|
938
556
|
anchorPosterior: opts.anchorPosterior,
|
|
939
|
-
anchorWeight: opts.anchorWeight ?? 2
|
|
557
|
+
anchorWeight: opts.anchorWeight ?? 2,
|
|
940
558
|
hardCountry: opts.hardCountry,
|
|
941
559
|
// Default-ON (#402): completion only fires for a dual-role region whose locality the parser
|
|
942
560
|
// dropped, and no-ops entirely when the backend has no relation (the browser WASM resolver, or
|
|
@@ -1048,7 +666,7 @@ class WOFResolver {
|
|
|
1048
666
|
state.localityNodePresent = true;
|
|
1049
667
|
}
|
|
1050
668
|
let resolved = null;
|
|
1051
|
-
if (placetype && state.lookupsRemaining > 0 && node.value.trim().length
|
|
669
|
+
if (placetype && state.lookupsRemaining > 0 && node.value.trim().length) {
|
|
1052
670
|
const picked = await this.#lookupAndPick(node, placetype, parentResolved, state);
|
|
1053
671
|
if (picked) {
|
|
1054
672
|
resolved = picked.top;
|
|
@@ -1082,7 +700,7 @@ class WOFResolver {
|
|
|
1082
700
|
// Proximity bias (viewport center, user location, …) — a SOFT re-rank the backend folds into
|
|
1083
701
|
// its exact-tier prominence; never a filter, so recall is untouched. This is how an ambiguous
|
|
1084
702
|
// bare postcode ("48026") follows the map view instead of a global population coin-flip.
|
|
1085
|
-
if (state.bias && state.bias.length
|
|
703
|
+
if (state.bias && state.bias.length) {
|
|
1086
704
|
query.bias = state.bias;
|
|
1087
705
|
}
|
|
1088
706
|
// Pass the inherited parent constraint to the backend when available — `parentID` scopes to
|
|
@@ -1128,7 +746,7 @@ class WOFResolver {
|
|
|
1128
746
|
// resolvable node into an unresolved one, retry once WITHOUT the parent constraint — we
|
|
1129
747
|
// prefer a parent-scoped hit but never sacrifice recall. The country constraint is kept, so
|
|
1130
748
|
// this still can't wander to a foreign place. Same logical resolution → no extra budget.
|
|
1131
|
-
if (candidates.length
|
|
749
|
+
if (!candidates.length && state.parentFallback && query.parentID !== undefined) {
|
|
1132
750
|
delete query.parentID;
|
|
1133
751
|
candidates = await this.#backend.findPlace(query);
|
|
1134
752
|
}
|
|
@@ -1138,7 +756,7 @@ class WOFResolver {
|
|
|
1138
756
|
// its classifier attribution intact.
|
|
1139
757
|
return null;
|
|
1140
758
|
}
|
|
1141
|
-
if (candidates.length
|
|
759
|
+
if (!candidates.length)
|
|
1142
760
|
return null;
|
|
1143
761
|
// Postcode-anchor re-rank (#369): when a country posterior is supplied (from the address's
|
|
1144
762
|
// postcode), boost candidates by `anchorWeight * posterior[candidate.country]` and re-sort, so a
|
|
@@ -1176,6 +794,7 @@ class WOFResolver {
|
|
|
1176
794
|
// The within-tier key is now the backend's PROMINENCE (population + proximity, #938 units)
|
|
1177
795
|
// with the posterior as the additive country pin; score stays the final tiebreak. Backends
|
|
1178
796
|
// that don't populate `prominence` degrade to the additive score behavior.
|
|
797
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
|
|
1179
798
|
ranked = [...candidates].sort((a, b) => {
|
|
1180
799
|
const tier = Number(b.exactMatch ?? false) - Number(a.exactMatch ?? false);
|
|
1181
800
|
if (tier !== 0)
|
|
@@ -1257,7 +876,7 @@ function decorateNode(node, resolved, alternatives) {
|
|
|
1257
876
|
if (resolved.resolutionQuality) {
|
|
1258
877
|
node.metadata["resolution_quality"] = resolved.resolutionQuality;
|
|
1259
878
|
}
|
|
1260
|
-
if (alternatives.length
|
|
879
|
+
if (alternatives.length) {
|
|
1261
880
|
node.alternatives = alternatives;
|
|
1262
881
|
}
|
|
1263
882
|
}
|