@mailwoman/resolver 11.2.0 → 11.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/admin/coherence-passes.ts +1 -1
- package/lib/admin/county-designator.ts +1 -1
- package/lib/admin/former-division.ts +116 -0
- package/lib/bare-toponym-race.ts +1 -1
- package/lib/decorate-node.ts +102 -2
- package/lib/postcode/prefix.ts +7 -16
- package/lib/remote-resolver.ts +1 -1
- package/lib/resolve/parent-place.ts +109 -0
- package/lib/resolve/passes.ts +62 -11
- package/lib/resolve/sibling-locality.ts +150 -0
- package/lib/resolve.ts +255 -42
- package/lib/scope-retrieval.ts +91 -0
- package/lib/span-rescore.ts +65 -14
- package/lib/street/tier.ts +5 -8
- package/lib/toponym-prior.ts +57 -6
- package/out/admin/coherence-passes.js +1 -1
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/county-designator.js +1 -1
- package/out/admin/former-division.d.ts +47 -0
- package/out/admin/former-division.d.ts.map +1 -0
- package/out/admin/former-division.js +74 -0
- package/out/admin/former-division.js.map +1 -0
- package/out/bare-toponym-race.d.ts +1 -1
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/decorate-node.d.ts +22 -3
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +84 -1
- package/out/decorate-node.js.map +1 -1
- package/out/postcode/prefix.d.ts +4 -2
- package/out/postcode/prefix.d.ts.map +1 -1
- package/out/postcode/prefix.js +7 -13
- package/out/postcode/prefix.js.map +1 -1
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +1 -1
- package/out/remote-resolver.js.map +1 -1
- package/out/resolve/parent-place.d.ts +34 -0
- package/out/resolve/parent-place.d.ts.map +1 -0
- package/out/resolve/parent-place.js +76 -0
- package/out/resolve/parent-place.js.map +1 -0
- package/out/resolve/passes.d.ts +39 -8
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +17 -6
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve/sibling-locality.d.ts +34 -0
- package/out/resolve/sibling-locality.d.ts.map +1 -0
- package/out/resolve/sibling-locality.js +95 -0
- package/out/resolve/sibling-locality.js.map +1 -0
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +177 -39
- package/out/resolve.js.map +1 -1
- package/out/scope-retrieval.d.ts +44 -0
- package/out/scope-retrieval.d.ts.map +1 -0
- package/out/scope-retrieval.js +52 -0
- package/out/scope-retrieval.js.map +1 -0
- package/out/span-rescore.d.ts +15 -1
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +36 -9
- package/out/span-rescore.js.map +1 -1
- package/out/street/tier.d.ts +5 -1
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +5 -7
- package/out/street/tier.js.map +1 -1
- package/out/toponym-prior.d.ts +15 -0
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +37 -6
- package/out/toponym-prior.js.map +1 -1
- package/package.json +9 -9
|
@@ -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
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* `subregion` to the WOF `county` placetype, and the county's row stores the same name.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { countyDesignatorsFor, countyNameWithoutDesignator } from "@mailwoman/codex/county
|
|
12
|
+
import { countyDesignatorsFor, countyNameWithoutDesignator } from "@mailwoman/codex/designators/county"
|
|
13
13
|
import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -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",
|
|
@@ -53,8 +55,57 @@ export const PICK_METADATA_KEYS = [
|
|
|
53
55
|
"parent_fallback",
|
|
54
56
|
"variant_alias_exemption",
|
|
55
57
|
"county_designator_fallback",
|
|
58
|
+
"sibling_locality_containment",
|
|
56
59
|
] as const
|
|
57
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
|
+
|
|
58
109
|
/**
|
|
59
110
|
* Stamp a node with resolver-supplied attribution, displacing any prior classifier `source`
|
|
60
111
|
* / `sourceID` into `metadata.classifier_source` / `metadata.classifier_source_id`
|
|
@@ -63,15 +114,24 @@ export const PICK_METADATA_KEYS = [
|
|
|
63
114
|
* A node the resolver already decorated keeps its classifier stamps as the first decoration wrote them.
|
|
64
115
|
* When the new place differs from the one the node holds, the {@linkcode PICK_METADATA_KEYS}
|
|
65
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.
|
|
66
121
|
*/
|
|
67
122
|
export function decorateNode(
|
|
68
123
|
node: AddressNode,
|
|
69
124
|
resolved: CoordinateOptionalPlace,
|
|
70
|
-
alternatives: ResolvedPlace[]
|
|
125
|
+
alternatives: ResolvedPlace[],
|
|
126
|
+
considered: readonly ResolvedPlace[] = []
|
|
71
127
|
): void {
|
|
128
|
+
let displaced: readonly unknown[] = []
|
|
129
|
+
|
|
72
130
|
if (node.source === "resolver") {
|
|
73
131
|
// A pass can decorate a node again with the place it already holds, and that place's stamps stay true.
|
|
74
132
|
if (node.placeID !== `wof:${resolved.id}`) {
|
|
133
|
+
displaced = [placeOfNode(node), ...(node.alternatives ?? [])].filter(isPresent)
|
|
134
|
+
|
|
75
135
|
const meta = { ...node.metadata }
|
|
76
136
|
|
|
77
137
|
for (const key of PICK_METADATA_KEYS) {
|
|
@@ -120,6 +180,12 @@ export function decorateNode(
|
|
|
120
180
|
node.metadata["resolver_prominence"] = resolved.prominence
|
|
121
181
|
}
|
|
122
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
|
+
|
|
123
189
|
if (resolved.country) {
|
|
124
190
|
node.metadata["resolver_country"] = resolved.country
|
|
125
191
|
}
|
|
@@ -166,4 +232,38 @@ export function decorateNode(
|
|
|
166
232
|
if (alternatives.length) {
|
|
167
233
|
node.alternatives = alternatives
|
|
168
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
|
|
169
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
|
/**
|
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
|
+
}
|
package/lib/resolve/passes.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
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 type { PlacetypeMap } from "@mailwoman/codex/placetype-map"
|
|
4
|
+
import type { PlacetypeMap } from "@mailwoman/codex/labels/placetype-map"
|
|
5
5
|
import { isShapeExcludedPostcode, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
6
6
|
import {
|
|
7
7
|
type DefaultCountry,
|
|
8
8
|
EMPTY_PLACE_FIELDS,
|
|
9
|
+
type FormerDivisionLookup,
|
|
10
|
+
type FormerDivisionMatch,
|
|
11
|
+
hardCountryOf,
|
|
12
|
+
type ParentPlaceScope,
|
|
9
13
|
type ResolveCandidateTrace,
|
|
10
14
|
type ResolveNodeTrace,
|
|
11
15
|
type PostcodePrefixIndexLike,
|
|
@@ -13,11 +17,12 @@ import {
|
|
|
13
17
|
type ResolveOpts,
|
|
14
18
|
type ResolverBackend,
|
|
15
19
|
type ResolveSwitches,
|
|
20
|
+
ScopeStrength,
|
|
16
21
|
} from "@mailwoman/core/resolver"
|
|
17
22
|
import { PLACETYPE_SPECIFICITY } from "@mailwoman/gazetteer/whosonfirst/placetypes/specificity"
|
|
18
23
|
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
19
24
|
|
|
20
|
-
import { decorateNode, isResolvedWithCoord } from "#decorate-node"
|
|
25
|
+
import { decorateNode, isResolvedWithCoord, placeOfNode } from "#decorate-node"
|
|
21
26
|
import { findRescoreCandidate, hasResolvedPlace, postcodeCodeSubset } from "#span-rescore"
|
|
22
27
|
|
|
23
28
|
const TRACE_CANDIDATE_CAP = 10
|
|
@@ -134,6 +139,7 @@ export function createNodeTraceRecorder(sink: (record: ResolveNodeTrace) => void
|
|
|
134
139
|
country: c.country,
|
|
135
140
|
placetype: c.placetype,
|
|
136
141
|
score: c.score,
|
|
142
|
+
scoreUnmeasured: c.scoreUnmeasured ?? null,
|
|
137
143
|
prominence: c.prominence ?? null,
|
|
138
144
|
importance: c.importance ?? null,
|
|
139
145
|
population: c.population ?? null,
|
|
@@ -167,6 +173,15 @@ export function createNodeTraceRecorder(sink: (record: ResolveNodeTrace) => void
|
|
|
167
173
|
/**
|
|
168
174
|
* The options and mutable state that one `resolveTree` call passes through its tree walk.
|
|
169
175
|
*/
|
|
176
|
+
/**
|
|
177
|
+
* A caller's parent place as the walk reads it: the scope, the place's record, and its ancestors' ids.
|
|
178
|
+
*/
|
|
179
|
+
export interface ResolvedParentPlace {
|
|
180
|
+
scope: ParentPlaceScope
|
|
181
|
+
place: ResolvedPlace
|
|
182
|
+
ancestorIDs: ReadonlySet<number>
|
|
183
|
+
}
|
|
184
|
+
|
|
170
185
|
export interface ResolutionState {
|
|
171
186
|
lookupsRemaining: number
|
|
172
187
|
placetypeMap: PlacetypeMap
|
|
@@ -267,15 +282,28 @@ export interface ResolutionState {
|
|
|
267
282
|
diagnoseUnreachable?: boolean
|
|
268
283
|
|
|
269
284
|
/**
|
|
270
|
-
* The
|
|
271
|
-
*
|
|
285
|
+
* The soft country scope: a prior on region and locality candidates that applies
|
|
286
|
+
* while {@link ResolutionState.defaultCountry} is null and no anchor posterior applies.
|
|
287
|
+
*
|
|
288
|
+
* {@link ResolutionState.defaultCountry} holds only a hard scope.
|
|
289
|
+
*/
|
|
290
|
+
softCountry: DefaultCountry | null
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The weight of {@link ResolutionState.softCountry}, in log10-population units.
|
|
272
294
|
*/
|
|
273
|
-
|
|
295
|
+
softCountryWeight: number
|
|
274
296
|
|
|
275
297
|
/**
|
|
276
|
-
*
|
|
298
|
+
* Whether a soft scope acts inside retrieval, and a hard default country
|
|
299
|
+
* probes for the places it excluded.
|
|
277
300
|
*/
|
|
278
|
-
|
|
301
|
+
scopeCountryRetrieval: boolean
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* The caller's parent place with its record and the ids of its ancestors, or null.
|
|
305
|
+
*/
|
|
306
|
+
parentPlace: ResolvedParentPlace | null
|
|
279
307
|
|
|
280
308
|
/**
|
|
281
309
|
* Returns a candidate's capital level for bounded capital promotion.
|
|
@@ -311,6 +339,17 @@ export interface ResolutionState {
|
|
|
311
339
|
*/
|
|
312
340
|
countyDesignatorFallback: boolean
|
|
313
341
|
|
|
342
|
+
/**
|
|
343
|
+
* The former divisions a `subregion` or `region` value is checked against when no
|
|
344
|
+
* current division answers it, present only under the `formerDivisions` switch.
|
|
345
|
+
*/
|
|
346
|
+
formerDivisionLookup?: FormerDivisionLookup
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* The walked nodes no current division answered, with the former divisions their values name.
|
|
350
|
+
*/
|
|
351
|
+
pendingFormerDivisions?: Map<AddressNode, readonly FormerDivisionMatch[]>
|
|
352
|
+
|
|
314
353
|
/**
|
|
315
354
|
* The tree's first region value.
|
|
316
355
|
*
|
|
@@ -340,24 +379,36 @@ export function firstPostcodeValue(roots: readonly AddressNode[]): string | null
|
|
|
340
379
|
* appending a resolved `locality` node on a hit.
|
|
341
380
|
*
|
|
342
381
|
* When no span matches, it tries the postcode node's code part.
|
|
382
|
+
*
|
|
383
|
+
* A hard default country filters the probes, and a bare whole-input span reads it as a prior.
|
|
384
|
+
* `softCountry` is a soft scope that acts inside retrieval: span probes read it only as a prior.
|
|
343
385
|
*/
|
|
344
386
|
export async function applySpanRescore(
|
|
345
387
|
roots: AddressNode[],
|
|
346
388
|
raw: string,
|
|
347
389
|
backend: ResolverBackend,
|
|
348
|
-
opts: ResolvedOpts
|
|
390
|
+
opts: ResolvedOpts,
|
|
391
|
+
softCountry: string | null = null
|
|
349
392
|
): Promise<void> {
|
|
350
393
|
if (hasResolvedPlace(roots, opts.spanRescoreWeakResolution ?? false)) return
|
|
351
394
|
|
|
395
|
+
const hardCountry = hardCountryOf(opts.defaultCountry)
|
|
396
|
+
|
|
352
397
|
let hit
|
|
353
398
|
|
|
354
399
|
try {
|
|
355
400
|
hit = await findRescoreCandidate(raw, roots, backend, {
|
|
356
|
-
|
|
401
|
+
...(hardCountry
|
|
402
|
+
? { country: hardCountry }
|
|
403
|
+
: softCountry
|
|
404
|
+
? { country: softCountry, countryStrength: ScopeStrength.Soft }
|
|
405
|
+
: {}),
|
|
406
|
+
scopeCountryRetrieval: opts.scopeCountryRetrieval,
|
|
357
407
|
postcode: firstPostcodeValue(roots),
|
|
358
408
|
thresholdKm: opts.spanRescoreThresholdKm,
|
|
359
409
|
|
|
360
410
|
postalCompoundRecovery: opts.postalCompoundRecovery,
|
|
411
|
+
...(opts.softCountryWeight != null ? { bareToponymCountryWeight: opts.softCountryWeight } : {}),
|
|
361
412
|
...(opts.isVenueCategoryWord ? { isVenueCategoryWord: opts.isVenueCategoryWord } : {}),
|
|
362
413
|
})
|
|
363
414
|
} catch {
|
|
@@ -366,7 +417,7 @@ export async function applySpanRescore(
|
|
|
366
417
|
|
|
367
418
|
if (!hit && opts.postalCompoundRecovery) {
|
|
368
419
|
try {
|
|
369
|
-
await recoverPostcodeNode(roots, backend,
|
|
420
|
+
await recoverPostcodeNode(roots, backend, hardCountry, opts.traceSink)
|
|
370
421
|
} catch {}
|
|
371
422
|
}
|
|
372
423
|
|
|
@@ -491,7 +542,7 @@ async function reconcileLocalitiesToAnchor(
|
|
|
491
542
|
}
|
|
492
543
|
|
|
493
544
|
if (reconciling) {
|
|
494
|
-
const displaced: ResolvedPlace = {
|
|
545
|
+
const displaced: ResolvedPlace = placeOfNode(node) ?? {
|
|
495
546
|
...EMPTY_PLACE_FIELDS,
|
|
496
547
|
id: 0,
|
|
497
548
|
name: String(node.metadata?.["resolver_name"] ?? node.value),
|