@mailwoman/resolver 10.7.0 → 11.0.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 +1 -5
- package/lib/admin/coherence-passes.ts +2 -2
- package/lib/admin/winner.ts +24 -4
- package/lib/bare-toponym-race.ts +1 -1
- package/lib/decorate-node.ts +19 -3
- package/lib/index.ts +1 -0
- package/lib/postcode/country-coherence.ts +4 -4
- package/lib/postcode/shape-coherence.ts +40 -51
- package/lib/remote-resolver.ts +1 -2
- package/lib/resolve/country-code.ts +102 -0
- package/lib/resolve/landmark.ts +227 -0
- package/lib/resolve/passes.ts +16 -60
- package/lib/resolve/region-city.ts +71 -0
- package/lib/resolve.ts +96 -67
- package/lib/span-rescore.ts +34 -3
- package/lib/street/tier.ts +117 -8
- package/out/admin/coherence-passes.js +2 -2
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/winner.d.ts +10 -2
- package/out/admin/winner.d.ts.map +1 -1
- package/out/admin/winner.js +18 -2
- package/out/admin/winner.js.map +1 -1
- package/out/bare-toponym-race.js +1 -1
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +15 -3
- package/out/decorate-node.js.map +1 -1
- package/out/index.d.ts +1 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +1 -1
- package/out/index.js.map +1 -1
- package/out/postcode/country-coherence.d.ts.map +1 -1
- package/out/postcode/country-coherence.js +4 -4
- package/out/postcode/country-coherence.js.map +1 -1
- package/out/postcode/shape-coherence.d.ts +12 -16
- package/out/postcode/shape-coherence.d.ts.map +1 -1
- package/out/postcode/shape-coherence.js +31 -40
- package/out/postcode/shape-coherence.js.map +1 -1
- package/out/remote-resolver.d.ts +1 -2
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +0 -1
- package/out/remote-resolver.js.map +1 -1
- package/out/resolve/country-code.d.ts +49 -0
- package/out/resolve/country-code.d.ts.map +1 -0
- package/out/resolve/country-code.js +88 -0
- package/out/resolve/country-code.js.map +1 -0
- package/out/resolve/landmark.d.ts +26 -0
- package/out/resolve/landmark.d.ts.map +1 -0
- package/out/resolve/landmark.js +181 -0
- package/out/resolve/landmark.js.map +1 -0
- package/out/resolve/passes.d.ts +8 -30
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +8 -28
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve/region-city.d.ts +29 -0
- package/out/resolve/region-city.d.ts.map +1 -0
- package/out/resolve/region-city.js +58 -0
- package/out/resolve/region-city.js.map +1 -0
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +74 -60
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts +5 -0
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +26 -3
- package/out/span-rescore.js.map +1 -1
- package/out/street/tier.d.ts +13 -2
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +94 -7
- package/out/street/tier.js.map +1 -1
- package/package.json +10 -7
- package/lib/backend-capabilities.ts +0 -89
- package/out/backend-capabilities.d.ts +0 -36
- package/out/backend-capabilities.d.ts.map +0 -1
- package/out/backend-capabilities.js +0 -77
- package/out/backend-capabilities.js.map +0 -1
package/lib/resolve.ts
CHANGED
|
@@ -10,18 +10,17 @@
|
|
|
10
10
|
import type { ComponentTag } from "@mailwoman/codex/component"
|
|
11
11
|
import { matchCountry, matchSubdivision } from "@mailwoman/codex/country"
|
|
12
12
|
import { DEFAULT_PLACETYPE_MAP, isPlacetypeFallback } from "@mailwoman/codex/placetype-map"
|
|
13
|
-
import { collectNodes, type AddressNode, type AddressTree
|
|
13
|
+
import { collectNodes, isShapeExcludedPostcode, type AddressNode, type AddressTree } from "@mailwoman/core/decoder"
|
|
14
14
|
import {
|
|
15
|
-
type BackendCapabilityGap,
|
|
16
15
|
type DefaultCountry,
|
|
17
16
|
type ResolveNodeTrace,
|
|
18
17
|
type ResolvedPlace,
|
|
19
18
|
type ResolveOpts,
|
|
20
19
|
type Resolver,
|
|
21
20
|
type ResolverBackend,
|
|
22
|
-
countriesFromPostcodeFormat,
|
|
23
21
|
resolveSwitches,
|
|
24
22
|
} from "@mailwoman/core/resolver"
|
|
23
|
+
import { countriesFromPostcodeFormat } from "@mailwoman/postcode/postcode-format"
|
|
25
24
|
|
|
26
25
|
import {
|
|
27
26
|
applyAdminCoherence,
|
|
@@ -31,7 +30,6 @@ import {
|
|
|
31
30
|
} from "#admin/coherence-passes"
|
|
32
31
|
import { adminContainmentVerdict, firstRegionQualifier, partitionByContainment } from "#admin/containment"
|
|
33
32
|
import { resolveCompoundMunicipality } from "#admin/jp-municipality"
|
|
34
|
-
import { describeCapabilityGaps, reportCapabilityGaps } from "#backend-capabilities"
|
|
35
33
|
import {
|
|
36
34
|
BARE_REGION_DOMINANCE_LOG10,
|
|
37
35
|
bareCountryCandidate,
|
|
@@ -47,7 +45,9 @@ import {
|
|
|
47
45
|
stampPostcodeCountryScope,
|
|
48
46
|
} from "#postcode/country-coherence"
|
|
49
47
|
import { type CoordinateOptionalPlace, postcodePrefixResolvedPlace, probePostcodePrefix } from "#postcode/prefix"
|
|
50
|
-
import { applyPostcodeShapeCoherence
|
|
48
|
+
import { applyPostcodeShapeCoherence } from "#postcode/shape-coherence"
|
|
49
|
+
import { applyCountryCode, readRegionCodeAsCountry } from "#resolve/country-code"
|
|
50
|
+
import { applyLandmark } from "#resolve/landmark"
|
|
51
51
|
import {
|
|
52
52
|
applyPostcodeConsistency,
|
|
53
53
|
applyRooftopConsistency,
|
|
@@ -56,10 +56,10 @@ import {
|
|
|
56
56
|
DIAGNOSTIC_BANDS,
|
|
57
57
|
firstPostcodeValue,
|
|
58
58
|
NOOP_TRACE_RECORDER,
|
|
59
|
-
pickCompletion,
|
|
60
59
|
type ResolutionState,
|
|
61
60
|
type ResolvedOpts,
|
|
62
61
|
} from "#resolve/passes"
|
|
62
|
+
import { applyLocalityAsRegion, applyRegionCity } from "#resolve/region-city"
|
|
63
63
|
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "#street/tier"
|
|
64
64
|
import {
|
|
65
65
|
type CapitalLevelFn,
|
|
@@ -122,16 +122,10 @@ class WOFResolver implements Resolver {
|
|
|
122
122
|
* Coverage metadata surfaced from the backend artifact.
|
|
123
123
|
*/
|
|
124
124
|
readonly artifactCoverage: Resolver["artifactCoverage"]
|
|
125
|
-
/**
|
|
126
|
-
* Optional backend capabilities that the implementation lacks.
|
|
127
|
-
*/
|
|
128
|
-
readonly capabilityGaps: readonly BackendCapabilityGap[]
|
|
129
125
|
|
|
130
126
|
constructor(backend: ResolverBackend) {
|
|
131
127
|
this.#backend = backend
|
|
132
128
|
this.artifactCoverage = backend.artifactCoverage ?? null
|
|
133
|
-
this.capabilityGaps = describeCapabilityGaps(backend)
|
|
134
|
-
reportCapabilityGaps(this.capabilityGaps)
|
|
135
129
|
}
|
|
136
130
|
|
|
137
131
|
/**
|
|
@@ -139,9 +133,12 @@ class WOFResolver implements Resolver {
|
|
|
139
133
|
*/
|
|
140
134
|
findPlace: Resolver["findPlace"] = (query) => this.#backend.findPlace(query)
|
|
141
135
|
|
|
142
|
-
async resolveTree(
|
|
136
|
+
async resolveTree(parsed: AddressTree, callerOpts: ResolveOpts = {}): Promise<AddressTree> {
|
|
137
|
+
const tree = readRegionCodeAsCountry(parsed)
|
|
138
|
+
|
|
143
139
|
// Every on/off switch resolves against the one defaults table, so the reads below are plain truthiness.
|
|
144
140
|
const opts: ResolvedOpts = { ...callerOpts, ...resolveSwitches(callerOpts) }
|
|
141
|
+
|
|
145
142
|
// Optional early postcode-shape pass.
|
|
146
143
|
// Runs sync and can only narrow candidates.
|
|
147
144
|
const shapeVerdict = opts.postcodeShapeCoherence ? applyPostcodeShapeCoherence(tree.roots) : null
|
|
@@ -175,16 +172,12 @@ class WOFResolver implements Resolver {
|
|
|
175
172
|
localeCountryPriorWeight: opts.localeCountryPriorWeight ?? DEFAULT_COUNTRY_PRIOR_WEIGHT,
|
|
176
173
|
capitalLevel: opts.capitalLevel,
|
|
177
174
|
hardCountry: opts.hardCountry,
|
|
178
|
-
// Dual-role hierarchy completion (default on, backend-dependent).
|
|
179
|
-
hierarchyCompletion: opts.hierarchyCompletion,
|
|
180
175
|
includeAncestors: opts.includeAncestors,
|
|
181
176
|
// Optional admin containment rerank uses a pre-scanned region qualifier.
|
|
182
177
|
adminContainmentRerank: opts.adminContainmentRerank,
|
|
178
|
+
dependentLocalityContainment: opts.dependentLocalityContainment,
|
|
183
179
|
regionQualifier: firstRegionQualifier(tree.roots),
|
|
184
180
|
...(opts.traceSink ? { traceSink: opts.traceSink } : {}),
|
|
185
|
-
localityNodePresent: false,
|
|
186
|
-
resolvedRegion: null,
|
|
187
|
-
resolvedRegionNode: null,
|
|
188
181
|
}
|
|
189
182
|
|
|
190
183
|
// Optional pre-walk postcode-country coherence pass.
|
|
@@ -240,11 +233,6 @@ class WOFResolver implements Resolver {
|
|
|
240
233
|
}
|
|
241
234
|
}
|
|
242
235
|
|
|
243
|
-
// If enabled, add a dropped dual-role locality as a region interpretation.
|
|
244
|
-
if (state.hierarchyCompletion && state.resolvedRegion && state.resolvedRegionNode && !state.localityNodePresent) {
|
|
245
|
-
this.#completeRegionRole(state.resolvedRegion, state.resolvedRegionNode)
|
|
246
|
-
}
|
|
247
|
-
|
|
248
236
|
// Admin coherence passes (default on).
|
|
249
237
|
if (opts.adminCoherence) {
|
|
250
238
|
// Clear contradictory parent-fallback picks before coherence passes.
|
|
@@ -254,6 +242,17 @@ class WOFResolver implements Resolver {
|
|
|
254
242
|
await applyExplicitCountryCoherence(newRoots, this.#backend)
|
|
255
243
|
// Re-resolve foreign region/locality pairs blocked by locale scope.
|
|
256
244
|
await applyRegionCountryCoherence(newRoots, this.#backend, state.defaultCountry?.country ?? null)
|
|
245
|
+
await applyRegionCity(newRoots, this.#backend)
|
|
246
|
+
|
|
247
|
+
if (isCallerScope(state.defaultCountry)) {
|
|
248
|
+
await applyLocalityAsRegion(newRoots, this.#backend, state.defaultCountry!.country)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
await applyCountryCode(newRoots, opts.writtenInput ?? tree.raw, this.#backend)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (opts.landmarks) {
|
|
255
|
+
await applyLandmark(newRoots, opts.writtenInput ?? tree.raw, opts.landmarks)
|
|
257
256
|
}
|
|
258
257
|
|
|
259
258
|
// The postcode and rooftop passes hold a locality to the same distance from their point.
|
|
@@ -264,9 +263,13 @@ class WOFResolver implements Resolver {
|
|
|
264
263
|
await applyPostcodeConsistency(newRoots, consistencyThresholdKm, opts.postcodeConsistencyMaxMoveKm)
|
|
265
264
|
}
|
|
266
265
|
|
|
266
|
+
// A register can key a borough's or neighborhood's addresses to an ancestor's name,
|
|
267
|
+
// so a miss retries the ancestors.
|
|
268
|
+
const ancestorsOf = this.#backend.ancestors?.bind(this.#backend)
|
|
269
|
+
|
|
267
270
|
// Optional exact address-point tier.
|
|
268
271
|
if (opts.addressPoints) {
|
|
269
|
-
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback)
|
|
272
|
+
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf)
|
|
270
273
|
}
|
|
271
274
|
|
|
272
275
|
// Optional interpolation tier after exact points.
|
|
@@ -280,9 +283,14 @@ class WOFResolver implements Resolver {
|
|
|
280
283
|
await applySpanRescore(newRoots, tree.raw, this.#backend, opts)
|
|
281
284
|
}
|
|
282
285
|
|
|
286
|
+
// OpenStreetMap places answer after span rescore, so a town that holds the whole name keeps it.
|
|
287
|
+
if (opts.osmPlaces) {
|
|
288
|
+
await applyLandmark(newRoots, opts.writtenInput ?? tree.raw, opts.osmPlaces)
|
|
289
|
+
}
|
|
290
|
+
|
|
283
291
|
// Retry exact address-point tier once after span-rescore recovery.
|
|
284
292
|
if (opts.addressPoints) {
|
|
285
|
-
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback)
|
|
293
|
+
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf)
|
|
286
294
|
}
|
|
287
295
|
|
|
288
296
|
// A rooftop row keyed by locality name can lie in a namesake of the selected locality.
|
|
@@ -298,28 +306,6 @@ class WOFResolver implements Resolver {
|
|
|
298
306
|
return { raw: tree.raw, roots: newRoots }
|
|
299
307
|
}
|
|
300
308
|
|
|
301
|
-
/**
|
|
302
|
-
* Add a dropped dual-role locality as a `locality` interpretation on a resolved region node.
|
|
303
|
-
*/
|
|
304
|
-
#completeRegionRole(region: CoordinateOptionalPlace, regionNode: AddressNode): void {
|
|
305
|
-
if (typeof region.id !== "number" || !this.#backend.coincidentLocalitiesFor) return
|
|
306
|
-
const loc = pickCompletion(this.#backend.coincidentLocalitiesFor(region.id))
|
|
307
|
-
|
|
308
|
-
if (!loc) return
|
|
309
|
-
|
|
310
|
-
const interpretation: Interpretation = {
|
|
311
|
-
tag: "locality",
|
|
312
|
-
placeID: `wof:${loc.id}`,
|
|
313
|
-
sourceID: `${loc.placetype}:${loc.id}`,
|
|
314
|
-
lat: loc.lat,
|
|
315
|
-
lon: loc.lon,
|
|
316
|
-
confidence: 0,
|
|
317
|
-
metadata: { relationship_type: loc.relationshipType, resolver_completed: true, resolver_name: loc.name },
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
regionNode.interpretations = [...(regionNode.interpretations ?? []), interpretation]
|
|
321
|
-
}
|
|
322
|
-
|
|
323
309
|
async #walk(
|
|
324
310
|
node: AddressNode,
|
|
325
311
|
parentResolved: CoordinateOptionalPlace | null,
|
|
@@ -331,11 +317,6 @@ class WOFResolver implements Resolver {
|
|
|
331
317
|
|
|
332
318
|
const placetype = state.placetypeMap[node.tag as ComponentTag]
|
|
333
319
|
|
|
334
|
-
// Track whether a locality node exists for hierarchy completion.
|
|
335
|
-
if (placetype === "locality") {
|
|
336
|
-
state.localityNodePresent = true
|
|
337
|
-
}
|
|
338
|
-
|
|
339
320
|
let resolved: CoordinateOptionalPlace | null = null
|
|
340
321
|
|
|
341
322
|
// Skip lookup for shape-excluded postcode spans.
|
|
@@ -380,12 +361,6 @@ class WOFResolver implements Resolver {
|
|
|
380
361
|
if (state.includeAncestors && this.#backend.ancestors) {
|
|
381
362
|
decorated.metadata = { ...decorated.metadata, ancestors: this.#backend.ancestors(picked.top.id) }
|
|
382
363
|
}
|
|
383
|
-
|
|
384
|
-
// Capture first resolved region for hierarchy completion.
|
|
385
|
-
if (placetype === "region" && state.resolvedRegion === null) {
|
|
386
|
-
state.resolvedRegion = picked.top
|
|
387
|
-
state.resolvedRegionNode = decorated
|
|
388
|
-
}
|
|
389
364
|
}
|
|
390
365
|
}
|
|
391
366
|
|
|
@@ -412,7 +387,12 @@ class WOFResolver implements Resolver {
|
|
|
412
387
|
if (band === probed) continue
|
|
413
388
|
|
|
414
389
|
try {
|
|
415
|
-
const hits = await this.#backend.findPlace({
|
|
390
|
+
const hits = await this.#backend.findPlace({
|
|
391
|
+
...query,
|
|
392
|
+
placetype: band,
|
|
393
|
+
parentID: undefined,
|
|
394
|
+
parentQualifierID: undefined,
|
|
395
|
+
})
|
|
416
396
|
|
|
417
397
|
if (hits.length) {
|
|
418
398
|
found.push({ placetype: band, n: hits.length })
|
|
@@ -449,10 +429,7 @@ class WOFResolver implements Resolver {
|
|
|
449
429
|
query.bias = state.bias
|
|
450
430
|
}
|
|
451
431
|
|
|
452
|
-
|
|
453
|
-
if (parentResolved && typeof parentResolved.id === "number") {
|
|
454
|
-
query.parentID = parentResolved.id
|
|
455
|
-
}
|
|
432
|
+
const parentContainment = applyParentScope(query, node, parentResolved, state)
|
|
456
433
|
|
|
457
434
|
// Country scope precedence: parent -> default -> node hint -> hard country.
|
|
458
435
|
const countryHint = node.metadata?.["country_hint"]
|
|
@@ -463,7 +440,7 @@ class WOFResolver implements Resolver {
|
|
|
463
440
|
const country =
|
|
464
441
|
parentResolved?.country ??
|
|
465
442
|
defaultCountryForLookup ??
|
|
466
|
-
(typeof countryHint === "string" ? countryHint :
|
|
443
|
+
(typeof countryHint === "string" ? countryHint : null) ??
|
|
467
444
|
state.hardCountry
|
|
468
445
|
|
|
469
446
|
if (country) {
|
|
@@ -659,7 +636,9 @@ class WOFResolver implements Resolver {
|
|
|
659
636
|
|
|
660
637
|
// Final containment partition.
|
|
661
638
|
// It reads the address's own qualifier and preserves the exact-match tier.
|
|
662
|
-
|
|
639
|
+
const qualifierPartition = containmentEligible || parentContainment
|
|
640
|
+
|
|
641
|
+
if (qualifierPartition) {
|
|
663
642
|
ranked = partitionByContainment(
|
|
664
643
|
ranked,
|
|
665
644
|
(c) => c.containedByQualifier === true,
|
|
@@ -716,7 +695,7 @@ class WOFResolver implements Resolver {
|
|
|
716
695
|
|
|
717
696
|
// Trace receipts for mechanism-level observability.
|
|
718
697
|
const pickMetadata = {
|
|
719
|
-
...(
|
|
698
|
+
...containmentStamps(qualifierPartition, parentContainment, ranked, top),
|
|
720
699
|
...(top.variantAliasExempted === true ? { variant_alias_exemption: true } : {}),
|
|
721
700
|
// Mark picks admitted outside resolved parent scope.
|
|
722
701
|
...((parentResolved && typeof parentResolved.id === "number" && query.parentID == null) ||
|
|
@@ -733,6 +712,56 @@ class WOFResolver implements Resolver {
|
|
|
733
712
|
}
|
|
734
713
|
}
|
|
735
714
|
|
|
715
|
+
/**
|
|
716
|
+
* Scopes a lookup to its resolved parent and reports whether the parent is a containment qualifier.
|
|
717
|
+
*
|
|
718
|
+
* A parent is a `parentID` region scope.
|
|
719
|
+
* A `dependent_locality` node under the `dependentLocalityContainment` switch passes
|
|
720
|
+
* its parent as `parentQualifierID` instead (#2804).
|
|
721
|
+
*
|
|
722
|
+
* The candidate table's region scope holds region ids, so a locality parent never satisfies it.
|
|
723
|
+
*
|
|
724
|
+
* The containment partition admits the sub-locality rows the locality band excludes.
|
|
725
|
+
*/
|
|
726
|
+
function applyParentScope(
|
|
727
|
+
query: Parameters<ResolverBackend["findPlace"]>[0],
|
|
728
|
+
node: AddressNode,
|
|
729
|
+
parentResolved: CoordinateOptionalPlace | null,
|
|
730
|
+
state: ResolutionState
|
|
731
|
+
): boolean {
|
|
732
|
+
if (!parentResolved || typeof parentResolved.id !== "number") return false
|
|
733
|
+
|
|
734
|
+
if (state.dependentLocalityContainment && node.tag === "dependent_locality") {
|
|
735
|
+
query.parentQualifierID = parentResolved.id
|
|
736
|
+
|
|
737
|
+
return true
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
query.parentID = parentResolved.id
|
|
741
|
+
|
|
742
|
+
return false
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* The pick metadata a containment partition writes: the tri-state verdict when a qualifier was
|
|
747
|
+
* read, and `contained_by_parent` when a dependent locality's pick sits inside its parent.
|
|
748
|
+
*
|
|
749
|
+
* The geocode result's admin ladder reads the second stamp to let the pick answer ahead of the locality.
|
|
750
|
+
*/
|
|
751
|
+
function containmentStamps(
|
|
752
|
+
qualifierPartition: boolean,
|
|
753
|
+
parentContainment: boolean,
|
|
754
|
+
ranked: readonly ResolvedPlace[],
|
|
755
|
+
top: ResolvedPlace
|
|
756
|
+
): Record<string, unknown> {
|
|
757
|
+
if (!qualifierPartition) return {}
|
|
758
|
+
|
|
759
|
+
return {
|
|
760
|
+
admin_containment: adminContainmentVerdict(ranked),
|
|
761
|
+
...(parentContainment && top.containedByQualifier === true ? { contained_by_parent: true } : {}),
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
736
765
|
/**
|
|
737
766
|
* Promote capitals and stamp `capital_promotion` when the leader changes.
|
|
738
767
|
* Metadata only.
|
|
@@ -740,7 +769,7 @@ class WOFResolver implements Resolver {
|
|
|
740
769
|
function promoteCapitalsWithReceipt<
|
|
741
770
|
T extends Pick<ResolvedPlace, "score" | "name" | "country" | "lat" | "lon"> &
|
|
742
771
|
Partial<Pick<ResolvedPlace, "exactMatch" | "importance" | "prominence">>,
|
|
743
|
-
>(ranked: readonly T[], level: CapitalLevelFn, node: { metadata?: Record<string, unknown> }): T[] {
|
|
772
|
+
>(ranked: readonly T[], level: CapitalLevelFn, node: { metadata?: Record<string, unknown> | null }): T[] {
|
|
744
773
|
const promoted = promoteCapitals(ranked, level)
|
|
745
774
|
|
|
746
775
|
if (promoted[0] !== ranked[0]) {
|
package/lib/span-rescore.ts
CHANGED
|
@@ -11,9 +11,10 @@
|
|
|
11
11
|
import { firstNodeWhere, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
12
12
|
import type { ResolvedPlace, ResolverBackend, WeakResolutionReading } from "@mailwoman/core/resolver"
|
|
13
13
|
import { isRegionAbbreviationToken } from "@mailwoman/query-shape/region-abbreviations"
|
|
14
|
-
import {
|
|
14
|
+
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
15
15
|
|
|
16
16
|
import { partitionByContainment } from "#admin/containment"
|
|
17
|
+
import { bareSublocalityCandidate } from "#bare-toponym-race"
|
|
17
18
|
import { foldName } from "#fold-name"
|
|
18
19
|
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, rankByCountryPrior, rankByImportance } from "#toponym-prior"
|
|
19
20
|
|
|
@@ -58,6 +59,11 @@ export interface SpanRescoreOptions {
|
|
|
58
59
|
* @defaultValue `false`
|
|
59
60
|
*/
|
|
60
61
|
postalCompoundRecovery?: boolean
|
|
62
|
+
/**
|
|
63
|
+
* Returns whether a word is a POI category, so a venue's last word is not re-read as a locality.
|
|
64
|
+
* A caller without the taxonomy leaves every span eligible.
|
|
65
|
+
*/
|
|
66
|
+
isVenueCategoryWord?: (word: string) => boolean
|
|
61
67
|
/**
|
|
62
68
|
* When the span covers the whole unqualified input, treat {@link country} as an additive prior
|
|
63
69
|
* instead of a hard gazetteer filter; `false` restores the hard filter byte-for-byte.
|
|
@@ -420,6 +426,9 @@ export async function findRescoreCandidate(
|
|
|
420
426
|
const qualified = !!postcode || hasAdminQualifier(roots)
|
|
421
427
|
const wholeInput = toks.length ? { start: toks[0]!.start, end: toks.at(-1)!.end } : null
|
|
422
428
|
const softCountryEligible = softCountry && !!country && !qualified && !!wholeInput
|
|
429
|
+
const venue = firstNodeWhere(roots, (n) => n.tag === "venue")
|
|
430
|
+
|
|
431
|
+
const venueCoversInput = !!venue && !!wholeInput && venue.start <= wholeInput.start && wholeInput.end <= venue.end
|
|
423
432
|
|
|
424
433
|
for (const sp of spans) {
|
|
425
434
|
const key = foldName(sp.text)
|
|
@@ -429,6 +438,11 @@ export async function findRescoreCandidate(
|
|
|
429
438
|
// also needs an unqualified tree and a caller country.
|
|
430
439
|
const wholeSpan = !!wholeInput && sp.start === wholeInput.start && sp.end === wholeInput.end
|
|
431
440
|
|
|
441
|
+
// A category word ending a multi-word venue states what the place is, as `Airport`
|
|
442
|
+
// in `Narita Airport`, so a locality of that name is a coincidence of spelling.
|
|
443
|
+
// Another last word can state the location, as `London` in `Tower of London`.
|
|
444
|
+
if (venue && sp.end === venue.end && sp.start > venue.start && opts.isVenueCategoryWord?.(sp.text)) continue
|
|
445
|
+
|
|
432
446
|
// A sub-span that drops a word of the name is a corruption rather than a
|
|
433
447
|
// recovery. see `remainderIsContext`.
|
|
434
448
|
if (opts.spanRescoreRequireContextRemainder && !wholeSpan && !remainderIsContext(sp)) continue
|
|
@@ -476,7 +490,7 @@ export async function findRescoreCandidate(
|
|
|
476
490
|
: ranked
|
|
477
491
|
|
|
478
492
|
const withinThreshold = (p: ResolvedPlace): boolean =>
|
|
479
|
-
!anchor || thresholdKm <= 0 ||
|
|
493
|
+
!anchor || thresholdKm <= 0 || haversine(anchor.lat, anchor.lon, p.lat, p.lon) <= thresholdKm
|
|
480
494
|
|
|
481
495
|
for (const h of exact) {
|
|
482
496
|
if (!withinThreshold(h)) continue
|
|
@@ -492,6 +506,23 @@ export async function findRescoreCandidate(
|
|
|
492
506
|
alternatives: exact.filter((a) => a !== h && withinThreshold(a)),
|
|
493
507
|
}
|
|
494
508
|
}
|
|
509
|
+
|
|
510
|
+
// A whole-input venue can be a sub-locality row, as `Golden Gate Park` is a neighborhood,
|
|
511
|
+
// and that row states the whole name where a shorter span would state part of it.
|
|
512
|
+
if (wholeSpan && venueCoversInput) {
|
|
513
|
+
const sublocality = await bareSublocalityCandidate(backend, sp.text, country)
|
|
514
|
+
|
|
515
|
+
if (sublocality && withinThreshold(sublocality)) {
|
|
516
|
+
return {
|
|
517
|
+
text: sp.text,
|
|
518
|
+
start: sp.start,
|
|
519
|
+
end: sp.end,
|
|
520
|
+
place: sublocality,
|
|
521
|
+
postcodeVerified: anchor !== null,
|
|
522
|
+
alternatives: [],
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
495
526
|
}
|
|
496
527
|
|
|
497
528
|
// Joint country recovery: when the scoped pass finds no match and a postcode
|
|
@@ -521,7 +552,7 @@ export async function findRescoreCandidate(
|
|
|
521
552
|
|
|
522
553
|
const verified = pcHits.find((p) => p.lat !== 0 || p.lon !== 0)
|
|
523
554
|
|
|
524
|
-
if (verified &&
|
|
555
|
+
if (verified && haversine(verified.lat, verified.lon, h.lat, h.lon) <= thresholdKm) {
|
|
525
556
|
// Admission uses per-candidate postcode verification.
|
|
526
557
|
// The postcode already picked the country, so this pass has no remaining
|
|
527
558
|
// country ambiguity to report.
|
package/lib/street/tier.ts
CHANGED
|
@@ -7,7 +7,11 @@
|
|
|
7
7
|
import { isFrenchStreetWord } from "@mailwoman/codex/fr"
|
|
8
8
|
import { isStreetDirectionalToken } from "@mailwoman/codex/us"
|
|
9
9
|
import { collectNodes, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
10
|
-
import type { AddressPointLookup, InterpolationLookup, StreetCentroidLookup } from "@mailwoman/core/resolver"
|
|
10
|
+
import type { AddressPointLookup, Ancestor, InterpolationLookup, StreetCentroidLookup } from "@mailwoman/core/resolver"
|
|
11
|
+
import { WOFPrefix } from "@mailwoman/gazetteer/whosonfirst/placetypes/definition"
|
|
12
|
+
import { SETTLEMENT_PLACETYPES } from "@mailwoman/gazetteer/whosonfirst/placetypes/specificity"
|
|
13
|
+
import type { LatLonBounds } from "@mailwoman/spatial"
|
|
14
|
+
import { isLatLonBounds, latLonBoundsContain } from "@mailwoman/spatial/bbox"
|
|
11
15
|
import { TextSpliterator } from "spliterator"
|
|
12
16
|
|
|
13
17
|
import { foldName } from "#fold-name"
|
|
@@ -30,6 +34,43 @@ const isDirectionalUnit = (value: string): boolean => isStreetDirectionalToken(v
|
|
|
30
34
|
|
|
31
35
|
const LOCALITY_BBOX_RADIUS_DEG = 0.25
|
|
32
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Reads the resolved place's own extent from `resolver_bbox`, which `decorateNode`
|
|
39
|
+
* writes only for an extent with area.
|
|
40
|
+
*/
|
|
41
|
+
function resolvedExtent(node: AddressNode | null | undefined): LatLonBounds | null {
|
|
42
|
+
const box = node?.metadata?.["resolver_bbox"]
|
|
43
|
+
|
|
44
|
+
return isLatLonBounds(box) ? box : null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns the names of the resolved locality's settlement ancestors, nearest first.
|
|
49
|
+
*
|
|
50
|
+
* A register keyed to a city stores an address in one of its boroughs or neighborhoods
|
|
51
|
+
* under the city's name, so a register could store any of these as the address's locality.
|
|
52
|
+
*/
|
|
53
|
+
function ancestorLocalityNames(
|
|
54
|
+
node: AddressNode | null,
|
|
55
|
+
ancestorsOf: ((id: number | string) => Ancestor[]) | undefined
|
|
56
|
+
): string[] {
|
|
57
|
+
if (!node || !ancestorsOf || typeof node.placeID !== "string" || !node.placeID.startsWith(WOFPrefix)) return []
|
|
58
|
+
|
|
59
|
+
const seen = new Set([foldName(node.value)])
|
|
60
|
+
const names: string[] = []
|
|
61
|
+
|
|
62
|
+
for (const ancestor of ancestorsOf(node.placeID.slice(WOFPrefix.length))) {
|
|
63
|
+
const key = foldName(ancestor.name)
|
|
64
|
+
|
|
65
|
+
if (!SETTLEMENT_PLACETYPES.has(ancestor.placetype) || !key || seen.has(key)) continue
|
|
66
|
+
|
|
67
|
+
seen.add(key)
|
|
68
|
+
names.push(ancestor.name.trim())
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return names
|
|
72
|
+
}
|
|
73
|
+
|
|
33
74
|
/**
|
|
34
75
|
* Returns every `(street, house_number)` pair in the tree, ordered by how close the
|
|
35
76
|
* two spans sit, with a number before the street winning a tie.
|
|
@@ -79,8 +120,19 @@ function firstOfTag(roots: readonly AddressNode[], tag: AddressNode["tag"]): Add
|
|
|
79
120
|
* and number pair the lookup finds.
|
|
80
121
|
*
|
|
81
122
|
* With `bboxFallback`, the lookup is also bounded to a box around the resolved locality's coordinate.
|
|
123
|
+
*
|
|
124
|
+
* With `ancestorsOf`, a miss is retried with the names of the resolved locality's WOF ancestors,
|
|
125
|
+
* nearest first, for a register that keys a borough or neighborhood to its city.
|
|
126
|
+
* An ancestor-keyed row answers only when it lies inside the resolved locality's own bounding box,
|
|
127
|
+
* because the ancestor key also matches the same street and number in the ancestor's other parts.
|
|
128
|
+
* A locality without a bounding box gets no retry.
|
|
82
129
|
*/
|
|
83
|
-
export function applyAddressPoint(
|
|
130
|
+
export function applyAddressPoint(
|
|
131
|
+
roots: AddressNode[],
|
|
132
|
+
lookup: AddressPointLookup,
|
|
133
|
+
bboxFallback?: boolean,
|
|
134
|
+
ancestorsOf?: (id: number | string) => Ancestor[]
|
|
135
|
+
): void {
|
|
84
136
|
const pairs = streetNumberPairs(roots)
|
|
85
137
|
const directionalUnit = [...walkNodes(roots)].find((n) => n.tag === "unit" && isDirectionalUnit(n.value))
|
|
86
138
|
const localityNode = firstOfTag(roots, "locality")
|
|
@@ -96,13 +148,31 @@ export function applyAddressPoint(roots: AddressNode[], lookup: AddressPointLook
|
|
|
96
148
|
|
|
97
149
|
let bbox: { minLat: number; maxLat: number; minLon: number; maxLon: number } | null = null
|
|
98
150
|
|
|
151
|
+
// A row stating no locality or postcode matches on its street and number,
|
|
152
|
+
// and the box keeps it in the stated place.
|
|
153
|
+
// The box is the town's own extent where the gazetteer holds one, because `W 1st St`
|
|
154
|
+
// repeats in the next town inside any fixed radius.
|
|
155
|
+
// A stated postcode adds the radius to it, because a rural ZIP Code's mailing town covers
|
|
156
|
+
// addresses far outside that town's own extent, and a row stating another postcode stays refused.
|
|
99
157
|
if (bboxFallback && localityNode?.lat != null && localityNode.lon != null) {
|
|
100
|
-
|
|
158
|
+
const extent = resolvedExtent(localityNode)
|
|
159
|
+
|
|
160
|
+
const radius = {
|
|
101
161
|
minLat: localityNode.lat - LOCALITY_BBOX_RADIUS_DEG,
|
|
102
162
|
maxLat: localityNode.lat + LOCALITY_BBOX_RADIUS_DEG,
|
|
103
163
|
minLon: localityNode.lon - LOCALITY_BBOX_RADIUS_DEG,
|
|
104
164
|
maxLon: localityNode.lon + LOCALITY_BBOX_RADIUS_DEG,
|
|
105
165
|
}
|
|
166
|
+
|
|
167
|
+
bbox =
|
|
168
|
+
extent && postcode
|
|
169
|
+
? {
|
|
170
|
+
minLat: Math.min(extent.minLat, radius.minLat),
|
|
171
|
+
maxLat: Math.max(extent.maxLat, radius.maxLat),
|
|
172
|
+
minLon: Math.min(extent.minLon, radius.minLon),
|
|
173
|
+
maxLon: Math.max(extent.maxLon, radius.maxLon),
|
|
174
|
+
}
|
|
175
|
+
: (extent ?? radius)
|
|
106
176
|
}
|
|
107
177
|
|
|
108
178
|
let street: AddressNode | null = null
|
|
@@ -113,10 +183,10 @@ export function applyAddressPoint(roots: AddressNode[], lookup: AddressPointLook
|
|
|
113
183
|
hit = lookup.find({
|
|
114
184
|
street: assembleStreetValue(pair.street, directionalUnit),
|
|
115
185
|
number: pair.houseNumber.value,
|
|
116
|
-
postcode,
|
|
117
|
-
locality,
|
|
118
|
-
region,
|
|
119
|
-
subregion,
|
|
186
|
+
postcode: postcode ?? null,
|
|
187
|
+
locality: locality ?? null,
|
|
188
|
+
region: region ?? null,
|
|
189
|
+
subregion: subregion ?? null,
|
|
120
190
|
bbox,
|
|
121
191
|
})
|
|
122
192
|
|
|
@@ -128,6 +198,34 @@ export function applyAddressPoint(roots: AddressNode[], lookup: AddressPointLook
|
|
|
128
198
|
}
|
|
129
199
|
}
|
|
130
200
|
|
|
201
|
+
let ancestorLocality: string | null = null
|
|
202
|
+
const extent = resolvedExtent(localityNode)
|
|
203
|
+
|
|
204
|
+
if (!hit && locality && extent) {
|
|
205
|
+
retry: for (const name of ancestorLocalityNames(localityNode, ancestorsOf)) {
|
|
206
|
+
for (const pair of pairs) {
|
|
207
|
+
const candidate = lookup.find({
|
|
208
|
+
street: assembleStreetValue(pair.street, directionalUnit),
|
|
209
|
+
number: pair.houseNumber.value,
|
|
210
|
+
postcode: postcode ?? null,
|
|
211
|
+
locality: name,
|
|
212
|
+
region: region ?? null,
|
|
213
|
+
subregion: subregion ?? null,
|
|
214
|
+
bbox,
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
if (candidate && latLonBoundsContain(extent, candidate.lat, candidate.lon)) {
|
|
218
|
+
hit = candidate
|
|
219
|
+
street = pair.street
|
|
220
|
+
houseNumber = pair.houseNumber
|
|
221
|
+
ancestorLocality = name
|
|
222
|
+
|
|
223
|
+
break retry
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
131
229
|
if (!hit || !street || !houseNumber) return
|
|
132
230
|
|
|
133
231
|
houseNumber.metadata = { ...houseNumber.metadata, resolution_tier: "address_point" }
|
|
@@ -143,6 +241,7 @@ export function applyAddressPoint(roots: AddressNode[], lookup: AddressPointLook
|
|
|
143
241
|
postcode: hit.postcode || null,
|
|
144
242
|
},
|
|
145
243
|
resolution_tier: "address_point",
|
|
244
|
+
...(ancestorLocality ? { address_point_ancestor_locality: ancestorLocality } : {}),
|
|
146
245
|
}
|
|
147
246
|
}
|
|
148
247
|
|
|
@@ -152,6 +251,11 @@ export function applyAddressPoint(roots: AddressNode[], lookup: AddressPointLook
|
|
|
152
251
|
*
|
|
153
252
|
* It writes `interpolated_point` rather than `address_point`, so an estimate
|
|
154
253
|
* never overwrites a real situs point.
|
|
254
|
+
*
|
|
255
|
+
* A query without a postcode is scoped only by its locality, so its estimate must lie inside the resolved
|
|
256
|
+
* locality's bounding box, and an estimate outside it is refused as a namesake street elsewhere.
|
|
257
|
+
* A postcode-scoped estimate is not checked, because a mailing city's postcode
|
|
258
|
+
* can reach past the WOF place's extent.
|
|
155
259
|
*/
|
|
156
260
|
export function applyInterpolation(
|
|
157
261
|
roots: AddressNode[],
|
|
@@ -164,13 +268,14 @@ export function applyInterpolation(
|
|
|
164
268
|
|
|
165
269
|
const resolvedLocality = [...walkNodes(roots)].find((n) => n.tag === "locality" && n.lat != null && n.lon != null)
|
|
166
270
|
|
|
167
|
-
const localityCoord = resolvedLocality ? { lat: resolvedLocality.lat!, lon: resolvedLocality.lon! } :
|
|
271
|
+
const localityCoord = resolvedLocality ? { lat: resolvedLocality.lat!, lon: resolvedLocality.lon! } : null
|
|
168
272
|
|
|
169
273
|
if (!pairs.length) return
|
|
170
274
|
|
|
171
275
|
if (pairs.some((pair) => pair.street.metadata?.["resolution_tier"] === "address_point")) return
|
|
172
276
|
|
|
173
277
|
const near = postcode ? undefined : localityCoord
|
|
278
|
+
const extent = postcode ? null : resolvedExtent(resolvedLocality)
|
|
174
279
|
|
|
175
280
|
let street: AddressNode | null = null
|
|
176
281
|
let houseNumber: AddressNode | null = null
|
|
@@ -184,6 +289,10 @@ export function applyInterpolation(
|
|
|
184
289
|
...(near ? { near } : {}),
|
|
185
290
|
})
|
|
186
291
|
|
|
292
|
+
if (hit && extent && !latLonBoundsContain(extent, hit.lat, hit.lon)) {
|
|
293
|
+
hit = null
|
|
294
|
+
}
|
|
295
|
+
|
|
187
296
|
if (hit) {
|
|
188
297
|
street = pair.street
|
|
189
298
|
houseNumber = pair.houseNumber
|
|
@@ -127,8 +127,8 @@ function revertResolverDecoration(node) {
|
|
|
127
127
|
const meta = { ...node.metadata };
|
|
128
128
|
const priorSource = meta["classifier_source"];
|
|
129
129
|
const priorSourceID = meta["classifier_source_id"];
|
|
130
|
-
node.source = typeof priorSource === "string" ? priorSource :
|
|
131
|
-
node.sourceID = typeof priorSourceID === "string" ? priorSourceID :
|
|
130
|
+
node.source = typeof priorSource === "string" ? priorSource : null;
|
|
131
|
+
node.sourceID = typeof priorSourceID === "string" ? priorSourceID : null;
|
|
132
132
|
for (const key of [
|
|
133
133
|
"classifier_source",
|
|
134
134
|
"classifier_source_id",
|