@mailwoman/resolver 10.1.0 → 10.2.1
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 +10 -10
- package/lib/admin/containment.ts +4 -4
- package/lib/admin/jp-municipality.ts +1 -1
- package/lib/admin/winner.ts +7 -7
- package/lib/backend-capabilities.ts +3 -3
- package/lib/decorate-node.ts +8 -8
- package/lib/plausibility.ts +6 -6
- package/lib/postcode/country-coherence.ts +30 -14
- package/lib/postcode/prefix.ts +4 -2
- package/lib/postcode/shape-coherence.ts +4 -4
- package/lib/remote-resolver.ts +6 -4
- package/lib/rerank.ts +9 -6
- package/lib/resolve/passes.ts +47 -33
- package/lib/resolve.ts +56 -38
- package/lib/span-rescore.ts +27 -26
- package/lib/street/tier.ts +20 -21
- package/lib/toponym-prior.ts +1 -1
- package/out/admin/coherence-passes.d.ts +1 -1
- package/out/admin/coherence-passes.d.ts.map +1 -1
- package/out/admin/coherence-passes.js +6 -6
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/containment.d.ts +2 -2
- package/out/admin/containment.d.ts.map +1 -1
- package/out/admin/containment.js +2 -2
- package/out/admin/containment.js.map +1 -1
- package/out/admin/jp-municipality.d.ts +1 -1
- package/out/admin/jp-municipality.d.ts.map +1 -1
- package/out/admin/winner.d.ts +2 -2
- package/out/admin/winner.d.ts.map +1 -1
- package/out/admin/winner.js +5 -5
- package/out/admin/winner.js.map +1 -1
- package/out/backend-capabilities.d.ts +1 -1
- package/out/backend-capabilities.d.ts.map +1 -1
- package/out/backend-capabilities.js +3 -2
- package/out/backend-capabilities.js.map +1 -1
- package/out/decorate-node.js +8 -8
- package/out/decorate-node.js.map +1 -1
- package/out/plausibility.d.ts +3 -3
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +3 -3
- package/out/plausibility.js.map +1 -1
- package/out/postcode/country-coherence.d.ts +7 -7
- package/out/postcode/country-coherence.d.ts.map +1 -1
- package/out/postcode/country-coherence.js +23 -7
- package/out/postcode/country-coherence.js.map +1 -1
- package/out/postcode/prefix.d.ts +1 -1
- package/out/postcode/prefix.d.ts.map +1 -1
- package/out/postcode/prefix.js +3 -1
- package/out/postcode/prefix.js.map +1 -1
- package/out/postcode/shape-coherence.d.ts +2 -2
- package/out/postcode/shape-coherence.d.ts.map +1 -1
- package/out/postcode/shape-coherence.js +2 -2
- package/out/postcode/shape-coherence.js.map +1 -1
- package/out/remote-resolver.d.ts +5 -3
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +3 -1
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +6 -3
- package/out/rerank.d.ts.map +1 -1
- package/out/rerank.js +3 -3
- package/out/rerank.js.map +1 -1
- package/out/resolve/passes.d.ts +14 -11
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +27 -21
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +51 -37
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts +12 -8
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +10 -10
- package/out/span-rescore.js.map +1 -1
- package/out/street/tier.d.ts +1 -1
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +17 -17
- package/out/street/tier.js.map +1 -1
- package/out/toponym-prior.d.ts +1 -1
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js.map +1 -1
- package/package.json +7 -7
|
@@ -13,10 +13,10 @@ import { decorateNode, isResolvedWithCoord } from "#decorate-node"
|
|
|
13
13
|
|
|
14
14
|
const REGION_LINEAGE_PLACETYPES: ReadonlySet<string> = new Set(PLACETYPE_FILTER_GROUPS["region"] ?? ["region"])
|
|
15
15
|
|
|
16
|
-
function placeIDValue(node: AddressNode): string |
|
|
16
|
+
function placeIDValue(node: AddressNode): string | null {
|
|
17
17
|
const match = /^[a-z]+:(.+)$/u.exec(node.placeID ?? "")
|
|
18
18
|
|
|
19
|
-
return match?.[1]
|
|
19
|
+
return match?.[1] ?? null
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
@@ -28,7 +28,7 @@ function placeIDValue(node: AddressNode): string | undefined {
|
|
|
28
28
|
*/
|
|
29
29
|
export function applyParentFallbackContradiction(roots: readonly AddressNode[]): void {
|
|
30
30
|
const visit = (node: AddressNode, regionAncestor: AddressNode | null): void => {
|
|
31
|
-
const regionHere = node.tag === "region" && placeIDValue(node)
|
|
31
|
+
const regionHere = node.tag === "region" && placeIDValue(node) ? node : regionAncestor
|
|
32
32
|
|
|
33
33
|
if (regionHere && node !== regionHere && node.metadata?.["parent_fallback"] === true) {
|
|
34
34
|
const regionID = placeIDValue(regionHere)
|
|
@@ -39,7 +39,7 @@ export function applyParentFallbackContradiction(roots: readonly AddressNode[]):
|
|
|
39
39
|
|
|
40
40
|
const regions = (ancestors ?? []).filter((a) => REGION_LINEAGE_PLACETYPES.has(a.placetype))
|
|
41
41
|
|
|
42
|
-
if (regionID
|
|
42
|
+
if (regionID && regions.length && !regions.some((a) => String(a.id) === regionID)) {
|
|
43
43
|
delete node.lat
|
|
44
44
|
delete node.lon
|
|
45
45
|
delete node.placeID
|
|
@@ -83,7 +83,7 @@ export async function applyAdminCoherence(roots: readonly AddressNode[], backend
|
|
|
83
83
|
regionHere &&
|
|
84
84
|
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
85
85
|
!isResolvedWithCoord(node) &&
|
|
86
|
-
node.value.trim()
|
|
86
|
+
node.value.trim()
|
|
87
87
|
) {
|
|
88
88
|
await reconcileAdminPair(regionHere, node, backend)
|
|
89
89
|
}
|
|
@@ -103,7 +103,7 @@ async function reconcileAdminPair(
|
|
|
103
103
|
localityNode: AddressNode,
|
|
104
104
|
backend: ResolverBackend
|
|
105
105
|
): Promise<void> {
|
|
106
|
-
const regionCands = ((regionNode.alternatives as ResolvedPlace[] |
|
|
106
|
+
const regionCands = ((regionNode.alternatives as ResolvedPlace[] | null) ?? []).filter((r) => r.exactMatch)
|
|
107
107
|
|
|
108
108
|
for (const region of regionCands) {
|
|
109
109
|
const scoped = await backend.findPlace({
|
|
@@ -232,7 +232,7 @@ export async function applyExplicitCountryCoherence(
|
|
|
232
232
|
backend: ResolverBackend
|
|
233
233
|
): Promise<void> {
|
|
234
234
|
const visit = async (node: AddressNode, countryToken: AddressNode | null, regionAbove: boolean): Promise<void> => {
|
|
235
|
-
const countryHere = node.tag === "country" && node.value.trim()
|
|
235
|
+
const countryHere = node.tag === "country" && node.value.trim() ? node : countryToken
|
|
236
236
|
|
|
237
237
|
const regionHere = regionAbove || ((node.tag === "region" || node.tag === "subregion") && isResolvedWithCoord(node))
|
|
238
238
|
|
|
@@ -291,7 +291,7 @@ async function reconcileExplicitCountry(
|
|
|
291
291
|
export async function applyRegionCountryCoherence(
|
|
292
292
|
roots: readonly AddressNode[],
|
|
293
293
|
backend: ResolverBackend,
|
|
294
|
-
defaultCountry: string |
|
|
294
|
+
defaultCountry: string | null
|
|
295
295
|
): Promise<void> {
|
|
296
296
|
if (!defaultCountry) return
|
|
297
297
|
|
|
@@ -302,7 +302,7 @@ export async function applyRegionCountryCoherence(
|
|
|
302
302
|
regionHere &&
|
|
303
303
|
!isResolvedWithCoord(regionHere) &&
|
|
304
304
|
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
305
|
-
node.value.trim()
|
|
305
|
+
node.value.trim()
|
|
306
306
|
) {
|
|
307
307
|
await reconcileRegionCountry(regionHere, node, backend, defaultCountry)
|
|
308
308
|
}
|
|
@@ -329,7 +329,7 @@ async function reconcileRegionCountry(
|
|
|
329
329
|
|
|
330
330
|
if (sub.country.toUpperCase() === defaultCountry.toUpperCase()) return
|
|
331
331
|
|
|
332
|
-
const localityCountry = (localityNode.metadata?.["resolver_country"] as string |
|
|
332
|
+
const localityCountry = (localityNode.metadata?.["resolver_country"] as string | null)?.toUpperCase()
|
|
333
333
|
|
|
334
334
|
if (localityCountry === sub.country.toUpperCase()) return
|
|
335
335
|
|
package/lib/admin/containment.ts
CHANGED
|
@@ -14,8 +14,8 @@ import { firstNodeWhere, type AddressNode } from "@mailwoman/core/decoder"
|
|
|
14
14
|
* walk threads onto locality lookups, deliberately the same node the admin-coherence
|
|
15
15
|
* verdicts read so the two populations coincide.
|
|
16
16
|
*/
|
|
17
|
-
export function firstRegionQualifier(roots: readonly AddressNode[]): string |
|
|
18
|
-
return firstNodeWhere(roots, (n) => n.tag === "region" && n.value.trim())?.value.trim()
|
|
17
|
+
export function firstRegionQualifier(roots: readonly AddressNode[]): string | null {
|
|
18
|
+
return firstNodeWhere(roots, (n) => n.tag === "region" && n.value.trim())?.value.trim() ?? null
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -58,14 +58,14 @@ export function partitionByContainment<T>(
|
|
|
58
58
|
* no stamps at all → `"unavailable"`.
|
|
59
59
|
*/
|
|
60
60
|
export function adminContainmentVerdict(
|
|
61
|
-
candidates: ReadonlyArray<{ containedByQualifier?: boolean |
|
|
61
|
+
candidates: ReadonlyArray<{ containedByQualifier?: boolean | null }>
|
|
62
62
|
): "contained" | "no_contained_candidate" | "unavailable" {
|
|
63
63
|
let evaluated = false
|
|
64
64
|
|
|
65
65
|
for (const candidate of candidates) {
|
|
66
66
|
if (candidate.containedByQualifier === true) return "contained"
|
|
67
67
|
|
|
68
|
-
if (candidate.containedByQualifier
|
|
68
|
+
if (candidate.containedByQualifier != null) {
|
|
69
69
|
evaluated = true
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -80,7 +80,7 @@ function stamp<Pick extends { metadata?: Record<string, unknown> }>(
|
|
|
80
80
|
* Two probes at most, each drawn from the caller's budget (`hasBudget`).
|
|
81
81
|
*/
|
|
82
82
|
export async function resolveCompoundMunicipality<
|
|
83
|
-
Place extends { regionScopeMiss?: boolean },
|
|
83
|
+
Place extends { regionScopeMiss?: boolean | null },
|
|
84
84
|
Pick extends { top: Place; metadata?: Record<string, unknown> },
|
|
85
85
|
>(
|
|
86
86
|
value: string,
|
package/lib/admin/winner.ts
CHANGED
|
@@ -52,7 +52,7 @@ export const ADMIN_LADDER_LOCALITY_FIRST: ReadonlyArray<string> = [
|
|
|
52
52
|
*/
|
|
53
53
|
export interface ResolvedPostcodeHit {
|
|
54
54
|
value: string
|
|
55
|
-
resolverName: string |
|
|
55
|
+
resolverName: string | null
|
|
56
56
|
/**
|
|
57
57
|
* ISO-3166 alpha-2 code where the resolver placed the postcode.
|
|
58
58
|
*
|
|
@@ -67,11 +67,11 @@ export interface ResolvedPostcodeHit {
|
|
|
67
67
|
* Pick the admin fallback order for one resolved tree: postcode-first when the code is
|
|
68
68
|
* unit-grade or the address system's area-grade codes are finer than its localities.
|
|
69
69
|
*/
|
|
70
|
-
export function adminLadderFor(postcode: ResolvedPostcodeHit |
|
|
71
|
-
if (postcode
|
|
70
|
+
export function adminLadderFor(postcode: ResolvedPostcodeHit | null): ReadonlyArray<string> {
|
|
71
|
+
if (!postcode) return ADMIN_LADDER_LOCALITY_FIRST
|
|
72
72
|
|
|
73
73
|
const leads =
|
|
74
|
-
isUnitGradePostcodeHit(postcode.value, postcode.resolverName) || areaPostcodeLeadsLocality(postcode.country)
|
|
74
|
+
isUnitGradePostcodeHit(postcode.value, postcode.resolverName) || areaPostcodeLeadsLocality(postcode.country ?? null)
|
|
75
75
|
|
|
76
76
|
return leads ? ADMIN_LADDER_POSTCODE_FIRST : ADMIN_LADDER_LOCALITY_FIRST
|
|
77
77
|
}
|
|
@@ -89,7 +89,7 @@ export function adminLadderForNodes(nodes: readonly AddressNode[]): ReadonlyArra
|
|
|
89
89
|
|
|
90
90
|
return adminLadderFor({
|
|
91
91
|
value: node.value,
|
|
92
|
-
resolverName: node.metadata?.["resolver_name"] as string | undefined,
|
|
92
|
+
resolverName: (node.metadata?.["resolver_name"] as string | undefined) ?? null,
|
|
93
93
|
...(typeof country === "string" ? { country } : {}),
|
|
94
94
|
})
|
|
95
95
|
}
|
|
@@ -130,8 +130,8 @@ export function resolvedSpecificity(candidate: ResolvedSpecificityInput): number
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
const leads =
|
|
133
|
-
isUnitGradePostcodeHit(candidate.value ?? "", candidate.resolverName) ||
|
|
134
|
-
areaPostcodeLeadsLocality(candidate.country)
|
|
133
|
+
isUnitGradePostcodeHit(candidate.value ?? "", candidate.resolverName ?? null) ||
|
|
134
|
+
areaPostcodeLeadsLocality(candidate.country ?? null)
|
|
135
135
|
|
|
136
136
|
return leads ? (PLACETYPE_SPECIFICITY["postalcode"] ?? Number.NEGATIVE_INFINITY) : AREA_GRADE_POSTALCODE_SPECIFICITY
|
|
137
137
|
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* reports those gaps without changing resolution behavior.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import type
|
|
12
|
+
import { type BackendCapabilityGap, RESOLVE_SWITCH_DEFAULTS, type ResolverBackend } from "@mailwoman/core/resolver"
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* The optional backend methods whose absence degrades a passing default, paired with what stops working.
|
|
@@ -22,14 +22,14 @@ const CONSEQUENTIAL_CAPABILITIES: ReadonlyArray<Omit<BackendCapabilityGap, "back
|
|
|
22
22
|
{
|
|
23
23
|
capability: "ancestors",
|
|
24
24
|
option: "hierarchyCompletion",
|
|
25
|
-
defaultOn:
|
|
25
|
+
defaultOn: RESOLVE_SWITCH_DEFAULTS.hierarchyCompletion,
|
|
26
26
|
degrades:
|
|
27
27
|
"the containment lineage is never read: a parse that drops the locality of a city-state or dependent locality keeps the gap, and `metadata.ancestors` (the lineage the Nominatim and Photon drop-ins expose) is never populated",
|
|
28
28
|
},
|
|
29
29
|
{
|
|
30
30
|
capability: "coincidentLocalitiesFor",
|
|
31
31
|
option: "hierarchyCompletion",
|
|
32
|
-
defaultOn:
|
|
32
|
+
defaultOn: RESOLVE_SWITCH_DEFAULTS.hierarchyCompletion,
|
|
33
33
|
degrades:
|
|
34
34
|
"the dual-role pass never runs: a region that is also a locality under the same name resolves to one role only",
|
|
35
35
|
},
|
package/lib/decorate-node.ts
CHANGED
|
@@ -29,14 +29,14 @@ export function decorateNode(
|
|
|
29
29
|
resolved: CoordinateOptionalPlace,
|
|
30
30
|
alternatives: ResolvedPlace[]
|
|
31
31
|
): void {
|
|
32
|
-
if (node.source
|
|
32
|
+
if (node.source || node.sourceID) {
|
|
33
33
|
const meta = { ...node.metadata }
|
|
34
34
|
|
|
35
|
-
if (node.source
|
|
35
|
+
if (node.source) {
|
|
36
36
|
meta["classifier_source"] = node.source
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
if (node.sourceID
|
|
39
|
+
if (node.sourceID) {
|
|
40
40
|
meta["classifier_source_id"] = node.sourceID
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -48,7 +48,7 @@ export function decorateNode(
|
|
|
48
48
|
|
|
49
49
|
// `0,0` is the gazetteer's unlocated sentinel, so an unlocated place gets both coordinates
|
|
50
50
|
// cleared rather than a coordinate that satisfies every `lat != null` guard downstream.
|
|
51
|
-
const located = resolved.lat
|
|
51
|
+
const located = resolved.lat != null && resolved.lon != null && (resolved.lat !== 0 || resolved.lon !== 0)
|
|
52
52
|
|
|
53
53
|
if (located) {
|
|
54
54
|
node.lat = resolved.lat
|
|
@@ -63,7 +63,7 @@ export function decorateNode(
|
|
|
63
63
|
|
|
64
64
|
// Additive metadata only.
|
|
65
65
|
// No part of the resolve reads it back.
|
|
66
|
-
if (resolved.prominence
|
|
66
|
+
if (resolved.prominence != null) {
|
|
67
67
|
node.metadata["resolver_prominence"] = resolved.prominence
|
|
68
68
|
}
|
|
69
69
|
|
|
@@ -73,15 +73,15 @@ export function decorateNode(
|
|
|
73
73
|
|
|
74
74
|
// Written only when the backend supplies a value, because `resolver_*: 0`
|
|
75
75
|
// would assert an unmeasured value.
|
|
76
|
-
if (resolved.referential
|
|
76
|
+
if (resolved.referential != null) {
|
|
77
77
|
node.metadata["resolver_referential"] = resolved.referential
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
if (resolved.encyclopedic
|
|
80
|
+
if (resolved.encyclopedic != null) {
|
|
81
81
|
node.metadata["resolver_encyclopedic"] = resolved.encyclopedic
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
if (resolved.importance
|
|
84
|
+
if (resolved.importance != null) {
|
|
85
85
|
node.metadata["resolver_importance"] = resolved.importance
|
|
86
86
|
}
|
|
87
87
|
|
package/lib/plausibility.ts
CHANGED
|
@@ -33,7 +33,7 @@ export interface ResolvedCoordinate {
|
|
|
33
33
|
/**
|
|
34
34
|
* The canonical place URI, such as `wof:…`, when the resolver supplied one.
|
|
35
35
|
*/
|
|
36
|
-
placeID
|
|
36
|
+
placeID: string | null
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
@@ -46,12 +46,12 @@ export function finestResolvedCoordinate(tree: AddressTree): ResolvedCoordinate
|
|
|
46
46
|
let bestTier = -1
|
|
47
47
|
|
|
48
48
|
const visit = (node: AddressNode): void => {
|
|
49
|
-
if (node.lat
|
|
49
|
+
if (node.lat != null && node.lon != null) {
|
|
50
50
|
const tier = RESOLUTION_TIER[node.tag] ?? 4
|
|
51
51
|
|
|
52
52
|
if (tier > bestTier) {
|
|
53
53
|
bestTier = tier
|
|
54
|
-
best = { tag: node.tag, lat: node.lat, lon: node.lon, placeID: node.placeID }
|
|
54
|
+
best = { tag: node.tag, lat: node.lat, lon: node.lon, placeID: node.placeID ?? null }
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -134,12 +134,12 @@ export interface PlausibilityVerdict {
|
|
|
134
134
|
* Why the resolution is implausible, set only when `implausible` is true:
|
|
135
135
|
* `country-centroid` or `outside-expected-country`.
|
|
136
136
|
*/
|
|
137
|
-
reason
|
|
137
|
+
reason: "country-centroid" | "outside-expected-country" | null
|
|
138
138
|
|
|
139
139
|
/**
|
|
140
140
|
* The coordinate the verdict was based on, present when anything resolved.
|
|
141
141
|
*/
|
|
142
|
-
coordinate
|
|
142
|
+
coordinate: ResolvedCoordinate | null
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
/**
|
|
@@ -183,5 +183,5 @@ export function isImplausibleResolution(tree: AddressTree, opts: PlausibilityOpt
|
|
|
183
183
|
return { implausible: true, reason: "outside-expected-country", coordinate }
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
return { implausible: false, coordinate: coordinate ??
|
|
186
|
+
return { implausible: false, reason: null, coordinate: coordinate ?? null }
|
|
187
187
|
}
|
|
@@ -57,19 +57,19 @@ export interface PostcodeCountryScope {
|
|
|
57
57
|
*
|
|
58
58
|
* It is set only for `pair` evidence, because the other verdicts compare no second point.
|
|
59
59
|
*/
|
|
60
|
-
distanceKm
|
|
60
|
+
distanceKm: number | null
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* The postcode place behind the verdict.
|
|
64
64
|
* It is absent for `locality` evidence.
|
|
65
65
|
*/
|
|
66
|
-
postcodePlace
|
|
66
|
+
postcodePlace: ResolvedPlace | null
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* The locality place behind the verdict.
|
|
70
70
|
* It is absent for `postcode` evidence.
|
|
71
71
|
*/
|
|
72
|
-
localityPlace
|
|
72
|
+
localityPlace: ResolvedPlace | null
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -85,17 +85,17 @@ export interface PostcodeCountryScopeOpts {
|
|
|
85
85
|
postcode: string
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
|
-
* The caller's default country filter, or
|
|
88
|
+
* The caller's default country filter, or null when no default applies.
|
|
89
89
|
*
|
|
90
90
|
* Without a default, only the `pair` test runs.
|
|
91
91
|
* The pass returns a scope when exactly one country qualifies.
|
|
92
92
|
*/
|
|
93
|
-
defaultCountry: string |
|
|
93
|
+
defaultCountry: string | null
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
96
|
* The consistency radius in kilometers.
|
|
97
97
|
*
|
|
98
|
-
*
|
|
98
|
+
* @defaultValue {@linkcode POSTCODE_COUNTRY_COHERENCE_THRESHOLD_KM}
|
|
99
99
|
*/
|
|
100
100
|
thresholdKm?: number
|
|
101
101
|
|
|
@@ -134,13 +134,13 @@ export function localityValuesInDocumentOrder(roots: readonly AddressNode[]): st
|
|
|
134
134
|
/**
|
|
135
135
|
* Returns the first value of {@link localityValuesInDocumentOrder}.
|
|
136
136
|
*/
|
|
137
|
-
export function firstLocalityValue(roots: readonly AddressNode[]): string |
|
|
138
|
-
return localityValuesInDocumentOrder(roots)[0]
|
|
137
|
+
export function firstLocalityValue(roots: readonly AddressNode[]): string | null {
|
|
138
|
+
return localityValuesInDocumentOrder(roots)[0] ?? null
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
function collectInDocumentOrder(nodes: readonly AddressNode[], tag: string, out: string[], seen: Set<string>): void {
|
|
142
142
|
for (const n of nodes) {
|
|
143
|
-
if (n.tag === tag && n.value.trim()
|
|
143
|
+
if (n.tag === tag && n.value.trim()) {
|
|
144
144
|
const value = n.value.trim()
|
|
145
145
|
const key = value.toLowerCase()
|
|
146
146
|
|
|
@@ -250,7 +250,7 @@ export async function findPostcodeCountryScope(
|
|
|
250
250
|
opts: PostcodeCountryScopeOpts
|
|
251
251
|
): Promise<PostcodeCountryScope | null> {
|
|
252
252
|
const postcode = opts.postcode.trim()
|
|
253
|
-
const defaultCountry = opts.defaultCountry?.trim().toUpperCase() ||
|
|
253
|
+
const defaultCountry = opts.defaultCountry?.trim().toUpperCase() || null
|
|
254
254
|
|
|
255
255
|
if (!postcode) return null
|
|
256
256
|
|
|
@@ -323,11 +323,19 @@ export async function findPostcodeCountryScope(
|
|
|
323
323
|
const ownPostcode = pcHolders.get(country)
|
|
324
324
|
|
|
325
325
|
const contradicted =
|
|
326
|
-
ownPostcode
|
|
326
|
+
ownPostcode != null &&
|
|
327
327
|
haversineKm(ownPostcode.lat, ownPostcode.lon, localityPlace.lat, localityPlace.lon) > thresholdKm
|
|
328
328
|
|
|
329
329
|
if (country !== defaultCountry && !contradicted && !verdicts.has(country)) {
|
|
330
|
-
verdicts.set(country, {
|
|
330
|
+
verdicts.set(country, {
|
|
331
|
+
country,
|
|
332
|
+
postcode,
|
|
333
|
+
locality,
|
|
334
|
+
evidence: "locality",
|
|
335
|
+
distanceKm: null,
|
|
336
|
+
postcodePlace: null,
|
|
337
|
+
localityPlace,
|
|
338
|
+
})
|
|
331
339
|
}
|
|
332
340
|
}
|
|
333
341
|
|
|
@@ -339,7 +347,15 @@ export async function findPostcodeCountryScope(
|
|
|
339
347
|
const [country, postcodePlace] = [...pcHolders.entries()][0]!
|
|
340
348
|
|
|
341
349
|
if (country !== defaultCountry) {
|
|
342
|
-
return {
|
|
350
|
+
return {
|
|
351
|
+
country,
|
|
352
|
+
postcode,
|
|
353
|
+
locality: localities[0]!,
|
|
354
|
+
evidence: "postcode",
|
|
355
|
+
distanceKm: null,
|
|
356
|
+
postcodePlace,
|
|
357
|
+
localityPlace: null,
|
|
358
|
+
}
|
|
343
359
|
}
|
|
344
360
|
}
|
|
345
361
|
|
|
@@ -370,7 +386,7 @@ export function stampPostcodeCountryScope(roots: readonly AddressNode[], scope:
|
|
|
370
386
|
|
|
371
387
|
postcode_country_scope_evidence: scope.evidence,
|
|
372
388
|
|
|
373
|
-
...(scope.distanceKm
|
|
389
|
+
...(scope.distanceKm != null ? { postcode_country_scope_km: scope.distanceKm } : {}),
|
|
374
390
|
}
|
|
375
391
|
}
|
|
376
392
|
}
|
package/lib/postcode/prefix.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { EMPTY_PLACE_FIELDS } from "@mailwoman/core/resolver"
|
|
7
8
|
import type { PostcodePrefixIndexLike, PostcodePrefixNode, ResolvedPlace } from "@mailwoman/core/resolver"
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -42,7 +43,7 @@ export function derivePostcodePrefix(code: string, country?: string): string | n
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
/**
|
|
45
|
-
* The
|
|
46
|
+
* The result of a successful prefix probe.
|
|
46
47
|
*/
|
|
47
48
|
export interface PostcodePrefixProbeResult {
|
|
48
49
|
/**
|
|
@@ -90,12 +91,13 @@ export function postcodePrefixResolvedPlace(
|
|
|
90
91
|
index: PostcodePrefixIndexLike
|
|
91
92
|
): CoordinateOptionalPlace {
|
|
92
93
|
return {
|
|
94
|
+
...EMPTY_PLACE_FIELDS,
|
|
93
95
|
id: 0,
|
|
94
96
|
name: prefix,
|
|
95
97
|
placetype: "postalcode",
|
|
96
98
|
|
|
97
99
|
country: index.country?.toUpperCase() ?? "",
|
|
98
|
-
...(node.lat
|
|
100
|
+
...(node.lat != null && node.lon != null ? { lat: node.lat, lon: node.lon } : {}),
|
|
99
101
|
score: 0,
|
|
100
102
|
exactMatch: false,
|
|
101
103
|
}
|
|
@@ -19,9 +19,9 @@ const SYSTEM_UNIVERSE: ReadonlySet<string> = new Set<string>(SYSTEM_CODES.map((s
|
|
|
19
19
|
export interface PostcodeShapeVerdict {
|
|
20
20
|
/**
|
|
21
21
|
* The upper-case postcode systems of the first confirmed span, passed to
|
|
22
|
-
* `findPostcodeCountryScope` as its candidate systems; `
|
|
22
|
+
* `findPostcodeCountryScope` as its candidate systems; `null` when no span was confirmed.
|
|
23
23
|
*/
|
|
24
|
-
narrowing
|
|
24
|
+
narrowing: string[] | null
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Postcode values whose shape fits a system used by a sibling country or region span.
|
|
@@ -84,7 +84,7 @@ function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
|
|
|
84
84
|
* `house_number`, and any other span is stamped `postcode_shape_excluded`.
|
|
85
85
|
*/
|
|
86
86
|
export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): PostcodeShapeVerdict {
|
|
87
|
-
const verdict: PostcodeShapeVerdict = { confirmed: [], excluded: [], abstained: [] }
|
|
87
|
+
const verdict: PostcodeShapeVerdict = { narrowing: null, confirmed: [], excluded: [], abstained: [] }
|
|
88
88
|
|
|
89
89
|
const postcodes = collectNodes(roots, (n) => n.tag === "postcode" && n.value.trim())
|
|
90
90
|
|
|
@@ -109,7 +109,7 @@ export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): Post
|
|
|
109
109
|
|
|
110
110
|
node.metadata = { ...node.metadata, postcode_shape_systems: intersection }
|
|
111
111
|
|
|
112
|
-
if (verdict.narrowing
|
|
112
|
+
if (!verdict.narrowing) {
|
|
113
113
|
verdict.narrowing = intersection
|
|
114
114
|
}
|
|
115
115
|
|
package/lib/remote-resolver.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import type { AddressTree } from "@mailwoman/core/decoder"
|
|
8
8
|
import { stringifyJSON } from "@mailwoman/core/json"
|
|
9
|
-
import type { ResolveOpts, Resolver } from "@mailwoman/core/resolver"
|
|
9
|
+
import type { BackendCapabilityGap, GazetteerArtifactCoverage, ResolveOpts, Resolver } from "@mailwoman/core/resolver"
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Describes the `ResolveOpts` sent over the wire.
|
|
@@ -19,8 +19,8 @@ export type SerializableResolveOpts = Omit<ResolveOpts, "addressPoints" | "inter
|
|
|
19
19
|
/**
|
|
20
20
|
* Strip the live lookup handles from `ResolveOpts` so the rest can be JSON-serialized over http.
|
|
21
21
|
*/
|
|
22
|
-
export function serializableResolveOpts(opts?: ResolveOpts): SerializableResolveOpts |
|
|
23
|
-
if (!opts) return
|
|
22
|
+
export function serializableResolveOpts(opts?: ResolveOpts): SerializableResolveOpts | null {
|
|
23
|
+
if (!opts) return null
|
|
24
24
|
const { addressPoints: _ap, interpolation: _ip, ...rest } = opts
|
|
25
25
|
|
|
26
26
|
return rest
|
|
@@ -69,7 +69,7 @@ export interface RemoteResolverOpts {
|
|
|
69
69
|
*/
|
|
70
70
|
export interface ResolveTreeRequest {
|
|
71
71
|
tree: AddressTree
|
|
72
|
-
opts?: SerializableResolveOpts
|
|
72
|
+
opts?: SerializableResolveOpts | null
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -90,6 +90,8 @@ export class RemoteResolver implements Resolver {
|
|
|
90
90
|
readonly #fetch: RemoteResolverFetch
|
|
91
91
|
readonly #timeoutMs: number
|
|
92
92
|
readonly #headers: Record<string, string>
|
|
93
|
+
readonly artifactCoverage: GazetteerArtifactCoverage | null = null
|
|
94
|
+
readonly capabilityGaps: readonly BackendCapabilityGap[] | null = null
|
|
93
95
|
|
|
94
96
|
constructor(opts: RemoteResolverOpts) {
|
|
95
97
|
if (!opts.endpoint) throw new Error("RemoteResolver: `endpoint` is required")
|
package/lib/rerank.ts
CHANGED
|
@@ -43,7 +43,7 @@ export interface RerankedCandidate<T = unknown> extends RerankCandidate<T> {
|
|
|
43
43
|
/**
|
|
44
44
|
* Why it was vetoed, when it was.
|
|
45
45
|
*/
|
|
46
|
-
reason
|
|
46
|
+
reason: string | null
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
export interface RerankResult<T = unknown> {
|
|
@@ -69,8 +69,11 @@ export type ResolveTree = (tree: AddressTree) => Promise<AddressTree>
|
|
|
69
69
|
|
|
70
70
|
export interface RerankOpts {
|
|
71
71
|
/**
|
|
72
|
-
* Resolve at most this many candidates
|
|
73
|
-
*
|
|
72
|
+
* Resolve at most this many candidates.
|
|
73
|
+
*
|
|
74
|
+
* Each costs a resolver round-trip, so this is the latency knob.
|
|
75
|
+
*
|
|
76
|
+
* @defaultValue `5`
|
|
74
77
|
*/
|
|
75
78
|
maxResolve?: number
|
|
76
79
|
/**
|
|
@@ -105,7 +108,7 @@ export async function rerankByResolution<T>(
|
|
|
105
108
|
const candidate = candidates[i]!
|
|
106
109
|
|
|
107
110
|
if (i >= maxResolve) {
|
|
108
|
-
ranked.push({ ...candidate, resolved: null, implausible: false })
|
|
111
|
+
ranked.push({ ...candidate, resolved: null, implausible: false, reason: null })
|
|
109
112
|
|
|
110
113
|
continue
|
|
111
114
|
}
|
|
@@ -117,7 +120,7 @@ export async function rerankByResolution<T>(
|
|
|
117
120
|
} catch {
|
|
118
121
|
// A resolver failure is not evidence against the parse, so the model's rank stands
|
|
119
122
|
// rather than vetoing a possibly-correct hypothesis on an outage.
|
|
120
|
-
ranked.push({ ...candidate, resolved: null, implausible: false })
|
|
123
|
+
ranked.push({ ...candidate, resolved: null, implausible: false, reason: null })
|
|
121
124
|
|
|
122
125
|
continue
|
|
123
126
|
}
|
|
@@ -131,7 +134,7 @@ export async function rerankByResolution<T>(
|
|
|
131
134
|
...candidate,
|
|
132
135
|
resolved,
|
|
133
136
|
implausible: verdict.implausible,
|
|
134
|
-
|
|
137
|
+
reason: verdict.reason,
|
|
135
138
|
})
|
|
136
139
|
}
|
|
137
140
|
|