@mailwoman/resolver 11.0.0 → 11.1.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 +3 -3
- package/lib/admin/coherence-passes.ts +14 -20
- package/lib/admin/containment.ts +6 -5
- package/lib/admin/jp-municipality.ts +3 -3
- package/lib/admin/winner.ts +5 -5
- package/lib/admin.ts +2 -5
- package/lib/bare-toponym-race.ts +12 -10
- package/lib/decorate-node.ts +50 -8
- package/lib/fold-name.ts +8 -8
- package/lib/index.ts +11 -8
- package/lib/plausibility.ts +2 -5
- package/lib/postcode/country-coherence.ts +4 -7
- package/lib/postcode/prefix.ts +4 -7
- package/lib/postcode/shape-coherence.ts +3 -6
- package/lib/postcode.ts +2 -5
- package/lib/remote-resolver.ts +2 -5
- package/lib/rerank.ts +7 -8
- package/lib/resolve/country-code.ts +22 -18
- package/lib/resolve/landmark.ts +19 -12
- package/lib/resolve/passes.ts +13 -9
- package/lib/resolve/region-city.ts +192 -8
- package/lib/resolve/whole-input.ts +49 -0
- package/lib/resolve.ts +41 -8
- package/lib/span-rescore.ts +60 -27
- package/lib/street/evidence.ts +2 -5
- package/lib/street/tier.ts +9 -12
- package/lib/street.ts +2 -5
- package/lib/toponym-prior.ts +4 -7
- package/out/admin/coherence-passes.d.ts +2 -7
- package/out/admin/coherence-passes.d.ts.map +1 -1
- package/out/admin/coherence-passes.js +14 -20
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/containment.d.ts +3 -5
- package/out/admin/containment.d.ts.map +1 -1
- package/out/admin/containment.js +5 -5
- package/out/admin/containment.js.map +1 -1
- package/out/admin/jp-municipality.d.ts +0 -3
- package/out/admin/jp-municipality.d.ts.map +1 -1
- package/out/admin/jp-municipality.js +2 -15
- package/out/admin/jp-municipality.js.map +1 -1
- package/out/admin/winner.d.ts +2 -5
- package/out/admin/winner.d.ts.map +1 -1
- package/out/admin/winner.js +2 -9
- package/out/admin/winner.js.map +1 -1
- package/out/admin.d.ts +0 -5
- package/out/admin.d.ts.map +1 -1
- package/out/admin.js +2 -5
- package/out/admin.js.map +1 -1
- package/out/bare-toponym-race.d.ts +0 -11
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/bare-toponym-race.js +3 -12
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts +14 -6
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +45 -10
- package/out/decorate-node.js.map +1 -1
- package/out/fold-name.d.ts +5 -8
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +7 -8
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +8 -8
- package/out/index.d.ts.map +1 -1
- package/out/index.js +10 -8
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +0 -5
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +2 -5
- package/out/plausibility.js.map +1 -1
- package/out/postcode/country-coherence.d.ts +2 -7
- package/out/postcode/country-coherence.d.ts.map +1 -1
- package/out/postcode/country-coherence.js +3 -6
- package/out/postcode/country-coherence.js.map +1 -1
- package/out/postcode/prefix.d.ts +2 -7
- package/out/postcode/prefix.d.ts.map +1 -1
- package/out/postcode/prefix.js +3 -6
- package/out/postcode/prefix.js.map +1 -1
- package/out/postcode/shape-coherence.d.ts +1 -6
- package/out/postcode/shape-coherence.d.ts.map +1 -1
- package/out/postcode/shape-coherence.js +2 -5
- package/out/postcode/shape-coherence.js.map +1 -1
- package/out/postcode.d.ts +0 -5
- package/out/postcode.d.ts.map +1 -1
- package/out/postcode.js +2 -5
- package/out/postcode.js.map +1 -1
- package/out/remote-resolver.d.ts +0 -5
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +2 -5
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +3 -7
- package/out/rerank.d.ts.map +1 -1
- package/out/rerank.js +3 -10
- package/out/rerank.js.map +1 -1
- package/out/resolve/country-code.d.ts +3 -24
- package/out/resolve/country-code.d.ts.map +1 -1
- package/out/resolve/country-code.js +21 -18
- package/out/resolve/country-code.js.map +1 -1
- package/out/resolve/landmark.d.ts +10 -11
- package/out/resolve/landmark.d.ts.map +1 -1
- package/out/resolve/landmark.js +18 -12
- package/out/resolve/landmark.js.map +1 -1
- package/out/resolve/passes.d.ts +2 -7
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +11 -8
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve/region-city.d.ts +34 -7
- package/out/resolve/region-city.d.ts.map +1 -1
- package/out/resolve/region-city.js +159 -8
- package/out/resolve/region-city.js.map +1 -1
- package/out/resolve/whole-input.d.ts +14 -0
- package/out/resolve/whole-input.d.ts.map +1 -0
- package/out/resolve/whole-input.js +37 -0
- package/out/resolve/whole-input.js.map +1 -0
- package/out/resolve.d.ts +0 -8
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +24 -12
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts +12 -16
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +45 -18
- package/out/span-rescore.js.map +1 -1
- package/out/street/evidence.d.ts +0 -5
- package/out/street/evidence.d.ts.map +1 -1
- package/out/street/evidence.js +2 -5
- package/out/street/evidence.js.map +1 -1
- package/out/street/tier.d.ts +3 -8
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +7 -10
- package/out/street/tier.js.map +1 -1
- package/out/street.d.ts +0 -5
- package/out/street.d.ts.map +1 -1
- package/out/street.js +2 -5
- package/out/street.js.map +1 -1
- package/out/toponym-prior.d.ts +2 -7
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +4 -7
- package/out/toponym-prior.js.map +1 -1
- package/package.json +9 -9
package/lib/resolve/passes.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*/
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
6
3
|
|
|
7
4
|
import type { PlacetypeMap } from "@mailwoman/codex/placetype-map"
|
|
8
5
|
import { isShapeExcludedPostcode, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
@@ -311,7 +308,7 @@ export interface ResolutionState {
|
|
|
311
308
|
/**
|
|
312
309
|
* The tree's first region value.
|
|
313
310
|
*
|
|
314
|
-
* The region and locality nodes are siblings, so the walk
|
|
311
|
+
* The region and locality nodes are siblings, so the walk wouldn't otherwise
|
|
315
312
|
* pass the region to the locality lookup.
|
|
316
313
|
* It applies only with {@link ResolutionState.adminContainmentRerank}
|
|
317
314
|
* and no caller-supplied default country.
|
|
@@ -355,7 +352,6 @@ export async function applySpanRescore(
|
|
|
355
352
|
thresholdKm: opts.spanRescoreThresholdKm,
|
|
356
353
|
|
|
357
354
|
postalCompoundRecovery: opts.postalCompoundRecovery,
|
|
358
|
-
spanRescoreRequireContextRemainder: opts.spanRescoreRequireContextRemainder,
|
|
359
355
|
...(opts.isVenueCategoryWord ? { isVenueCategoryWord: opts.isVenueCategoryWord } : {}),
|
|
360
356
|
})
|
|
361
357
|
} catch {
|
|
@@ -382,7 +378,15 @@ export async function applySpanRescore(
|
|
|
382
378
|
|
|
383
379
|
decorateNode(node, hit.place, hit.alternatives)
|
|
384
380
|
|
|
385
|
-
|
|
381
|
+
// `rescore_drops_name` tells the geocode that this locality may scope a venue lookup.
|
|
382
|
+
// It answers only when a venue answers near it.
|
|
383
|
+
node.metadata = {
|
|
384
|
+
...node.metadata,
|
|
385
|
+
span_rescore: true,
|
|
386
|
+
rescore_postcode_verified: hit.postcodeVerified,
|
|
387
|
+
rescore_drops_name: hit.dropsName,
|
|
388
|
+
}
|
|
389
|
+
|
|
386
390
|
roots.push(node)
|
|
387
391
|
|
|
388
392
|
if (opts.traceSink) {
|
|
@@ -519,7 +523,7 @@ const ROOFTOP_NAMESAKE_LIMIT = 25
|
|
|
519
523
|
*
|
|
520
524
|
* A rooftop row is an observed point, so an unreconciled locality is never moved onto it.
|
|
521
525
|
* The node records the distance as `rooftop_outside_locality_km`, and the result
|
|
522
|
-
* reports the locality as a component the answer
|
|
526
|
+
* reports the locality as a component the answer didn't follow.
|
|
523
527
|
*/
|
|
524
528
|
export async function applyRooftopConsistency(
|
|
525
529
|
roots: readonly AddressNode[],
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*/
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
6
3
|
|
|
7
4
|
import { walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
8
5
|
import type { ResolverBackend } from "@mailwoman/core/resolver"
|
|
6
|
+
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
9
7
|
|
|
10
8
|
import { decorateNode } from "#decorate-node"
|
|
11
9
|
import { streetNumberPairs } from "#street/tier"
|
|
@@ -20,13 +18,22 @@ import { streetNumberPairs } from "#street/tier"
|
|
|
20
18
|
*
|
|
21
19
|
* A place held at both levels occurs wherever a city is its own province, so the test reads the hierarchy.
|
|
22
20
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
21
|
+
* `Altona, Hamburg` and `Triana, Sevilla` state a resolved district, and a district also sits inside a city.
|
|
22
|
+
*
|
|
23
|
+
* A bare region stays a region, because only a street and number or a district
|
|
24
|
+
* say the input means somewhere inside a city.
|
|
25
|
+
*
|
|
26
|
+
* A locality the gazetteer didn't resolve leaves the region as the only place,
|
|
27
|
+
* and Bristol's region point lies in the Bristol Channel, so the city answers there too.
|
|
25
28
|
*/
|
|
26
29
|
export async function applyRegionCity(roots: AddressNode[], backend: ResolverBackend): Promise<void> {
|
|
27
30
|
const nodes = [...walkNodes(roots)]
|
|
28
31
|
|
|
29
|
-
|
|
32
|
+
// A street with a number, or a resolved district, places the input inside a city.
|
|
33
|
+
const insideACity =
|
|
34
|
+
streetNumberPairs(roots).length > 0 || nodes.some((node) => node.tag === "dependent_locality" && node.placeID)
|
|
35
|
+
|
|
36
|
+
if (nodes.some((node) => node.tag === "locality" && node.placeID) || !insideACity) return
|
|
30
37
|
|
|
31
38
|
const region = nodes.find((node) => node.tag === "region" && node.placeID?.startsWith("wof:"))
|
|
32
39
|
|
|
@@ -44,6 +51,24 @@ export async function applyRegionCity(roots: AddressNode[], backend: ResolverBac
|
|
|
44
51
|
region.tag = "locality"
|
|
45
52
|
decorateNode(region, city, others)
|
|
46
53
|
region.metadata = { ...region.metadata, region_city: true }
|
|
54
|
+
|
|
55
|
+
// A district inside the city's box answers ahead of the city, as the walk's containment stamp lets it.
|
|
56
|
+
for (const district of nodes) {
|
|
57
|
+
if (district.tag !== "dependent_locality" || !district.placeID || district.lat == null || district.lon == null)
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
const box = city.bbox
|
|
61
|
+
|
|
62
|
+
if (
|
|
63
|
+
box &&
|
|
64
|
+
district.lat >= box.minLat &&
|
|
65
|
+
district.lat <= box.maxLat &&
|
|
66
|
+
district.lon >= box.minLon &&
|
|
67
|
+
district.lon <= box.maxLon
|
|
68
|
+
) {
|
|
69
|
+
district.metadata = { ...district.metadata, contained_by_parent: true }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
47
72
|
}
|
|
48
73
|
|
|
49
74
|
/**
|
|
@@ -69,3 +94,162 @@ export async function applyLocalityAsRegion(
|
|
|
69
94
|
locality.tag = "region"
|
|
70
95
|
decorateNode(locality, region, others)
|
|
71
96
|
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Reads a country as the region of the same name when only that region holds the stated town.
|
|
100
|
+
*
|
|
101
|
+
* `Savannah, Georgia` parses `Georgia` as the country, and the country holds no Savannah.
|
|
102
|
+
* `Tbilisi, Georgia` keeps the country, because the country holds Tbilisi.
|
|
103
|
+
*
|
|
104
|
+
* A caller's US scope leaves the country of Georgia unresolved, so that node has no country to test against.
|
|
105
|
+
*/
|
|
106
|
+
export async function applyCountryAsRegion(roots: AddressNode[], backend: ResolverBackend): Promise<void> {
|
|
107
|
+
const nodes = [...walkNodes(roots)]
|
|
108
|
+
const country = nodes.find((node) => node.tag === "country" && node.value.trim())
|
|
109
|
+
const locality = nodes.find((node) => node.tag === "locality" && node.value.trim())
|
|
110
|
+
|
|
111
|
+
if (!country || !locality || nodes.some((node) => node.tag === "region")) return
|
|
112
|
+
|
|
113
|
+
const stated = country.metadata?.["resolver_country"]
|
|
114
|
+
|
|
115
|
+
if (typeof stated === "string") {
|
|
116
|
+
const [local] = await backend.findPlace({ text: locality.value, placetype: "locality", country: stated })
|
|
117
|
+
|
|
118
|
+
if (local?.exactMatch) return
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const region of await backend.findPlace({ text: country.value, placetype: "region" })) {
|
|
122
|
+
if (!region.exactMatch) continue
|
|
123
|
+
|
|
124
|
+
const [city, ...others] = await backend.findPlace({
|
|
125
|
+
text: locality.value,
|
|
126
|
+
placetype: "locality",
|
|
127
|
+
parentID: region.id,
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
if (!city?.exactMatch || city.regionScopeMiss) continue
|
|
131
|
+
|
|
132
|
+
country.tag = "region"
|
|
133
|
+
decorateNode(country, region, [])
|
|
134
|
+
decorateNode(locality, city, others)
|
|
135
|
+
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The tags whose spans together write a street's name.
|
|
142
|
+
*/
|
|
143
|
+
const STREET_PART_TAGS: ReadonlySet<string> = new Set([
|
|
144
|
+
"street_prefix",
|
|
145
|
+
"street_prefix_particle",
|
|
146
|
+
"street",
|
|
147
|
+
"street_suffix",
|
|
148
|
+
])
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* How far a town may sit from the stated postcode's point and still be the place the postcode serves.
|
|
152
|
+
*/
|
|
153
|
+
const TOWN_POSTCODE_REACH_KM = 15
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Removes the given nodes wherever they sit in the tree.
|
|
157
|
+
*/
|
|
158
|
+
function removeNodes(list: AddressNode[], drop: ReadonlySet<AddressNode>): void {
|
|
159
|
+
for (let index = list.length - 1; index >= 0; index--) {
|
|
160
|
+
const node = list[index]!
|
|
161
|
+
|
|
162
|
+
if (drop.has(node)) {
|
|
163
|
+
list.splice(index, 1)
|
|
164
|
+
} else if (node.children?.length) {
|
|
165
|
+
removeNodes(node.children, drop)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Reads an unnumbered street as the town of that name when the town holds the stated postcode.
|
|
172
|
+
*
|
|
173
|
+
* `Alameda, CA 94501` and `Crocketts Bluff, AR 72038` parse the town as a street,
|
|
174
|
+
* because a name before a region and postcode is where a street sits in a full address.
|
|
175
|
+
* A street name and a town name agree this close to the postcode's point only when they
|
|
176
|
+
* name one place, so a real street such as `Main Street, CA 94501` keeps its tag.
|
|
177
|
+
*/
|
|
178
|
+
export async function applyStreetAsTown(roots: AddressNode[], backend: ResolverBackend): Promise<void> {
|
|
179
|
+
const nodes = [...walkNodes(roots)]
|
|
180
|
+
const parts = nodes.filter((node) => STREET_PART_TAGS.has(node.tag)).toSorted((a, b) => a.start - b.start)
|
|
181
|
+
const postcode = nodes.find((node) => node.tag === "postcode" && node.lat != null && node.lon != null)
|
|
182
|
+
|
|
183
|
+
if (!parts.some((node) => node.tag === "street") || !postcode) return
|
|
184
|
+
|
|
185
|
+
if (nodes.some((node) => node.tag === "house_number" || node.tag === "locality" || node.tag === "venue")) return
|
|
186
|
+
|
|
187
|
+
const region = nodes.find((node) => node.tag === "region" && node.placeID?.startsWith("wof:"))
|
|
188
|
+
const country = postcode.metadata?.["resolver_country"]
|
|
189
|
+
|
|
190
|
+
if (!region && typeof country !== "string") return
|
|
191
|
+
|
|
192
|
+
const name = parts.map((node) => node.value.trim()).join(" ")
|
|
193
|
+
|
|
194
|
+
const [town, ...others] = await backend.findPlace({
|
|
195
|
+
text: name,
|
|
196
|
+
placetype: "locality",
|
|
197
|
+
...(region ? { parentID: Number(region.placeID!.slice("wof:".length)) } : { country: country as string }),
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
if (!town?.exactMatch || town.regionScopeMiss || town.lat == null || town.lon == null) return
|
|
201
|
+
|
|
202
|
+
if (haversine(postcode.lat!, postcode.lon!, town.lat, town.lon, "km") > TOWN_POSTCODE_REACH_KM) return
|
|
203
|
+
|
|
204
|
+
const [first, ...rest] = parts
|
|
205
|
+
|
|
206
|
+
first!.tag = "locality"
|
|
207
|
+
first!.value = name
|
|
208
|
+
first!.end = parts.at(-1)!.end
|
|
209
|
+
removeNodes(roots, new Set(rest))
|
|
210
|
+
decorateNode(first!, town, others)
|
|
211
|
+
first!.metadata = { ...first!.metadata, street_as_town: true }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Reads a venue as a town when the input states only a country beside it
|
|
216
|
+
* and the country holds a town of that name.
|
|
217
|
+
*
|
|
218
|
+
* `Road Town, British Virgin Islands` parses the town as a venue.
|
|
219
|
+
* This runs ahead of the landmark layer, so an airport alias under `Biggin Hill`
|
|
220
|
+
* yields to the town that holds the name.
|
|
221
|
+
*/
|
|
222
|
+
export async function applyVenueAsTown(roots: AddressNode[], backend: ResolverBackend): Promise<void> {
|
|
223
|
+
const nodes = [...walkNodes(roots)]
|
|
224
|
+
const venue = nodes.find((node) => node.tag === "venue" && !node.placeID && node.value.trim())
|
|
225
|
+
|
|
226
|
+
const country = nodes.find(
|
|
227
|
+
(node) => node.tag === "country" && typeof node.metadata?.["resolver_country"] === "string"
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
if (!venue || !country || nodes.some((node) => node !== venue && node !== country)) return
|
|
231
|
+
|
|
232
|
+
// A name written in two forms, `Derry/Londonderry` or `Biel/Bienne`, is read whole and then as each form.
|
|
233
|
+
const forms = [venue.value, ...venue.value.split("/").map((form) => form.trim())].filter(
|
|
234
|
+
(form, index, all) => form && all.indexOf(form) === index
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
let found: Awaited<ReturnType<ResolverBackend["findPlace"]>> = []
|
|
238
|
+
|
|
239
|
+
for (const form of forms) {
|
|
240
|
+
found = await backend.findPlace({
|
|
241
|
+
text: form,
|
|
242
|
+
placetype: "locality",
|
|
243
|
+
country: country.metadata!["resolver_country"] as string,
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
if (found[0]?.exactMatch) break
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const [town, ...others] = found
|
|
250
|
+
|
|
251
|
+
if (!town?.exactMatch) return
|
|
252
|
+
|
|
253
|
+
venue.tag = "locality"
|
|
254
|
+
decorateNode(venue, town, others)
|
|
255
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
import { walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
5
|
+
import type { ResolverBackend } from "@mailwoman/core/resolver"
|
|
6
|
+
|
|
7
|
+
import { decorateNode } from "#decorate-node"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A character that separates the parts of an address in some script.
|
|
11
|
+
*/
|
|
12
|
+
const SEPARATOR = /[\s,,、;;/|·・]/u
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Reads an input as one place when its whole text names that place and the parse resolved none of its parts.
|
|
16
|
+
*
|
|
17
|
+
* `ミュンヘン` and `裕廊东` hold no separator.
|
|
18
|
+
* The en-US parser reads them as a venue, or splits them into fragments the
|
|
19
|
+
* gazetteer doesn't key, while it keys both whole names.
|
|
20
|
+
*
|
|
21
|
+
* An unspaced address whose parts resolve, or whose whole text identifies no place, keeps its parse.
|
|
22
|
+
* This runs after the landmark layer, so a venue the landmark layer knows keeps that answer.
|
|
23
|
+
*/
|
|
24
|
+
export async function applyWholeInputPlace(
|
|
25
|
+
roots: AddressNode[],
|
|
26
|
+
written: string,
|
|
27
|
+
backend: ResolverBackend,
|
|
28
|
+
country: string | null
|
|
29
|
+
): Promise<void> {
|
|
30
|
+
const text = written.trim()
|
|
31
|
+
const nodes = [...walkNodes(roots)]
|
|
32
|
+
|
|
33
|
+
if (!text || SEPARATOR.test(text) || nodes.some((node) => node.placeID)) return
|
|
34
|
+
|
|
35
|
+
// A lone admin span already looked up its whole text, under the placetype and scope the walk applies.
|
|
36
|
+
// Fragments and a venue span didn't.
|
|
37
|
+
if (nodes.length < 2 && nodes[0]?.tag !== "venue") return
|
|
38
|
+
|
|
39
|
+
const found = await backend.findPlace({ text, ...(country ? { country } : {}) }).catch(() => [])
|
|
40
|
+
const [place, ...others] = found
|
|
41
|
+
|
|
42
|
+
if (!place?.exactMatch) return
|
|
43
|
+
|
|
44
|
+
const whole = { tag: "locality", value: text, start: 0, end: text.length, confidence: 1, children: [] } as AddressNode
|
|
45
|
+
|
|
46
|
+
decorateNode(whole, place, others)
|
|
47
|
+
whole.metadata = { ...whole.metadata, whole_input_place: true }
|
|
48
|
+
roots.splice(0, roots.length, whole)
|
|
49
|
+
}
|
package/lib/resolve.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
5
|
* `resolveTree` walks an `AddressTree` top-down and decorates matched nodes.
|
|
6
|
+
*
|
|
7
7
|
* Child lookups are scoped by resolved parents to reduce ambiguity.
|
|
8
8
|
*/
|
|
9
9
|
|
|
@@ -59,7 +59,14 @@ import {
|
|
|
59
59
|
type ResolutionState,
|
|
60
60
|
type ResolvedOpts,
|
|
61
61
|
} from "#resolve/passes"
|
|
62
|
-
import {
|
|
62
|
+
import {
|
|
63
|
+
applyCountryAsRegion,
|
|
64
|
+
applyLocalityAsRegion,
|
|
65
|
+
applyRegionCity,
|
|
66
|
+
applyStreetAsTown,
|
|
67
|
+
applyVenueAsTown,
|
|
68
|
+
} from "#resolve/region-city"
|
|
69
|
+
import { applyWholeInputPlace } from "#resolve/whole-input"
|
|
63
70
|
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "#street/tier"
|
|
64
71
|
import {
|
|
65
72
|
type CapitalLevelFn,
|
|
@@ -219,6 +226,9 @@ class WOFResolver implements Resolver {
|
|
|
219
226
|
newRoots.push(await this.#walk(root, /* parentResolved */ null, state))
|
|
220
227
|
}
|
|
221
228
|
|
|
229
|
+
// A later pass that gives one of these nodes a different place removes the walk's ancestors with the old place.
|
|
230
|
+
const walkedWithAncestors = collectNodes(newRoots, (n) => n.metadata?.["ancestors"] != null)
|
|
231
|
+
|
|
222
232
|
// Stamp metadata for postcode-country override.
|
|
223
233
|
if (postcodeScope) {
|
|
224
234
|
stampPostcodeCountryScope(newRoots, postcodeScope)
|
|
@@ -243,6 +253,9 @@ class WOFResolver implements Resolver {
|
|
|
243
253
|
// Re-resolve foreign region/locality pairs blocked by locale scope.
|
|
244
254
|
await applyRegionCountryCoherence(newRoots, this.#backend, state.defaultCountry?.country ?? null)
|
|
245
255
|
await applyRegionCity(newRoots, this.#backend)
|
|
256
|
+
await applyCountryAsRegion(newRoots, this.#backend)
|
|
257
|
+
await applyVenueAsTown(newRoots, this.#backend)
|
|
258
|
+
await applyStreetAsTown(newRoots, this.#backend)
|
|
246
259
|
|
|
247
260
|
if (isCallerScope(state.defaultCountry)) {
|
|
248
261
|
await applyLocalityAsRegion(newRoots, this.#backend, state.defaultCountry!.country)
|
|
@@ -255,6 +268,15 @@ class WOFResolver implements Resolver {
|
|
|
255
268
|
await applyLandmark(newRoots, opts.writtenInput ?? tree.raw, opts.landmarks)
|
|
256
269
|
}
|
|
257
270
|
|
|
271
|
+
if (opts.adminCoherence) {
|
|
272
|
+
await applyWholeInputPlace(
|
|
273
|
+
newRoots,
|
|
274
|
+
opts.writtenInput ?? tree.raw,
|
|
275
|
+
this.#backend,
|
|
276
|
+
isCallerScope(state.defaultCountry) ? state.defaultCountry!.country : null
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
258
280
|
// The postcode and rooftop passes hold a locality to the same distance from their point.
|
|
259
281
|
const consistencyThresholdKm = opts.postcodeConsistencyThresholdKm ?? 50
|
|
260
282
|
|
|
@@ -269,7 +291,7 @@ class WOFResolver implements Resolver {
|
|
|
269
291
|
|
|
270
292
|
// Optional exact address-point tier.
|
|
271
293
|
if (opts.addressPoints) {
|
|
272
|
-
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf)
|
|
294
|
+
await applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf)
|
|
273
295
|
}
|
|
274
296
|
|
|
275
297
|
// Optional interpolation tier after exact points.
|
|
@@ -290,7 +312,7 @@ class WOFResolver implements Resolver {
|
|
|
290
312
|
|
|
291
313
|
// Retry exact address-point tier once after span-rescore recovery.
|
|
292
314
|
if (opts.addressPoints) {
|
|
293
|
-
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf)
|
|
315
|
+
await applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf)
|
|
294
316
|
}
|
|
295
317
|
|
|
296
318
|
// A rooftop row keyed by locality name can lie in a namesake of the selected locality.
|
|
@@ -303,6 +325,17 @@ class WOFResolver implements Resolver {
|
|
|
303
325
|
applyStreetCentroid(newRoots, tree.raw, opts.streetCentroids, opts.streetCountryHints ?? [])
|
|
304
326
|
}
|
|
305
327
|
|
|
328
|
+
// The walk's ancestors are read again for the place that replaced its pick.
|
|
329
|
+
if (this.#backend.ancestors) {
|
|
330
|
+
for (const node of walkedWithAncestors) {
|
|
331
|
+
const id = node.placeID?.startsWith("wof:") ? Number(node.placeID.slice(4)) : Number.NaN
|
|
332
|
+
|
|
333
|
+
if (node.metadata?.["ancestors"] == null && Number.isInteger(id)) {
|
|
334
|
+
node.metadata = { ...node.metadata, ancestors: this.#backend.ancestors(id) }
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
306
339
|
return { raw: tree.raw, roots: newRoots }
|
|
307
340
|
}
|
|
308
341
|
|
|
@@ -529,7 +562,7 @@ class WOFResolver implements Resolver {
|
|
|
529
562
|
rec.reachable(await this.#probeOtherBands(query, placetype))
|
|
530
563
|
}
|
|
531
564
|
} catch (error) {
|
|
532
|
-
// Defensive: backend errors
|
|
565
|
+
// Defensive: backend errors shouldn't abort the whole tree walk.
|
|
533
566
|
rec.check(`backend_error: ${(error as Error).message}`)
|
|
534
567
|
rec.emit(null)
|
|
535
568
|
|
package/lib/span-rescore.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
+
* Recover a dropped or fragmented locality from the raw text when a parse fails to resolve,
|
|
6
|
+
* by matching contiguous raw-token spans against the same-country gazetteer.
|
|
5
7
|
*
|
|
6
|
-
*
|
|
7
|
-
* matching contiguous raw-token spans against the same-country gazetteer. Default-on with an
|
|
8
|
-
* explicit `ResolveOpts.spanRescore: false` opt-out.
|
|
8
|
+
* Default-on with an explicit `ResolveOpts.spanRescore: false` opt-out.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { firstNodeWhere, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
|
|
@@ -60,7 +60,7 @@ export interface SpanRescoreOptions {
|
|
|
60
60
|
*/
|
|
61
61
|
postalCompoundRecovery?: boolean
|
|
62
62
|
/**
|
|
63
|
-
* Returns whether a word is a POI category, so a venue's last word
|
|
63
|
+
* Returns whether a word is a POI category, so a venue's last word isn't re-read as a locality.
|
|
64
64
|
* A caller without the taxonomy leaves every span eligible.
|
|
65
65
|
*/
|
|
66
66
|
isVenueCategoryWord?: (word: string) => boolean
|
|
@@ -77,14 +77,6 @@ export interface SpanRescoreOptions {
|
|
|
77
77
|
* @defaultValue {@linkcode DEFAULT_COUNTRY_PRIOR_WEIGHT} (2), and 0 removes the locale's say entirely.
|
|
78
78
|
*/
|
|
79
79
|
bareToponymCountryWeight?: number
|
|
80
|
-
/**
|
|
81
|
-
* Admit a proper sub-span only when every token it leaves behind is a subdivision code or a number.
|
|
82
|
-
*
|
|
83
|
-
* See `remainderIsContext` for the rule.
|
|
84
|
-
*
|
|
85
|
-
* @defaultValue `false`
|
|
86
|
-
*/
|
|
87
|
-
spanRescoreRequireContextRemainder?: boolean
|
|
88
80
|
}
|
|
89
81
|
|
|
90
82
|
/**
|
|
@@ -116,6 +108,13 @@ export interface RescoreCandidate {
|
|
|
116
108
|
* so the top-1-vs-top-2 dominance margin stays computable.
|
|
117
109
|
*/
|
|
118
110
|
alternatives: ResolvedPlace[]
|
|
111
|
+
/**
|
|
112
|
+
* Whether the span is a proper sub-span that leaves a word of the name behind,
|
|
113
|
+
* as `Manchester` leaves `The` and `Art Gallery` in `The Manchester Art Gallery`.
|
|
114
|
+
*
|
|
115
|
+
* Such a span may scope a venue lookup, but it isn't by itself an answer to the input.
|
|
116
|
+
*/
|
|
117
|
+
dropsName: boolean
|
|
119
118
|
}
|
|
120
119
|
|
|
121
120
|
interface RawTok {
|
|
@@ -158,7 +157,7 @@ export function postcodeCodeSubset(postcode: string): string {
|
|
|
158
157
|
const BARE_TOPONYM_FETCH = 20
|
|
159
158
|
|
|
160
159
|
/**
|
|
161
|
-
* True when the tree
|
|
160
|
+
* True when the tree identifies its own admin context — a region, subregion, country,
|
|
162
161
|
* postcode or house number the parser read out of the input — so the soft-country
|
|
163
162
|
* prior stands down and the hard filter keeps its say.
|
|
164
163
|
*/
|
|
@@ -194,7 +193,7 @@ function resolvedWeakly(node: AddressNode, reading: WeakResolutionReading): bool
|
|
|
194
193
|
|
|
195
194
|
/**
|
|
196
195
|
* True if any node in the tree already has a resolved place id — the brake on span rescore.
|
|
197
|
-
* with `weakReading`, a node whose resolution is weak under that reading
|
|
196
|
+
* with `weakReading`, a node whose resolution is weak under that reading doesn't hold it.
|
|
198
197
|
*/
|
|
199
198
|
export function hasResolvedPlace(
|
|
200
199
|
roots: readonly AddressNode[],
|
|
@@ -209,7 +208,7 @@ export function hasResolvedPlace(
|
|
|
209
208
|
*
|
|
210
209
|
* The whole span stays probeable.
|
|
211
210
|
* Single-token spans are excluded.
|
|
212
|
-
* The guard
|
|
211
|
+
* The guard doesn't depend on confidence.
|
|
213
212
|
*/
|
|
214
213
|
function multiTokenNameInteriors(roots: readonly AddressNode[], raw: string): Array<[number, number]> {
|
|
215
214
|
const out: Array<[number, number]> = []
|
|
@@ -231,7 +230,7 @@ function multiTokenNameInteriors(roots: readonly AddressNode[], raw: string): Ar
|
|
|
231
230
|
/**
|
|
232
231
|
* The confident ranges split by what a span containing one means: `hard`
|
|
233
232
|
* (house number, postcode, street body) refuses any overlap, while `affix` refuses
|
|
234
|
-
* a span only when it
|
|
233
|
+
* a span only when it doesn't strictly contain the affix.
|
|
235
234
|
*/
|
|
236
235
|
interface ConfidentRanges {
|
|
237
236
|
hard: Array<[number, number]>
|
|
@@ -240,7 +239,7 @@ interface ConfidentRanges {
|
|
|
240
239
|
|
|
241
240
|
/**
|
|
242
241
|
* The character ranges the parse read as street material, context only
|
|
243
|
-
* when the range
|
|
242
|
+
* when the range doesn't overlap the span under test.
|
|
244
243
|
*/
|
|
245
244
|
function streetRanges(roots: readonly AddressNode[]): Array<[number, number]> {
|
|
246
245
|
const out: Array<[number, number]> = []
|
|
@@ -405,12 +404,22 @@ export async function findRescoreCandidate(
|
|
|
405
404
|
/**
|
|
406
405
|
* Whether every token a sub-span leaves behind is context rather than identity,
|
|
407
406
|
* testing the remainder's shape rather than the span being proper.
|
|
407
|
+
*
|
|
408
|
+
* A span that's its own comma-separated segment states the location,
|
|
409
|
+
* as `Kalkaska` does in `Evergreen Cemetery, Kalkaska`.
|
|
410
|
+
* Otherwise each remaining token must be a subdivision code, a number,
|
|
411
|
+
* or part of a street the parse read beside the span.
|
|
408
412
|
*/
|
|
409
413
|
const remainderIsContext = (span: { start: number; end: number }): boolean => {
|
|
410
414
|
const outside = toks.filter((t) => t.end <= span.start || t.start >= span.end)
|
|
411
415
|
|
|
412
416
|
if (!outside.length) return true
|
|
413
417
|
|
|
418
|
+
const before = raw.slice(0, span.start).trimEnd()
|
|
419
|
+
const after = raw.slice(span.end).trimStart()
|
|
420
|
+
|
|
421
|
+
if ((!before || before.endsWith(",")) && (!after || after.startsWith(","))) return true
|
|
422
|
+
|
|
414
423
|
const streetContext = streets.filter(([start, end]) => end <= span.start || start >= span.end)
|
|
415
424
|
|
|
416
425
|
return outside.every(
|
|
@@ -430,6 +439,11 @@ export async function findRescoreCandidate(
|
|
|
430
439
|
|
|
431
440
|
const venueCoversInput = !!venue && !!wholeInput && venue.start <= wholeInput.start && wholeInput.end <= venue.end
|
|
432
441
|
|
|
442
|
+
// The first hit whose span drops a word of the name.
|
|
443
|
+
// A later span that keeps the name answers ahead of it, as `Kalkaska` does over
|
|
444
|
+
// `Evergreen` in `Evergreen Cemetery, Kalkaska`.
|
|
445
|
+
let nameDropping: RescoreCandidate | null = null
|
|
446
|
+
|
|
433
447
|
for (const sp of spans) {
|
|
434
448
|
const key = foldName(sp.text)
|
|
435
449
|
|
|
@@ -443,9 +457,9 @@ export async function findRescoreCandidate(
|
|
|
443
457
|
// Another last word can state the location, as `London` in `Tower of London`.
|
|
444
458
|
if (venue && sp.end === venue.end && sp.start > venue.start && opts.isVenueCategoryWord?.(sp.text)) continue
|
|
445
459
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if (
|
|
460
|
+
const dropsName = !wholeSpan && !remainderIsContext(sp)
|
|
461
|
+
|
|
462
|
+
if (nameDropping && dropsName) continue
|
|
449
463
|
|
|
450
464
|
const bare = softCountryEligible && wholeSpan
|
|
451
465
|
const qualifier = wholeSpan ? null : qualifierRemainder(sp)
|
|
@@ -492,19 +506,26 @@ export async function findRescoreCandidate(
|
|
|
492
506
|
const withinThreshold = (p: ResolvedPlace): boolean =>
|
|
493
507
|
!anchor || thresholdKm <= 0 || haversine(anchor.lat, anchor.lon, p.lat, p.lon) <= thresholdKm
|
|
494
508
|
|
|
495
|
-
|
|
496
|
-
if (!withinThreshold(h)) continue
|
|
509
|
+
const h = exact.find(withinThreshold)
|
|
497
510
|
|
|
511
|
+
if (h) {
|
|
498
512
|
// `postcodeVerified` is true only when an anchor existed and validated this match within
|
|
499
513
|
// `thresholdKm`; otherwise the match is returned unrestricted but flagged lower-precision.
|
|
500
|
-
|
|
514
|
+
const candidate: RescoreCandidate = {
|
|
501
515
|
text: sp.text,
|
|
502
516
|
start: sp.start,
|
|
503
517
|
end: sp.end,
|
|
504
518
|
place: h,
|
|
505
519
|
postcodeVerified: anchor !== null,
|
|
506
520
|
alternatives: exact.filter((a) => a !== h && withinThreshold(a)),
|
|
521
|
+
dropsName,
|
|
507
522
|
}
|
|
523
|
+
|
|
524
|
+
if (!dropsName) return candidate
|
|
525
|
+
|
|
526
|
+
nameDropping = candidate
|
|
527
|
+
|
|
528
|
+
continue
|
|
508
529
|
}
|
|
509
530
|
|
|
510
531
|
// A whole-input venue can be a sub-locality row, as `Golden Gate Park` is a neighborhood,
|
|
@@ -520,11 +541,14 @@ export async function findRescoreCandidate(
|
|
|
520
541
|
place: sublocality,
|
|
521
542
|
postcodeVerified: anchor !== null,
|
|
522
543
|
alternatives: [],
|
|
544
|
+
dropsName,
|
|
523
545
|
}
|
|
524
546
|
}
|
|
525
547
|
}
|
|
526
548
|
}
|
|
527
549
|
|
|
550
|
+
if (nameDropping) return nameDropping
|
|
551
|
+
|
|
528
552
|
// Joint country recovery: when the scoped pass finds no match and a postcode
|
|
529
553
|
// is present, re-probe unscoped and accept a cross-country promotion only
|
|
530
554
|
// when its code subset resolves postcode-verified within the radius.
|
|
@@ -556,7 +580,16 @@ export async function findRescoreCandidate(
|
|
|
556
580
|
// Admission uses per-candidate postcode verification.
|
|
557
581
|
// The postcode already picked the country, so this pass has no remaining
|
|
558
582
|
// country ambiguity to report.
|
|
559
|
-
|
|
583
|
+
// The postcode verified the place, so the span answers even when it drops a word of the name.
|
|
584
|
+
return {
|
|
585
|
+
text: sp.text,
|
|
586
|
+
start: sp.start,
|
|
587
|
+
end: sp.end,
|
|
588
|
+
place: h,
|
|
589
|
+
postcodeVerified: true,
|
|
590
|
+
alternatives: [],
|
|
591
|
+
dropsName: false,
|
|
592
|
+
}
|
|
560
593
|
}
|
|
561
594
|
}
|
|
562
595
|
}
|
package/lib/street/evidence.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*/
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
6
3
|
|
|
7
4
|
import type { Exclusion } from "@mailwoman/evidence"
|
|
8
5
|
|