@mailwoman/resolver 11.2.0 → 11.4.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/lib/admin/coherence-passes.ts +1 -1
- package/lib/admin/county-designator.ts +1 -1
- package/lib/admin/former-division.ts +116 -0
- package/lib/bare-toponym-race.ts +1 -1
- package/lib/decorate-node.ts +102 -2
- package/lib/postcode/prefix.ts +7 -16
- package/lib/remote-resolver.ts +1 -1
- package/lib/resolve/parent-place.ts +109 -0
- package/lib/resolve/passes.ts +62 -11
- package/lib/resolve/sibling-locality.ts +150 -0
- package/lib/resolve.ts +255 -42
- package/lib/scope-retrieval.ts +91 -0
- package/lib/span-rescore.ts +65 -14
- package/lib/street/tier.ts +5 -8
- package/lib/toponym-prior.ts +57 -6
- package/out/admin/coherence-passes.js +1 -1
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/county-designator.js +1 -1
- package/out/admin/former-division.d.ts +47 -0
- package/out/admin/former-division.d.ts.map +1 -0
- package/out/admin/former-division.js +74 -0
- package/out/admin/former-division.js.map +1 -0
- package/out/bare-toponym-race.d.ts +1 -1
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/decorate-node.d.ts +22 -3
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +84 -1
- package/out/decorate-node.js.map +1 -1
- package/out/postcode/prefix.d.ts +4 -2
- package/out/postcode/prefix.d.ts.map +1 -1
- package/out/postcode/prefix.js +7 -13
- package/out/postcode/prefix.js.map +1 -1
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +1 -1
- package/out/remote-resolver.js.map +1 -1
- package/out/resolve/parent-place.d.ts +34 -0
- package/out/resolve/parent-place.d.ts.map +1 -0
- package/out/resolve/parent-place.js +76 -0
- package/out/resolve/parent-place.js.map +1 -0
- package/out/resolve/passes.d.ts +39 -8
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +17 -6
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve/sibling-locality.d.ts +34 -0
- package/out/resolve/sibling-locality.d.ts.map +1 -0
- package/out/resolve/sibling-locality.js +95 -0
- package/out/resolve/sibling-locality.js.map +1 -0
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +177 -39
- package/out/resolve.js.map +1 -1
- package/out/scope-retrieval.d.ts +44 -0
- package/out/scope-retrieval.d.ts.map +1 -0
- package/out/scope-retrieval.js +52 -0
- package/out/scope-retrieval.js.map +1 -0
- package/out/span-rescore.d.ts +15 -1
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +36 -9
- package/out/span-rescore.js.map +1 -1
- package/out/street/tier.d.ts +5 -1
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +5 -7
- package/out/street/tier.js.map +1 -1
- package/out/toponym-prior.d.ts +15 -0
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +37 -6
- package/out/toponym-prior.js.map +1 -1
- package/package.json +9 -9
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sibling-locality containment: when a tree holds two resolved `locality` spans
|
|
6
|
+
* and the gazetteer places a candidate of one inside the other's place, the inner
|
|
7
|
+
* span resolves inside the outer place as a dependent locality.
|
|
8
|
+
*
|
|
9
|
+
* `Kensington, London` can decode as two localities.
|
|
10
|
+
* Resolved apart, `Kensington` takes the largest namesake, Kensington, California, beside London.
|
|
11
|
+
*
|
|
12
|
+
* The gazetteer's containment decides which span is inner, so `London, Kensington` reads the same.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { collectNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
16
|
+
import type { ResolvedPlace, ResolveNodeTrace, ResolverBackend } from "@mailwoman/core/resolver"
|
|
17
|
+
|
|
18
|
+
import { adminContainmentVerdict, partitionByContainment } from "#admin/containment"
|
|
19
|
+
import { decorateNode } from "#decorate-node"
|
|
20
|
+
import { createNodeTraceRecorder } from "#resolve/passes"
|
|
21
|
+
import { rankByImportance } from "#toponym-prior"
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The metadata stamp naming the outer place a sibling span resolved inside.
|
|
25
|
+
*/
|
|
26
|
+
export const SIBLING_LOCALITY_CONTAINMENT = "sibling_locality_containment"
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The most locality spans the pass pairs, because each ordered pair costs one lookup.
|
|
30
|
+
*/
|
|
31
|
+
const MAX_SIBLING_LOCALITIES = 3
|
|
32
|
+
|
|
33
|
+
interface SiblingLocalityOptions {
|
|
34
|
+
/**
|
|
35
|
+
* The resolver placetype a `locality` span looks up.
|
|
36
|
+
*/
|
|
37
|
+
placetype: string
|
|
38
|
+
limit: number
|
|
39
|
+
traceSink: ((record: ResolveNodeTrace) => void) | null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* One ordered pair's reading: the inner span's candidates ranked with the outer place as their qualifier.
|
|
44
|
+
*/
|
|
45
|
+
interface ContainmentReading {
|
|
46
|
+
inner: AddressNode
|
|
47
|
+
outer: AddressNode
|
|
48
|
+
outerID: number
|
|
49
|
+
ranked: ResolvedPlace[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function wofID(node: AddressNode): number | null {
|
|
53
|
+
const id = node.placeID?.startsWith("wof:") ? Number(node.placeID.slice(4)) : Number.NaN
|
|
54
|
+
|
|
55
|
+
return Number.isInteger(id) && id > 0 ? id : null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The inner span's candidates looked up with the outer place as the containment qualifier,
|
|
60
|
+
* contained first, or null when none lies inside the outer place.
|
|
61
|
+
*/
|
|
62
|
+
async function readContainment(
|
|
63
|
+
inner: AddressNode,
|
|
64
|
+
outer: AddressNode,
|
|
65
|
+
outerID: number,
|
|
66
|
+
backend: ResolverBackend,
|
|
67
|
+
opts: SiblingLocalityOptions
|
|
68
|
+
): Promise<ContainmentReading | null> {
|
|
69
|
+
const country = outer.metadata?.["resolver_country"]
|
|
70
|
+
|
|
71
|
+
const hits = await backend.findPlace({
|
|
72
|
+
text: inner.value,
|
|
73
|
+
placetype: opts.placetype,
|
|
74
|
+
parentQualifierID: outerID,
|
|
75
|
+
...(typeof country === "string" && country ? { country } : {}),
|
|
76
|
+
limit: opts.limit,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const inside = (c: ResolvedPlace) => c.containedByQualifier === true && Number(c.id) !== outerID
|
|
80
|
+
|
|
81
|
+
if (!hits.some(inside)) return null
|
|
82
|
+
|
|
83
|
+
const ranked = partitionByContainment(rankByImportance(hits), inside, (c) => c.exactMatch === true)
|
|
84
|
+
|
|
85
|
+
return { inner, outer, outerID, ranked }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Resolves each inner `locality` span inside its sibling's place and retags it `dependent_locality`.
|
|
90
|
+
*
|
|
91
|
+
* A pair is read in both orders.
|
|
92
|
+
* A pair whose spans each hold a candidate inside the other, or that resolved
|
|
93
|
+
* to one place, is left as it was.
|
|
94
|
+
*/
|
|
95
|
+
export async function applySiblingLocalityContainment(
|
|
96
|
+
roots: readonly AddressNode[],
|
|
97
|
+
backend: ResolverBackend,
|
|
98
|
+
opts: SiblingLocalityOptions
|
|
99
|
+
): Promise<void> {
|
|
100
|
+
const localities = collectNodes(roots, (n) => n.tag === "locality" && wofID(n) != null).slice(
|
|
101
|
+
0,
|
|
102
|
+
MAX_SIBLING_LOCALITIES
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
if (localities.length < 2) return
|
|
106
|
+
|
|
107
|
+
const retagged = new Set<AddressNode>()
|
|
108
|
+
|
|
109
|
+
for (const [i, a] of localities.entries()) {
|
|
110
|
+
for (const b of localities.slice(i + 1)) {
|
|
111
|
+
if (retagged.has(a) || retagged.has(b)) continue
|
|
112
|
+
|
|
113
|
+
const aID = wofID(a)!
|
|
114
|
+
const bID = wofID(b)!
|
|
115
|
+
|
|
116
|
+
if (aID === bID) continue
|
|
117
|
+
|
|
118
|
+
const aInB = await readContainment(a, b, bID, backend, opts)
|
|
119
|
+
const bInA = await readContainment(b, a, aID, backend, opts)
|
|
120
|
+
|
|
121
|
+
// The gazetteer must order the pair one way only.
|
|
122
|
+
const reading = aInB && !bInA ? aInB : bInA && !aInB ? bInA : null
|
|
123
|
+
|
|
124
|
+
if (!reading) continue
|
|
125
|
+
|
|
126
|
+
const [top, ...alternatives] = reading.ranked
|
|
127
|
+
|
|
128
|
+
decorateNode(reading.inner, top!, alternatives)
|
|
129
|
+
reading.inner.tag = "dependent_locality"
|
|
130
|
+
|
|
131
|
+
reading.inner.metadata = {
|
|
132
|
+
...reading.inner.metadata,
|
|
133
|
+
admin_containment: adminContainmentVerdict(reading.ranked),
|
|
134
|
+
contained_by_parent: true,
|
|
135
|
+
[SIBLING_LOCALITY_CONTAINMENT]: `wof:${reading.outerID}`,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
retagged.add(reading.inner)
|
|
139
|
+
|
|
140
|
+
if (opts.traceSink) {
|
|
141
|
+
const rec = createNodeTraceRecorder(opts.traceSink)
|
|
142
|
+
|
|
143
|
+
rec.bind(reading.inner, opts.placetype, { parentQualifierID: reading.outerID, limit: opts.limit }, opts.limit)
|
|
144
|
+
rec.check(SIBLING_LOCALITY_CONTAINMENT)
|
|
145
|
+
rec.stage("sibling_containment", reading.ranked)
|
|
146
|
+
rec.emit({ id: top!.id, name: top!.name, source: "sibling_locality_containment" })
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
package/lib/resolve.ts
CHANGED
|
@@ -9,10 +9,12 @@
|
|
|
9
9
|
|
|
10
10
|
import type { ComponentTag } from "@mailwoman/codex/component"
|
|
11
11
|
import { matchCountry, matchSubdivision } from "@mailwoman/codex/country"
|
|
12
|
-
import { DEFAULT_PLACETYPE_MAP, isPlacetypeFallback } from "@mailwoman/codex/placetype-map"
|
|
12
|
+
import { DEFAULT_PLACETYPE_MAP, isPlacetypeFallback } from "@mailwoman/codex/labels/placetype-map"
|
|
13
13
|
import { collectNodes, isShapeExcludedPostcode, type AddressNode, type AddressTree } from "@mailwoman/core/decoder"
|
|
14
14
|
import {
|
|
15
15
|
type DefaultCountry,
|
|
16
|
+
hardCountryOf,
|
|
17
|
+
ScopeStrength,
|
|
16
18
|
type ResolveNodeTrace,
|
|
17
19
|
type ResolvedPlace,
|
|
18
20
|
type ResolveOpts,
|
|
@@ -20,6 +22,7 @@ import {
|
|
|
20
22
|
type ResolverBackend,
|
|
21
23
|
resolveSwitches,
|
|
22
24
|
} from "@mailwoman/core/resolver"
|
|
25
|
+
import { isPresent } from "@mailwoman/core/values/objects"
|
|
23
26
|
import { countriesFromPostcodeFormat } from "@mailwoman/postcode/postcode-format"
|
|
24
27
|
|
|
25
28
|
import {
|
|
@@ -30,6 +33,7 @@ import {
|
|
|
30
33
|
} from "#admin/coherence-passes"
|
|
31
34
|
import { adminContainmentVerdict, firstRegionQualifier, partitionByContainment } from "#admin/containment"
|
|
32
35
|
import { COUNTY_DESIGNATOR_FALLBACK, countyFromDesignatedLocality } from "#admin/county-designator"
|
|
36
|
+
import { acceptFormerDivisions, FORMER_DIVISION_TAGS } from "#admin/former-division"
|
|
33
37
|
import { resolveCompoundMunicipality } from "#admin/jp-municipality"
|
|
34
38
|
import {
|
|
35
39
|
BARE_REGION_DOMINANCE_LOG10,
|
|
@@ -49,6 +53,12 @@ import { type CoordinateOptionalPlace, postcodePrefixResolvedPlace, probePostcod
|
|
|
49
53
|
import { applyPostcodeShapeCoherence } from "#postcode/shape-coherence"
|
|
50
54
|
import { applyCountryCode, readRegionCodeAsCountry } from "#resolve/country-code"
|
|
51
55
|
import { applyLandmark } from "#resolve/landmark"
|
|
56
|
+
import {
|
|
57
|
+
agreesWithParent,
|
|
58
|
+
applyCallerParentQualifier,
|
|
59
|
+
applyParentPlaceCountry,
|
|
60
|
+
readParentPlace,
|
|
61
|
+
} from "#resolve/parent-place"
|
|
52
62
|
import {
|
|
53
63
|
applyPostcodeConsistency,
|
|
54
64
|
applyRooftopConsistency,
|
|
@@ -68,7 +78,14 @@ import {
|
|
|
68
78
|
applyStreetAsTown,
|
|
69
79
|
applyVenueAsTown,
|
|
70
80
|
} from "#resolve/region-city"
|
|
81
|
+
import { applySiblingLocalityContainment } from "#resolve/sibling-locality"
|
|
71
82
|
import { applyWholeInputPlace } from "#resolve/whole-input"
|
|
83
|
+
import {
|
|
84
|
+
placesOutsideCountryScope,
|
|
85
|
+
promoteSoftCountry,
|
|
86
|
+
retrieveWithSoftCountry,
|
|
87
|
+
softScopeRetrieves,
|
|
88
|
+
} from "#scope-retrieval"
|
|
72
89
|
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "#street/tier"
|
|
73
90
|
import {
|
|
74
91
|
type CapitalLevelFn,
|
|
@@ -168,7 +185,12 @@ class WOFResolver implements Resolver {
|
|
|
168
185
|
minWinningScore: opts.minWinningScore ?? 0,
|
|
169
186
|
minScoreRefusals: 0,
|
|
170
187
|
candidatesPerLookup: opts.candidatesPerLookup ?? 5,
|
|
171
|
-
|
|
188
|
+
// One scope value: a hard scope filters, and a soft one ranks.
|
|
189
|
+
defaultCountry: hardCountryOf(opts.defaultCountry) ? opts.defaultCountry! : null,
|
|
190
|
+
softCountry: opts.defaultCountry?.strength === ScopeStrength.Soft ? opts.defaultCountry : null,
|
|
191
|
+
softCountryWeight: opts.softCountryWeight ?? DEFAULT_COUNTRY_PRIOR_WEIGHT,
|
|
192
|
+
scopeCountryRetrieval: opts.scopeCountryRetrieval,
|
|
193
|
+
parentPlace: opts.parentPlace ? readParentPlace(this.#backend, opts.parentPlace) : null,
|
|
172
194
|
bareLocalityNode: loneBareLocalityNode(tree, opts.placetypeMap ?? DEFAULT_PLACETYPE_MAP),
|
|
173
195
|
parentFallback: opts.parentFallback,
|
|
174
196
|
// Only enable unreachable diagnostics when a trace sink exists.
|
|
@@ -186,8 +208,6 @@ class WOFResolver implements Resolver {
|
|
|
186
208
|
bias: opts.bias,
|
|
187
209
|
anchorPosterior: opts.anchorPosterior,
|
|
188
210
|
anchorWeight: opts.anchorWeight ?? 2,
|
|
189
|
-
localeCountryPrior: opts.localeCountryPrior,
|
|
190
|
-
localeCountryPriorWeight: opts.localeCountryPriorWeight ?? DEFAULT_COUNTRY_PRIOR_WEIGHT,
|
|
191
211
|
capitalLevel: opts.capitalLevel,
|
|
192
212
|
hardCountry: opts.hardCountry,
|
|
193
213
|
includeAncestors: opts.includeAncestors,
|
|
@@ -195,10 +215,15 @@ class WOFResolver implements Resolver {
|
|
|
195
215
|
adminContainmentRerank: opts.adminContainmentRerank,
|
|
196
216
|
dependentLocalityContainment: opts.dependentLocalityContainment,
|
|
197
217
|
countyDesignatorFallback: opts.countyDesignatorFallback,
|
|
218
|
+
...(opts.formerDivisions && opts.formerDivisionLookup
|
|
219
|
+
? { formerDivisionLookup: opts.formerDivisionLookup, pendingFormerDivisions: new Map() }
|
|
220
|
+
: {}),
|
|
198
221
|
regionQualifier: firstRegionQualifier(tree.roots),
|
|
199
222
|
...(opts.traceSink ? { traceSink: opts.traceSink } : {}),
|
|
200
223
|
}
|
|
201
224
|
|
|
225
|
+
applyParentPlaceCountry(state)
|
|
226
|
+
|
|
202
227
|
// Optional pre-walk postcode-country coherence pass.
|
|
203
228
|
// This is the only pass allowed to override `defaultCountry`.
|
|
204
229
|
let postcodeScope: PostcodeCountryScope | null = null
|
|
@@ -241,6 +266,14 @@ class WOFResolver implements Resolver {
|
|
|
241
266
|
// A later pass that gives one of these nodes a different place removes the walk's ancestors with the old place.
|
|
242
267
|
const walkedWithAncestors = collectNodes(newRoots, (n) => n.metadata?.["ancestors"] != null)
|
|
243
268
|
|
|
269
|
+
if (opts.siblingLocalityContainment) {
|
|
270
|
+
await applySiblingLocalityContainment(newRoots, this.#backend, {
|
|
271
|
+
placetype: state.placetypeMap.locality ?? "locality",
|
|
272
|
+
limit: state.candidatesPerLookup,
|
|
273
|
+
traceSink: state.traceSink ?? null,
|
|
274
|
+
})
|
|
275
|
+
}
|
|
276
|
+
|
|
244
277
|
// Stamp metadata for postcode-country override.
|
|
245
278
|
if (postcodeScope) {
|
|
246
279
|
stampPostcodeCountryScope(newRoots, postcodeScope)
|
|
@@ -314,7 +347,14 @@ class WOFResolver implements Resolver {
|
|
|
314
347
|
// Span-rescore fallback tier (default on).
|
|
315
348
|
// Skip when a min-score refusal already occurred.
|
|
316
349
|
if (opts.spanRescore && state.minScoreRefusals === 0) {
|
|
317
|
-
await applySpanRescore(
|
|
350
|
+
await applySpanRescore(
|
|
351
|
+
newRoots,
|
|
352
|
+
tree.raw,
|
|
353
|
+
this.#backend,
|
|
354
|
+
// A hard parent's country filters the probes as a caller's default country does.
|
|
355
|
+
state.parentPlace && state.defaultCountry ? { ...opts, defaultCountry: state.defaultCountry } : opts,
|
|
356
|
+
!state.defaultCountry && softScopeRetrieves(state) ? state.softCountry!.country : null
|
|
357
|
+
)
|
|
318
358
|
}
|
|
319
359
|
|
|
320
360
|
// OpenStreetMap places answer after span rescore, so a town that holds the whole name keeps it.
|
|
@@ -337,6 +377,11 @@ class WOFResolver implements Resolver {
|
|
|
337
377
|
applyStreetCentroid(newRoots, tree.raw, opts.streetCentroids, opts.streetCountryHints ?? [])
|
|
338
378
|
}
|
|
339
379
|
|
|
380
|
+
// A value no current division answered is accepted when a former division of that name covered the settlement.
|
|
381
|
+
if (state.pendingFormerDivisions?.size && this.#backend.ancestors) {
|
|
382
|
+
acceptFormerDivisions(newRoots, state.pendingFormerDivisions, this.#backend.ancestors.bind(this.#backend))
|
|
383
|
+
}
|
|
384
|
+
|
|
340
385
|
// The walk's ancestors are read again for the place that replaced its pick.
|
|
341
386
|
if (this.#backend.ancestors) {
|
|
342
387
|
for (const node of walkedWithAncestors) {
|
|
@@ -389,7 +434,7 @@ class WOFResolver implements Resolver {
|
|
|
389
434
|
|
|
390
435
|
if (picked) {
|
|
391
436
|
resolved = picked.top
|
|
392
|
-
decorateNode(decorated, picked.top, picked.alternatives)
|
|
437
|
+
decorateNode(decorated, picked.top, picked.alternatives, picked.considered)
|
|
393
438
|
|
|
394
439
|
if (picked.metadata) {
|
|
395
440
|
decorated.metadata = { ...decorated.metadata, ...picked.metadata }
|
|
@@ -408,6 +453,14 @@ class WOFResolver implements Resolver {
|
|
|
408
453
|
if (state.includeAncestors && this.#backend.ancestors) {
|
|
409
454
|
decorated.metadata = { ...decorated.metadata, ancestors: this.#backend.ancestors(picked.top.id) }
|
|
410
455
|
}
|
|
456
|
+
} else if (state.formerDivisionLookup && FORMER_DIVISION_TAGS.has(node.tag)) {
|
|
457
|
+
// No current division answered, so the value may name a former one, checked after the walk.
|
|
458
|
+
const country = parentResolved?.country ?? state.defaultCountry?.country ?? null
|
|
459
|
+
const matches = state.formerDivisionLookup.divisionsNamed(node.value, country)
|
|
460
|
+
|
|
461
|
+
if (matches.length) {
|
|
462
|
+
state.pendingFormerDivisions!.set(decorated, matches)
|
|
463
|
+
}
|
|
411
464
|
}
|
|
412
465
|
}
|
|
413
466
|
|
|
@@ -453,6 +506,83 @@ class WOFResolver implements Resolver {
|
|
|
453
506
|
return found
|
|
454
507
|
}
|
|
455
508
|
|
|
509
|
+
/**
|
|
510
|
+
* A soft scope's own rows merged into an unscoped lookup's candidates, under scope retrieval.
|
|
511
|
+
*
|
|
512
|
+
* It returns `candidates` unchanged without a soft country, when the lookup already
|
|
513
|
+
* has a country, or when the soft scope ranks only.
|
|
514
|
+
*/
|
|
515
|
+
async #withSoftCountryRows(
|
|
516
|
+
query: Parameters<ResolverBackend["findPlace"]>[0],
|
|
517
|
+
candidates: ResolvedPlace[],
|
|
518
|
+
softCountry: string | null,
|
|
519
|
+
state: ResolutionState,
|
|
520
|
+
rec: NodeTraceRecorder
|
|
521
|
+
): Promise<ResolvedPlace[]> {
|
|
522
|
+
if (!softCountry || query.country || !softScopeRetrieves(state)) return candidates
|
|
523
|
+
|
|
524
|
+
rec.check("soft_country_retrieval")
|
|
525
|
+
const merged = await retrieveWithSoftCountry(this.#backend, query, candidates, softCountry)
|
|
526
|
+
|
|
527
|
+
rec.stage("soft_country", merged)
|
|
528
|
+
|
|
529
|
+
return merged
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* A hard caller parent's split of the ranked candidates into those its lineage agrees with
|
|
534
|
+
* and the excluded rest, each marked `outsideScope`.
|
|
535
|
+
*
|
|
536
|
+
* Without a hard parent or an ancestors lookup, every candidate is kept.
|
|
537
|
+
*/
|
|
538
|
+
#applyHardParent(
|
|
539
|
+
ranked: ResolvedPlace[],
|
|
540
|
+
qualifierIsParent: boolean,
|
|
541
|
+
state: ResolutionState,
|
|
542
|
+
rec: NodeTraceRecorder
|
|
543
|
+
): { kept: ResolvedPlace[]; excluded: ResolvedPlace[] } {
|
|
544
|
+
const parent = state.parentPlace
|
|
545
|
+
|
|
546
|
+
if (parent?.scope.strength !== ScopeStrength.Hard || !this.#backend.ancestors) return { kept: ranked, excluded: [] }
|
|
547
|
+
|
|
548
|
+
const ancestors = this.#backend.ancestors.bind(this.#backend)
|
|
549
|
+
const agrees = (c: ResolvedPlace) => agreesWithParent(c, parent, ancestors, qualifierIsParent)
|
|
550
|
+
const kept = ranked.filter(agrees)
|
|
551
|
+
|
|
552
|
+
rec.stage("parent_scope", kept)
|
|
553
|
+
|
|
554
|
+
return {
|
|
555
|
+
kept,
|
|
556
|
+
excluded: ranked.filter((c) => !agrees(c)).map((c) => ({ ...c, outsideScope: true as const })),
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* The same-name places outside a hard default country, for the node's
|
|
562
|
+
* `considered` list, under scope retrieval.
|
|
563
|
+
*
|
|
564
|
+
* The probe changes no pick, so a backend error is traced and answers no places.
|
|
565
|
+
*/
|
|
566
|
+
async #placesOutsideScope(
|
|
567
|
+
query: Parameters<ResolverBackend["findPlace"]>[0],
|
|
568
|
+
state: ResolutionState,
|
|
569
|
+
rec: NodeTraceRecorder
|
|
570
|
+
): Promise<ResolvedPlace[]> {
|
|
571
|
+
if (!state.scopeCountryRetrieval || !query.country) return []
|
|
572
|
+
|
|
573
|
+
try {
|
|
574
|
+
const outside = await placesOutsideCountryScope(this.#backend, query, query.country)
|
|
575
|
+
|
|
576
|
+
rec.check(`outside_scope: ${outside.length}`)
|
|
577
|
+
|
|
578
|
+
return outside
|
|
579
|
+
} catch (error) {
|
|
580
|
+
rec.check(`outside_scope_error: ${(error as Error).message}`)
|
|
581
|
+
|
|
582
|
+
return []
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
456
586
|
/**
|
|
457
587
|
* The county of a designated `locality` value's name in its region, when the locality
|
|
458
588
|
* lookup found no locality known to be inside that region.
|
|
@@ -546,6 +676,11 @@ class WOFResolver implements Resolver {
|
|
|
546
676
|
): Promise<{
|
|
547
677
|
top: CoordinateOptionalPlace
|
|
548
678
|
alternatives: ResolvedPlace[]
|
|
679
|
+
/**
|
|
680
|
+
* The probe winners the pick passed over.
|
|
681
|
+
* Only the geocode result's candidate list reads them.
|
|
682
|
+
*/
|
|
683
|
+
considered?: ResolvedPlace[]
|
|
549
684
|
metadata?: Record<string, unknown>
|
|
550
685
|
} | null> {
|
|
551
686
|
state.lookupsRemaining--
|
|
@@ -563,6 +698,8 @@ class WOFResolver implements Resolver {
|
|
|
563
698
|
|
|
564
699
|
const parentContainment = applyParentScope(query, node, parentResolved, state)
|
|
565
700
|
|
|
701
|
+
const callerParentQualifies = applyCallerParentQualifier(query, state)
|
|
702
|
+
|
|
566
703
|
// Country scope precedence: parent -> default -> node hint -> hard country.
|
|
567
704
|
const countryHint = node.metadata?.["country_hint"]
|
|
568
705
|
|
|
@@ -589,7 +726,9 @@ class WOFResolver implements Resolver {
|
|
|
589
726
|
state.adminContainmentRerank &&
|
|
590
727
|
placetype === "locality" &&
|
|
591
728
|
state.regionQualifier != null &&
|
|
592
|
-
!isCallerScope(state.defaultCountry)
|
|
729
|
+
!isCallerScope(state.defaultCountry) &&
|
|
730
|
+
// The backend reads one qualifier, and the caller's parent outranks the parsed region.
|
|
731
|
+
!state.parentPlace
|
|
593
732
|
|
|
594
733
|
if (containmentEligible) {
|
|
595
734
|
query.regionQualifier = state.regionQualifier!
|
|
@@ -612,16 +751,31 @@ class WOFResolver implements Resolver {
|
|
|
612
751
|
|
|
613
752
|
let candidates: ResolvedPlace[]
|
|
614
753
|
|
|
754
|
+
// The anchor posterior and both country scopes act on region and locality lookups only.
|
|
755
|
+
const anchorEligible = placetype === "region" || placetype === "locality"
|
|
756
|
+
|
|
757
|
+
// A soft scope ranks a lookup no hard country, tree parent or anchor posterior already scopes.
|
|
758
|
+
const softCountry =
|
|
759
|
+
anchorEligible && !state.defaultCountry && !state.anchorPosterior ? (state.softCountry?.country ?? null) : null
|
|
760
|
+
|
|
761
|
+
// The places a hard scope excluded, reported on `considered`.
|
|
762
|
+
let outsideScope: ResolvedPlace[] = []
|
|
763
|
+
|
|
615
764
|
// For unconstrained postalcodes, probe format-implied countries only.
|
|
616
765
|
if (placetype === "postalcode" && !query.country && state.postcodeFormatCountries?.length) {
|
|
617
766
|
rec.check("postcode_format_probe")
|
|
618
767
|
let best: ResolvedPlace | null = null
|
|
768
|
+
const countryWinners: ResolvedPlace[] = []
|
|
619
769
|
|
|
620
770
|
for (const impliedCountry of state.postcodeFormatCountries) {
|
|
621
771
|
try {
|
|
622
772
|
const hits = await this.#backend.findPlace({ ...query, country: impliedCountry })
|
|
623
773
|
const top = hits[0]
|
|
624
774
|
|
|
775
|
+
if (top) {
|
|
776
|
+
countryWinners.push(top)
|
|
777
|
+
}
|
|
778
|
+
|
|
625
779
|
// Keep the best per-country winner by score.
|
|
626
780
|
if (top && (!best || top.score > best.score)) {
|
|
627
781
|
best = top
|
|
@@ -634,7 +788,7 @@ class WOFResolver implements Resolver {
|
|
|
634
788
|
if (best) {
|
|
635
789
|
rec.emit({ id: best.id, name: best.name, source: "postcode_format_probe" })
|
|
636
790
|
|
|
637
|
-
return { top: best, alternatives: [] }
|
|
791
|
+
return { top: best, alternatives: [], considered: countryWinners.filter((c) => c !== best) }
|
|
638
792
|
}
|
|
639
793
|
|
|
640
794
|
candidates = []
|
|
@@ -656,6 +810,14 @@ class WOFResolver implements Resolver {
|
|
|
656
810
|
rec.stage("parent_fallback", candidates)
|
|
657
811
|
}
|
|
658
812
|
|
|
813
|
+
candidates = await this.#withSoftCountryRows(query, candidates, softCountry, state, rec)
|
|
814
|
+
|
|
815
|
+
// Only a lookup the default country filtered reports the same-name places outside it.
|
|
816
|
+
const filteredByDefault =
|
|
817
|
+
anchorEligible && !parentResolved && query.country != null && query.country === defaultCountryForLookup
|
|
818
|
+
|
|
819
|
+
outsideScope = filteredByDefault ? await this.#placesOutsideScope(query, state, rec) : []
|
|
820
|
+
|
|
659
821
|
// Optional diagnostics: record other bands that can resolve this value.
|
|
660
822
|
if (!candidates.length && state.diagnoseUnreachable) {
|
|
661
823
|
rec.reachable(await this.#probeOtherBands(query, placetype))
|
|
@@ -686,6 +848,10 @@ class WOFResolver implements Resolver {
|
|
|
686
848
|
const bareCountry = isBareRace ? await bareCountryCandidate(this.#backend, node.value, query.country) : null
|
|
687
849
|
const bareRegion = isBareRace ? await bareRegionCandidate(this.#backend, node.value, query.country) : null
|
|
688
850
|
|
|
851
|
+
// The race probes a pick passes over, kept for the geocode result's candidate list.
|
|
852
|
+
const raceLosers = (picked: ResolvedPlace | null): ResolvedPlace[] =>
|
|
853
|
+
[bareCountry, bareRegion].filter(isPresent).filter((probe) => probe !== picked)
|
|
854
|
+
|
|
689
855
|
if (!candidates.length) {
|
|
690
856
|
// With no locality candidates, a bare name takes an admin or sub-locality namesake.
|
|
691
857
|
const pick = isBareRace
|
|
@@ -696,7 +862,12 @@ class WOFResolver implements Resolver {
|
|
|
696
862
|
rec.check(pick.check)
|
|
697
863
|
rec.emit({ id: pick.place.id, name: pick.place.name, source: pick.source })
|
|
698
864
|
|
|
699
|
-
return {
|
|
865
|
+
return {
|
|
866
|
+
top: pick.place,
|
|
867
|
+
alternatives: [],
|
|
868
|
+
considered: [...raceLosers(pick.place), ...outsideScope],
|
|
869
|
+
metadata: pick.metadata,
|
|
870
|
+
}
|
|
700
871
|
}
|
|
701
872
|
|
|
702
873
|
rec.emit(null)
|
|
@@ -704,40 +875,12 @@ class WOFResolver implements Resolver {
|
|
|
704
875
|
return null
|
|
705
876
|
}
|
|
706
877
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
const anchorEligible = placetype === "region" || placetype === "locality"
|
|
710
|
-
let ranked = candidates
|
|
711
|
-
|
|
712
|
-
if (state.anchorPosterior && anchorEligible && candidates.length > 1) {
|
|
713
|
-
ranked = rankByAnchorPosterior(candidates, state.anchorPosterior, state.anchorWeight)
|
|
714
|
-
rec.stage("anchor", ranked)
|
|
715
|
-
ranked = rankWithinPosteriorCountry(ranked, state.capitalLevel, node)
|
|
716
|
-
rec.stage("importance", ranked)
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
// Optional locale-country soft prior adds to candidate scores while retaining every candidate.
|
|
720
|
-
// Disabled when default country or anchor posterior is present.
|
|
721
|
-
if (state.localeCountryPrior && !state.defaultCountry && !state.anchorPosterior && anchorEligible) {
|
|
722
|
-
ranked = rankByCountryPrior(ranked, state.localeCountryPrior, state.localeCountryPriorWeight)
|
|
723
|
-
rec.stage("locale_prior", ranked)
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
// Importance-first rerank (the posterior branch above runs its own, within the winning country).
|
|
727
|
-
if (!state.anchorPosterior) {
|
|
728
|
-
ranked = rankByImportance(ranked)
|
|
729
|
-
rec.stage("importance", ranked)
|
|
730
|
-
|
|
731
|
-
// Optional bounded capital promotion.
|
|
732
|
-
if (state.capitalLevel) {
|
|
733
|
-
ranked = promoteCapitalsWithReceipt(ranked, state.capitalLevel, node)
|
|
734
|
-
rec.stage("capital", ranked)
|
|
735
|
-
}
|
|
736
|
-
}
|
|
878
|
+
const softPromotion = rankByPriors(candidates, anchorEligible, softCountry, node, state, rec)
|
|
879
|
+
let ranked = softPromotion.ranked
|
|
737
880
|
|
|
738
881
|
// Final containment partition.
|
|
739
882
|
// It reads the address's own qualifier and preserves the exact-match tier.
|
|
740
|
-
const qualifierPartition = containmentEligible || parentContainment
|
|
883
|
+
const qualifierPartition = containmentEligible || parentContainment || callerParentQualifies
|
|
741
884
|
|
|
742
885
|
if (qualifierPartition) {
|
|
743
886
|
ranked = partitionByContainment(
|
|
@@ -761,6 +904,18 @@ class WOFResolver implements Resolver {
|
|
|
761
904
|
rec.stage("exact_type", ranked)
|
|
762
905
|
}
|
|
763
906
|
|
|
907
|
+
const parentScoped = this.#applyHardParent(ranked, callerParentQualifies, state, rec)
|
|
908
|
+
|
|
909
|
+
ranked = parentScoped.kept
|
|
910
|
+
outsideScope = [...parentScoped.excluded, ...outsideScope]
|
|
911
|
+
|
|
912
|
+
if (!ranked.length) {
|
|
913
|
+
rec.check("parent_scope_excluded_all")
|
|
914
|
+
rec.emit(null)
|
|
915
|
+
|
|
916
|
+
return null
|
|
917
|
+
}
|
|
918
|
+
|
|
764
919
|
const top = ranked[0]!
|
|
765
920
|
|
|
766
921
|
if (top.score < state.minWinningScore) {
|
|
@@ -776,14 +931,24 @@ class WOFResolver implements Resolver {
|
|
|
776
931
|
rec.check("bare_country_repick")
|
|
777
932
|
rec.emit({ id: bareCountry.id, name: bareCountry.name, source: "bare_country" })
|
|
778
933
|
|
|
779
|
-
return {
|
|
934
|
+
return {
|
|
935
|
+
top: bareCountry,
|
|
936
|
+
alternatives: ranked,
|
|
937
|
+
considered: [...raceLosers(bareCountry), ...outsideScope],
|
|
938
|
+
metadata: { bare_country_repick: true },
|
|
939
|
+
}
|
|
780
940
|
}
|
|
781
941
|
|
|
782
942
|
if (bareRegion && logPopulation(bareRegion) >= logPopulation(top) + BARE_REGION_DOMINANCE_LOG10) {
|
|
783
943
|
rec.check("bare_region_repick")
|
|
784
944
|
rec.emit({ id: bareRegion.id, name: bareRegion.name, source: "bare_region" })
|
|
785
945
|
|
|
786
|
-
return {
|
|
946
|
+
return {
|
|
947
|
+
top: bareRegion,
|
|
948
|
+
alternatives: ranked,
|
|
949
|
+
considered: [...raceLosers(bareRegion), ...outsideScope],
|
|
950
|
+
metadata: { bare_region_repick: true },
|
|
951
|
+
}
|
|
787
952
|
}
|
|
788
953
|
|
|
789
954
|
// Stamp fallback quality when a macro-type winner stands in.
|
|
@@ -798,6 +963,8 @@ class WOFResolver implements Resolver {
|
|
|
798
963
|
const pickMetadata = {
|
|
799
964
|
...containmentStamps(qualifierPartition, parentContainment, ranked, top),
|
|
800
965
|
...(top.variantAliasExempted === true ? { variant_alias_exemption: true } : {}),
|
|
966
|
+
// The soft scope's promotion moved this place ahead of a larger namesake.
|
|
967
|
+
...(top === softPromotion.promoted ? { soft_country_promotion: softCountry } : {}),
|
|
801
968
|
// Mark picks admitted outside resolved parent scope.
|
|
802
969
|
...((parentResolved && typeof parentResolved.id === "number" && query.parentID == null) ||
|
|
803
970
|
top.regionScopeMiss === true
|
|
@@ -808,6 +975,7 @@ class WOFResolver implements Resolver {
|
|
|
808
975
|
return {
|
|
809
976
|
top,
|
|
810
977
|
alternatives: ranked.slice(1),
|
|
978
|
+
considered: [...raceLosers(null), ...outsideScope],
|
|
811
979
|
...(Object.keys(pickMetadata).length ? { metadata: pickMetadata } : {}),
|
|
812
980
|
}
|
|
813
981
|
}
|
|
@@ -863,6 +1031,51 @@ function containmentStamps(
|
|
|
863
1031
|
}
|
|
864
1032
|
}
|
|
865
1033
|
|
|
1034
|
+
/**
|
|
1035
|
+
* Ranks a lookup's candidates by its priors in order: the postcode anchor posterior,
|
|
1036
|
+
* the ranking-only soft scope, importance with capital promotion, then the retrieval soft scope.
|
|
1037
|
+
*
|
|
1038
|
+
* `promoted` is the place the retrieval soft scope moved to the lead, or null.
|
|
1039
|
+
*/
|
|
1040
|
+
function rankByPriors(
|
|
1041
|
+
candidates: ResolvedPlace[],
|
|
1042
|
+
anchorEligible: boolean,
|
|
1043
|
+
softCountry: string | null,
|
|
1044
|
+
node: AddressNode,
|
|
1045
|
+
state: ResolutionState,
|
|
1046
|
+
rec: NodeTraceRecorder
|
|
1047
|
+
): { ranked: ResolvedPlace[]; promoted: ResolvedPlace | null } {
|
|
1048
|
+
let ranked = candidates
|
|
1049
|
+
|
|
1050
|
+
// Optional anchor-based rerank using postcode country posterior, kept tier-safe by exact-match first.
|
|
1051
|
+
if (state.anchorPosterior && anchorEligible && candidates.length > 1) {
|
|
1052
|
+
ranked = rankByAnchorPosterior(candidates, state.anchorPosterior, state.anchorWeight)
|
|
1053
|
+
rec.stage("anchor", ranked)
|
|
1054
|
+
ranked = rankWithinPosteriorCountry(ranked, state.capitalLevel, node)
|
|
1055
|
+
rec.stage("importance", ranked)
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
// Without scope retrieval, the soft scope adds to candidate scores ahead of the importance rerank.
|
|
1059
|
+
if (softCountry && !softScopeRetrieves(state)) {
|
|
1060
|
+
ranked = rankByCountryPrior(ranked, softCountry, state.softCountryWeight)
|
|
1061
|
+
rec.stage("locale_prior", ranked)
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
// Importance-first rerank (the posterior branch above runs its own, within the winning country).
|
|
1065
|
+
if (!state.anchorPosterior) {
|
|
1066
|
+
ranked = rankByImportance(ranked)
|
|
1067
|
+
rec.stage("importance", ranked)
|
|
1068
|
+
|
|
1069
|
+
// Optional bounded capital promotion.
|
|
1070
|
+
if (state.capitalLevel) {
|
|
1071
|
+
ranked = promoteCapitalsWithReceipt(ranked, state.capitalLevel, node)
|
|
1072
|
+
rec.stage("capital", ranked)
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
return promoteSoftCountry(ranked, softCountry, state, rec)
|
|
1077
|
+
}
|
|
1078
|
+
|
|
866
1079
|
/**
|
|
867
1080
|
* Sorts candidates by the postcode country posterior, exact matches first.
|
|
868
1081
|
*
|