@mailwoman/resolver 8.1.0 → 8.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/fold-name.ts +20 -0
- package/index.ts +6 -0
- 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.map +1 -1
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +12 -4
- package/out/plausibility.d.ts.map +1 -1
- 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 +23 -414
- 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 +13 -4
- package/remote-resolver.ts +23 -7
- package/rerank.ts +37 -12
- package/resolve.ts +74 -485
- 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,413 +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 (#374): the raw half-segment heuristic underestimates the true spread
|
|
201
|
-
// (~72% coverage on Travis); ×1.70 → a 90% bound. The ARTIFACT's own multiplier (read from the shard's
|
|
202
|
-
// `interp_calibration` metadata table at open time — `lookup.radiusCalibration`) is the default; an
|
|
203
|
-
// explicit caller factor is the @internal instrument override. Neither present (shards predating the
|
|
204
|
-
// metadata table, no caller factor) keeps the raw value, byte-stable. Preserve the raw radius for
|
|
205
|
-
// transparency.
|
|
206
|
-
const factor = radiusCalibration ?? lookup.radiusCalibration;
|
|
207
|
-
const calibrated = factor ? Math.round(hit.uncertaintyM * factor) : hit.uncertaintyM;
|
|
208
|
-
street.metadata = {
|
|
209
|
-
...street.metadata,
|
|
210
|
-
interpolated_point: { lat: hit.lat, lon: hit.lon, source: hit.source, release: hit.release },
|
|
211
|
-
resolution_tier: "interpolated",
|
|
212
|
-
uncertainty_m: calibrated,
|
|
213
|
-
...(factor ? { uncertainty_raw_m: hit.uncertaintyM, uncertainty_calibration: factor } : {}),
|
|
214
|
-
interpolation_method: hit.method,
|
|
215
|
-
...(hit.parityMatched !== undefined ? { parity_matched: hit.parityMatched } : {}),
|
|
216
|
-
...(hit.bracket !== undefined ? { interpolation_bracket: hit.bracket } : {}),
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* French thoroughfare (voie) type tokens — the leading word that marks a street-only span as a THOROUGHFARE rather than
|
|
221
|
-
* a place name ("Place Bellecour", "Cours de l'Intendance", "Quai des Bateliers"). Used by the #1042 street-centroid
|
|
222
|
-
* tier to recognize a thoroughfare that the model mis-parsed as a `locality` (the FR no-street class, #901).
|
|
223
|
-
* Deliberately generous — a false positive simply misses the exact street-centroid lookup and no-ops; the lookup is the
|
|
224
|
-
* real gate.
|
|
225
|
-
*/
|
|
226
|
-
const FR_VOIE_TYPES = new Set([
|
|
227
|
-
"rue",
|
|
228
|
-
"ruelle",
|
|
229
|
-
"venelle",
|
|
230
|
-
"avenue",
|
|
231
|
-
"av",
|
|
232
|
-
"ave",
|
|
233
|
-
"boulevard",
|
|
234
|
-
"bd",
|
|
235
|
-
"bld",
|
|
236
|
-
"bvd",
|
|
237
|
-
"boul",
|
|
238
|
-
"place",
|
|
239
|
-
"pl",
|
|
240
|
-
"cours",
|
|
241
|
-
"quai",
|
|
242
|
-
"impasse",
|
|
243
|
-
"imp",
|
|
244
|
-
"allee",
|
|
245
|
-
"all",
|
|
246
|
-
"chemin",
|
|
247
|
-
"ch",
|
|
248
|
-
"che",
|
|
249
|
-
"passage",
|
|
250
|
-
"pas",
|
|
251
|
-
"square",
|
|
252
|
-
"sq",
|
|
253
|
-
"faubourg",
|
|
254
|
-
"fg",
|
|
255
|
-
"fbg",
|
|
256
|
-
"route",
|
|
257
|
-
"rte",
|
|
258
|
-
"esplanade",
|
|
259
|
-
"promenade",
|
|
260
|
-
"sentier",
|
|
261
|
-
"sente",
|
|
262
|
-
"villa",
|
|
263
|
-
"cite",
|
|
264
|
-
"hameau",
|
|
265
|
-
"montee",
|
|
266
|
-
"chaussee",
|
|
267
|
-
"traverse",
|
|
268
|
-
"mail",
|
|
269
|
-
"clos",
|
|
270
|
-
"voie",
|
|
271
|
-
"quartier",
|
|
272
|
-
"lotissement",
|
|
273
|
-
"residence",
|
|
274
|
-
"rond",
|
|
275
|
-
]);
|
|
276
|
-
/** Fold to lower-case, diacritic-stripped, punctuation-free tokens — mirrors `street-normalize.ts`'s `fold`. */
|
|
277
|
-
function foldVoieTokens(s) {
|
|
278
|
-
return s
|
|
279
|
-
.normalize("NFKD")
|
|
280
|
-
.replace(/[̀-ͯ]/g, "")
|
|
281
|
-
.toLowerCase()
|
|
282
|
-
.replace(/[.,'’]/g, "")
|
|
283
|
-
.replace(/-/g, " ")
|
|
284
|
-
.split(/\s+/)
|
|
285
|
-
.filter(Boolean);
|
|
286
|
-
}
|
|
287
|
-
/** Does a string START with a French thoroughfare type token ("Rue …", "Place …")? */
|
|
288
|
-
function isVoieShaped(s) {
|
|
289
|
-
const first = foldVoieTokens(s)[0];
|
|
290
|
-
return first !== undefined && FR_VOIE_TYPES.has(first);
|
|
291
|
-
}
|
|
292
|
-
/** Push `v` (trimmed, non-empty, deduped, capped) onto `list`. */
|
|
293
|
-
function pushCandidate(list, v, cap) {
|
|
294
|
-
const t = v?.trim();
|
|
295
|
-
if (t && list.length < cap && !list.includes(t)) {
|
|
296
|
-
list.push(t);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Street-centroid tier (#1042): the street-level rung BELOW the exact/interpolation tiers and ABOVE admin-centroid
|
|
301
|
-
* resolution. For a STREET-ONLY query (a thoroughfare with NO house number), stamp the street's centroid onto a
|
|
302
|
-
* `street` node so a consumer gets a street-level coordinate instead of the commune centroid (or a wrong namesake).
|
|
303
|
-
*
|
|
304
|
-
* The FR no-street class mis-parses the thoroughfare — "Place Bellecour, Lyon" parses `region=Lyon`, `locality="Place
|
|
305
|
-
* Bellecour"`; "Avenue des Champs-Élysées" truncates — so this recovers the thoroughfare + commune RAW-TEXT-first (the
|
|
306
|
-
* same substrate as span-rescore), preferring parsed nodes and falling back to the comma-split raw query. A
|
|
307
|
-
* thoroughfare is recognized by its leading voie type ({@link isVoieShaped}); the commune is any non-voie span. Every
|
|
308
|
-
* (thoroughfare, commune) pair is probed against the exact street-centroid lookup, first hit wins — a false candidate
|
|
309
|
-
* simply misses. Additive only: fires ONLY when no house number is present (rooftop tiers untouched) and no
|
|
310
|
-
* street-level coordinate already resolved, and never alters admin resolution.
|
|
311
|
-
*/
|
|
312
|
-
function applyStreetCentroid(roots, raw, provider, hints) {
|
|
313
|
-
let streetNode;
|
|
314
|
-
let houseNumber = false;
|
|
315
|
-
let postcode;
|
|
316
|
-
const adminValues = []; // region / locality values, in tree order (thoroughfare and commune both hide here)
|
|
317
|
-
const resolvedCountries = []; // countries the tree actually resolved to — a post-resolution country hint
|
|
318
|
-
const stack = [...roots];
|
|
319
|
-
while (stack.length > 0) {
|
|
320
|
-
const n = stack.pop();
|
|
321
|
-
if (n.tag === "house_number") {
|
|
322
|
-
houseNumber = true;
|
|
323
|
-
}
|
|
324
|
-
if (n.tag === "street" && !streetNode) {
|
|
325
|
-
streetNode = n;
|
|
326
|
-
}
|
|
327
|
-
// Never shadow a real street-level coordinate the exact/interp tiers already stamped.
|
|
328
|
-
if (n.metadata?.["resolution_tier"] === "address_point" || n.metadata?.["resolution_tier"] === "interpolated") {
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
if (!postcode && n.tag === "postcode" && n.value.trim()) {
|
|
332
|
-
postcode = n.value.trim();
|
|
333
|
-
}
|
|
334
|
-
if ((n.tag === "region" || n.tag === "locality" || n.tag === "dependent_locality") && n.value.trim()) {
|
|
335
|
-
adminValues.push(n.value.trim());
|
|
336
|
-
}
|
|
337
|
-
const rc = n.metadata?.["resolver_country"]?.trim().toLowerCase();
|
|
338
|
-
if (rc && !resolvedCountries.includes(rc)) {
|
|
339
|
-
resolvedCountries.push(rc);
|
|
340
|
-
}
|
|
341
|
-
stack.push(...n.children);
|
|
342
|
-
}
|
|
343
|
-
if (houseNumber)
|
|
344
|
-
return; // street-only tier — a numbered address is the rooftop tiers' job
|
|
345
|
-
// Candidate countries: pre-resolution hints (defaultCountry + ungated placer) then the resolved countries. BAN is
|
|
346
|
-
// FR-only, so a non-FR candidate simply yields no lookup; the exact (street, base-commune) match is the real filter.
|
|
347
|
-
const countries = [];
|
|
348
|
-
for (const c of [...hints, ...resolvedCountries]) {
|
|
349
|
-
const cc = c?.trim().toLowerCase();
|
|
350
|
-
if (cc && !countries.includes(cc)) {
|
|
351
|
-
countries.push(cc);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
const lookups = countries.map((c) => provider(c)).filter((l) => l != null);
|
|
355
|
-
if (lookups.length === 0)
|
|
356
|
-
return;
|
|
357
|
-
const rawSegments = raw
|
|
358
|
-
.split(",")
|
|
359
|
-
.map((s) => s.trim())
|
|
360
|
-
.filter(Boolean);
|
|
361
|
-
const CAP = 5;
|
|
362
|
-
// Thoroughfare candidates (parsed-first, then raw): the assembled street node, any voie-shaped parsed value, any
|
|
363
|
-
// voie-shaped raw comma-segment. The parse often truncates these (Champs-Élysées → "Avenue des Champs"), so the raw
|
|
364
|
-
// segment is the recovery — a candidate that misses just advances to the next.
|
|
365
|
-
const thoroughfares = [];
|
|
366
|
-
if (streetNode) {
|
|
367
|
-
pushCandidate(thoroughfares, assembleStreetValue(streetNode), CAP);
|
|
368
|
-
}
|
|
369
|
-
for (const v of adminValues) {
|
|
370
|
-
if (isVoieShaped(v)) {
|
|
371
|
-
pushCandidate(thoroughfares, v, CAP);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
for (const s of rawSegments) {
|
|
375
|
-
if (isVoieShaped(s)) {
|
|
376
|
-
pushCandidate(thoroughfares, s, CAP);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
if (thoroughfares.length === 0)
|
|
380
|
-
return;
|
|
381
|
-
// Commune candidates: non-voie parsed admin values, then non-voie raw segments (a truncated/garbled parse loses the
|
|
382
|
-
// commune — "Rue de la République, Marseille" parses `locality="e"` — so the raw "Marseille" is the recovery).
|
|
383
|
-
const communes = [];
|
|
384
|
-
for (const v of adminValues) {
|
|
385
|
-
if (!isVoieShaped(v)) {
|
|
386
|
-
pushCandidate(communes, v, CAP);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
for (const s of rawSegments) {
|
|
390
|
-
if (!isVoieShaped(s) && !thoroughfares.includes(s)) {
|
|
391
|
-
pushCandidate(communes, s, CAP);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
for (const lookup of lookups) {
|
|
395
|
-
for (const street of thoroughfares) {
|
|
396
|
-
let hit = postcode ? lookup.find({ street, postcode }) : null;
|
|
397
|
-
let matchedCommune;
|
|
398
|
-
for (let i = 0; !hit && i < communes.length; i++) {
|
|
399
|
-
hit = lookup.find({ street, locality: communes[i] });
|
|
400
|
-
if (hit) {
|
|
401
|
-
matchedCommune = communes[i];
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
if (!hit)
|
|
405
|
-
continue;
|
|
406
|
-
const target = streetNode ??
|
|
407
|
-
(() => {
|
|
408
|
-
const injected = {
|
|
409
|
-
tag: "street",
|
|
410
|
-
value: street,
|
|
411
|
-
start: 0,
|
|
412
|
-
end: 0,
|
|
413
|
-
confidence: 0.5,
|
|
414
|
-
children: [],
|
|
415
|
-
};
|
|
416
|
-
roots.push(injected);
|
|
417
|
-
return injected;
|
|
418
|
-
})();
|
|
419
|
-
target.metadata = {
|
|
420
|
-
...target.metadata,
|
|
421
|
-
street_centroid: { lat: hit.lat, lon: hit.lon, source: hit.source, release: hit.release },
|
|
422
|
-
resolution_tier: "street",
|
|
423
|
-
uncertainty_m: hit.uncertaintyM,
|
|
424
|
-
};
|
|
425
|
-
// #1058: a commune-scoped hit is REGISTER evidence of the street's locality — record it for
|
|
426
|
-
// the geocode layer's locality/city decoration, and drop any span-rescored locality that
|
|
427
|
-
// contradicts it. Span-rescore injects SPECULATIVELY (a low-confidence street prefix like
|
|
428
|
-
// "Rue" exact-matches the commune Rue in Somme); the register's exact (street, commune)
|
|
429
|
-
// match is strictly stronger, so the injected token-of-the-street must not survive as the
|
|
430
|
-
// result's city.
|
|
431
|
-
if (matchedCommune) {
|
|
432
|
-
target.metadata = { ...target.metadata, street_locality: matchedCommune };
|
|
433
|
-
for (let i = roots.length - 1; i >= 0; i--) {
|
|
434
|
-
const n = roots[i];
|
|
435
|
-
if (n.tag !== "locality" || n.metadata?.["span_rescore"] !== true)
|
|
436
|
-
continue;
|
|
437
|
-
const names = [n.value, n.metadata?.["resolver_name"] ?? ""];
|
|
438
|
-
if (!names.some((name) => foldName(name) === foldName(matchedCommune))) {
|
|
439
|
-
roots.splice(i, 1);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
/** Case/diacritic-insensitive fold for commune-name comparison (#1058) — mirrors span-rescore's `norm`. */
|
|
448
|
-
function foldName(s) {
|
|
449
|
-
return s
|
|
450
|
-
.toLowerCase()
|
|
451
|
-
.normalize("NFD")
|
|
452
|
-
.replace(/[^a-z0-9 ]/g, " ")
|
|
453
|
-
.replace(/\s+/g, " ")
|
|
454
|
-
.trim();
|
|
455
|
-
}
|
|
456
60
|
/**
|
|
457
61
|
* Span-rescore tier (#370): opt-in last-resort locality recovery. Runs ONLY when the tree resolved NOTHING (the #685
|
|
458
62
|
* brake — never disturb a working coordinate). Enumerates raw-token spans, exact- matches the same-country gazetteer
|
|
@@ -538,7 +142,9 @@ async function recoverPostcodeNode(roots, backend, country) {
|
|
|
538
142
|
}
|
|
539
143
|
}
|
|
540
144
|
}
|
|
541
|
-
/**
|
|
145
|
+
/**
|
|
146
|
+
* A resolved node carries a real coordinate (placeID set + non-zero lat/lon).
|
|
147
|
+
*/
|
|
542
148
|
function isResolvedWithCoord(n) {
|
|
543
149
|
return !!(n.placeID && typeof n.lat === "number" && typeof n.lon === "number" && (n.lat !== 0 || n.lon !== 0));
|
|
544
150
|
}
|
|
@@ -565,7 +171,7 @@ function applyPostcodeConsistency(roots, gateKm) {
|
|
|
565
171
|
// The resolved postcode anchor (first one with a real coordinate).
|
|
566
172
|
let anchor = null;
|
|
567
173
|
const findAnchor = [...roots];
|
|
568
|
-
while (findAnchor.length
|
|
174
|
+
while (findAnchor.length) {
|
|
569
175
|
const n = findAnchor.pop();
|
|
570
176
|
if (n.tag === "postcode" && isResolvedWithCoord(n)) {
|
|
571
177
|
anchor = { lat: n.lat, lon: n.lon };
|
|
@@ -576,7 +182,7 @@ function applyPostcodeConsistency(roots, gateKm) {
|
|
|
576
182
|
if (!anchor)
|
|
577
183
|
return; // no postcode→point — nothing to disambiguate against (gate can't fire)
|
|
578
184
|
const stack = [...roots];
|
|
579
|
-
while (stack.length
|
|
185
|
+
while (stack.length) {
|
|
580
186
|
const node = stack.pop();
|
|
581
187
|
stack.push(...node.children);
|
|
582
188
|
if ((node.tag !== "locality" && node.tag !== "dependent_locality") || !isResolvedWithCoord(node))
|
|
@@ -591,6 +197,7 @@ function applyPostcodeConsistency(roots, gateKm) {
|
|
|
591
197
|
.filter((a) => a.lat !== 0 || a.lon !== 0)
|
|
592
198
|
.map((a) => ({ a, d: haversineKm(anchor.lat, anchor.lon, a.lat, a.lon) }))
|
|
593
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
|
|
594
201
|
.sort((x, y) => x.d - y.d)[0];
|
|
595
202
|
if (reconciling) {
|
|
596
203
|
// Swap to the consistent instance; the displaced winner becomes an alternative.
|
|
@@ -632,7 +239,7 @@ async function applyAdminCoherence(roots, backend) {
|
|
|
632
239
|
if (regionHere &&
|
|
633
240
|
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
634
241
|
!isResolvedWithCoord(node) &&
|
|
635
|
-
node.value.trim().length
|
|
242
|
+
node.value.trim().length) {
|
|
636
243
|
await reconcileAdminPair(regionHere, node, backend);
|
|
637
244
|
}
|
|
638
245
|
for (const child of node.children) {
|
|
@@ -750,6 +357,7 @@ function revertResolverDecoration(node) {
|
|
|
750
357
|
"resolution_quality",
|
|
751
358
|
"postcode_city_mismatch",
|
|
752
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
|
|
753
361
|
delete meta[key];
|
|
754
362
|
}
|
|
755
363
|
node.metadata = meta;
|
|
@@ -778,7 +386,7 @@ function revertResolverDecoration(node) {
|
|
|
778
386
|
*/
|
|
779
387
|
async function applyExplicitCountryCoherence(roots, backend) {
|
|
780
388
|
const visit = async (node, countryToken, regionAbove) => {
|
|
781
|
-
const countryHere = node.tag === "country" && node.value.trim().length
|
|
389
|
+
const countryHere = node.tag === "country" && node.value.trim().length ? node : countryToken;
|
|
782
390
|
const regionHere = regionAbove || node.tag === "region" || node.tag === "subregion";
|
|
783
391
|
// Fire only when the explicit country is the locality's NEAREST admin context (no region/subregion between).
|
|
784
392
|
// When a region IS present, applyAdminCoherence + the region's `parentID` scope already disambiguate the
|
|
@@ -863,7 +471,7 @@ async function applyRegionCountryCoherence(roots, backend, defaultCountry) {
|
|
|
863
471
|
if (regionHere &&
|
|
864
472
|
!isResolvedWithCoord(regionHere) &&
|
|
865
473
|
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
866
|
-
node.value.trim().length
|
|
474
|
+
node.value.trim().length) {
|
|
867
475
|
await reconcileRegionCountry(regionHere, node, backend, defaultCountry);
|
|
868
476
|
}
|
|
869
477
|
for (const child of node.children) {
|
|
@@ -946,7 +554,7 @@ class WOFResolver {
|
|
|
946
554
|
postcode: firstPostcodeValue(tree.roots),
|
|
947
555
|
bias: opts.bias,
|
|
948
556
|
anchorPosterior: opts.anchorPosterior,
|
|
949
|
-
anchorWeight: opts.anchorWeight ?? 2
|
|
557
|
+
anchorWeight: opts.anchorWeight ?? 2,
|
|
950
558
|
hardCountry: opts.hardCountry,
|
|
951
559
|
// Default-ON (#402): completion only fires for a dual-role region whose locality the parser
|
|
952
560
|
// dropped, and no-ops entirely when the backend has no relation (the browser WASM resolver, or
|
|
@@ -1058,7 +666,7 @@ class WOFResolver {
|
|
|
1058
666
|
state.localityNodePresent = true;
|
|
1059
667
|
}
|
|
1060
668
|
let resolved = null;
|
|
1061
|
-
if (placetype && state.lookupsRemaining > 0 && node.value.trim().length
|
|
669
|
+
if (placetype && state.lookupsRemaining > 0 && node.value.trim().length) {
|
|
1062
670
|
const picked = await this.#lookupAndPick(node, placetype, parentResolved, state);
|
|
1063
671
|
if (picked) {
|
|
1064
672
|
resolved = picked.top;
|
|
@@ -1092,7 +700,7 @@ class WOFResolver {
|
|
|
1092
700
|
// Proximity bias (viewport center, user location, …) — a SOFT re-rank the backend folds into
|
|
1093
701
|
// its exact-tier prominence; never a filter, so recall is untouched. This is how an ambiguous
|
|
1094
702
|
// bare postcode ("48026") follows the map view instead of a global population coin-flip.
|
|
1095
|
-
if (state.bias && state.bias.length
|
|
703
|
+
if (state.bias && state.bias.length) {
|
|
1096
704
|
query.bias = state.bias;
|
|
1097
705
|
}
|
|
1098
706
|
// Pass the inherited parent constraint to the backend when available — `parentID` scopes to
|
|
@@ -1138,7 +746,7 @@ class WOFResolver {
|
|
|
1138
746
|
// resolvable node into an unresolved one, retry once WITHOUT the parent constraint — we
|
|
1139
747
|
// prefer a parent-scoped hit but never sacrifice recall. The country constraint is kept, so
|
|
1140
748
|
// this still can't wander to a foreign place. Same logical resolution → no extra budget.
|
|
1141
|
-
if (candidates.length
|
|
749
|
+
if (!candidates.length && state.parentFallback && query.parentID !== undefined) {
|
|
1142
750
|
delete query.parentID;
|
|
1143
751
|
candidates = await this.#backend.findPlace(query);
|
|
1144
752
|
}
|
|
@@ -1148,7 +756,7 @@ class WOFResolver {
|
|
|
1148
756
|
// its classifier attribution intact.
|
|
1149
757
|
return null;
|
|
1150
758
|
}
|
|
1151
|
-
if (candidates.length
|
|
759
|
+
if (!candidates.length)
|
|
1152
760
|
return null;
|
|
1153
761
|
// Postcode-anchor re-rank (#369): when a country posterior is supplied (from the address's
|
|
1154
762
|
// postcode), boost candidates by `anchorWeight * posterior[candidate.country]` and re-sort, so a
|
|
@@ -1186,6 +794,7 @@ class WOFResolver {
|
|
|
1186
794
|
// The within-tier key is now the backend's PROMINENCE (population + proximity, #938 units)
|
|
1187
795
|
// with the posterior as the additive country pin; score stays the final tiebreak. Backends
|
|
1188
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
|
|
1189
798
|
ranked = [...candidates].sort((a, b) => {
|
|
1190
799
|
const tier = Number(b.exactMatch ?? false) - Number(a.exactMatch ?? false);
|
|
1191
800
|
if (tier !== 0)
|
|
@@ -1267,7 +876,7 @@ function decorateNode(node, resolved, alternatives) {
|
|
|
1267
876
|
if (resolved.resolutionQuality) {
|
|
1268
877
|
node.metadata["resolution_quality"] = resolved.resolutionQuality;
|
|
1269
878
|
}
|
|
1270
|
-
if (alternatives.length
|
|
879
|
+
if (alternatives.length) {
|
|
1271
880
|
node.alternatives = alternatives;
|
|
1272
881
|
}
|
|
1273
882
|
}
|