@mailwoman/resolver 9.1.0 → 9.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/README.md +89 -0
- package/admin-coherence-passes.ts +435 -0
- package/admin-containment.ts +101 -0
- package/admin-winner.ts +178 -0
- package/backend-capabilities.ts +88 -0
- package/bare-toponym-race.ts +129 -0
- package/decorate-node.ts +127 -0
- package/fold-name.ts +19 -3
- package/index.ts +14 -0
- 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 +82 -0
- package/out/admin-containment.js.map +1 -0
- package/out/admin-winner.d.ts +109 -0
- package/out/admin-winner.d.ts.map +1 -0
- package/out/admin-winner.js +119 -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 +4 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +3 -1
- package/out/index.js.map +1 -1
- package/out/postcode-country-coherence.d.ts +31 -10
- package/out/postcode-country-coherence.d.ts.map +1 -1
- package/out/postcode-country-coherence.js +137 -56
- package/out/postcode-country-coherence.js.map +1 -1
- package/out/postcode-prefix.d.ts.map +1 -1
- package/out/postcode-prefix.js +3 -2
- package/out/postcode-prefix.js.map +1 -1
- package/out/postcode-shape-coherence.js.map +1 -1
- package/out/resolve.d.ts +1 -1
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +461 -416
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +55 -13
- package/out/span-rescore.js.map +1 -1
- package/out/street-evidence.js +1 -1
- package/out/street-evidence.js.map +1 -1
- package/out/street-tier.d.ts.map +1 -1
- package/out/street-tier.js +26 -5
- package/out/street-tier.js.map +1 -1
- package/out/toponym-prior.d.ts +42 -18
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +75 -7
- package/out/toponym-prior.js.map +1 -1
- package/package.json +98 -7
- package/postcode-country-coherence.ts +150 -58
- package/postcode-prefix.ts +3 -2
- package/postcode-shape-coherence.ts +1 -1
- package/resolve.ts +626 -531
- package/span-rescore.ts +64 -15
- package/street-evidence.ts +1 -1
- package/street-tier.ts +29 -5
- package/toponym-prior.ts +96 -7
package/span-rescore.ts
CHANGED
|
@@ -29,6 +29,7 @@ import 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 { foldName } from "./fold-name.ts"
|
|
32
33
|
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, rankByCountryPrior, rankByImportance } from "./toponym-prior.ts"
|
|
33
34
|
|
|
34
35
|
export interface SpanRescoreOptions {
|
|
@@ -120,17 +121,6 @@ export interface RescoreCandidate {
|
|
|
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
|
|
@@ -228,6 +218,41 @@ export function hasResolvedPlace(roots: readonly AddressNode[]): boolean {
|
|
|
228
218
|
* NY" is a street suffix, not a locality, and without this guard the recovery exact-matches it against a same-named
|
|
229
219
|
* place ("Ave", France) and injects a bogus locality.
|
|
230
220
|
*/
|
|
221
|
+
/**
|
|
222
|
+
* Ranges of MULTI-TOKEN `country` / `region` spans, used to block their INTERIOR tokens from being re-read as
|
|
223
|
+
* standalone places. The whole span itself stays probeable — only proper sub-spans are refused.
|
|
224
|
+
*
|
|
225
|
+
* The parse grouping is the claim: when the model binds `Papua`, `New` and `Guinea` into one country span, `New` is a
|
|
226
|
+
* modifier inside a name, not a name. Probing it anyway matches a real US place (`New`, wof:1276997945) and pins the
|
|
227
|
+
* address to Kentucky — an answer strictly worse than none, because it is confident and wrong.
|
|
228
|
+
*
|
|
229
|
+
* Deliberately NOT confidence-gated, unlike {@link confidentRanges}. The country node in that case carries 0.68, under
|
|
230
|
+
* the 0.7 bar, and a low-confidence GROUPING is still a grouping: the tokens were read as one name either way, and the
|
|
231
|
+
* interior of a name the parse doubts is not thereby a better standalone candidate. Single-token spans are excluded —
|
|
232
|
+
* there is no interior to protect.
|
|
233
|
+
*/
|
|
234
|
+
function multiTokenNameInteriors(roots: readonly AddressNode[], raw: string): Array<[number, number]> {
|
|
235
|
+
const out: Array<[number, number]> = []
|
|
236
|
+
const stack: AddressNode[] = [...roots]
|
|
237
|
+
|
|
238
|
+
while (stack.length) {
|
|
239
|
+
const n = stack.pop()!
|
|
240
|
+
|
|
241
|
+
if (
|
|
242
|
+
(n.tag === "country" || n.tag === "region") &&
|
|
243
|
+
Number.isFinite(n.start) &&
|
|
244
|
+
Number.isFinite(n.end) &&
|
|
245
|
+
tokenizeRaw(raw.slice(n.start, n.end)).length > 1
|
|
246
|
+
) {
|
|
247
|
+
out.push([n.start, n.end])
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
stack.push(...n.children)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return out
|
|
254
|
+
}
|
|
255
|
+
|
|
231
256
|
function confidentRanges(
|
|
232
257
|
roots: readonly AddressNode[],
|
|
233
258
|
threshold: number,
|
|
@@ -323,6 +348,11 @@ export async function findRescoreCandidate(
|
|
|
323
348
|
const toks = tokenizeRaw(raw)
|
|
324
349
|
const avoid = confidentRanges(roots, threshold, raw, opts.postalCompoundRecovery ?? false)
|
|
325
350
|
const overlapsAvoid = (s: number, e: number) => avoid.some(([as, ae]) => s < ae && as < e)
|
|
351
|
+
// Proper sub-spans of a multi-token country/region name are refused; the whole span is not.
|
|
352
|
+
const nameInteriors = multiTokenNameInteriors(roots, raw)
|
|
353
|
+
|
|
354
|
+
const isNameInterior = (s: number, e: number) =>
|
|
355
|
+
nameInteriors.some(([ns, ne]) => s >= ns && e <= ne && !(s === ns && e === ne))
|
|
326
356
|
|
|
327
357
|
// Enumerate contiguous spans, LONGEST first — the gold locality is usually the more-specific
|
|
328
358
|
// (longer) name; longest-wins lets it beat its own ambiguous prefix.
|
|
@@ -341,6 +371,8 @@ export async function findRescoreCandidate(
|
|
|
341
371
|
const end = toks[i + len - 1]!.end
|
|
342
372
|
|
|
343
373
|
if (overlapsAvoid(start, end)) continue
|
|
374
|
+
|
|
375
|
+
if (isNameInterior(start, end)) continue
|
|
344
376
|
spans.push({ text: raw.slice(start, end), start, end, len })
|
|
345
377
|
}
|
|
346
378
|
}
|
|
@@ -373,10 +405,16 @@ export async function findRescoreCandidate(
|
|
|
373
405
|
const softCountryEligible = softCountry && !!country && !qualified && !!wholeInput
|
|
374
406
|
|
|
375
407
|
for (const sp of spans) {
|
|
376
|
-
const key =
|
|
408
|
+
const key = foldName(sp.text)
|
|
377
409
|
|
|
378
410
|
if (key.length < 2 || /^\d+$/.test(key)) continue // skip bare numbers / empties
|
|
379
|
-
|
|
411
|
+
// Whole-input coverage and the soft-country prior are SEPARATE gates. `bare` (the prior) also
|
|
412
|
+
// needs an unqualified tree + a caller country; `wholeSpan` alone decides the alias tier below —
|
|
413
|
+
// a scope-less bare "Riyadh"/"Frankfurt" is still a NAMING (their Latin surfaces live in alias
|
|
414
|
+
// rows: الرياض / Frankfurt am Main), and conflating the two gates cost exactly those rows on the
|
|
415
|
+
// 2026-08-15 board before this line split them.
|
|
416
|
+
const wholeSpan = !!wholeInput && sp.start === wholeInput.start && sp.end === wholeInput.end
|
|
417
|
+
const bare = softCountryEligible && wholeSpan
|
|
380
418
|
|
|
381
419
|
const hits = bare
|
|
382
420
|
? rankByCountryPrior(
|
|
@@ -384,7 +422,18 @@ export async function findRescoreCandidate(
|
|
|
384
422
|
country,
|
|
385
423
|
countryWeight
|
|
386
424
|
)
|
|
387
|
-
:
|
|
425
|
+
: // A SUB-span probe is a RE-READING of a token the parse classified into a longer span — it
|
|
426
|
+
// never NAMED an alias, so alias-keyed rows are off for it (`primaryOnly`, the #1632
|
|
427
|
+
// Savile→Rhu door). A whole-input span keeps the alias tier regardless of scope (the
|
|
428
|
+
// Москва/Riyadh-class exonym recall, #1546).
|
|
429
|
+
await backend.findPlace({
|
|
430
|
+
text: sp.text,
|
|
431
|
+
country,
|
|
432
|
+
postcode,
|
|
433
|
+
placetype: "locality",
|
|
434
|
+
limit: 5,
|
|
435
|
+
...(wholeSpan ? {} : { primaryOnly: true }),
|
|
436
|
+
})
|
|
388
437
|
|
|
389
438
|
// #1546: NO primary-name re-check here — the backend's `exactMatch` IS the name-OR-alias surface
|
|
390
439
|
// equality (the names table / alt_names bag), so a query matches a place when ANY stored name
|
|
@@ -438,7 +487,7 @@ export async function findRescoreCandidate(
|
|
|
438
487
|
const code = postcodeCodeSubset(postcode) || postcode.trim()
|
|
439
488
|
|
|
440
489
|
for (const sp of spans) {
|
|
441
|
-
const key =
|
|
490
|
+
const key = foldName(sp.text)
|
|
442
491
|
|
|
443
492
|
if (key.length < 2 || /^\d+$/.test(key)) continue
|
|
444
493
|
const hits = await backend.findPlace({ text: sp.text, placetype: "locality", limit: 5 })
|
package/street-evidence.ts
CHANGED
|
@@ -113,7 +113,7 @@ const FR_STREET_TYPE_WORDS: ReadonlySet<string> = new Set([
|
|
|
113
113
|
* True when the folded surface contains no token outside {@link FR_STREET_TYPE_WORDS} — i.e. it is pure type/particle.
|
|
114
114
|
*/
|
|
115
115
|
export function isPureTypeVocabulary(foldedSurface: string): boolean {
|
|
116
|
-
const tokens = foldedSurface.split(" ").filter(
|
|
116
|
+
const tokens = foldedSurface.split(" ").filter((value) => value.length > 0)
|
|
117
117
|
|
|
118
118
|
if (!tokens.length) return true
|
|
119
119
|
|
package/street-tier.ts
CHANGED
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
import { isStreetDirectionalToken } from "@mailwoman/codex/us"
|
|
16
16
|
import type { AddressNode } from "@mailwoman/core/decoder"
|
|
17
17
|
import type { AddressPointLookup, InterpolationLookup, StreetCentroidLookup } from "@mailwoman/core/resolver"
|
|
18
|
-
import { haversineKm } from "@mailwoman/spatial"
|
|
19
18
|
|
|
20
19
|
import { foldName } from "./fold-name.ts"
|
|
21
20
|
|
|
@@ -146,7 +145,16 @@ export function applyAddressPoint(roots: AddressNode[], lookup: AddressPointLook
|
|
|
146
145
|
|
|
147
146
|
street.metadata = {
|
|
148
147
|
...street.metadata,
|
|
149
|
-
address_point: {
|
|
148
|
+
address_point: {
|
|
149
|
+
lat: hit.lat,
|
|
150
|
+
lon: hit.lon,
|
|
151
|
+
source: hit.source,
|
|
152
|
+
release: hit.release,
|
|
153
|
+
// The register row's OWN scope tags, when carried — a rooftop consumer can decorate the
|
|
154
|
+
// commune/postcode the register attests even when the query never named them.
|
|
155
|
+
...(hit.localityNorm ? { locality_norm: hit.localityNorm } : {}),
|
|
156
|
+
...(hit.postcode ? { postcode: hit.postcode } : {}),
|
|
157
|
+
},
|
|
150
158
|
resolution_tier: "address_point",
|
|
151
159
|
}
|
|
152
160
|
}
|
|
@@ -167,6 +175,7 @@ export function applyInterpolation(
|
|
|
167
175
|
let houseNumber: AddressNode | undefined
|
|
168
176
|
let directionalUnit: AddressNode | undefined
|
|
169
177
|
let postcode: string | undefined
|
|
178
|
+
let localityCoord: { lat: number; lon: number } | undefined
|
|
170
179
|
const stack = [...roots]
|
|
171
180
|
|
|
172
181
|
while (stack.length) {
|
|
@@ -188,6 +197,13 @@ export function applyInterpolation(
|
|
|
188
197
|
postcode = n.value.trim()
|
|
189
198
|
}
|
|
190
199
|
|
|
200
|
+
// The resolved locality's coordinate — the `near` tie-breaker the interpolator may consult when
|
|
201
|
+
// the query carries no postcode and the covering ranges span several ZIPs (the borough-namesake
|
|
202
|
+
// class). Only a RESOLVED locality qualifies; an unresolved one contributes nothing.
|
|
203
|
+
if (n.tag === "locality" && !localityCoord && n.lat != null && n.lon != null) {
|
|
204
|
+
localityCoord = { lat: n.lat, lon: n.lon }
|
|
205
|
+
}
|
|
206
|
+
|
|
191
207
|
stack.push(...n.children)
|
|
192
208
|
}
|
|
193
209
|
|
|
@@ -195,7 +211,15 @@ export function applyInterpolation(
|
|
|
195
211
|
|
|
196
212
|
// The fall-through gate: an exact situs point already won — never override it with an estimate.
|
|
197
213
|
if (street.metadata?.["resolution_tier"] === "address_point") return
|
|
198
|
-
|
|
214
|
+
|
|
215
|
+
const near = postcode ? undefined : localityCoord
|
|
216
|
+
|
|
217
|
+
const hit = lookup.find({
|
|
218
|
+
street: assembleStreetValue(street, directionalUnit),
|
|
219
|
+
number: houseNumber.value,
|
|
220
|
+
postcode,
|
|
221
|
+
...(near ? { near } : {}),
|
|
222
|
+
})
|
|
199
223
|
|
|
200
224
|
if (!hit) return
|
|
201
225
|
// Conformal-calibrated radius (#374): the raw half-segment heuristic underestimates the true spread
|
|
@@ -288,7 +312,7 @@ function foldVoieTokens(s: string): string[] {
|
|
|
288
312
|
.replaceAll(/[.,'’]/g, "")
|
|
289
313
|
.replaceAll("-", " ")
|
|
290
314
|
.split(/\s+/)
|
|
291
|
-
.filter(
|
|
315
|
+
.filter((token) => token.length > 0)
|
|
292
316
|
}
|
|
293
317
|
|
|
294
318
|
/**
|
|
@@ -391,7 +415,7 @@ export function applyStreetCentroid(
|
|
|
391
415
|
const rawSegments = raw
|
|
392
416
|
.split(",")
|
|
393
417
|
.map((s) => s.trim())
|
|
394
|
-
.filter(
|
|
418
|
+
.filter((segment) => segment.length > 0)
|
|
395
419
|
|
|
396
420
|
const CAP = 5
|
|
397
421
|
|
package/toponym-prior.ts
CHANGED
|
@@ -41,7 +41,10 @@
|
|
|
41
41
|
* rank population gave it while the scored rows reorder among themselves. On a candidate.db built
|
|
42
42
|
* before the #28 `importance` column, nothing is scored, so {@link rankByImportance} is
|
|
43
43
|
* byte-stable by construction.
|
|
44
|
-
* 3. **Stable.** Equal keys keep their incoming order, so the backend's own ranking survives underneath.
|
|
44
|
+
* 3. **Stable.** Equal keys keep their incoming order, so the backend's own ranking survives underneath. That order
|
|
45
|
+
* carries decisions only the backend can make — the candidate backend's seat tiebreak
|
|
46
|
+
* (`resolver-wof-sqlite/primary-preference.ts`) reaches an end-to-end answer solely through this stability
|
|
47
|
+
* (#1729), so equal-key stability here is load-bearing, not cosmetic.
|
|
45
48
|
*/
|
|
46
49
|
|
|
47
50
|
import type { ResolvedPlace } from "@mailwoman/core/resolver"
|
|
@@ -76,7 +79,7 @@ export const DEFAULT_COUNTRY_PRIOR_WEIGHT = 2
|
|
|
76
79
|
* policy (ROAD_TO_V9) says: within a country the geocoder ranks referentially; the blended prior's job is the
|
|
77
80
|
* cross-country question — which country's bearer a bare query meant.
|
|
78
81
|
*/
|
|
79
|
-
|
|
82
|
+
const SAME_COUNTRY_IMPORTANCE_TIE_BAND = 0.02
|
|
80
83
|
|
|
81
84
|
/**
|
|
82
85
|
* The fields a ranking key reads. Structural rather than `ResolvedPlace` so the backend's own `PlaceCandidate` (a
|
|
@@ -97,7 +100,7 @@ function rankWithinTier<T extends Rankable>(candidates: readonly T[], compare: (
|
|
|
97
100
|
|
|
98
101
|
for (const c of candidates) {
|
|
99
102
|
// `exactMatch` is TRI-STATE: true / false / undefined (a backend path that stamps no flag —
|
|
100
|
-
// e.g.
|
|
103
|
+
// e.g. WOFSQLitePlaceLookup's postcode-area neighbours from #fetchLocalitiesByID). Only a
|
|
101
104
|
// stated TRUE earns the exact tier; undefined must not outrank a real fuzzy name match
|
|
102
105
|
// (the 2026-08-10 de.native_locality incident: 75 Saxon towns lost to nameless neighbours).
|
|
103
106
|
;(c.exactMatch === true ? exact : rest).push(c)
|
|
@@ -223,10 +226,10 @@ function orderMeasuredByImportance<T extends Rankable>(rows: readonly T[]): T[]
|
|
|
223
226
|
* fewer than two candidates are scored.
|
|
224
227
|
*
|
|
225
228
|
* The producer is the candidate build's `importance` column (#28) — the BLENDED prior (the concordance's
|
|
226
|
-
* encyclopedia-derived channel
|
|
227
|
-
* `candidate-schema.ts` → `CandidateTable.importance` for why the strict channel is deliberately
|
|
228
|
-
* On an artifact predating the column, `importance` is `undefined` on every candidate the
|
|
229
|
-
* abstention below keeps the ranking byte-stable.
|
|
229
|
+
* encyclopedia-derived channel clamped around a population-derived base — `place-importance-schema.ts`'s
|
|
230
|
+
* `blendImportance`; see `candidate-schema.ts` → `CandidateTable.importance` for why the strict channel is deliberately
|
|
231
|
+
* NOT what lands there). On an artifact predating the column, `importance` is `undefined` on every candidate the
|
|
232
|
+
* backend produces and the abstention below keeps the ranking byte-stable.
|
|
230
233
|
*
|
|
231
234
|
* Flip decisions, ratified 2026-08-11: bare `Moscow`→Москва, `Manchester`→GB, `Fulda`→DE, `Cambridge`→GB are ACCEPTED
|
|
232
235
|
* behavior — all cross-country contests, where this prior answers the question population cannot. Bare `Springfield`
|
|
@@ -276,3 +279,89 @@ export function rankByCountryPrior<T extends Rankable>(
|
|
|
276
279
|
|
|
277
280
|
return rankWithinTier(candidates, (a, b) => key(b) - key(a))
|
|
278
281
|
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Capital status of a resolved candidate, answered by the caller's reference (#1880 —
|
|
285
|
+
* `@mailwoman/resolver-wof-sqlite/capitals` is the shipped implementation): 2 for a national capital, 1 for an admin-1
|
|
286
|
+
* seat, 0 for neither. The resolver stays backend-agnostic — it never loads the reference, it only consumes the
|
|
287
|
+
* verdict.
|
|
288
|
+
*/
|
|
289
|
+
export type CapitalLevelFn = (place: Pick<ResolvedPlace, "name" | "country" | "lat" | "lon">) => number
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The level a candidate must hold to be PROMOTED: national capitals only. Admin-1 seats stay un-promoted, and that
|
|
293
|
+
* scope is forced by decided rows, not caution — a seat margin of even 1 log10 unit flips bare `Springfield` to
|
|
294
|
+
* Springfield, Illinois against the ratified 2026-08-11 referential decision
|
|
295
|
+
* ({@link SAME_COUNTRY_IMPORTANCE_TIE_BAND}), and sends bare `Hamilton` to the Waikato seat (Hamilton NZ, 2.8x smaller
|
|
296
|
+
* than Hamilton, Ontario), both measured on the shipped `candidate.db`. The reference still records seats; no consumer
|
|
297
|
+
* promotes on them.
|
|
298
|
+
*/
|
|
299
|
+
const PROMOTABLE_CAPITAL_LEVEL = 2
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Margin a national capital may give away, in log10-population units: a namesake must be more than 10^2 = 100x more
|
|
303
|
+
* populous to hold the lead. Bare "San José" reaches the Costa Rican capital (342,188) over San Jose, California
|
|
304
|
+
* (969,655 — 2.8x); bare "Hamilton" stays on Hamilton, Ontario (519,949) over Bermuda's 902-person capital (576x).
|
|
305
|
+
*/
|
|
306
|
+
export const NATIONAL_CAPITAL_MARGIN_LOG10 = 2
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Bounded capital promotion (#1880) — the third soft key for the bare-toponym class, applied AFTER
|
|
310
|
+
* {@link rankByImportance} because the fame prior is what decides this class and a capital signal parked below the
|
|
311
|
+
* deciding stage measurably never reaches an answer (the iteration-1 board run: the candidate-order bonus reordered
|
|
312
|
+
* `findPlace` on every target row and moved 0 of the 12).
|
|
313
|
+
*
|
|
314
|
+
* The rule: within the exact tier, take the first NATIONAL-capital row and walk it upward past each row it is within
|
|
315
|
+
* {@link NATIONAL_CAPITAL_MARGIN_LOG10} of, stopping at the first row over the margin or at another national capital.
|
|
316
|
+
* Same three house rules as the other keys — tier-safe (never crosses the exact/partial boundary; the cost is real and
|
|
317
|
+
* accepted: a capital that only PARTIALLY matched the query — Saint George's, Grenada for the abbreviated "St.
|
|
318
|
+
* George's" — stays behind every exact match, because crossing that line is how iteration 1 answered bare "Djibouti"
|
|
319
|
+
* with the capital city instead of the country), positive evidence only (no capital in the list → identity, and the
|
|
320
|
+
* margin reads the same `prominence ?? score` size every other key does), and stable (non-promoted rows keep their
|
|
321
|
+
* order).
|
|
322
|
+
*
|
|
323
|
+
* The margin deliberately re-uses log10-population units rather than the importance scale: the ratified importance flip
|
|
324
|
+
* decisions (Whitby/Windsor/Springfield) sit on gaps of 0.004–0.02, while the capital contests this key exists for sit
|
|
325
|
+
* on gaps of 0.21–0.24 — an importance-scale bonus big enough to win them would dwarf every ratified decision on the
|
|
326
|
+
* same scale.
|
|
327
|
+
*/
|
|
328
|
+
export function promoteCapitals<T extends Rankable & Pick<ResolvedPlace, "name" | "country" | "lat" | "lon">>(
|
|
329
|
+
candidates: readonly T[],
|
|
330
|
+
level: CapitalLevelFn | undefined
|
|
331
|
+
): T[] {
|
|
332
|
+
if (!level || candidates.length < 2) return [...candidates]
|
|
333
|
+
|
|
334
|
+
const exact: T[] = []
|
|
335
|
+
const rest: T[] = []
|
|
336
|
+
|
|
337
|
+
for (const c of candidates) {
|
|
338
|
+
// Tri-state exactMatch, same reading as rankWithinTier: only a stated TRUE earns the tier.
|
|
339
|
+
;(c.exactMatch === true ? exact : rest).push(c)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const tier = exact.length ? exact : rest
|
|
343
|
+
const best = tier.findIndex((c) => level(c) >= PROMOTABLE_CAPITAL_LEVEL)
|
|
344
|
+
|
|
345
|
+
if (best <= 0) return [...candidates]
|
|
346
|
+
|
|
347
|
+
let target = best
|
|
348
|
+
|
|
349
|
+
while (target > 0) {
|
|
350
|
+
const above = tier[target - 1]!
|
|
351
|
+
|
|
352
|
+
if (level(above) >= PROMOTABLE_CAPITAL_LEVEL) break
|
|
353
|
+
|
|
354
|
+
if (size(tier[best]!) + NATIONAL_CAPITAL_MARGIN_LOG10 < size(above)) break
|
|
355
|
+
|
|
356
|
+
target--
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (target === best) return [...candidates]
|
|
360
|
+
|
|
361
|
+
const reordered = [...tier]
|
|
362
|
+
const [capital] = reordered.splice(best, 1)
|
|
363
|
+
|
|
364
|
+
reordered.splice(target, 0, capital!)
|
|
365
|
+
|
|
366
|
+
return exact.length ? [...reordered, ...rest] : reordered
|
|
367
|
+
}
|