@mailwoman/resolver 9.1.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 +89 -0
- package/lib/admin/coherence-passes.ts +435 -0
- package/lib/admin/containment.ts +92 -0
- package/lib/admin/index.ts +10 -0
- package/lib/admin/jp-municipality.ts +97 -0
- package/lib/admin/winner.ts +189 -0
- package/lib/backend-capabilities.ts +88 -0
- package/lib/bare-toponym-race.ts +129 -0
- package/lib/decorate-node.ts +127 -0
- package/lib/fold-name.ts +36 -0
- package/{index.ts → lib/index.ts} +27 -18
- package/{plausibility.ts → lib/plausibility.ts} +5 -4
- package/{postcode-country-coherence.ts → lib/postcode/country-coherence.ts} +173 -86
- package/lib/postcode/index.ts +9 -0
- package/{postcode-prefix.ts → lib/postcode/prefix.ts} +7 -6
- 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/lib/resolve/index.ts +982 -0
- package/lib/resolve/passes.ts +544 -0
- package/{span-rescore.ts → lib/span-rescore.ts} +114 -99
- package/{street-evidence.ts → lib/street/evidence.ts} +40 -7
- package/lib/street/index.ts +8 -0
- package/{street-tier.ts → lib/street/tier.ts} +138 -99
- package/{toponym-prior.ts → lib/toponym-prior.ts} +97 -8
- package/out/admin/coherence-passes.d.ts +70 -0
- package/out/admin/coherence-passes.d.ts.map +1 -0
- package/out/admin/coherence-passes.js +327 -0
- package/out/admin/coherence-passes.js.map +1 -0
- package/out/admin/containment.d.ts +48 -0
- package/out/admin/containment.d.ts.map +1 -0
- package/out/admin/containment.js +76 -0
- 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 +114 -0
- package/out/admin/winner.d.ts.map +1 -0
- package/out/admin/winner.js +130 -0
- package/out/admin/winner.js.map +1 -0
- package/out/backend-capabilities.d.ts +37 -0
- package/out/backend-capabilities.d.ts.map +1 -0
- package/out/backend-capabilities.js +75 -0
- package/out/backend-capabilities.js.map +1 -0
- package/out/bare-toponym-race.d.ts +55 -0
- package/out/bare-toponym-race.d.ts.map +1 -0
- package/out/bare-toponym-race.js +110 -0
- package/out/bare-toponym-race.js.map +1 -0
- package/out/decorate-node.d.ts +24 -0
- package/out/decorate-node.d.ts.map +1 -0
- package/out/decorate-node.js +103 -0
- package/out/decorate-node.js.map +1 -0
- package/out/fold-name.d.ts +18 -3
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +19 -3
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +15 -12
- package/out/index.d.ts.map +1 -1
- package/out/index.js +9 -7
- 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} +46 -25
- package/out/postcode/country-coherence.d.ts.map +1 -0
- package/out/{postcode-country-coherence.js → postcode/country-coherence.js} +157 -77
- 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} +7 -6
- 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} +2 -2
- package/out/resolve/index.d.ts.map +1 -0
- package/out/resolve/index.js +795 -0
- 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 +87 -71
- 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} +23 -7
- 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} +127 -90
- package/out/street/tier.js.map +1 -0
- package/out/toponym-prior.d.ts +43 -19
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +76 -8
- package/out/toponym-prior.js.map +1 -1
- package/package.json +167 -19
- package/vitest.config.ts +5 -6
- package/fold-name.ts +0 -20
- 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 +0 -1049
- 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
- package/resolve.ts +0 -1369
|
@@ -20,16 +20,17 @@
|
|
|
20
20
|
* (`scripts/eval/span-rescore-validate.ts`, eval
|
|
21
21
|
* `docs/articles/evals/experiments/2026-06-23-370-span-rescore.mdx`): longest-exact-match-wins (the gold
|
|
22
22
|
* locality is usually the LONGER, more-specific name — shortest- wins grabs the ambiguous prefix
|
|
23
|
-
* "Tomaszów" of "Tomaszów Mazowiecki", 135 km off), and a postcode- consistency
|
|
23
|
+
* "Tomaszów" of "Tomaszów Mazowiecki", 135 km off), and a postcode- consistency check that rejects
|
|
24
24
|
* a match far from where the postcode resolves (kills coverage-gap false-positives where the
|
|
25
25
|
* backend has postcode coverage).
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
-
import type
|
|
28
|
+
import { firstNodeWhere, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
29
29
|
import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
|
|
30
30
|
import { haversineKm } from "@mailwoman/spatial"
|
|
31
31
|
|
|
32
|
-
import {
|
|
32
|
+
import { foldName } from "#fold-name"
|
|
33
|
+
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, rankByCountryPrior, rankByImportance } from "#toponym-prior"
|
|
33
34
|
|
|
34
35
|
export interface SpanRescoreOptions {
|
|
35
36
|
/**
|
|
@@ -37,15 +38,15 @@ export interface SpanRescoreOptions {
|
|
|
37
38
|
*/
|
|
38
39
|
country?: string
|
|
39
40
|
/**
|
|
40
|
-
* Sibling postcode — used both as the backend disambiguation hint AND the consistency-
|
|
41
|
+
* Sibling postcode — used both as the backend disambiguation hint AND the consistency-check anchor.
|
|
41
42
|
*/
|
|
42
43
|
postcode?: string
|
|
43
44
|
/**
|
|
44
|
-
* Reject a candidate whose coordinate is farther than this (km) from the postcode anchor. The
|
|
45
|
+
* Reject a candidate whose coordinate is farther than this (km) from the postcode anchor. The check only fires when
|
|
45
46
|
* the postcode resolves to a point in the backend; otherwise it can't and the match is accepted (so it never
|
|
46
47
|
* penalizes a backend without postcode coverage). 0 disables. Default 50.
|
|
47
48
|
*/
|
|
48
|
-
|
|
49
|
+
thresholdKm?: number
|
|
49
50
|
/**
|
|
50
51
|
* Max contiguous raw tokens to treat as one locality span. Default 4.
|
|
51
52
|
*/
|
|
@@ -93,18 +94,18 @@ export interface RescoreCandidate {
|
|
|
93
94
|
*/
|
|
94
95
|
place: ResolvedPlace
|
|
95
96
|
/**
|
|
96
|
-
* Whether the postcode-consistency
|
|
97
|
-
* was validated within `
|
|
98
|
-
* postcode→point coverage for this country, so the match wasn't geo-validated — the ~83%-precision
|
|
99
|
-
* surfaces this as `metadata.
|
|
100
|
-
* map. Deliberately NOT folded into the calibrated `confidence` — that would break the
|
|
101
|
-
* calibrated 0.83 must not be confused with a rescore plug-in estimate).
|
|
97
|
+
* Whether the postcode-consistency check FIRED for this recovery — i.e. the postcode resolved to a point and the
|
|
98
|
+
* match was validated within `thresholdKm` of it. `true` = high-precision (postcode- consistent); `false` =
|
|
99
|
+
* unrestricted (no postcode→point coverage for this country, so the match wasn't geo-validated — the ~83%-precision
|
|
100
|
+
* case). The caller surfaces this as `metadata.rescore_postcode_verified` so a consumer can threshold on it WITHOUT a
|
|
101
|
+
* hidden per-country coverage map. Deliberately NOT folded into the calibrated `confidence` — that would break the
|
|
102
|
+
* isotonic guarantee (a true calibrated 0.83 must not be confused with a rescore plug-in estimate).
|
|
102
103
|
*/
|
|
103
|
-
|
|
104
|
+
postcodeVerified: boolean
|
|
104
105
|
/**
|
|
105
106
|
* The SAME-SPAN namesake runner-ups — the other exact-name matches this recovery's own lookup already returned, in
|
|
106
|
-
* the backend's rank order, minus the winner and minus anything the postcode
|
|
107
|
-
* exactly one place.
|
|
107
|
+
* the backend's rank order, minus the winner and minus anything the postcode check rejected. Empty when the span
|
|
108
|
+
* named exactly one place.
|
|
108
109
|
*
|
|
109
110
|
* #1537: these were being discarded. A name the model reads as a `street` ("Springfield", "Berlin", "Manchester",
|
|
110
111
|
* "Moscow", "Fulda") never reaches the admin walk, so the whole tree comes back unresolved and THIS tier is what
|
|
@@ -114,23 +115,12 @@ export interface RescoreCandidate {
|
|
|
114
115
|
* backend, 2026-08-07: those five queries returned 1 candidate each while `Cambridge`/`Athens`/`Paris` — the same
|
|
115
116
|
* class, but parsed as `locality` — returned 4-5.
|
|
116
117
|
*
|
|
117
|
-
* The winner is unchanged by their presence: the pick is still the first
|
|
118
|
+
* The winner is unchanged by their presence: the pick is still the first check-passing exact match, and these are the
|
|
118
119
|
* ones that came after it. This costs no extra query — they were in the same `findPlace` response.
|
|
119
120
|
*/
|
|
120
121
|
alternatives: ResolvedPlace[]
|
|
121
122
|
}
|
|
122
123
|
|
|
123
|
-
/**
|
|
124
|
-
* Normalize for exact comparison: lowercase, strip diacritics + punctuation, collapse whitespace.
|
|
125
|
-
*/
|
|
126
|
-
const norm = (s: string): string =>
|
|
127
|
-
s
|
|
128
|
-
.toLowerCase()
|
|
129
|
-
.normalize("NFD")
|
|
130
|
-
.replaceAll(/[^a-z0-9 ]/g, " ")
|
|
131
|
-
.replaceAll(/\s+/g, " ")
|
|
132
|
-
.trim()
|
|
133
|
-
|
|
134
124
|
interface RawTok {
|
|
135
125
|
text: string
|
|
136
126
|
start: number
|
|
@@ -179,47 +169,25 @@ const BARE_TOPONYM_FETCH = 20
|
|
|
179
169
|
* the hard filter keeps its say: this is what holds 'Berlin, Wisconsin' on Berlin, Wisconsin.
|
|
180
170
|
*/
|
|
181
171
|
function hasAdminQualifier(roots: readonly AddressNode[]): boolean {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
n.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
) {
|
|
195
|
-
return true
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (n.children?.length) {
|
|
199
|
-
stack.push(...n.children)
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return false
|
|
172
|
+
return (
|
|
173
|
+
firstNodeWhere(
|
|
174
|
+
roots,
|
|
175
|
+
(n) =>
|
|
176
|
+
(n.tag === "region" ||
|
|
177
|
+
n.tag === "subregion" ||
|
|
178
|
+
n.tag === "country" ||
|
|
179
|
+
n.tag === "postcode" ||
|
|
180
|
+
n.tag === "house_number") &&
|
|
181
|
+
n.value.trim().length > 0
|
|
182
|
+
) !== undefined
|
|
183
|
+
)
|
|
204
184
|
}
|
|
205
185
|
|
|
206
186
|
/**
|
|
207
187
|
* True if any node in the tree already carries a resolved place id — the #685 brake.
|
|
208
188
|
*/
|
|
209
189
|
export function hasResolvedPlace(roots: readonly AddressNode[]): boolean {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
while (stack.length) {
|
|
213
|
-
const n = stack.pop()!
|
|
214
|
-
|
|
215
|
-
if (n.placeID) return true
|
|
216
|
-
|
|
217
|
-
if (n.children?.length) {
|
|
218
|
-
stack.push(...n.children)
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return false
|
|
190
|
+
return firstNodeWhere(roots, (n) => Boolean(n.placeID)) !== undefined
|
|
223
191
|
}
|
|
224
192
|
|
|
225
193
|
/**
|
|
@@ -228,6 +196,36 @@ export function hasResolvedPlace(roots: readonly AddressNode[]): boolean {
|
|
|
228
196
|
* NY" is a street suffix, not a locality, and without this guard the recovery exact-matches it against a same-named
|
|
229
197
|
* place ("Ave", France) and injects a bogus locality.
|
|
230
198
|
*/
|
|
199
|
+
/**
|
|
200
|
+
* Ranges of MULTI-TOKEN `country` / `region` spans, used to block their INTERIOR tokens from being re-read as
|
|
201
|
+
* standalone places. The whole span itself stays probeable — only proper sub-spans are refused.
|
|
202
|
+
*
|
|
203
|
+
* The parse grouping is the claim: when the model binds `Papua`, `New` and `Guinea` into one country span, `New` is a
|
|
204
|
+
* modifier inside a name, not a name. Probing it anyway matches a real US place (`New`, wof:1276997945) and pins the
|
|
205
|
+
* address to Kentucky — an answer strictly worse than none, because it is confident and wrong.
|
|
206
|
+
*
|
|
207
|
+
* Deliberately NOT confidence-conditioned, unlike {@link confidentRanges}. The country node in that case carries 0.68,
|
|
208
|
+
* under the 0.7 bar, and a low-confidence GROUPING is still a grouping: the tokens were read as one name either way,
|
|
209
|
+
* and the interior of a name the parse doubts is not thereby a better standalone candidate. Single-token spans are
|
|
210
|
+
* excluded — there is no interior to protect.
|
|
211
|
+
*/
|
|
212
|
+
function multiTokenNameInteriors(roots: readonly AddressNode[], raw: string): Array<[number, number]> {
|
|
213
|
+
const out: Array<[number, number]> = []
|
|
214
|
+
|
|
215
|
+
for (const n of walkNodes(roots)) {
|
|
216
|
+
if (
|
|
217
|
+
(n.tag === "country" || n.tag === "region") &&
|
|
218
|
+
Number.isFinite(n.start) &&
|
|
219
|
+
Number.isFinite(n.end) &&
|
|
220
|
+
tokenizeRaw(raw.slice(n.start, n.end)).length > 1
|
|
221
|
+
) {
|
|
222
|
+
out.push([n.start, n.end])
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return out
|
|
227
|
+
}
|
|
228
|
+
|
|
231
229
|
function confidentRanges(
|
|
232
230
|
roots: readonly AddressNode[],
|
|
233
231
|
threshold: number,
|
|
@@ -235,11 +233,8 @@ function confidentRanges(
|
|
|
235
233
|
postalCompoundRecovery: boolean
|
|
236
234
|
): Array<[number, number]> {
|
|
237
235
|
const out: Array<[number, number]> = []
|
|
238
|
-
const stack: AddressNode[] = [...roots]
|
|
239
|
-
|
|
240
|
-
while (stack.length) {
|
|
241
|
-
const n = stack.pop()!
|
|
242
236
|
|
|
237
|
+
for (const n of walkNodes(roots)) {
|
|
243
238
|
if (
|
|
244
239
|
(n.tag === "postcode" ||
|
|
245
240
|
n.tag === "house_number" ||
|
|
@@ -263,10 +258,6 @@ function confidentRanges(
|
|
|
263
258
|
out.push([n.start, n.end])
|
|
264
259
|
}
|
|
265
260
|
}
|
|
266
|
-
|
|
267
|
-
if (n.children?.length) {
|
|
268
|
-
stack.push(...n.children)
|
|
269
|
-
}
|
|
270
261
|
}
|
|
271
262
|
|
|
272
263
|
return out
|
|
@@ -274,7 +265,7 @@ function confidentRanges(
|
|
|
274
265
|
|
|
275
266
|
/**
|
|
276
267
|
* Find the best locality the raw text exact-matches in the gazetteer. Returns null when nothing matches (or the
|
|
277
|
-
* postcode
|
|
268
|
+
* postcode check rejects every match). Callers test `hasResolvedPlace` first.
|
|
278
269
|
*/
|
|
279
270
|
export async function findRescoreCandidate(
|
|
280
271
|
raw: string,
|
|
@@ -282,21 +273,21 @@ export async function findRescoreCandidate(
|
|
|
282
273
|
backend: ResolverBackend,
|
|
283
274
|
opts: SpanRescoreOptions = {}
|
|
284
275
|
): Promise<RescoreCandidate | null> {
|
|
285
|
-
const
|
|
276
|
+
const thresholdKm = opts.thresholdKm ?? 50
|
|
286
277
|
const maxSpan = opts.maxSpanTokens ?? 4
|
|
287
278
|
const threshold = opts.confidentThreshold ?? 0.7
|
|
288
279
|
const country = opts.country
|
|
289
280
|
const postcode = opts.postcode?.trim() || undefined
|
|
290
281
|
|
|
291
282
|
// Postcode-consistency anchor: where does the postcode itself resolve? (No-op when the backend has
|
|
292
|
-
// no postcode coverage — findPlace returns nothing → no anchor →
|
|
283
|
+
// no postcode coverage — findPlace returns nothing → no anchor → check can't fire → match accepted.)
|
|
293
284
|
let anchor: { lat: number; lon: number } | null = null
|
|
294
285
|
|
|
295
|
-
if (postcode &&
|
|
286
|
+
if (postcode && thresholdKm > 0) {
|
|
296
287
|
// #961: both anchor probes are postalcode-TYPED. Untyped, a truncated code fragment (v5.3.0
|
|
297
288
|
// emits "250 Zabiče" → subset "250") name-matches arbitrary places ("Chak No 250", PK) and the
|
|
298
|
-
// false anchor then
|
|
299
|
-
// accepted
|
|
289
|
+
// false anchor then EXCLUDES the true village. A typed miss leaves anchor=null → the match is
|
|
290
|
+
// accepted unrestricted, which is the correct degradation for an unverifiable code.
|
|
300
291
|
const pcHits = await backend.findPlace({ text: postcode, country, placetype: "postalcode", limit: 2 })
|
|
301
292
|
const a = pcHits.find((h) => h.lat !== 0 || h.lon !== 0)
|
|
302
293
|
|
|
@@ -305,7 +296,7 @@ export async function findRescoreCandidate(
|
|
|
305
296
|
}
|
|
306
297
|
|
|
307
298
|
// #942: the globbed compound ("1382 Kožljek") matches no bare-code row — retry the anchor with
|
|
308
|
-
// the code-shaped token subset so the consistency
|
|
299
|
+
// the code-shaped token subset so the consistency check can validate the recovered city.
|
|
309
300
|
if (!anchor && opts.postalCompoundRecovery) {
|
|
310
301
|
const code = postcodeCodeSubset(postcode)
|
|
311
302
|
|
|
@@ -323,6 +314,11 @@ export async function findRescoreCandidate(
|
|
|
323
314
|
const toks = tokenizeRaw(raw)
|
|
324
315
|
const avoid = confidentRanges(roots, threshold, raw, opts.postalCompoundRecovery ?? false)
|
|
325
316
|
const overlapsAvoid = (s: number, e: number) => avoid.some(([as, ae]) => s < ae && as < e)
|
|
317
|
+
// Proper sub-spans of a multi-token country/region name are refused; the whole span is not.
|
|
318
|
+
const nameInteriors = multiTokenNameInteriors(roots, raw)
|
|
319
|
+
|
|
320
|
+
const isNameInterior = (s: number, e: number) =>
|
|
321
|
+
nameInteriors.some(([ns, ne]) => s >= ns && e <= ne && !(s === ns && e === ne))
|
|
326
322
|
|
|
327
323
|
// Enumerate contiguous spans, LONGEST first — the gold locality is usually the more-specific
|
|
328
324
|
// (longer) name; longest-wins lets it beat its own ambiguous prefix.
|
|
@@ -341,6 +337,8 @@ export async function findRescoreCandidate(
|
|
|
341
337
|
const end = toks[i + len - 1]!.end
|
|
342
338
|
|
|
343
339
|
if (overlapsAvoid(start, end)) continue
|
|
340
|
+
|
|
341
|
+
if (isNameInterior(start, end)) continue
|
|
344
342
|
spans.push({ text: raw.slice(start, end), start, end, len })
|
|
345
343
|
}
|
|
346
344
|
}
|
|
@@ -355,12 +353,12 @@ export async function findRescoreCandidate(
|
|
|
355
353
|
//
|
|
356
354
|
// So for this ONE query shape the filter is demoted to an additive prior: probe the gazetteer
|
|
357
355
|
// unscoped, then rank with `rankByCountryPrior` so an in-country place may be up to 100x smaller and
|
|
358
|
-
// still win. This is the #912
|
|
356
|
+
// still win. This is the #912 change ("Paris under en-US must not be hard-scoped to Paris, Texas")
|
|
359
357
|
// applied at the tier that actually decides the class — #912 lives upstream in the CLI and keys on a
|
|
360
358
|
// `locality`-tagged tree, but the model reads a bare famous name as a `street`, so the shape that
|
|
361
359
|
// needs it most is exactly the shape that guard cannot see.
|
|
362
360
|
//
|
|
363
|
-
// The shape
|
|
361
|
+
// The shape check is deliberately narrow. `qualified` below is the D-rule guard: a postcode, a region,
|
|
364
362
|
// a country or a house number in the tree means the address named its own context and a locale guess
|
|
365
363
|
// is no longer the only evidence — 'Berlin, Wisconsin' keeps resolving to Berlin, Wisconsin. And the
|
|
366
364
|
// span must cover the WHOLE input: a sub-span of a longer query is not a bare toponym, so
|
|
@@ -373,10 +371,16 @@ export async function findRescoreCandidate(
|
|
|
373
371
|
const softCountryEligible = softCountry && !!country && !qualified && !!wholeInput
|
|
374
372
|
|
|
375
373
|
for (const sp of spans) {
|
|
376
|
-
const key =
|
|
374
|
+
const key = foldName(sp.text)
|
|
377
375
|
|
|
378
376
|
if (key.length < 2 || /^\d+$/.test(key)) continue // skip bare numbers / empties
|
|
379
|
-
|
|
377
|
+
// Whole-input coverage and the soft-country prior are SEPARATE checks. `bare` (the prior) also
|
|
378
|
+
// needs an unqualified tree + a caller country; `wholeSpan` alone decides the alias tier below —
|
|
379
|
+
// a scope-less bare "Riyadh"/"Frankfurt" is still a NAMING (their Latin surfaces live in alias
|
|
380
|
+
// rows: الرياض / Frankfurt am Main), and conflating the two checks cost exactly those rows on the
|
|
381
|
+
// 2026-08-15 board before this line split them.
|
|
382
|
+
const wholeSpan = !!wholeInput && sp.start === wholeInput.start && sp.end === wholeInput.end
|
|
383
|
+
const bare = softCountryEligible && wholeSpan
|
|
380
384
|
|
|
381
385
|
const hits = bare
|
|
382
386
|
? rankByCountryPrior(
|
|
@@ -384,7 +388,18 @@ export async function findRescoreCandidate(
|
|
|
384
388
|
country,
|
|
385
389
|
countryWeight
|
|
386
390
|
)
|
|
387
|
-
:
|
|
391
|
+
: // A SUB-span probe is a RE-READING of a token the parse classified into a longer span — it
|
|
392
|
+
// never NAMED an alias, so alias-keyed rows are off for it (`primaryOnly`, the #1632
|
|
393
|
+
// Savile→Rhu door). A whole-input span keeps the alias tier regardless of scope (the
|
|
394
|
+
// Москва/Riyadh-class exonym recall, #1546).
|
|
395
|
+
await backend.findPlace({
|
|
396
|
+
text: sp.text,
|
|
397
|
+
country,
|
|
398
|
+
postcode,
|
|
399
|
+
placetype: "locality",
|
|
400
|
+
limit: 5,
|
|
401
|
+
...(wholeSpan ? {} : { primaryOnly: true }),
|
|
402
|
+
})
|
|
388
403
|
|
|
389
404
|
// #1546: NO primary-name re-check here — the backend's `exactMatch` IS the name-OR-alias surface
|
|
390
405
|
// equality (the names table / alt_names bag), so a query matches a place when ANY stored name
|
|
@@ -392,7 +407,7 @@ export async function findRescoreCandidate(
|
|
|
392
407
|
// non-Latin-primary class: Москва folds to "" and could never equal "moscow", so Moscow RU never
|
|
393
408
|
// entered the list and Moscow, Idaho won by default among the Latin-named bearers — population-
|
|
394
409
|
// first ranking starved, not violated. The alias surface is the recall; ranking then does its job.
|
|
395
|
-
// The postcode
|
|
410
|
+
// The postcode check below still applies to every admitted candidate, Moscow RU included.
|
|
396
411
|
//
|
|
397
412
|
// #17: importance-first WITHIN the admitted set. The key is the #28 blended fame prior
|
|
398
413
|
// (`PlaceCandidate.importance`, produced by the candidate build) — the only key that separates
|
|
@@ -402,25 +417,25 @@ export async function findRescoreCandidate(
|
|
|
402
417
|
// put it.
|
|
403
418
|
const exact = rankByImportance(hits.filter((h) => h.exactMatch && (h.lat !== 0 || h.lon !== 0)))
|
|
404
419
|
|
|
405
|
-
const
|
|
406
|
-
!anchor ||
|
|
420
|
+
const withinThreshold = (p: ResolvedPlace): boolean =>
|
|
421
|
+
!anchor || thresholdKm <= 0 || haversineKm(anchor.lat, anchor.lon, p.lat, p.lon) <= thresholdKm
|
|
407
422
|
|
|
408
423
|
for (const h of exact) {
|
|
409
|
-
if (!
|
|
424
|
+
if (!withinThreshold(h)) continue
|
|
410
425
|
|
|
411
|
-
//
|
|
412
|
-
// (no postcode→point coverage), the match is
|
|
426
|
+
// conditional = the postcode anchor existed AND validated this match (within thresholdKm). When no anchor
|
|
427
|
+
// (no postcode→point coverage), the match is unrestricted — returned, but flagged lower-precision.
|
|
413
428
|
//
|
|
414
|
-
// #1537: carry the rest of the SAME lookup's exact matches as the namesake runner-ups.
|
|
415
|
-
// same rule as the winner — a candidate the postcode
|
|
429
|
+
// #1537: carry the rest of the SAME lookup's exact matches as the namesake runner-ups. Check-filtered on the
|
|
430
|
+
// same rule as the winner — a candidate the postcode check rejected is not a namesake worth offering, it is a
|
|
416
431
|
// place the evidence already excluded. Rank order is the backend's, preserved by `filter`.
|
|
417
432
|
return {
|
|
418
433
|
text: sp.text,
|
|
419
434
|
start: sp.start,
|
|
420
435
|
end: sp.end,
|
|
421
436
|
place: h,
|
|
422
|
-
|
|
423
|
-
alternatives: exact.filter((a) => a !== h &&
|
|
437
|
+
postcodeVerified: anchor !== null,
|
|
438
|
+
alternatives: exact.filter((a) => a !== h && withinThreshold(a)),
|
|
424
439
|
}
|
|
425
440
|
}
|
|
426
441
|
}
|
|
@@ -429,16 +444,16 @@ export async function findRescoreCandidate(
|
|
|
429
444
|
// CLI's en-US default scoped both the anchor and the village probe to US, so the SI floor never
|
|
430
445
|
// fired through geocode-core while the same rows resolved 25/25 on the resolver harness. When the
|
|
431
446
|
// scoped pass finds nothing and a postcode is present, re-probe the spans UNSCOPED (the admin
|
|
432
|
-
// gazetteer is one
|
|
433
|
-
// code subset resolved in the CANDIDATE's own country (postcode
|
|
434
|
-
// cross-country promotion is accepted ONLY postcode-verified within the
|
|
447
|
+
// gazetteer is one extract, all countries), then verify each exact candidate against the postcode's
|
|
448
|
+
// code subset resolved in the CANDIDATE's own country (postcode extracts route by country). A
|
|
449
|
+
// cross-country promotion is accepted ONLY postcode-verified within the radius — never unverified —
|
|
435
450
|
// so a US-shaped query can't wander abroad on a name coincidence (the 48026 guard: resolved
|
|
436
451
|
// trees never reach this code, and unresolved ones must pass the joint postcode check).
|
|
437
|
-
if (opts.postalCompoundRecovery && postcode &&
|
|
452
|
+
if (opts.postalCompoundRecovery && postcode && thresholdKm > 0) {
|
|
438
453
|
const code = postcodeCodeSubset(postcode) || postcode.trim()
|
|
439
454
|
|
|
440
455
|
for (const sp of spans) {
|
|
441
|
-
const key =
|
|
456
|
+
const key = foldName(sp.text)
|
|
442
457
|
|
|
443
458
|
if (key.length < 2 || /^\d+$/.test(key)) continue
|
|
444
459
|
const hits = await backend.findPlace({ text: sp.text, placetype: "locality", limit: 5 })
|
|
@@ -460,13 +475,13 @@ export async function findRescoreCandidate(
|
|
|
460
475
|
|
|
461
476
|
const verified = pcHits.find((p) => p.lat !== 0 || p.lon !== 0)
|
|
462
477
|
|
|
463
|
-
if (verified && haversineKm(verified.lat, verified.lon, h.lat, h.lon) <=
|
|
478
|
+
if (verified && haversineKm(verified.lat, verified.lon, h.lat, h.lon) <= thresholdKm) {
|
|
464
479
|
// No alternatives from this pass, deliberately. Admission here is per-candidate postcode
|
|
465
480
|
// verification — one `findPlace` per runner-up — and the class it serves is the opposite of the
|
|
466
481
|
// namesake one: a postcode is present and has already picked the country, so there is nothing
|
|
467
482
|
// ambiguous left to declare. The bare-toponym queries #1537 is about never reach this branch (it
|
|
468
483
|
// requires a postcode).
|
|
469
|
-
return { text: sp.text, start: sp.start, end: sp.end, place: h,
|
|
484
|
+
return { text: sp.text, start: sp.start, end: sp.end, place: h, postcodeVerified: true, alternatives: [] }
|
|
470
485
|
}
|
|
471
486
|
}
|
|
472
487
|
}
|
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
* mirroring the `PlaceLookup` pattern.
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
|
+
import type { Exclusion } from "@mailwoman/evidence"
|
|
31
|
+
|
|
30
32
|
/**
|
|
31
33
|
* A street-name existence probe. Backend-agnostic; the FR instance is BAN street-centroids, a future US instance is
|
|
32
34
|
* TIGER, etc. (per the registry-backed-structured-prediction doctrine tiers).
|
|
@@ -113,7 +115,7 @@ const FR_STREET_TYPE_WORDS: ReadonlySet<string> = new Set([
|
|
|
113
115
|
* True when the folded surface contains no token outside {@link FR_STREET_TYPE_WORDS} — i.e. it is pure type/particle.
|
|
114
116
|
*/
|
|
115
117
|
export function isPureTypeVocabulary(foldedSurface: string): boolean {
|
|
116
|
-
const tokens = foldedSurface.split(" ").filter(
|
|
118
|
+
const tokens = foldedSurface.split(" ").filter((value) => value.length > 0)
|
|
117
119
|
|
|
118
120
|
if (!tokens.length) return true
|
|
119
121
|
|
|
@@ -147,10 +149,18 @@ export interface PickByStreetEvidenceOpts {
|
|
|
147
149
|
* G2 — the margin cap. A candidate whose score is more than this far below rank-1 is never promoted by evidence
|
|
148
150
|
* (without it, evidence reaches deep down the list and moves off correct rank-1 parses). Default 2.5 — the value the
|
|
149
151
|
* v2 board measured (148 fixes / 3 breaks). UNCALIBRATED across models: re-fit when the span head retrains, since raw
|
|
150
|
-
* score margins are not comparable across models. (Plan #1134 pre-registers an isotonic ambiguity
|
|
152
|
+
* score margins are not comparable across models. (Plan #1134 pre-registers an isotonic ambiguity check to replace
|
|
151
153
|
* it.)
|
|
152
154
|
*/
|
|
153
155
|
marginCap?: number
|
|
156
|
+
/**
|
|
157
|
+
* One entry per candidate, positionally aligned. A non-null entry DEMOTES that candidate by one bit — it is
|
|
158
|
+
* considered only after every un-excluded sibling. It is never removed: with every candidate excluded the pick is
|
|
159
|
+
* still rank-1, because the worst case this policy accepts is the model's own ranking.
|
|
160
|
+
*
|
|
161
|
+
* Omitted or all-null reproduces the measured v2 policy exactly.
|
|
162
|
+
*/
|
|
163
|
+
exclusions?: ReadonlyArray<Exclusion | null>
|
|
154
164
|
}
|
|
155
165
|
|
|
156
166
|
export interface StreetEvidencePick<T = unknown> {
|
|
@@ -166,6 +176,11 @@ export interface StreetEvidencePick<T = unknown> {
|
|
|
166
176
|
* True when evidence MOVED the pick off rank-1 (a rank-2-beats-rank-1 correction — loggable training signal).
|
|
167
177
|
*/
|
|
168
178
|
moved: boolean
|
|
179
|
+
/**
|
|
180
|
+
* Indices an exclusion demoted, in input order. Empty when no exclusion applied — a loggable record of what the
|
|
181
|
+
* coverage check licensed, distinct from what evidence found.
|
|
182
|
+
*/
|
|
183
|
+
demoted: number[]
|
|
169
184
|
}
|
|
170
185
|
|
|
171
186
|
/**
|
|
@@ -173,7 +188,8 @@ export interface StreetEvidencePick<T = unknown> {
|
|
|
173
188
|
* first candidate whose street surface passes ALL of: (1) exists in the index, (2) G1 — not pure type vocabulary, (3)
|
|
174
189
|
* G2 — within `marginCap` of rank-1. If none passes, return rank-1 (fail-open). Positive evidence only; the model's
|
|
175
190
|
* order is preserved among equal-evidence candidates. This is the `resolver/rerank.ts` anti-Pelias discipline applied
|
|
176
|
-
* to the name signal: one bit, no blending.
|
|
191
|
+
* to the name signal: one bit, no blending. `opts.exclusions` adds one more bit in the same fold: a coverage-licensed
|
|
192
|
+
* absence demotes its candidate behind every un-excluded sibling and never removes it.
|
|
177
193
|
*
|
|
178
194
|
* @param candidates Parse candidates, rank-1 FIRST (the caller sorts by score descending).
|
|
179
195
|
*/
|
|
@@ -190,22 +206,39 @@ export function pickByStreetEvidence<T>(
|
|
|
190
206
|
|
|
191
207
|
const rank1 = candidates[0]!
|
|
192
208
|
const topScore = rank1.score
|
|
209
|
+
const demoted: number[] = []
|
|
210
|
+
const considered: number[] = []
|
|
193
211
|
|
|
194
212
|
for (let i = 0; i < candidates.length; i++) {
|
|
213
|
+
if (opts.exclusions?.[i]) {
|
|
214
|
+
demoted.push(i)
|
|
215
|
+
} else {
|
|
216
|
+
considered.push(i)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// An excluded candidate is considered only after every un-excluded sibling; it is never dropped.
|
|
221
|
+
const order = demoted.length ? [...considered, ...demoted] : considered
|
|
222
|
+
|
|
223
|
+
for (const i of order) {
|
|
195
224
|
const c = candidates[i]!
|
|
196
225
|
|
|
197
226
|
if (!c.streetSurface) continue
|
|
198
227
|
|
|
199
|
-
// G2:
|
|
200
|
-
if (topScore - c.score > marginCap)
|
|
228
|
+
// G2: the margin is measured from rank-1's score, whatever position the candidate is considered at.
|
|
229
|
+
if (topScore - c.score > marginCap) continue
|
|
201
230
|
|
|
202
231
|
// G1: a pure street-type/particle surface (bare `rue`) carries no name — no evidence credit.
|
|
203
232
|
if (isPureTypeVocabulary(foldStreetSurface(c.streetSurface))) continue
|
|
204
233
|
|
|
205
234
|
if (evidence.hasStreetName(c.streetSurface, opts.scope)) {
|
|
206
|
-
return { candidate: c, index: i, moved: i > 0 }
|
|
235
|
+
return { candidate: c, index: i, moved: i > 0, demoted }
|
|
207
236
|
}
|
|
208
237
|
}
|
|
209
238
|
|
|
210
|
-
|
|
239
|
+
// Fail-open: the first un-excluded candidate, which is rank-1 unless an exclusion demoted it, and rank-1 again when
|
|
240
|
+
// every candidate is excluded.
|
|
241
|
+
const fallback = order[0] ?? 0
|
|
242
|
+
|
|
243
|
+
return { candidate: candidates[fallback]!, index: fallback, moved: fallback > 0, demoted }
|
|
211
244
|
}
|