@mailwoman/resolver 11.1.0 → 11.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/admin/coherence-passes.ts +1 -1
- package/lib/admin/county-designator.ts +91 -0
- package/lib/admin/former-division.ts +116 -0
- package/lib/bare-toponym-race.ts +1 -1
- package/lib/decorate-node.ts +103 -2
- package/lib/postcode/prefix.ts +7 -16
- package/lib/postcode/shape-coherence.ts +2 -2
- package/lib/remote-resolver.ts +1 -1
- package/lib/resolve/parent-place.ts +109 -0
- package/lib/resolve/passes.ts +68 -11
- package/lib/resolve/sibling-locality.ts +150 -0
- package/lib/resolve.ts +407 -78
- package/lib/scope-retrieval.ts +91 -0
- package/lib/span-rescore.ts +65 -14
- 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.d.ts +33 -0
- package/out/admin/county-designator.d.ts.map +1 -0
- package/out/admin/county-designator.js +65 -0
- package/out/admin/county-designator.js.map +1 -0
- 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 +85 -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/postcode/shape-coherence.js +2 -2
- package/out/postcode/shape-coherence.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 +44 -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 +273 -69
- 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/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
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
3
|
|
|
4
4
|
import { matchCountry, matchSubdivision } from "@mailwoman/codex/country"
|
|
5
|
-
import { PLACETYPE_FILTER_GROUPS } from "@mailwoman/codex/placetype-map"
|
|
5
|
+
import { PLACETYPE_FILTER_GROUPS } from "@mailwoman/codex/labels/placetype-map"
|
|
6
6
|
import type { AddressNode } from "@mailwoman/core/decoder"
|
|
7
7
|
import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
|
|
8
8
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @file A `locality` span written as a county with its designator, resolved as the WOF `county` of its region.
|
|
6
|
+
*
|
|
7
|
+
* The model labels `Harris County` in `Harris County, TX` as `locality`, and no locality of that name is in
|
|
8
|
+
* Texas, so the lookup returns no candidate or a namesake elsewhere and the region answers. The codex maps
|
|
9
|
+
* `subregion` to the WOF `county` placetype, and the county's row stores the same name.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { countyDesignatorsFor, countyNameWithoutDesignator } from "@mailwoman/codex/designators/county"
|
|
13
|
+
import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The metadata key the walk stamps on a node this fallback resolved.
|
|
17
|
+
* The walk retags such a node `subregion`.
|
|
18
|
+
*/
|
|
19
|
+
export const COUNTY_DESIGNATOR_FALLBACK = "county_designator_fallback"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Where the county is looked for: its country, and the region either as a resolved place id
|
|
23
|
+
* or as the written region qualifier the walk read from the tree.
|
|
24
|
+
*/
|
|
25
|
+
export interface CountyDesignatorScope {
|
|
26
|
+
country: string
|
|
27
|
+
regionID: number | string | null
|
|
28
|
+
regionQualifier: string | null
|
|
29
|
+
limit: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The county of the designated value's name inside its region, with the other counties of that name there.
|
|
34
|
+
*
|
|
35
|
+
* It is `null` when the value has no designator for the country, when the country's
|
|
36
|
+
* counties take a tag other than `subregion` (IE's are WOF regions), when the region
|
|
37
|
+
* is unknown, or when no county of that name is inside it.
|
|
38
|
+
*
|
|
39
|
+
* The whole value is probed first, because WOF keys most US counties as `<name> county` too.
|
|
40
|
+
* The name without its designator is probed second.
|
|
41
|
+
*
|
|
42
|
+
* A hit the backend admitted outside the region (`regionScopeMiss`) is refused.
|
|
43
|
+
*/
|
|
44
|
+
export async function countyFromDesignatedLocality(
|
|
45
|
+
backend: ResolverBackend,
|
|
46
|
+
value: string,
|
|
47
|
+
scope: CountyDesignatorScope
|
|
48
|
+
): Promise<{ top: ResolvedPlace; alternatives: ResolvedPlace[] } | null> {
|
|
49
|
+
// The walk retags a hit `subregion`, the tag of a WOF `county`.
|
|
50
|
+
if (countyDesignatorsFor(scope.country)?.tag !== "subregion") return null
|
|
51
|
+
|
|
52
|
+
const bare = countyNameWithoutDesignator(value, scope.country)
|
|
53
|
+
|
|
54
|
+
if (!bare) return null
|
|
55
|
+
|
|
56
|
+
const regionIDs = await regionIDsFor(backend, scope)
|
|
57
|
+
|
|
58
|
+
for (const parentID of regionIDs) {
|
|
59
|
+
for (const text of [value, bare]) {
|
|
60
|
+
const hits = await backend.findPlace({
|
|
61
|
+
text,
|
|
62
|
+
placetype: "county",
|
|
63
|
+
country: scope.country,
|
|
64
|
+
parentID,
|
|
65
|
+
limit: scope.limit,
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
const inside = hits.filter((hit) => hit.regionScopeMiss !== true)
|
|
69
|
+
|
|
70
|
+
if (inside.length) return { top: inside[0]!, alternatives: inside.slice(1) }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The region ids to scope the county probe by: the resolved parent's, or else the
|
|
79
|
+
* exact-match regions the written qualifier names in the country.
|
|
80
|
+
*/
|
|
81
|
+
async function regionIDsFor(backend: ResolverBackend, scope: CountyDesignatorScope): Promise<Array<number | string>> {
|
|
82
|
+
if (scope.regionID != null) return [scope.regionID]
|
|
83
|
+
|
|
84
|
+
const qualifier = scope.regionQualifier?.trim()
|
|
85
|
+
|
|
86
|
+
if (!qualifier) return []
|
|
87
|
+
|
|
88
|
+
const regions = await backend.findPlace({ text: qualifier, placetype: "region", country: scope.country, limit: 3 })
|
|
89
|
+
|
|
90
|
+
return regions.filter((region) => region.exactMatch === true && region.regionScopeMiss !== true).map((r) => r.id)
|
|
91
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @file A `subregion` or `region` value that's the name of a former division, such as the county of Avon
|
|
6
|
+
* (1974 to 1996) in `34 Coldharbour Rd, Redland, Avon, Bristol BS6 7NA`.
|
|
7
|
+
*
|
|
8
|
+
* No current division answers the value, so the walk keeps no place for it and scopes no lookup by it.
|
|
9
|
+
* After the walk, the value is accepted when a former division of that name is linked, by the
|
|
10
|
+
* containment evidence the former-divisions table records, to a place in the resolved locality's
|
|
11
|
+
* lineage. An accepted node is stamped {@link FORMER_DIVISION} and keeps no place, so it moves no result.
|
|
12
|
+
* A value that no linked former division explains keeps the walk's result unchanged.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { type AddressNode, collectNodes } from "@mailwoman/core/decoder"
|
|
16
|
+
import { type Ancestor, type FormerDivisionMatch, wofIDFromPlaceID } from "@mailwoman/core/resolver"
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The metadata key an accepted node's stamp is stored under.
|
|
20
|
+
*/
|
|
21
|
+
export const FORMER_DIVISION = "former_division"
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The tags a former division can stand in.
|
|
25
|
+
*/
|
|
26
|
+
export const FORMER_DIVISION_TAGS: ReadonlySet<string> = new Set(["subregion", "region"])
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The resolved tags whose lineage a former division is checked against.
|
|
30
|
+
*/
|
|
31
|
+
const SETTLEMENT_TAGS: ReadonlySet<string> = new Set(["locality", "dependent_locality", "municipality", "district"])
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What an accepted node's stamp records: the division and the resolved place it's linked through.
|
|
35
|
+
*/
|
|
36
|
+
export interface FormerDivisionStamp {
|
|
37
|
+
name: string
|
|
38
|
+
country: string
|
|
39
|
+
kind: string
|
|
40
|
+
geonames_id: number | null
|
|
41
|
+
wof_id: number | null
|
|
42
|
+
evidence: readonly string[]
|
|
43
|
+
/**
|
|
44
|
+
* The resolved place whose lineage holds a place the division covered.
|
|
45
|
+
*/
|
|
46
|
+
linked_place: string
|
|
47
|
+
/**
|
|
48
|
+
* The WOF id in that lineage the division is linked to.
|
|
49
|
+
*/
|
|
50
|
+
linked_id: number
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The first match linked to a resolved settlement's lineage, with that settlement and the linked id.
|
|
55
|
+
*/
|
|
56
|
+
function linkedMatch(
|
|
57
|
+
matches: readonly FormerDivisionMatch[],
|
|
58
|
+
settlements: readonly AddressNode[],
|
|
59
|
+
ancestors: (id: number) => readonly Ancestor[]
|
|
60
|
+
): { match: FormerDivisionMatch; place: string; id: number } | null {
|
|
61
|
+
for (const settlement of settlements) {
|
|
62
|
+
const id = wofIDFromPlaceID(settlement.placeID ?? null)
|
|
63
|
+
|
|
64
|
+
if (id == null) continue
|
|
65
|
+
|
|
66
|
+
const lineage = [id, ...ancestors(id).map((a) => Number(a.id))]
|
|
67
|
+
|
|
68
|
+
for (const match of matches) {
|
|
69
|
+
const linked = lineage.find((candidate) => match.linkedIDs.has(candidate))
|
|
70
|
+
|
|
71
|
+
if (linked != null) return { match, place: settlement.placeID!, id: linked }
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Stamps each pending node whose former division is linked to the tree's resolved settlement.
|
|
80
|
+
*
|
|
81
|
+
* `pending` maps a walked node that no current division answered to the former divisions its value names.
|
|
82
|
+
* It returns the number of nodes stamped.
|
|
83
|
+
*/
|
|
84
|
+
export function acceptFormerDivisions(
|
|
85
|
+
roots: readonly AddressNode[],
|
|
86
|
+
pending: ReadonlyMap<AddressNode, readonly FormerDivisionMatch[]>,
|
|
87
|
+
ancestors: (id: number) => readonly Ancestor[]
|
|
88
|
+
): number {
|
|
89
|
+
if (!pending.size) return 0
|
|
90
|
+
|
|
91
|
+
const settlements = collectNodes(roots, (n) => SETTLEMENT_TAGS.has(n.tag) && n.placeID != null)
|
|
92
|
+
let accepted = 0
|
|
93
|
+
|
|
94
|
+
for (const node of collectNodes(roots, (n) => pending.has(n))) {
|
|
95
|
+
const found = linkedMatch(pending.get(node)!, settlements, ancestors)
|
|
96
|
+
|
|
97
|
+
if (!found) continue
|
|
98
|
+
|
|
99
|
+
const stamp: FormerDivisionStamp = {
|
|
100
|
+
name: found.match.name,
|
|
101
|
+
country: found.match.country,
|
|
102
|
+
kind: found.match.kind,
|
|
103
|
+
geonames_id: found.match.geonamesID,
|
|
104
|
+
wof_id: found.match.wofID,
|
|
105
|
+
evidence: found.match.evidence,
|
|
106
|
+
linked_place: found.place,
|
|
107
|
+
linked_id: found.id,
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
node.metadata = { ...node.metadata, [FORMER_DIVISION]: stamp }
|
|
111
|
+
|
|
112
|
+
accepted++
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return accepted
|
|
116
|
+
}
|
package/lib/bare-toponym-race.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import type { ComponentTag } from "@mailwoman/codex/component"
|
|
16
|
-
import type { PlacetypeMap } from "@mailwoman/codex/placetype-map"
|
|
16
|
+
import type { PlacetypeMap } from "@mailwoman/codex/labels/placetype-map"
|
|
17
17
|
import type { AddressNode, AddressTree } from "@mailwoman/core/decoder"
|
|
18
18
|
import { loneValueNode } from "@mailwoman/core/decoder"
|
|
19
19
|
import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
|
package/lib/decorate-node.ts
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { AddressNode } from "@mailwoman/core/decoder"
|
|
10
|
-
import type
|
|
10
|
+
import { EMPTY_PLACE_FIELDS, type ResolvedPlace, ScoreUnmeasuredReason } from "@mailwoman/core/resolver"
|
|
11
|
+
import { isPresent } from "@mailwoman/core/values/objects"
|
|
11
12
|
|
|
12
13
|
import type { CoordinateOptionalPlace } from "#postcode/prefix"
|
|
13
14
|
|
|
@@ -37,6 +38,7 @@ export function isResolvedWithCoord(n: AddressNode): boolean {
|
|
|
37
38
|
*/
|
|
38
39
|
export const PICK_METADATA_KEYS = [
|
|
39
40
|
"resolver_score",
|
|
41
|
+
"resolver_score_unmeasured",
|
|
40
42
|
"resolver_name",
|
|
41
43
|
"resolver_prominence",
|
|
42
44
|
"resolver_country",
|
|
@@ -52,8 +54,58 @@ export const PICK_METADATA_KEYS = [
|
|
|
52
54
|
"admin_containment",
|
|
53
55
|
"parent_fallback",
|
|
54
56
|
"variant_alias_exemption",
|
|
57
|
+
"county_designator_fallback",
|
|
58
|
+
"sibling_locality_containment",
|
|
55
59
|
] as const
|
|
56
60
|
|
|
61
|
+
/**
|
|
62
|
+
* The most places {@linkcode decorateNode} keeps on a node's `considered` list.
|
|
63
|
+
*
|
|
64
|
+
* A default lookup retrieves 5 rows and the bare-toponym race adds 2, so 10 holds one
|
|
65
|
+
* re-pick's displaced place and runner-ups beside the probe losers.
|
|
66
|
+
* Passes that re-pick the same node repeatedly drop the oldest entries first.
|
|
67
|
+
*/
|
|
68
|
+
export const CONSIDERED_LIMIT = 10
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The place a resolver-decorated node holds, read back from its decoration, or null
|
|
72
|
+
* when the node holds no located place with an identifier.
|
|
73
|
+
*
|
|
74
|
+
* `sourceID` is `<placetype>:<id>` as {@linkcode decorateNode} writes it.
|
|
75
|
+
*/
|
|
76
|
+
export function placeOfNode(node: AddressNode): ResolvedPlace | null {
|
|
77
|
+
if (node.source !== "resolver" || !node.sourceID || !isResolvedWithCoord(node)) return null
|
|
78
|
+
|
|
79
|
+
const separator = node.sourceID.indexOf(":")
|
|
80
|
+
const rawID = node.sourceID.slice(separator + 1)
|
|
81
|
+
|
|
82
|
+
if (separator < 1 || !rawID || rawID === "0") return null
|
|
83
|
+
|
|
84
|
+
const meta = node.metadata ?? {}
|
|
85
|
+
const number = (key: string): number | null => (typeof meta[key] === "number" ? (meta[key] as number) : null)
|
|
86
|
+
const unmeasured = meta["resolver_score_unmeasured"]
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
...EMPTY_PLACE_FIELDS,
|
|
90
|
+
id: /^\d+$/u.test(rawID) ? Number(rawID) : rawID,
|
|
91
|
+
name: typeof meta["resolver_name"] === "string" ? meta["resolver_name"] : node.value,
|
|
92
|
+
placetype: node.sourceID.slice(0, separator),
|
|
93
|
+
country: typeof meta["resolver_country"] === "string" ? meta["resolver_country"] : null,
|
|
94
|
+
lat: node.lat!,
|
|
95
|
+
lon: node.lon!,
|
|
96
|
+
score: number("resolver_score") ?? 0,
|
|
97
|
+
scoreUnmeasured:
|
|
98
|
+
typeof unmeasured === "string"
|
|
99
|
+
? (unmeasured as ScoreUnmeasuredReason)
|
|
100
|
+
: number("resolver_score") == null
|
|
101
|
+
? ScoreUnmeasuredReason.Unrecorded
|
|
102
|
+
: null,
|
|
103
|
+
prominence: number("resolver_prominence"),
|
|
104
|
+
importance: number("resolver_importance"),
|
|
105
|
+
...(meta["resolver_matched_alias"] ? { matchedAlias: true as const } : {}),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
57
109
|
/**
|
|
58
110
|
* Stamp a node with resolver-supplied attribution, displacing any prior classifier `source`
|
|
59
111
|
* / `sourceID` into `metadata.classifier_source` / `metadata.classifier_source_id`
|
|
@@ -62,15 +114,24 @@ export const PICK_METADATA_KEYS = [
|
|
|
62
114
|
* A node the resolver already decorated keeps its classifier stamps as the first decoration wrote them.
|
|
63
115
|
* When the new place differs from the one the node holds, the {@linkcode PICK_METADATA_KEYS}
|
|
64
116
|
* and `alternatives` of the previous place are removed before the new place is written.
|
|
117
|
+
*
|
|
118
|
+
* The previous place and its alternatives move to `considered`, with the `considered`
|
|
119
|
+
* places the caller passes, so the geocode result still reports them.
|
|
120
|
+
* No ranking stage reads `considered`, so moving a place there changes no answer.
|
|
65
121
|
*/
|
|
66
122
|
export function decorateNode(
|
|
67
123
|
node: AddressNode,
|
|
68
124
|
resolved: CoordinateOptionalPlace,
|
|
69
|
-
alternatives: ResolvedPlace[]
|
|
125
|
+
alternatives: ResolvedPlace[],
|
|
126
|
+
considered: readonly ResolvedPlace[] = []
|
|
70
127
|
): void {
|
|
128
|
+
let displaced: readonly unknown[] = []
|
|
129
|
+
|
|
71
130
|
if (node.source === "resolver") {
|
|
72
131
|
// A pass can decorate a node again with the place it already holds, and that place's stamps stay true.
|
|
73
132
|
if (node.placeID !== `wof:${resolved.id}`) {
|
|
133
|
+
displaced = [placeOfNode(node), ...(node.alternatives ?? [])].filter(isPresent)
|
|
134
|
+
|
|
74
135
|
const meta = { ...node.metadata }
|
|
75
136
|
|
|
76
137
|
for (const key of PICK_METADATA_KEYS) {
|
|
@@ -119,6 +180,12 @@ export function decorateNode(
|
|
|
119
180
|
node.metadata["resolver_prominence"] = resolved.prominence
|
|
120
181
|
}
|
|
121
182
|
|
|
183
|
+
// `resolver_score` keeps the ranking key the passes compare.
|
|
184
|
+
// This stamp records that the key is unmeasured.
|
|
185
|
+
if (resolved.scoreUnmeasured) {
|
|
186
|
+
node.metadata["resolver_score_unmeasured"] = resolved.scoreUnmeasured
|
|
187
|
+
}
|
|
188
|
+
|
|
122
189
|
if (resolved.country) {
|
|
123
190
|
node.metadata["resolver_country"] = resolved.country
|
|
124
191
|
}
|
|
@@ -165,4 +232,38 @@ export function decorateNode(
|
|
|
165
232
|
if (alternatives.length) {
|
|
166
233
|
node.alternatives = alternatives
|
|
167
234
|
}
|
|
235
|
+
|
|
236
|
+
const kept = consideredPlaces([...considered, ...displaced, ...(node.considered ?? [])], resolved.id, alternatives)
|
|
237
|
+
|
|
238
|
+
if (kept.length) {
|
|
239
|
+
node.considered = kept
|
|
240
|
+
} else {
|
|
241
|
+
delete node.considered
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The places of `candidates` that aren't the node's pick or one of its alternatives,
|
|
247
|
+
* first occurrence of each identifier kept, at most {@linkcode CONSIDERED_LIMIT}.
|
|
248
|
+
*/
|
|
249
|
+
function consideredPlaces(
|
|
250
|
+
candidates: readonly unknown[],
|
|
251
|
+
pickID: number | string,
|
|
252
|
+
alternatives: readonly ResolvedPlace[]
|
|
253
|
+
): unknown[] {
|
|
254
|
+
const seen = new Set([String(pickID), ...alternatives.map((a) => String(a.id))])
|
|
255
|
+
const kept: unknown[] = []
|
|
256
|
+
|
|
257
|
+
for (const candidate of candidates) {
|
|
258
|
+
const id = (candidate as Partial<ResolvedPlace> | null)?.id
|
|
259
|
+
|
|
260
|
+
if (id == null || seen.has(String(id))) continue
|
|
261
|
+
|
|
262
|
+
seen.add(String(id))
|
|
263
|
+
kept.push(candidate)
|
|
264
|
+
|
|
265
|
+
if (kept.length === CONSIDERED_LIMIT) break
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return kept
|
|
168
269
|
}
|
package/lib/postcode/prefix.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
3
|
|
|
4
|
+
import { postcodePrefixAt, postcodePrefixLevel } from "@mailwoman/codex/labels/postcode/prefix"
|
|
4
5
|
import { EMPTY_PLACE_FIELDS } from "@mailwoman/core/resolver"
|
|
5
6
|
import type { PostcodePrefixIndexLike, PostcodePrefixNode, ResolvedPlace } from "@mailwoman/core/resolver"
|
|
6
7
|
|
|
@@ -13,30 +14,20 @@ import type { PostcodePrefixIndexLike, PostcodePrefixNode, ResolvedPlace } from
|
|
|
13
14
|
*/
|
|
14
15
|
export type CoordinateOptionalPlace = Omit<ResolvedPlace, "lat" | "lon"> & { lat?: number; lon?: number }
|
|
15
16
|
|
|
16
|
-
const MIN_GB_OUTWARD_CODE_LENGTH = 5
|
|
17
|
-
|
|
18
|
-
const MIN_US_SECTION_CODE_LENGTH = 3
|
|
19
|
-
|
|
20
17
|
/**
|
|
21
|
-
* Derives the key a postcode prefix index uses for `code
|
|
22
|
-
*
|
|
18
|
+
* Derives the key a postcode prefix index uses for `code`, at the prefix level
|
|
19
|
+
* the codex states for `country`.
|
|
20
|
+
*
|
|
21
|
+
* White space in the code is dropped first.
|
|
23
22
|
*
|
|
24
23
|
* @returns `null` for a country with no rule or a code too short to contain a prefix.
|
|
25
24
|
*/
|
|
26
25
|
export function derivePostcodePrefix(code: string, country?: string): string | null {
|
|
27
26
|
if (!code || !country) return null
|
|
28
27
|
|
|
29
|
-
const
|
|
28
|
+
const level = postcodePrefixLevel(country)
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
case "GB":
|
|
33
|
-
return compact.length >= MIN_GB_OUTWARD_CODE_LENGTH ? compact.slice(0, -3) : null
|
|
34
|
-
|
|
35
|
-
case "US":
|
|
36
|
-
return compact.length >= MIN_US_SECTION_CODE_LENGTH ? compact.slice(0, 3) : null
|
|
37
|
-
default:
|
|
38
|
-
return null
|
|
39
|
-
}
|
|
30
|
+
return level ? postcodePrefixAt(code.replaceAll(/\s+/g, ""), level) : null
|
|
40
31
|
}
|
|
41
32
|
|
|
42
33
|
/**
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
|
|
4
4
|
import { matchCountry, matchSubdivision, matchSubdivisionsAnywhere } from "@mailwoman/codex/country"
|
|
5
5
|
import { collectNodes, POSTCODE_SHAPE_EXCLUDED_KEY, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
6
|
-
import { acceptPostcode,
|
|
6
|
+
import { acceptPostcode, statesPostcodeSyntax } from "@mailwoman/postcode/country-formats"
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Whether libaddressinput states a postcode syntax for `country`, so a span can be checked against it.
|
|
10
10
|
*/
|
|
11
11
|
function statesSyntax(country: string): boolean {
|
|
12
|
-
return
|
|
12
|
+
return statesPostcodeSyntax(country)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
package/lib/remote-resolver.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
3
|
|
|
4
4
|
import type { AddressTree } from "@mailwoman/core/decoder"
|
|
5
|
-
import { stringifyJSON } from "@mailwoman/core/json"
|
|
6
5
|
import type { GazetteerArtifactCoverage, ResolveOpts, Resolver } from "@mailwoman/core/resolver"
|
|
6
|
+
import { stringifyJSON } from "@mailwoman/core/values/json"
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Describes the `ResolveOpts` sent over the wire.
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The caller's parent place: reading it from the backend, and the lineage test a hard parent applies.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
type ParentPlaceScope,
|
|
10
|
+
type ResolvedPlace,
|
|
11
|
+
type ResolverBackend,
|
|
12
|
+
ScopeStrength,
|
|
13
|
+
} from "@mailwoman/core/resolver"
|
|
14
|
+
|
|
15
|
+
import type { ResolutionState, ResolvedParentPlace } from "#resolve/passes"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Applies the country of the caller's parent place: a hard parent's as a caller default
|
|
19
|
+
* country, a soft parent's as the soft scope when no hard scope is set.
|
|
20
|
+
*
|
|
21
|
+
* It throws when a hard parent lies outside the caller's default country.
|
|
22
|
+
*/
|
|
23
|
+
export function applyParentPlaceCountry(state: ResolutionState): void {
|
|
24
|
+
const parent = state.parentPlace
|
|
25
|
+
|
|
26
|
+
if (!parent) return
|
|
27
|
+
|
|
28
|
+
const country = parent.place.country!.toUpperCase()
|
|
29
|
+
|
|
30
|
+
if (parent.scope.strength === ScopeStrength.Hard) {
|
|
31
|
+
const stated = state.defaultCountry
|
|
32
|
+
|
|
33
|
+
if (stated?.source === "caller" && stated.country.toUpperCase() !== country) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`parent place ${parent.scope.id} (${parent.place.name}) lies in ${country}, outside the default country ${stated.country}`
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
state.defaultCountry = { country, source: parent.scope.source }
|
|
40
|
+
state.softCountry = null
|
|
41
|
+
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!state.defaultCountry) {
|
|
46
|
+
state.softCountry = { country, source: parent.scope.source, strength: ScopeStrength.Soft }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Passes the caller's parent place as the containment qualifier of a lookup the
|
|
52
|
+
* walk's own parent doesn't qualify, and reports whether it did.
|
|
53
|
+
*/
|
|
54
|
+
export function applyCallerParentQualifier(
|
|
55
|
+
query: Parameters<ResolverBackend["findPlace"]>[0],
|
|
56
|
+
state: Pick<ResolutionState, "parentPlace">
|
|
57
|
+
): boolean {
|
|
58
|
+
if (!state.parentPlace || query.parentQualifierID != null) return false
|
|
59
|
+
|
|
60
|
+
query.parentQualifierID = state.parentPlace.scope.id
|
|
61
|
+
|
|
62
|
+
return true
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Reads the parent place's record and its ancestors' ids.
|
|
67
|
+
*
|
|
68
|
+
* It throws when the backend can't look a place up by id or read lineage,
|
|
69
|
+
* when the gazetteer holds no place with the id, or when the place has no country,
|
|
70
|
+
* because a scope it can't apply would answer as if the caller had set none.
|
|
71
|
+
*/
|
|
72
|
+
export function readParentPlace(backend: ResolverBackend, scope: ParentPlaceScope): ResolvedParentPlace {
|
|
73
|
+
if (!backend.place || !backend.ancestors) {
|
|
74
|
+
throw new Error(`parent place ${scope.id}: the resolver backend can't look up a place by id and its lineage`)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const place = backend.place(scope.id)
|
|
78
|
+
|
|
79
|
+
if (!place) throw new Error(`parent place ${scope.id}: the gazetteer holds no place with this id`)
|
|
80
|
+
|
|
81
|
+
if (!place.country) throw new Error(`parent place ${scope.id} (${place.name}): the gazetteer records no country`)
|
|
82
|
+
|
|
83
|
+
const ancestorIDs = new Set(backend.ancestors(scope.id).map((ancestor) => Number(ancestor.id)))
|
|
84
|
+
|
|
85
|
+
return { scope, place, ancestorIDs }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Whether a candidate agrees with the parent's lineage: it is the parent,
|
|
90
|
+
* lies inside it, or is one of its ancestors.
|
|
91
|
+
*
|
|
92
|
+
* When the lookup's containment qualifier was the parent, a candidate the backend
|
|
93
|
+
* marked contained needs no lineage read.
|
|
94
|
+
*/
|
|
95
|
+
export function agreesWithParent(
|
|
96
|
+
candidate: Pick<ResolvedPlace, "id" | "containedByQualifier">,
|
|
97
|
+
parent: ResolvedParentPlace,
|
|
98
|
+
ancestors: NonNullable<ResolverBackend["ancestors"]>,
|
|
99
|
+
qualifierIsParent: boolean
|
|
100
|
+
): boolean {
|
|
101
|
+
const id = Number(candidate.id)
|
|
102
|
+
const parentID = parent.scope.id
|
|
103
|
+
|
|
104
|
+
if (qualifierIsParent && candidate.containedByQualifier === true) return true
|
|
105
|
+
|
|
106
|
+
if (id === parentID || parent.ancestorIDs.has(id)) return true
|
|
107
|
+
|
|
108
|
+
return ancestors(candidate.id).some((ancestor) => Number(ancestor.id) === parentID)
|
|
109
|
+
}
|