@mailwoman/resolver 10.7.0 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -5
- package/lib/admin/coherence-passes.ts +2 -2
- package/lib/admin/winner.ts +24 -4
- package/lib/bare-toponym-race.ts +1 -1
- package/lib/decorate-node.ts +19 -3
- package/lib/index.ts +1 -0
- package/lib/postcode/country-coherence.ts +4 -4
- package/lib/postcode/shape-coherence.ts +40 -51
- package/lib/remote-resolver.ts +1 -2
- package/lib/resolve/country-code.ts +102 -0
- package/lib/resolve/landmark.ts +227 -0
- package/lib/resolve/passes.ts +16 -60
- package/lib/resolve/region-city.ts +71 -0
- package/lib/resolve.ts +96 -67
- package/lib/span-rescore.ts +34 -3
- package/lib/street/tier.ts +117 -8
- package/out/admin/coherence-passes.js +2 -2
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/winner.d.ts +10 -2
- package/out/admin/winner.d.ts.map +1 -1
- package/out/admin/winner.js +18 -2
- package/out/admin/winner.js.map +1 -1
- package/out/bare-toponym-race.js +1 -1
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +15 -3
- package/out/decorate-node.js.map +1 -1
- package/out/index.d.ts +1 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +1 -1
- package/out/index.js.map +1 -1
- package/out/postcode/country-coherence.d.ts.map +1 -1
- package/out/postcode/country-coherence.js +4 -4
- package/out/postcode/country-coherence.js.map +1 -1
- package/out/postcode/shape-coherence.d.ts +12 -16
- package/out/postcode/shape-coherence.d.ts.map +1 -1
- package/out/postcode/shape-coherence.js +31 -40
- package/out/postcode/shape-coherence.js.map +1 -1
- package/out/remote-resolver.d.ts +1 -2
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +0 -1
- package/out/remote-resolver.js.map +1 -1
- package/out/resolve/country-code.d.ts +49 -0
- package/out/resolve/country-code.d.ts.map +1 -0
- package/out/resolve/country-code.js +88 -0
- package/out/resolve/country-code.js.map +1 -0
- package/out/resolve/landmark.d.ts +26 -0
- package/out/resolve/landmark.d.ts.map +1 -0
- package/out/resolve/landmark.js +181 -0
- package/out/resolve/landmark.js.map +1 -0
- package/out/resolve/passes.d.ts +8 -30
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +8 -28
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve/region-city.d.ts +29 -0
- package/out/resolve/region-city.d.ts.map +1 -0
- package/out/resolve/region-city.js +58 -0
- package/out/resolve/region-city.js.map +1 -0
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +74 -60
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts +5 -0
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +26 -3
- package/out/span-rescore.js.map +1 -1
- package/out/street/tier.d.ts +13 -2
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +94 -7
- package/out/street/tier.js.map +1 -1
- package/package.json +10 -7
- package/lib/backend-capabilities.ts +0 -89
- package/out/backend-capabilities.d.ts +0 -36
- package/out/backend-capabilities.d.ts.map +0 -1
- package/out/backend-capabilities.js +0 -77
- package/out/backend-capabilities.js.map +0 -1
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Answers a query that is a place's name, with no street or postcode, from the landmark layer.
|
|
7
|
+
*
|
|
8
|
+
* The place of that name with the most Wikipedia language editions answers, and the others stay as alternatives. A
|
|
9
|
+
* prominence of log10(sitelinks) puts the lead on the scale the admin tier's `declared_ambiguity` margin reads, so a
|
|
10
|
+
* close call is declared by the same rule that declares one between two towns.
|
|
11
|
+
*
|
|
12
|
+
* A code written in capitals reads by one rule. A code a country is written as answers the country, and the airport
|
|
13
|
+
* holding the same IATA code is listed as an alternative: `AUS` is Australia, then Austin–Bergstrom. Every other code
|
|
14
|
+
* answers the place holding it: `SFO`, `LHR` and `DTW` are airports. `writtenCountryCode` states which codes a country
|
|
15
|
+
* claims.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
19
|
+
import {
|
|
20
|
+
EMPTY_PLACE_FIELDS,
|
|
21
|
+
landmarkPlaceID,
|
|
22
|
+
OSM_LANDMARK_PREFIX,
|
|
23
|
+
type LandmarkHit,
|
|
24
|
+
type LandmarkLookup,
|
|
25
|
+
type ResolvedPlace,
|
|
26
|
+
} from "@mailwoman/core/resolver"
|
|
27
|
+
import { isCountryAirportCollision } from "@mailwoman/registry/overlap/country-airport"
|
|
28
|
+
import { isLatLonBounds, latLonBoundsContain } from "@mailwoman/spatial/bbox"
|
|
29
|
+
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
30
|
+
|
|
31
|
+
import { decorateNode } from "#decorate-node"
|
|
32
|
+
import { foldName } from "#fold-name"
|
|
33
|
+
import { writtenCountryCode } from "#resolve/country-code"
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The tags that make an input an address.
|
|
37
|
+
* A landmark name cannot answer an address.
|
|
38
|
+
*/
|
|
39
|
+
const ADDRESS_TAGS: ReadonlySet<string> = new Set(["house_number", "postcode", "unit", "po_box"])
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The namesakes a landmark answer lists: OpenStreetMap can hold hundreds of parks under one name.
|
|
43
|
+
*/
|
|
44
|
+
const LISTED_ALTERNATIVES = 8
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The farthest a landmark may lie from a locality the input states, matching span rescore's postcode radius.
|
|
48
|
+
*/
|
|
49
|
+
const STATED_LOCALITY_RADIUS_KM = 50
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Keeps the hits an input's stated context permits: its country, and the surroundings of its locality.
|
|
53
|
+
*/
|
|
54
|
+
function hitsInContext(hits: readonly LandmarkHit[], context: readonly AddressNode[]): LandmarkHit[] {
|
|
55
|
+
const nodes = [...walkNodes(context)]
|
|
56
|
+
|
|
57
|
+
const country = nodes
|
|
58
|
+
.map((node) => node.metadata?.["resolver_country"])
|
|
59
|
+
.find((code): code is string => typeof code === "string")
|
|
60
|
+
|
|
61
|
+
const locality = nodes.find((node) => node.tag === "locality" && node.lat != null && node.lon != null)
|
|
62
|
+
const extent = locality?.metadata?.["resolver_bbox"]
|
|
63
|
+
|
|
64
|
+
const distance = (hit: LandmarkHit) => (locality ? haversine(locality.lat!, locality.lon!, hit.lat, hit.lon) : 0)
|
|
65
|
+
|
|
66
|
+
// OpenStreetMap holds a St Mary's in every village, so a stated town's own extent
|
|
67
|
+
// bounds its places where the gazetteer holds one.
|
|
68
|
+
// Wikipedia editions separate famous namesakes, so a Wikidata place keeps the wider radius.
|
|
69
|
+
const inLocality = (hit: LandmarkHit) =>
|
|
70
|
+
hit.qid.startsWith(OSM_LANDMARK_PREFIX) && isLatLonBounds(extent)
|
|
71
|
+
? latLonBoundsContain(extent, hit.lat, hit.lon)
|
|
72
|
+
: distance(hit) <= STATED_LOCALITY_RADIUS_KM
|
|
73
|
+
|
|
74
|
+
const kept = hits.filter((hit) => hit.country === country && inLocality(hit))
|
|
75
|
+
|
|
76
|
+
return kept.every((hit) => hit.qid.startsWith(OSM_LANDMARK_PREFIX))
|
|
77
|
+
? kept.toSorted((a, b) => distance(a) - distance(b))
|
|
78
|
+
: kept
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function placeOf(hit: LandmarkHit): ResolvedPlace {
|
|
82
|
+
const prominence = hit.importance ?? Math.log10(hit.sitelinks + 1)
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
...EMPTY_PLACE_FIELDS,
|
|
86
|
+
id: hit.qid,
|
|
87
|
+
name: hit.name,
|
|
88
|
+
placetype: "venue",
|
|
89
|
+
country: hit.country,
|
|
90
|
+
lat: hit.lat,
|
|
91
|
+
lon: hit.lon,
|
|
92
|
+
parent_id: null,
|
|
93
|
+
score: prominence,
|
|
94
|
+
prominence,
|
|
95
|
+
exactMatch: true,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Lists every landmark that holds the written code on the resolved country node, as its alternatives.
|
|
101
|
+
*
|
|
102
|
+
* `AUS` answers Australia, and Austin–Bergstrom International Airport is the
|
|
103
|
+
* reading a caller can pick instead.
|
|
104
|
+
* A word spelled like the code, such as the town of Usa under `USA`,
|
|
105
|
+
* is not a holder of the code and stays out.
|
|
106
|
+
*
|
|
107
|
+
* The list sits on a resolved country node, so a country the gazetteer left unresolved keeps its parse.
|
|
108
|
+
*
|
|
109
|
+
* The node also records `code_collision`, the identifier registry's verdict on whether the written
|
|
110
|
+
* code is both an officially assigned ISO 3166-1 alpha-3 code and an IATA airport code in its profile.
|
|
111
|
+
* The registry and the landmark layer read different sources, so the flag
|
|
112
|
+
* and the listed holders can disagree, and both are kept.
|
|
113
|
+
*/
|
|
114
|
+
function listCodeHoldersOnCountry(roots: AddressNode[], code: string, hits: readonly LandmarkHit[]): void {
|
|
115
|
+
const country = roots.find((root) => root.tag === "country" && typeof root.placeID === "string")
|
|
116
|
+
|
|
117
|
+
if (!country) return
|
|
118
|
+
|
|
119
|
+
country.metadata = { ...country.metadata, code_collision: isCountryAirportCollision(code) }
|
|
120
|
+
|
|
121
|
+
const holders = hits.filter((hit) => hit.code && hit.kind === "venue")
|
|
122
|
+
|
|
123
|
+
if (!holders.length) return
|
|
124
|
+
|
|
125
|
+
const listed = (country.alternatives as ResolvedPlace[] | undefined) ?? []
|
|
126
|
+
|
|
127
|
+
country.alternatives = [...listed, ...holders.map(placeOf)]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Replaces the parse's reading of a landmark name with a venue node that holds the answer.
|
|
132
|
+
*
|
|
133
|
+
* The first comma-separated part is the name, and any other part is context that has to have resolved.
|
|
134
|
+
* A settlement that leads the name stays with the admin ladder, because the ladder ranks settlements.
|
|
135
|
+
*/
|
|
136
|
+
export async function applyLandmark(roots: AddressNode[], raw: string, landmarks: LandmarkLookup): Promise<void> {
|
|
137
|
+
const nodes = [...walkNodes(roots)]
|
|
138
|
+
|
|
139
|
+
if (nodes.some((node) => ADDRESS_TAGS.has(node.tag))) return
|
|
140
|
+
|
|
141
|
+
const name = raw.split(",", 1)[0]!.trim()
|
|
142
|
+
|
|
143
|
+
if (!name) return
|
|
144
|
+
|
|
145
|
+
const hits = await landmarks.find(name)
|
|
146
|
+
const hasContext = raw.trim().length > name.length
|
|
147
|
+
|
|
148
|
+
// A bare country code answers the country, and the airport holding the same code is listed beside it.
|
|
149
|
+
// The registry's collision verdict is stamped whether or not the landmark layer holds the code.
|
|
150
|
+
// With context, `FRA, Germany` can still reach the airport.
|
|
151
|
+
if (!hasContext && writtenCountryCode(name)) {
|
|
152
|
+
listCodeHoldersOnCountry(roots, name, hits)
|
|
153
|
+
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!hits.length) return
|
|
158
|
+
|
|
159
|
+
const nameEnd = raw.indexOf(name) + name.length
|
|
160
|
+
const context = roots.filter((node) => node.start >= nameEnd && typeof node.placeID === "string")
|
|
161
|
+
|
|
162
|
+
if (hasContext && !context.length) return
|
|
163
|
+
|
|
164
|
+
const candidates = hasContext ? hitsInContext(hits, context) : hits
|
|
165
|
+
const leader = candidates[0]
|
|
166
|
+
|
|
167
|
+
if (!leader || leader.kind !== "venue") return
|
|
168
|
+
|
|
169
|
+
// A translation or alias is the weakest claim on a name, so it answers only where the
|
|
170
|
+
// gazetteer holds none: `Hamburg` is an alias of Hamburg Airport and the name of a city.
|
|
171
|
+
const resolved = roots.filter((root) => typeof root.placeID === "string")
|
|
172
|
+
const fromOSM = leader.qid.startsWith(OSM_LANDMARK_PREFIX)
|
|
173
|
+
|
|
174
|
+
// The parser reads `Wall Street` and `Times Square` as streets, and `Central Park` as a venue.
|
|
175
|
+
// It also reads some parks as a street with the suffix `Park`, and those stay places.
|
|
176
|
+
const inName = (node: AddressNode) => node.start < nameEnd
|
|
177
|
+
|
|
178
|
+
const readsAsPark = nodes.some(
|
|
179
|
+
(node) => node.tag === "street_suffix" && inName(node) && foldName(node.value) === "park"
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
const readsAsStreet = !readsAsPark && nodes.some((node) => node.tag === "street" && inName(node))
|
|
183
|
+
|
|
184
|
+
if (fromOSM) {
|
|
185
|
+
// OpenStreetMap holds a place spelled like a street in some other town: `Wall Street` also answers in Utah.
|
|
186
|
+
if (readsAsStreet) return
|
|
187
|
+
|
|
188
|
+
// A school or a park in OpenStreetMap can share its town's name,
|
|
189
|
+
// so a place holding the whole name keeps it.
|
|
190
|
+
// A park reading beats a row with no measured population, as `Piedmont Park`
|
|
191
|
+
// in South Carolina yields to the park in Atlanta.
|
|
192
|
+
const holdsName = (node: AddressNode) =>
|
|
193
|
+
typeof node.placeID === "string" &&
|
|
194
|
+
foldName(node.value) === foldName(name) &&
|
|
195
|
+
(!readsAsPark || Number(node.metadata?.["resolver_score"] ?? 0) > 0)
|
|
196
|
+
|
|
197
|
+
if (nodes.some(holdsName)) return
|
|
198
|
+
} else {
|
|
199
|
+
const nameResolved = resolved.some(
|
|
200
|
+
(root) => root.start < nameEnd || root.value.toLowerCase() === name.toLowerCase()
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
// `Baker Street` is an alias of the tube station, and the street keeps its name.
|
|
204
|
+
if (!leader.primary && !leader.code && (nameResolved || readsAsStreet)) return
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const node: AddressNode = {
|
|
208
|
+
// A famous street answers from Wikidata and stays a street: `Downing Street` holds a coordinate and a street.
|
|
209
|
+
tag: readsAsStreet ? "street" : "venue",
|
|
210
|
+
value: name.normalize("NFC").replaceAll(/\s+/gu, " "),
|
|
211
|
+
start: raw.indexOf(name),
|
|
212
|
+
end: nameEnd,
|
|
213
|
+
confidence: 1,
|
|
214
|
+
children: [],
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
decorateNode(node, placeOf(leader), candidates.slice(1, LISTED_ALTERNATIVES + 1).map(placeOf))
|
|
218
|
+
node.placeID = landmarkPlaceID(leader.qid)
|
|
219
|
+
|
|
220
|
+
node.metadata = {
|
|
221
|
+
...node.metadata,
|
|
222
|
+
landmark_sitelinks: leader.sitelinks,
|
|
223
|
+
...(fromOSM ? { landmark_source: "openstreetmap:planet" } : {}),
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
roots.splice(0, roots.length, node, ...context)
|
|
227
|
+
}
|
package/lib/resolve/passes.ts
CHANGED
|
@@ -5,27 +5,22 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { PlacetypeMap } from "@mailwoman/codex/placetype-map"
|
|
8
|
-
import { walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
8
|
+
import { isShapeExcludedPostcode, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
9
9
|
import {
|
|
10
|
-
type CoincidentLocality,
|
|
11
10
|
type DefaultCountry,
|
|
12
11
|
EMPTY_PLACE_FIELDS,
|
|
13
|
-
compareReferential,
|
|
14
12
|
type ResolveCandidateTrace,
|
|
15
13
|
type ResolveNodeTrace,
|
|
16
14
|
type PostcodePrefixIndexLike,
|
|
17
|
-
referentialFromPopulation,
|
|
18
15
|
type ResolvedPlace,
|
|
19
16
|
type ResolveOpts,
|
|
20
17
|
type ResolverBackend,
|
|
21
18
|
type ResolveSwitches,
|
|
22
19
|
} from "@mailwoman/core/resolver"
|
|
23
|
-
import { PLACETYPE_SPECIFICITY } from "@mailwoman/
|
|
24
|
-
import {
|
|
20
|
+
import { PLACETYPE_SPECIFICITY } from "@mailwoman/gazetteer/whosonfirst/placetypes/specificity"
|
|
21
|
+
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
25
22
|
|
|
26
23
|
import { decorateNode, isResolvedWithCoord } from "#decorate-node"
|
|
27
|
-
import type { CoordinateOptionalPlace } from "#postcode/prefix"
|
|
28
|
-
import { isShapeExcludedPostcode } from "#postcode/shape-coherence"
|
|
29
24
|
import { findRescoreCandidate, hasResolvedPlace, postcodeCodeSubset } from "#span-rescore"
|
|
30
25
|
|
|
31
26
|
const TRACE_CANDIDATE_CAP = 10
|
|
@@ -62,6 +57,7 @@ export interface NodeTraceRecorder {
|
|
|
62
57
|
query: {
|
|
63
58
|
country?: string
|
|
64
59
|
parentID?: string | number
|
|
60
|
+
parentQualifierID?: number
|
|
65
61
|
postcode?: string | null
|
|
66
62
|
regionQualifier?: string | null
|
|
67
63
|
limit?: number
|
|
@@ -156,6 +152,7 @@ export function createNodeTraceRecorder(sink: (record: ResolveNodeTrace) => void
|
|
|
156
152
|
query: {
|
|
157
153
|
country: ctx.query.country || null,
|
|
158
154
|
parentID: ctx.query.parentID ?? null,
|
|
155
|
+
parentQualifierID: ctx.query.parentQualifierID ?? null,
|
|
159
156
|
postcode: ctx.query.postcode || null,
|
|
160
157
|
regionQualifier: ctx.query.regionQualifier || null,
|
|
161
158
|
limit: ctx.query.limit ?? ctx.defaultLimit,
|
|
@@ -262,7 +259,7 @@ export interface ResolutionState {
|
|
|
262
259
|
* The callback that receives one trace record per lookup.
|
|
263
260
|
* The walk records no events when it is absent.
|
|
264
261
|
*/
|
|
265
|
-
traceSink?: (record: ResolveNodeTrace) => void
|
|
262
|
+
traceSink?: ((record: ResolveNodeTrace) => void) | null
|
|
266
263
|
|
|
267
264
|
/**
|
|
268
265
|
* Whether an empty lookup re-probes the other admin bands and traces which ones hold the value.
|
|
@@ -294,12 +291,6 @@ export interface ResolutionState {
|
|
|
294
291
|
*/
|
|
295
292
|
hardCountry?: string
|
|
296
293
|
|
|
297
|
-
/**
|
|
298
|
-
* Whether a resolved region gains its coincident locality of the same name as an
|
|
299
|
-
* interpretation when the tree has no locality node.
|
|
300
|
-
*/
|
|
301
|
-
hierarchyCompletion: boolean
|
|
302
|
-
|
|
303
294
|
/**
|
|
304
295
|
* Whether each resolved node gets its ancestor lineage in metadata, when the backend supports it.
|
|
305
296
|
*/
|
|
@@ -311,6 +302,12 @@ export interface ResolutionState {
|
|
|
311
302
|
*/
|
|
312
303
|
adminContainmentRerank: boolean
|
|
313
304
|
|
|
305
|
+
/**
|
|
306
|
+
* Whether a `dependent_locality` node's lookup passes its resolved parent as a containment
|
|
307
|
+
* qualifier and a contained pick is stamped to answer ahead of the locality.
|
|
308
|
+
*/
|
|
309
|
+
dependentLocalityContainment: boolean
|
|
310
|
+
|
|
314
311
|
/**
|
|
315
312
|
* The tree's first region value.
|
|
316
313
|
*
|
|
@@ -320,48 +317,6 @@ export interface ResolutionState {
|
|
|
320
317
|
* and no caller-supplied default country.
|
|
321
318
|
*/
|
|
322
319
|
regionQualifier?: string | null
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Whether any node maps to the `locality` placetype.
|
|
326
|
-
* Hierarchy completion runs only when this is false.
|
|
327
|
-
*/
|
|
328
|
-
localityNodePresent: boolean
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* The first resolved region place, used to look up its coincident locality.
|
|
332
|
-
*/
|
|
333
|
-
resolvedRegion: CoordinateOptionalPlace | null
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* The decorated node for {@link ResolutionState.resolvedRegion}.
|
|
337
|
-
*
|
|
338
|
-
* Hierarchy completion adds the locality interpretation to it in place.
|
|
339
|
-
*/
|
|
340
|
-
resolvedRegionNode: AddressNode | null
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Picks the completion locality among coincident candidates of the same name.
|
|
345
|
-
*
|
|
346
|
-
* It prefers the most populous candidate, then the nearest candidate.
|
|
347
|
-
* It returns `null` on an exact tie.
|
|
348
|
-
*/
|
|
349
|
-
export function pickCompletion(candidates: readonly CoincidentLocality[]): CoincidentLocality | null {
|
|
350
|
-
if (!candidates.length) return null
|
|
351
|
-
|
|
352
|
-
if (candidates.length === 1) return candidates[0]!
|
|
353
|
-
|
|
354
|
-
const ranked = [...candidates]
|
|
355
|
-
.map((c) => ({ c, referential: referentialFromPopulation(c.population), population: c.population }))
|
|
356
|
-
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array. toSorted would double-allocate on a hot path
|
|
357
|
-
.sort((a, b) => compareReferential(a, b) || a.c.distanceKm - b.c.distanceKm)
|
|
358
|
-
.map((x) => x.c)
|
|
359
|
-
|
|
360
|
-
const [first, second] = ranked
|
|
361
|
-
|
|
362
|
-
if (first!.population === second!.population && first!.distanceKm === second!.distanceKm) return null
|
|
363
|
-
|
|
364
|
-
return first!
|
|
365
320
|
}
|
|
366
321
|
|
|
367
322
|
/**
|
|
@@ -401,6 +356,7 @@ export async function applySpanRescore(
|
|
|
401
356
|
|
|
402
357
|
postalCompoundRecovery: opts.postalCompoundRecovery,
|
|
403
358
|
spanRescoreRequireContextRemainder: opts.spanRescoreRequireContextRemainder,
|
|
359
|
+
...(opts.isVenueCategoryWord ? { isVenueCategoryWord: opts.isVenueCategoryWord } : {}),
|
|
404
360
|
})
|
|
405
361
|
} catch {
|
|
406
362
|
return
|
|
@@ -444,7 +400,7 @@ async function recoverPostcodeNode(
|
|
|
444
400
|
roots: AddressNode[],
|
|
445
401
|
backend: ResolverBackend,
|
|
446
402
|
country: string | null,
|
|
447
|
-
traceSink?: (record: ResolveNodeTrace) => void
|
|
403
|
+
traceSink?: ((record: ResolveNodeTrace) => void) | null
|
|
448
404
|
): Promise<void> {
|
|
449
405
|
for (const n of walkNodes(roots)) {
|
|
450
406
|
if (n.tag === "postcode" && !n.placeID && n.value.trim()) {
|
|
@@ -504,7 +460,7 @@ async function reconcileLocalitiesToAnchor(
|
|
|
504
460
|
const nearest = (places: readonly ResolvedPlace[]) =>
|
|
505
461
|
places
|
|
506
462
|
.filter((a) => a.lat !== 0 || a.lon !== 0)
|
|
507
|
-
.map((a) => ({ a, d:
|
|
463
|
+
.map((a) => ({ a, d: haversine(anchor.lat, anchor.lon, a.lat, a.lon) }))
|
|
508
464
|
.filter((x) => x.d <= thresholdKm)
|
|
509
465
|
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array. toSorted would double-allocate on a hot path
|
|
510
466
|
.sort((x, y) => x.d - y.d)[0]
|
|
@@ -512,7 +468,7 @@ async function reconcileLocalitiesToAnchor(
|
|
|
512
468
|
for (const node of walkNodes(roots)) {
|
|
513
469
|
if ((node.tag !== "locality" && node.tag !== "dependent_locality") || !isResolvedWithCoord(node)) continue
|
|
514
470
|
|
|
515
|
-
const gapKm =
|
|
471
|
+
const gapKm = haversine(anchor.lat, anchor.lon, node.lat!, node.lon!)
|
|
516
472
|
|
|
517
473
|
if (gapKm <= thresholdKm) continue
|
|
518
474
|
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
8
|
+
import type { ResolverBackend } from "@mailwoman/core/resolver"
|
|
9
|
+
|
|
10
|
+
import { decorateNode } from "#decorate-node"
|
|
11
|
+
import { streetNumberPairs } from "#street/tier"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Reads a street address's region as the city of the same name when the
|
|
15
|
+
* gazetteer holds one inside that region.
|
|
16
|
+
*
|
|
17
|
+
* `34 Lê Duẩn, Sài Gòn, Hồ Chí Minh, Vietnam` parses with a region and no locality.
|
|
18
|
+
* The region's point can lie far from the street, and the address-point lookup
|
|
19
|
+
* has no locality to search around.
|
|
20
|
+
*
|
|
21
|
+
* A place held at both levels occurs wherever a city is its own province, so the test reads the hierarchy.
|
|
22
|
+
*
|
|
23
|
+
* A bare region stays a region, because only a street and number say the input
|
|
24
|
+
* means somewhere a street can be.
|
|
25
|
+
*/
|
|
26
|
+
export async function applyRegionCity(roots: AddressNode[], backend: ResolverBackend): Promise<void> {
|
|
27
|
+
const nodes = [...walkNodes(roots)]
|
|
28
|
+
|
|
29
|
+
if (nodes.some((node) => node.tag === "locality") || !streetNumberPairs(roots).length) return
|
|
30
|
+
|
|
31
|
+
const region = nodes.find((node) => node.tag === "region" && node.placeID?.startsWith("wof:"))
|
|
32
|
+
|
|
33
|
+
if (!region) return
|
|
34
|
+
|
|
35
|
+
const [city, ...others] = await backend.findPlace({
|
|
36
|
+
text: region.value,
|
|
37
|
+
placetype: "locality",
|
|
38
|
+
parentID: Number(region.placeID!.slice("wof:".length)),
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
// A lookup that finds the name only outside the parent answers with that place and marks the miss.
|
|
42
|
+
if (!city?.exactMatch || city.regionScopeMiss) return
|
|
43
|
+
|
|
44
|
+
region.tag = "locality"
|
|
45
|
+
decorateNode(region, city, others)
|
|
46
|
+
region.metadata = { ...region.metadata, region_city: true }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Reads a locality the gazetteer holds only as a region, inside a country the input states.
|
|
51
|
+
*
|
|
52
|
+
* `Likiep Atoll, RMI` parses the atoll as a locality, and the Marshall Islands hold it as a region.
|
|
53
|
+
* This runs after the locality band missed, so a place with a locality row keeps it.
|
|
54
|
+
*/
|
|
55
|
+
export async function applyLocalityAsRegion(
|
|
56
|
+
roots: AddressNode[],
|
|
57
|
+
backend: ResolverBackend,
|
|
58
|
+
country: string
|
|
59
|
+
): Promise<void> {
|
|
60
|
+
const nodes = [...walkNodes(roots)]
|
|
61
|
+
const locality = nodes.find((node) => node.tag === "locality" && !node.placeID && node.value.trim())
|
|
62
|
+
|
|
63
|
+
if (!locality || nodes.some((node) => node.tag === "region" && node.placeID)) return
|
|
64
|
+
|
|
65
|
+
const [region, ...others] = await backend.findPlace({ text: locality.value, placetype: "region", country })
|
|
66
|
+
|
|
67
|
+
if (!region?.exactMatch || region.country !== country) return
|
|
68
|
+
|
|
69
|
+
locality.tag = "region"
|
|
70
|
+
decorateNode(locality, region, others)
|
|
71
|
+
}
|