@mailwoman/resolver 11.5.0 → 12.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/lib/bare-toponym-race.ts +31 -0
- package/lib/decorate-node.ts +6 -0
- package/lib/outcome/fetch.ts +82 -0
- package/lib/outcome/ordering.ts +224 -0
- package/lib/outcome/span.ts +106 -0
- package/lib/outcome/walk.ts +132 -0
- package/lib/resolve/landmark.ts +57 -8
- package/lib/resolve/passes.ts +14 -0
- package/lib/resolve/priors.ts +123 -0
- package/lib/resolve/region-city.ts +53 -11
- package/lib/resolve.ts +152 -170
- package/out/bare-toponym-race.d.ts +8 -0
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/bare-toponym-race.js +27 -0
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts +1 -1
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +5 -0
- package/out/decorate-node.js.map +1 -1
- package/out/outcome/fetch.d.ts +39 -0
- package/out/outcome/fetch.d.ts.map +1 -0
- package/out/outcome/fetch.js +59 -0
- package/out/outcome/fetch.js.map +1 -0
- package/out/outcome/ordering.d.ts +56 -0
- package/out/outcome/ordering.d.ts.map +1 -0
- package/out/outcome/ordering.js +152 -0
- package/out/outcome/ordering.js.map +1 -0
- package/out/outcome/span.d.ts +52 -0
- package/out/outcome/span.d.ts.map +1 -0
- package/out/outcome/span.js +66 -0
- package/out/outcome/span.js.map +1 -0
- package/out/outcome/walk.d.ts +42 -0
- package/out/outcome/walk.d.ts.map +1 -0
- package/out/outcome/walk.js +83 -0
- package/out/outcome/walk.js.map +1 -0
- package/out/resolve/landmark.d.ts +14 -2
- package/out/resolve/landmark.d.ts.map +1 -1
- package/out/resolve/landmark.js +47 -8
- package/out/resolve/landmark.js.map +1 -1
- package/out/resolve/passes.d.ts +13 -1
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve/priors.d.ts +17 -0
- package/out/resolve/priors.d.ts.map +1 -0
- package/out/resolve/priors.js +79 -0
- package/out/resolve/priors.js.map +1 -0
- package/out/resolve/region-city.d.ts +7 -0
- package/out/resolve/region-city.d.ts.map +1 -1
- package/out/resolve/region-city.js +45 -10
- package/out/resolve/region-city.js.map +1 -1
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +113 -122
- package/out/resolve.js.map +1 -1
- package/package.json +10 -10
package/lib/resolve/landmark.ts
CHANGED
|
@@ -26,12 +26,15 @@ import {
|
|
|
26
26
|
type LandmarkLookup,
|
|
27
27
|
type ResolvedPlace,
|
|
28
28
|
} from "@mailwoman/core/resolver"
|
|
29
|
+
import { OutcomeLayer, type ResolveOutcome } from "@mailwoman/core/resolver/outcome"
|
|
29
30
|
import { isCountryAirportCollision } from "@mailwoman/registry/overlap/country-airport"
|
|
30
31
|
import { isLatLonBounds, latLonBoundsContain } from "@mailwoman/spatial/bbox"
|
|
31
32
|
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
32
33
|
|
|
33
34
|
import { decorateNode } from "#decorate-node"
|
|
34
35
|
import { foldName } from "#fold-name"
|
|
36
|
+
import { findWithOutcome, OUTCOME_SET_KEY, OUTCOME_TARGET_KEY, outcomeOrderOf } from "#outcome/fetch"
|
|
37
|
+
import { orderWithinLayerByOutcome } from "#outcome/ordering"
|
|
35
38
|
import { writtenCountryCode } from "#resolve/country-code"
|
|
36
39
|
|
|
37
40
|
/**
|
|
@@ -40,6 +43,13 @@ import { writtenCountryCode } from "#resolve/country-code"
|
|
|
40
43
|
*/
|
|
41
44
|
const ADDRESS_TAGS: ReadonlySet<string> = new Set(["house_number", "postcode", "unit", "po_box"])
|
|
42
45
|
|
|
46
|
+
/**
|
|
47
|
+
* The admin-qualifier tags that mark a query as qualified rather than bare.
|
|
48
|
+
*
|
|
49
|
+
* A foreign-script admin match yields to a landmark only on a bare query.
|
|
50
|
+
*/
|
|
51
|
+
const QUALIFIER_TAGS: ReadonlySet<string> = new Set(["region", "subregion", "country"])
|
|
52
|
+
|
|
43
53
|
/**
|
|
44
54
|
* The namesakes a landmark answer lists: OpenStreetMap can hold hundreds of parks under one name.
|
|
45
55
|
*/
|
|
@@ -48,7 +58,7 @@ const LISTED_ALTERNATIVES = 8
|
|
|
48
58
|
/**
|
|
49
59
|
* The farthest a landmark may lie from a locality the input states, matching span rescore's postcode radius.
|
|
50
60
|
*/
|
|
51
|
-
const STATED_LOCALITY_RADIUS_KM = 50
|
|
61
|
+
export const STATED_LOCALITY_RADIUS_KM = 50
|
|
52
62
|
|
|
53
63
|
/**
|
|
54
64
|
* Keeps the hits an input's stated context permits: its country, and the surroundings of its locality.
|
|
@@ -80,8 +90,15 @@ function hitsInContext(hits: readonly LandmarkHit[], context: readonly AddressNo
|
|
|
80
90
|
: kept
|
|
81
91
|
}
|
|
82
92
|
|
|
83
|
-
|
|
93
|
+
/**
|
|
94
|
+
* The place a landmark hit answers.
|
|
95
|
+
*
|
|
96
|
+
* A place from a hit read with an outcome query also holds its outcome set,
|
|
97
|
+
* its data layer, its Wikidata item and its comparable prominence.
|
|
98
|
+
*/
|
|
99
|
+
export function placeOfLandmark(hit: LandmarkHit): ResolvedPlace {
|
|
84
100
|
const prominence = hit.importance ?? Math.log10(hit.sitelinks + 1)
|
|
101
|
+
const fromOSM = hit.qid.startsWith(OSM_LANDMARK_PREFIX)
|
|
85
102
|
|
|
86
103
|
return {
|
|
87
104
|
...EMPTY_PLACE_FIELDS,
|
|
@@ -95,6 +112,15 @@ function placeOf(hit: LandmarkHit): ResolvedPlace {
|
|
|
95
112
|
score: prominence,
|
|
96
113
|
prominence,
|
|
97
114
|
exactMatch: true,
|
|
115
|
+
...(hit.outcomes
|
|
116
|
+
? {
|
|
117
|
+
outcomes: hit.outcomes,
|
|
118
|
+
layer: fromOSM ? OutcomeLayer.OpenStreetMap : OutcomeLayer.Landmarks,
|
|
119
|
+
wikidata: fromOSM ? null : hit.qid,
|
|
120
|
+
// An OpenStreetMap place has no Wikipedia edition count, so its `s(e)` is unknown.
|
|
121
|
+
sitelinkProminence: fromOSM ? null : Math.log10(hit.sitelinks + 1),
|
|
122
|
+
}
|
|
123
|
+
: {}),
|
|
98
124
|
}
|
|
99
125
|
}
|
|
100
126
|
|
|
@@ -126,7 +152,7 @@ function listCodeHoldersOnCountry(roots: AddressNode[], code: string, hits: read
|
|
|
126
152
|
|
|
127
153
|
const listed = (country.alternatives as ResolvedPlace[] | undefined) ?? []
|
|
128
154
|
|
|
129
|
-
country.alternatives = [...listed, ...holders.map(
|
|
155
|
+
country.alternatives = [...listed, ...holders.map(placeOfLandmark)]
|
|
130
156
|
}
|
|
131
157
|
|
|
132
158
|
/**
|
|
@@ -135,16 +161,24 @@ function listCodeHoldersOnCountry(roots: AddressNode[], code: string, hits: read
|
|
|
135
161
|
* The first comma-separated part is the name, and any other part is context that has to have resolved.
|
|
136
162
|
* A settlement that leads the name stays with the admin ladder, because the ladder ranks settlements.
|
|
137
163
|
*/
|
|
138
|
-
export async function applyLandmark(
|
|
164
|
+
export async function applyLandmark(
|
|
165
|
+
roots: AddressNode[],
|
|
166
|
+
raw: string,
|
|
167
|
+
landmarks: LandmarkLookup,
|
|
168
|
+
outcome: ResolveOutcome | null = null
|
|
169
|
+
): Promise<void> {
|
|
139
170
|
const nodes = [...walkNodes(roots)]
|
|
140
171
|
|
|
141
172
|
if (nodes.some((node) => ADDRESS_TAGS.has(node.tag))) return
|
|
142
173
|
|
|
174
|
+
// The walk already ordered the span across the data layers.
|
|
175
|
+
if (outcome && nodes.some((node) => node.metadata?.[OUTCOME_TARGET_KEY] === true && node.placeID)) return
|
|
176
|
+
|
|
143
177
|
const name = raw.split(",", 1)[0]!.trim()
|
|
144
178
|
|
|
145
179
|
if (!name) return
|
|
146
180
|
|
|
147
|
-
const hits = await landmarks.find(name)
|
|
181
|
+
const hits = outcome ? await findWithOutcome(landmarks, name, outcome) : await landmarks.find(name)
|
|
148
182
|
const hasContext = raw.trim().length > name.length
|
|
149
183
|
|
|
150
184
|
// A bare country code answers the country, and the airport holding the same code is listed beside it.
|
|
@@ -163,14 +197,25 @@ export async function applyLandmark(roots: AddressNode[], raw: string, landmarks
|
|
|
163
197
|
|
|
164
198
|
if (hasContext && !context.length) return
|
|
165
199
|
|
|
166
|
-
|
|
200
|
+
// The stated context already removed the hits it doesn't admit, so the outcome order runs over one class.
|
|
201
|
+
const inContext = hasContext ? hitsInContext(hits, context) : hits
|
|
202
|
+
const candidates = outcome ? orderWithinLayerByOutcome(inContext, outcomeOrderOf(outcome)) : inContext
|
|
167
203
|
const leader = candidates[0]
|
|
168
204
|
|
|
169
205
|
if (!leader || leader.kind !== "venue") return
|
|
170
206
|
|
|
171
207
|
// A translation or alias is the weakest claim on a name, so it answers only where the
|
|
172
208
|
// gazetteer holds none: `Hamburg` is an alias of Hamburg Airport and the name of a city.
|
|
209
|
+
// On a bare (unqualified) query, an admin match reached through `name_role ===
|
|
210
|
+
// "foreign-script"` doesn't block the landmark answer, so `Фавор` reaches Mount Tabor in
|
|
211
|
+
// Israel rather than Mount Tabor, Vermont, while a qualified query such as
|
|
212
|
+
// `הר תבור, ורמונט` keeps its admin match.
|
|
173
213
|
const resolved = roots.filter((root) => typeof root.placeID === "string")
|
|
214
|
+
const hasQualifier = hasContext || nodes.some((node) => QUALIFIER_TAGS.has(node.tag))
|
|
215
|
+
|
|
216
|
+
const isForeignScriptAdmin = (node: AddressNode): boolean =>
|
|
217
|
+
node.metadata?.["resolver_name_role"] === "foreign-script"
|
|
218
|
+
|
|
174
219
|
const fromOSM = leader.qid.startsWith(OSM_LANDMARK_PREFIX)
|
|
175
220
|
|
|
176
221
|
// The parser reads `Wall Street` and `Times Square` as streets, and `Central Park` as a venue.
|
|
@@ -198,13 +243,16 @@ export async function applyLandmark(roots: AddressNode[], raw: string, landmarks
|
|
|
198
243
|
// in South Carolina yields to the park in Atlanta.
|
|
199
244
|
const holdsName = (node: AddressNode) =>
|
|
200
245
|
typeof node.placeID === "string" &&
|
|
246
|
+
(hasQualifier || !isForeignScriptAdmin(node)) &&
|
|
201
247
|
foldName(node.value) === foldName(name) &&
|
|
202
248
|
(!readsAsPark || Number(node.metadata?.["resolver_score"] ?? 0) > 0)
|
|
203
249
|
|
|
204
250
|
if (nodes.some(holdsName)) return
|
|
205
251
|
} else {
|
|
206
252
|
const nameResolved = resolved.some(
|
|
207
|
-
(root) =>
|
|
253
|
+
(root) =>
|
|
254
|
+
(hasQualifier || !isForeignScriptAdmin(root)) &&
|
|
255
|
+
(root.start < nameEnd || root.value.toLowerCase() === name.toLowerCase())
|
|
208
256
|
)
|
|
209
257
|
|
|
210
258
|
// `Baker Street` is an alias of the tube station, and the street keeps its name.
|
|
@@ -221,13 +269,14 @@ export async function applyLandmark(roots: AddressNode[], raw: string, landmarks
|
|
|
221
269
|
children: [],
|
|
222
270
|
}
|
|
223
271
|
|
|
224
|
-
decorateNode(node,
|
|
272
|
+
decorateNode(node, placeOfLandmark(leader), candidates.slice(1, LISTED_ALTERNATIVES + 1).map(placeOfLandmark))
|
|
225
273
|
node.placeID = landmarkPlaceID(leader.qid)
|
|
226
274
|
|
|
227
275
|
node.metadata = {
|
|
228
276
|
...node.metadata,
|
|
229
277
|
landmark_sitelinks: leader.sitelinks,
|
|
230
278
|
...(fromOSM ? { landmark_source: "openstreetmap:planet" } : {}),
|
|
279
|
+
...(outcome ? { [OUTCOME_TARGET_KEY]: true, [OUTCOME_SET_KEY]: leader.outcomes ?? [] } : {}),
|
|
231
280
|
}
|
|
232
281
|
|
|
233
282
|
roots.splice(0, roots.length, node, ...context)
|
package/lib/resolve/passes.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type FormerDivisionLookup,
|
|
10
10
|
type FormerDivisionMatch,
|
|
11
11
|
hardCountryOf,
|
|
12
|
+
type LandmarkLookup,
|
|
12
13
|
type ParentPlaceScope,
|
|
13
14
|
type ResolveCandidateTrace,
|
|
14
15
|
type ResolveNodeTrace,
|
|
@@ -19,6 +20,7 @@ import {
|
|
|
19
20
|
type ResolveSwitches,
|
|
20
21
|
ScopeStrength,
|
|
21
22
|
} from "@mailwoman/core/resolver"
|
|
23
|
+
import type { ResolveOutcome } from "@mailwoman/core/resolver/outcome"
|
|
22
24
|
import { PLACETYPE_SPECIFICITY } from "@mailwoman/gazetteer/whosonfirst/placetypes/specificity"
|
|
23
25
|
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
24
26
|
|
|
@@ -273,6 +275,18 @@ export interface ResolutionState {
|
|
|
273
275
|
*/
|
|
274
276
|
traceSink?: ((record: ResolveNodeTrace) => void) | null
|
|
275
277
|
|
|
278
|
+
/**
|
|
279
|
+
* The request's outcome hint, filter and head order, and the span they apply to,
|
|
280
|
+
* with the landmark layers that span also reads.
|
|
281
|
+
*
|
|
282
|
+
* Absent, every lookup runs today's sequence.
|
|
283
|
+
*/
|
|
284
|
+
outcome?: {
|
|
285
|
+
request: ResolveOutcome
|
|
286
|
+
target: AddressNode
|
|
287
|
+
layers: readonly LandmarkLookup[]
|
|
288
|
+
}
|
|
289
|
+
|
|
276
290
|
/**
|
|
277
291
|
* Whether an empty lookup re-probes the other admin bands and traces which ones hold the value.
|
|
278
292
|
*
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Pass 3 of a lookup: the postcode anchor posterior, or the locale prior, importance and capitals.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { AddressNode } from "@mailwoman/core/decoder"
|
|
9
|
+
import type { ResolvedPlace } from "@mailwoman/core/resolver"
|
|
10
|
+
|
|
11
|
+
import type { NodeTraceRecorder, ResolutionState } from "#resolve/passes"
|
|
12
|
+
import { promoteSoftCountry, softScopeRetrieves } from "#scope-retrieval"
|
|
13
|
+
import { type CapitalLevelFn, promoteCapitals, rankByCountryPrior, rankByImportance } from "#toponym-prior"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Ranks a lookup's candidates by its priors in order: the postcode anchor posterior,
|
|
17
|
+
* the ranking-only soft scope, importance with capital promotion, then the retrieval soft scope.
|
|
18
|
+
*
|
|
19
|
+
* `promoted` is the place the retrieval soft scope moved to the lead, or null.
|
|
20
|
+
*/
|
|
21
|
+
export function rankByPriors(
|
|
22
|
+
candidates: ResolvedPlace[],
|
|
23
|
+
anchorEligible: boolean,
|
|
24
|
+
softCountry: string | null,
|
|
25
|
+
node: AddressNode,
|
|
26
|
+
state: ResolutionState,
|
|
27
|
+
rec: NodeTraceRecorder
|
|
28
|
+
): { ranked: ResolvedPlace[]; promoted: ResolvedPlace | null } {
|
|
29
|
+
let ranked = candidates
|
|
30
|
+
|
|
31
|
+
// Optional anchor-based rerank using postcode country posterior, kept tier-safe by exact-match first.
|
|
32
|
+
if (state.anchorPosterior && anchorEligible && candidates.length > 1) {
|
|
33
|
+
ranked = rankByAnchorPosterior(candidates, state.anchorPosterior, state.anchorWeight)
|
|
34
|
+
rec.stage("anchor", ranked)
|
|
35
|
+
ranked = rankWithinPosteriorCountry(ranked, state.capitalLevel, node)
|
|
36
|
+
rec.stage("importance", ranked)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Without scope retrieval, the soft scope adds to candidate scores ahead of the importance rerank.
|
|
40
|
+
if (softCountry && !softScopeRetrieves(state)) {
|
|
41
|
+
ranked = rankByCountryPrior(ranked, softCountry, state.softCountryWeight)
|
|
42
|
+
rec.stage("locale_prior", ranked)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Importance-first rerank (the posterior branch above runs its own, within the winning country).
|
|
46
|
+
if (!state.anchorPosterior) {
|
|
47
|
+
ranked = rankByImportance(ranked)
|
|
48
|
+
rec.stage("importance", ranked)
|
|
49
|
+
|
|
50
|
+
// Optional bounded capital promotion.
|
|
51
|
+
if (state.capitalLevel) {
|
|
52
|
+
ranked = promoteCapitalsWithReceipt(ranked, state.capitalLevel, node)
|
|
53
|
+
rec.stage("capital", ranked)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return promoteSoftCountry(ranked, softCountry, state, rec)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Sorts candidates by the postcode country posterior, exact matches first.
|
|
62
|
+
*
|
|
63
|
+
* Within a match tier, the key is a candidate's prominence (its score when it has none)
|
|
64
|
+
* plus `weight` times its country's posterior mass.
|
|
65
|
+
* A candidate with no country gets no posterior mass.
|
|
66
|
+
*/
|
|
67
|
+
function rankByAnchorPosterior<
|
|
68
|
+
T extends Pick<ResolvedPlace, "score" | "country"> & Partial<Pick<ResolvedPlace, "exactMatch" | "prominence">>,
|
|
69
|
+
>(candidates: readonly T[], posterior: Readonly<Record<string, number>>, weight: number): T[] {
|
|
70
|
+
const key = (c: T) => (c.prominence ?? c.score) + weight * (c.country ? (posterior[c.country] ?? 0) : 0)
|
|
71
|
+
|
|
72
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array. toSorted would double-allocate on a hot path
|
|
73
|
+
return [...candidates].sort(
|
|
74
|
+
(a, b) => Number(b.exactMatch ?? false) - Number(a.exactMatch ?? false) || key(b) - key(a) || b.score - a.score
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Reorders the posterior-ranked leader's peers, the candidates in its country and exact-match
|
|
80
|
+
* tier, by importance and capital promotion, as the path without a posterior ranks them.
|
|
81
|
+
*
|
|
82
|
+
* The posterior decides the country.
|
|
83
|
+
* Within that country this order makes a venue query (`Clayton Post Office, Clayton, WI`)
|
|
84
|
+
* pick the same locality as its bare form (`Clayton, WI`).
|
|
85
|
+
*/
|
|
86
|
+
function rankWithinPosteriorCountry<
|
|
87
|
+
T extends Pick<ResolvedPlace, "score" | "name" | "country" | "lat" | "lon"> &
|
|
88
|
+
Partial<Pick<ResolvedPlace, "exactMatch" | "importance" | "prominence">>,
|
|
89
|
+
>(
|
|
90
|
+
ranked: readonly T[],
|
|
91
|
+
capitalLevel: CapitalLevelFn | null | undefined,
|
|
92
|
+
node: { metadata?: Record<string, unknown> | null }
|
|
93
|
+
): T[] {
|
|
94
|
+
const leader = ranked[0]
|
|
95
|
+
|
|
96
|
+
if (!leader?.country) return [...ranked]
|
|
97
|
+
|
|
98
|
+
const isPeer = (c: T) => c.country === leader.country && (c.exactMatch === true) === (leader.exactMatch === true)
|
|
99
|
+
let peers = rankByImportance(ranked.filter(isPeer))
|
|
100
|
+
|
|
101
|
+
if (capitalLevel) {
|
|
102
|
+
peers = promoteCapitalsWithReceipt(peers, capitalLevel, node)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return [...peers, ...ranked.filter((c) => !isPeer(c))]
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Promote capitals and stamp `capital_promotion` when the leader changes.
|
|
110
|
+
* Metadata only.
|
|
111
|
+
*/
|
|
112
|
+
function promoteCapitalsWithReceipt<
|
|
113
|
+
T extends Pick<ResolvedPlace, "score" | "name" | "country" | "lat" | "lon"> &
|
|
114
|
+
Partial<Pick<ResolvedPlace, "exactMatch" | "importance" | "prominence">>,
|
|
115
|
+
>(ranked: readonly T[], level: CapitalLevelFn, node: { metadata?: Record<string, unknown> | null }): T[] {
|
|
116
|
+
const promoted = promoteCapitals(ranked, level)
|
|
117
|
+
|
|
118
|
+
if (promoted[0] !== ranked[0]) {
|
|
119
|
+
node.metadata = { ...node.metadata, capital_promotion: promoted[0]!.country ?? true }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return promoted
|
|
123
|
+
}
|
|
@@ -5,6 +5,7 @@ import { walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
|
5
5
|
import type { ResolverBackend } from "@mailwoman/core/resolver"
|
|
6
6
|
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
7
7
|
|
|
8
|
+
import { scopedSublocalityCandidates } from "#bare-toponym-race"
|
|
8
9
|
import { decorateNode } from "#decorate-node"
|
|
9
10
|
import { streetNumberPairs } from "#street/tier"
|
|
10
11
|
|
|
@@ -212,14 +213,11 @@ export async function applyStreetAsTown(roots: AddressNode[], backend: ResolverB
|
|
|
212
213
|
}
|
|
213
214
|
|
|
214
215
|
/**
|
|
215
|
-
*
|
|
216
|
-
* and the country holds a town of that name.
|
|
216
|
+
* The unresolved venue, country code, and slash-split forms for a venue beside a country.
|
|
217
217
|
*
|
|
218
|
-
*
|
|
219
|
-
* This runs ahead of the landmark layer, so an airport alias under `Biggin Hill`
|
|
220
|
-
* yields to the town that holds the name.
|
|
218
|
+
* Returns null when any other node is present or the venue already resolved.
|
|
221
219
|
*/
|
|
222
|
-
|
|
220
|
+
function venueCountryForms(roots: AddressNode[]): { venue: AddressNode; country: string; forms: string[] } | null {
|
|
223
221
|
const nodes = [...walkNodes(roots)]
|
|
224
222
|
const venue = nodes.find((node) => node.tag === "venue" && !node.placeID && node.value.trim())
|
|
225
223
|
|
|
@@ -227,20 +225,36 @@ export async function applyVenueAsTown(roots: AddressNode[], backend: ResolverBa
|
|
|
227
225
|
(node) => node.tag === "country" && typeof node.metadata?.["resolver_country"] === "string"
|
|
228
226
|
)
|
|
229
227
|
|
|
230
|
-
if (!venue || !country || nodes.some((node) => node !== venue && node !== country)) return
|
|
228
|
+
if (!venue || !country || nodes.some((node) => node !== venue && node !== country)) return null
|
|
231
229
|
|
|
232
230
|
// A name written in two forms, `Derry/Londonderry` or `Biel/Bienne`, is read whole and then as each form.
|
|
233
231
|
const forms = [venue.value, ...venue.value.split("/").map((form) => form.trim())].filter(
|
|
234
232
|
(form, index, all) => form && all.indexOf(form) === index
|
|
235
233
|
)
|
|
236
234
|
|
|
235
|
+
return { venue, country: country.metadata!["resolver_country"] as string, forms }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Reads a venue as a town when the input states only a country beside it
|
|
240
|
+
* and the country holds a town of that name.
|
|
241
|
+
*
|
|
242
|
+
* `Road Town, British Virgin Islands` parses the town as a venue.
|
|
243
|
+
* This runs ahead of the landmark layer, so an airport alias under `Biggin Hill`
|
|
244
|
+
* yields to the town that holds the name.
|
|
245
|
+
*/
|
|
246
|
+
export async function applyVenueAsTown(roots: AddressNode[], backend: ResolverBackend): Promise<void> {
|
|
247
|
+
const pair = venueCountryForms(roots)
|
|
248
|
+
|
|
249
|
+
if (!pair) return
|
|
250
|
+
|
|
237
251
|
let found: Awaited<ReturnType<ResolverBackend["findPlace"]>> = []
|
|
238
252
|
|
|
239
|
-
for (const form of forms) {
|
|
253
|
+
for (const form of pair.forms) {
|
|
240
254
|
found = await backend.findPlace({
|
|
241
255
|
text: form,
|
|
242
256
|
placetype: "locality",
|
|
243
|
-
country: country
|
|
257
|
+
country: pair.country,
|
|
244
258
|
})
|
|
245
259
|
|
|
246
260
|
if (found[0]?.exactMatch) break
|
|
@@ -250,6 +264,34 @@ export async function applyVenueAsTown(roots: AddressNode[], backend: ResolverBa
|
|
|
250
264
|
|
|
251
265
|
if (!town?.exactMatch) return
|
|
252
266
|
|
|
253
|
-
venue.tag = "locality"
|
|
254
|
-
decorateNode(venue, town, others)
|
|
267
|
+
pair.venue.tag = "locality"
|
|
268
|
+
decorateNode(pair.venue, town, others)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Reads an unresolved venue beside a country as an exact sub-locality in that country.
|
|
273
|
+
*
|
|
274
|
+
* Runs after the landmark layer so landmarks keep their landmark resolution.
|
|
275
|
+
* Settlements at `macrohood`, `neighbourhood`, or `microhood` rank remain reachable.
|
|
276
|
+
*/
|
|
277
|
+
export async function applyVenueAsSublocality(roots: AddressNode[], backend: ResolverBackend): Promise<void> {
|
|
278
|
+
const pair = venueCountryForms(roots)
|
|
279
|
+
|
|
280
|
+
if (!pair) return
|
|
281
|
+
|
|
282
|
+
for (const form of pair.forms) {
|
|
283
|
+
const found = await scopedSublocalityCandidates(backend, {
|
|
284
|
+
text: form,
|
|
285
|
+
country: pair.country,
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
const [place, ...others] = found
|
|
289
|
+
|
|
290
|
+
if (place) {
|
|
291
|
+
pair.venue.tag = "locality"
|
|
292
|
+
decorateNode(pair.venue, place, others)
|
|
293
|
+
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
}
|
|
255
297
|
}
|