@mailwoman/resolver 11.0.0 → 11.2.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 +4 -4
- package/lib/admin/coherence-passes.ts +14 -20
- package/lib/admin/containment.ts +6 -5
- package/lib/admin/county-designator.ts +91 -0
- 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 +51 -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 +5 -8
- 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 +19 -9
- package/lib/resolve/region-city.ts +192 -8
- package/lib/resolve/whole-input.ts +49 -0
- package/lib/resolve.ts +197 -48
- package/lib/span-rescore.ts +61 -28
- 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/county-designator.d.ts +33 -0
- package/out/admin/county-designator.d.ts.map +1 -0
- package/out/admin/county-designator.js +65 -0
- package/out/admin/county-designator.js.map +1 -0
- package/out/admin/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 +46 -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 +4 -7
- 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 +7 -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 +124 -46
- 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 +46 -19
- 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
|
@@ -1,9 +1,7 @@
|
|
|
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
|
import { walkNodes } from "@mailwoman/core/decoder";
|
|
4
|
+
import { haversine } from "@mailwoman/spatial/geometries/point/distance";
|
|
7
5
|
import { decorateNode } from "#decorate-node";
|
|
8
6
|
import { streetNumberPairs } from "#street/tier";
|
|
9
7
|
/**
|
|
@@ -16,12 +14,19 @@ import { streetNumberPairs } from "#street/tier";
|
|
|
16
14
|
*
|
|
17
15
|
* A place held at both levels occurs wherever a city is its own province, so the test reads the hierarchy.
|
|
18
16
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
17
|
+
* `Altona, Hamburg` and `Triana, Sevilla` state a resolved district, and a district also sits inside a city.
|
|
18
|
+
*
|
|
19
|
+
* A bare region stays a region, because only a street and number or a district
|
|
20
|
+
* say the input means somewhere inside a city.
|
|
21
|
+
*
|
|
22
|
+
* A locality the gazetteer didn't resolve leaves the region as the only place,
|
|
23
|
+
* and Bristol's region point lies in the Bristol Channel, so the city answers there too.
|
|
21
24
|
*/
|
|
22
25
|
export async function applyRegionCity(roots, backend) {
|
|
23
26
|
const nodes = [...walkNodes(roots)];
|
|
24
|
-
|
|
27
|
+
// A street with a number, or a resolved district, places the input inside a city.
|
|
28
|
+
const insideACity = streetNumberPairs(roots).length > 0 || nodes.some((node) => node.tag === "dependent_locality" && node.placeID);
|
|
29
|
+
if (nodes.some((node) => node.tag === "locality" && node.placeID) || !insideACity)
|
|
25
30
|
return;
|
|
26
31
|
const region = nodes.find((node) => node.tag === "region" && node.placeID?.startsWith("wof:"));
|
|
27
32
|
if (!region)
|
|
@@ -37,6 +42,19 @@ export async function applyRegionCity(roots, backend) {
|
|
|
37
42
|
region.tag = "locality";
|
|
38
43
|
decorateNode(region, city, others);
|
|
39
44
|
region.metadata = { ...region.metadata, region_city: true };
|
|
45
|
+
// A district inside the city's box answers ahead of the city, as the walk's containment stamp lets it.
|
|
46
|
+
for (const district of nodes) {
|
|
47
|
+
if (district.tag !== "dependent_locality" || !district.placeID || district.lat == null || district.lon == null)
|
|
48
|
+
continue;
|
|
49
|
+
const box = city.bbox;
|
|
50
|
+
if (box &&
|
|
51
|
+
district.lat >= box.minLat &&
|
|
52
|
+
district.lat <= box.maxLat &&
|
|
53
|
+
district.lon >= box.minLon &&
|
|
54
|
+
district.lon <= box.maxLon) {
|
|
55
|
+
district.metadata = { ...district.metadata, contained_by_parent: true };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
40
58
|
}
|
|
41
59
|
/**
|
|
42
60
|
* Reads a locality the gazetteer holds only as a region, inside a country the input states.
|
|
@@ -55,4 +73,137 @@ export async function applyLocalityAsRegion(roots, backend, country) {
|
|
|
55
73
|
locality.tag = "region";
|
|
56
74
|
decorateNode(locality, region, others);
|
|
57
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Reads a country as the region of the same name when only that region holds the stated town.
|
|
78
|
+
*
|
|
79
|
+
* `Savannah, Georgia` parses `Georgia` as the country, and the country holds no Savannah.
|
|
80
|
+
* `Tbilisi, Georgia` keeps the country, because the country holds Tbilisi.
|
|
81
|
+
*
|
|
82
|
+
* A caller's US scope leaves the country of Georgia unresolved, so that node has no country to test against.
|
|
83
|
+
*/
|
|
84
|
+
export async function applyCountryAsRegion(roots, backend) {
|
|
85
|
+
const nodes = [...walkNodes(roots)];
|
|
86
|
+
const country = nodes.find((node) => node.tag === "country" && node.value.trim());
|
|
87
|
+
const locality = nodes.find((node) => node.tag === "locality" && node.value.trim());
|
|
88
|
+
if (!country || !locality || nodes.some((node) => node.tag === "region"))
|
|
89
|
+
return;
|
|
90
|
+
const stated = country.metadata?.["resolver_country"];
|
|
91
|
+
if (typeof stated === "string") {
|
|
92
|
+
const [local] = await backend.findPlace({ text: locality.value, placetype: "locality", country: stated });
|
|
93
|
+
if (local?.exactMatch)
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
for (const region of await backend.findPlace({ text: country.value, placetype: "region" })) {
|
|
97
|
+
if (!region.exactMatch)
|
|
98
|
+
continue;
|
|
99
|
+
const [city, ...others] = await backend.findPlace({
|
|
100
|
+
text: locality.value,
|
|
101
|
+
placetype: "locality",
|
|
102
|
+
parentID: region.id,
|
|
103
|
+
});
|
|
104
|
+
if (!city?.exactMatch || city.regionScopeMiss)
|
|
105
|
+
continue;
|
|
106
|
+
country.tag = "region";
|
|
107
|
+
decorateNode(country, region, []);
|
|
108
|
+
decorateNode(locality, city, others);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The tags whose spans together write a street's name.
|
|
114
|
+
*/
|
|
115
|
+
const STREET_PART_TAGS = new Set([
|
|
116
|
+
"street_prefix",
|
|
117
|
+
"street_prefix_particle",
|
|
118
|
+
"street",
|
|
119
|
+
"street_suffix",
|
|
120
|
+
]);
|
|
121
|
+
/**
|
|
122
|
+
* How far a town may sit from the stated postcode's point and still be the place the postcode serves.
|
|
123
|
+
*/
|
|
124
|
+
const TOWN_POSTCODE_REACH_KM = 15;
|
|
125
|
+
/**
|
|
126
|
+
* Removes the given nodes wherever they sit in the tree.
|
|
127
|
+
*/
|
|
128
|
+
function removeNodes(list, drop) {
|
|
129
|
+
for (let index = list.length - 1; index >= 0; index--) {
|
|
130
|
+
const node = list[index];
|
|
131
|
+
if (drop.has(node)) {
|
|
132
|
+
list.splice(index, 1);
|
|
133
|
+
}
|
|
134
|
+
else if (node.children?.length) {
|
|
135
|
+
removeNodes(node.children, drop);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Reads an unnumbered street as the town of that name when the town holds the stated postcode.
|
|
141
|
+
*
|
|
142
|
+
* `Alameda, CA 94501` and `Crocketts Bluff, AR 72038` parse the town as a street,
|
|
143
|
+
* because a name before a region and postcode is where a street sits in a full address.
|
|
144
|
+
* A street name and a town name agree this close to the postcode's point only when they
|
|
145
|
+
* name one place, so a real street such as `Main Street, CA 94501` keeps its tag.
|
|
146
|
+
*/
|
|
147
|
+
export async function applyStreetAsTown(roots, backend) {
|
|
148
|
+
const nodes = [...walkNodes(roots)];
|
|
149
|
+
const parts = nodes.filter((node) => STREET_PART_TAGS.has(node.tag)).toSorted((a, b) => a.start - b.start);
|
|
150
|
+
const postcode = nodes.find((node) => node.tag === "postcode" && node.lat != null && node.lon != null);
|
|
151
|
+
if (!parts.some((node) => node.tag === "street") || !postcode)
|
|
152
|
+
return;
|
|
153
|
+
if (nodes.some((node) => node.tag === "house_number" || node.tag === "locality" || node.tag === "venue"))
|
|
154
|
+
return;
|
|
155
|
+
const region = nodes.find((node) => node.tag === "region" && node.placeID?.startsWith("wof:"));
|
|
156
|
+
const country = postcode.metadata?.["resolver_country"];
|
|
157
|
+
if (!region && typeof country !== "string")
|
|
158
|
+
return;
|
|
159
|
+
const name = parts.map((node) => node.value.trim()).join(" ");
|
|
160
|
+
const [town, ...others] = await backend.findPlace({
|
|
161
|
+
text: name,
|
|
162
|
+
placetype: "locality",
|
|
163
|
+
...(region ? { parentID: Number(region.placeID.slice("wof:".length)) } : { country: country }),
|
|
164
|
+
});
|
|
165
|
+
if (!town?.exactMatch || town.regionScopeMiss || town.lat == null || town.lon == null)
|
|
166
|
+
return;
|
|
167
|
+
if (haversine(postcode.lat, postcode.lon, town.lat, town.lon, "km") > TOWN_POSTCODE_REACH_KM)
|
|
168
|
+
return;
|
|
169
|
+
const [first, ...rest] = parts;
|
|
170
|
+
first.tag = "locality";
|
|
171
|
+
first.value = name;
|
|
172
|
+
first.end = parts.at(-1).end;
|
|
173
|
+
removeNodes(roots, new Set(rest));
|
|
174
|
+
decorateNode(first, town, others);
|
|
175
|
+
first.metadata = { ...first.metadata, street_as_town: true };
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Reads a venue as a town when the input states only a country beside it
|
|
179
|
+
* and the country holds a town of that name.
|
|
180
|
+
*
|
|
181
|
+
* `Road Town, British Virgin Islands` parses the town as a venue.
|
|
182
|
+
* This runs ahead of the landmark layer, so an airport alias under `Biggin Hill`
|
|
183
|
+
* yields to the town that holds the name.
|
|
184
|
+
*/
|
|
185
|
+
export async function applyVenueAsTown(roots, backend) {
|
|
186
|
+
const nodes = [...walkNodes(roots)];
|
|
187
|
+
const venue = nodes.find((node) => node.tag === "venue" && !node.placeID && node.value.trim());
|
|
188
|
+
const country = nodes.find((node) => node.tag === "country" && typeof node.metadata?.["resolver_country"] === "string");
|
|
189
|
+
if (!venue || !country || nodes.some((node) => node !== venue && node !== country))
|
|
190
|
+
return;
|
|
191
|
+
// A name written in two forms, `Derry/Londonderry` or `Biel/Bienne`, is read whole and then as each form.
|
|
192
|
+
const forms = [venue.value, ...venue.value.split("/").map((form) => form.trim())].filter((form, index, all) => form && all.indexOf(form) === index);
|
|
193
|
+
let found = [];
|
|
194
|
+
for (const form of forms) {
|
|
195
|
+
found = await backend.findPlace({
|
|
196
|
+
text: form,
|
|
197
|
+
placetype: "locality",
|
|
198
|
+
country: country.metadata["resolver_country"],
|
|
199
|
+
});
|
|
200
|
+
if (found[0]?.exactMatch)
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
const [town, ...others] = found;
|
|
204
|
+
if (!town?.exactMatch)
|
|
205
|
+
return;
|
|
206
|
+
venue.tag = "locality";
|
|
207
|
+
decorateNode(venue, town, others);
|
|
208
|
+
}
|
|
58
209
|
//# sourceMappingURL=region-city.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"region-city.js","sourceRoot":"","sources":["../../lib/resolve/region-city.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"region-city.js","sourceRoot":"","sources":["../../lib/resolve/region-city.ts"],"names":[],"mappings":"AAAA,kEAAkE;AACl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IAAI,IAAI,KAAK,OAAO,CAAC;QAAE,OAAM;IAE1F,0GAA0G;IAC1G,MAAM,KAAK,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,MAAM,CACvF,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,KAAK,CACzD,CAAA;IAED,IAAI,KAAK,GAAsD,EAAE,CAAA;IAEjE,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,KAAK,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC;YAC/B,IAAI,EAAE,IAAI;YACV,SAAS,EAAE,UAAU;YACrB,OAAO,EAAE,OAAO,CAAC,QAAS,CAAC,kBAAkB,CAAW;SACxD,CAAC,CAAA;QAEF,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,UAAU;YAAE,MAAK;IAChC,CAAC;IAED,MAAM,CAAC,IAAI,EAAE,GAAG,MAAM,CAAC,GAAG,KAAK,CAAA;IAE/B,IAAI,CAAC,IAAI,EAAE,UAAU;QAAE,OAAM;IAE7B,KAAK,CAAC,GAAG,GAAG,UAAU,CAAA;IACtB,YAAY,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,CAAA;AAClC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AddressNode } from "@mailwoman/core/decoder";
|
|
2
|
+
import type { ResolverBackend } from "@mailwoman/core/resolver";
|
|
3
|
+
/**
|
|
4
|
+
* Reads an input as one place when its whole text names that place and the parse resolved none of its parts.
|
|
5
|
+
*
|
|
6
|
+
* `ミュンヘン` and `裕廊东` hold no separator.
|
|
7
|
+
* The en-US parser reads them as a venue, or splits them into fragments the
|
|
8
|
+
* gazetteer doesn't key, while it keys both whole names.
|
|
9
|
+
*
|
|
10
|
+
* An unspaced address whose parts resolve, or whose whole text identifies no place, keeps its parse.
|
|
11
|
+
* This runs after the landmark layer, so a venue the landmark layer knows keeps that answer.
|
|
12
|
+
*/
|
|
13
|
+
export declare function applyWholeInputPlace(roots: AddressNode[], written: string, backend: ResolverBackend, country: string | null): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=whole-input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"whole-input.d.ts","sourceRoot":"","sources":["../../lib/resolve/whole-input.ts"],"names":[],"mappings":"AAGA,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAS/D;;;;;;;;;GASG;AACH,wBAAsB,oBAAoB,CACzC,KAAK,EAAE,WAAW,EAAE,EACpB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,eAAe,EACxB,OAAO,EAAE,MAAM,GAAG,IAAI,GACpB,OAAO,CAAC,IAAI,CAAC,CAoBf"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
import { walkNodes } from "@mailwoman/core/decoder";
|
|
4
|
+
import { decorateNode } from "#decorate-node";
|
|
5
|
+
/**
|
|
6
|
+
* A character that separates the parts of an address in some script.
|
|
7
|
+
*/
|
|
8
|
+
const SEPARATOR = /[\s,,、;;/|·・]/u;
|
|
9
|
+
/**
|
|
10
|
+
* Reads an input as one place when its whole text names that place and the parse resolved none of its parts.
|
|
11
|
+
*
|
|
12
|
+
* `ミュンヘン` and `裕廊东` hold no separator.
|
|
13
|
+
* The en-US parser reads them as a venue, or splits them into fragments the
|
|
14
|
+
* gazetteer doesn't key, while it keys both whole names.
|
|
15
|
+
*
|
|
16
|
+
* An unspaced address whose parts resolve, or whose whole text identifies no place, keeps its parse.
|
|
17
|
+
* This runs after the landmark layer, so a venue the landmark layer knows keeps that answer.
|
|
18
|
+
*/
|
|
19
|
+
export async function applyWholeInputPlace(roots, written, backend, country) {
|
|
20
|
+
const text = written.trim();
|
|
21
|
+
const nodes = [...walkNodes(roots)];
|
|
22
|
+
if (!text || SEPARATOR.test(text) || nodes.some((node) => node.placeID))
|
|
23
|
+
return;
|
|
24
|
+
// A lone admin span already looked up its whole text, under the placetype and scope the walk applies.
|
|
25
|
+
// Fragments and a venue span didn't.
|
|
26
|
+
if (nodes.length < 2 && nodes[0]?.tag !== "venue")
|
|
27
|
+
return;
|
|
28
|
+
const found = await backend.findPlace({ text, ...(country ? { country } : {}) }).catch(() => []);
|
|
29
|
+
const [place, ...others] = found;
|
|
30
|
+
if (!place?.exactMatch)
|
|
31
|
+
return;
|
|
32
|
+
const whole = { tag: "locality", value: text, start: 0, end: text.length, confidence: 1, children: [] };
|
|
33
|
+
decorateNode(whole, place, others);
|
|
34
|
+
whole.metadata = { ...whole.metadata, whole_input_place: true };
|
|
35
|
+
roots.splice(0, roots.length, whole);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=whole-input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"whole-input.js","sourceRoot":"","sources":["../../lib/resolve/whole-input.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;AAElE,OAAO,EAAE,SAAS,EAAoB,MAAM,yBAAyB,CAAA;AAGrE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAE7C;;GAEG;AACH,MAAM,SAAS,GAAG,gBAAgB,CAAA;AAElC;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACzC,KAAoB,EACpB,OAAe,EACf,OAAwB,EACxB,OAAsB;IAEtB,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI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
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* `resolveTree` walks an `AddressTree` top-down and decorates matched nodes.
|
|
7
|
-
* Child lookups are scoped by resolved parents to reduce ambiguity.
|
|
8
|
-
*/
|
|
9
1
|
import { type Resolver, type ResolverBackend } from "@mailwoman/core/resolver";
|
|
10
2
|
/**
|
|
11
3
|
* Build a `Resolver` from a `ResolverBackend`.
|
package/out/resolve.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../lib/resolve.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../lib/resolve.ts"],"names":[],"mappings":"AAaA,OAAO,EAKN,KAAK,QAAQ,EACb,KAAK,eAAe,EAEpB,MAAM,0BAA0B,CAAA;AA2DjC;;GAEG;AACH,OAAO,EAAE,4BAA4B,EAAE,MAAM,iBAAiB,CAAA;AAE9D,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,eAAe,GAAG,QAAQ,CAEpE"}
|
package/out/resolve.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* `resolveTree` walks an `AddressTree` top-down and decorates matched nodes.
|
|
7
|
-
* Child lookups are scoped by resolved parents to reduce ambiguity.
|
|
8
|
-
*/
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
9
3
|
import { matchCountry, matchSubdivision } from "@mailwoman/codex/country";
|
|
10
4
|
import { DEFAULT_PLACETYPE_MAP, isPlacetypeFallback } from "@mailwoman/codex/placetype-map";
|
|
11
5
|
import { collectNodes, isShapeExcludedPostcode } from "@mailwoman/core/decoder";
|
|
@@ -13,6 +7,7 @@ import { resolveSwitches, } from "@mailwoman/core/resolver";
|
|
|
13
7
|
import { countriesFromPostcodeFormat } from "@mailwoman/postcode/postcode-format";
|
|
14
8
|
import { applyAdminCoherence, applyExplicitCountryCoherence, applyParentFallbackContradiction, applyRegionCountryCoherence, } from "#admin/coherence-passes";
|
|
15
9
|
import { adminContainmentVerdict, firstRegionQualifier, partitionByContainment } from "#admin/containment";
|
|
10
|
+
import { COUNTY_DESIGNATOR_FALLBACK, countyFromDesignatedLocality } from "#admin/county-designator";
|
|
16
11
|
import { resolveCompoundMunicipality } from "#admin/jp-municipality";
|
|
17
12
|
import { BARE_REGION_DOMINANCE_LOG10, bareCountryCandidate, bareRegionCandidate, emptyLocalityPick, logPopulation, loneBareLocalityNode, } from "#bare-toponym-race";
|
|
18
13
|
import { decorateNode } from "#decorate-node";
|
|
@@ -22,7 +17,8 @@ import { applyPostcodeShapeCoherence } from "#postcode/shape-coherence";
|
|
|
22
17
|
import { applyCountryCode, readRegionCodeAsCountry } from "#resolve/country-code";
|
|
23
18
|
import { applyLandmark } from "#resolve/landmark";
|
|
24
19
|
import { applyPostcodeConsistency, applyRooftopConsistency, applySpanRescore, createNodeTraceRecorder, DIAGNOSTIC_BANDS, firstPostcodeValue, NOOP_TRACE_RECORDER, } from "#resolve/passes";
|
|
25
|
-
import { applyLocalityAsRegion, applyRegionCity } from "#resolve/region-city";
|
|
20
|
+
import { applyCountryAsRegion, applyLocalityAsRegion, applyRegionCity, applyStreetAsTown, applyVenueAsTown, } from "#resolve/region-city";
|
|
21
|
+
import { applyWholeInputPlace } from "#resolve/whole-input";
|
|
26
22
|
import { applyAddressPoint, applyInterpolation, applyStreetCentroid } from "#street/tier";
|
|
27
23
|
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, promoteCapitals, rankByCountryPrior, rankByImportance, } from "#toponym-prior";
|
|
28
24
|
/**
|
|
@@ -118,6 +114,7 @@ class WOFResolver {
|
|
|
118
114
|
// Optional admin containment rerank uses a pre-scanned region qualifier.
|
|
119
115
|
adminContainmentRerank: opts.adminContainmentRerank,
|
|
120
116
|
dependentLocalityContainment: opts.dependentLocalityContainment,
|
|
117
|
+
countyDesignatorFallback: opts.countyDesignatorFallback,
|
|
121
118
|
regionQualifier: firstRegionQualifier(tree.roots),
|
|
122
119
|
...(opts.traceSink ? { traceSink: opts.traceSink } : {}),
|
|
123
120
|
};
|
|
@@ -152,6 +149,8 @@ class WOFResolver {
|
|
|
152
149
|
for (const root of tree.roots) {
|
|
153
150
|
newRoots.push(await this.#walk(root, /* parentResolved */ null, state));
|
|
154
151
|
}
|
|
152
|
+
// A later pass that gives one of these nodes a different place removes the walk's ancestors with the old place.
|
|
153
|
+
const walkedWithAncestors = collectNodes(newRoots, (n) => n.metadata?.["ancestors"] != null);
|
|
155
154
|
// Stamp metadata for postcode-country override.
|
|
156
155
|
if (postcodeScope) {
|
|
157
156
|
stampPostcodeCountryScope(newRoots, postcodeScope);
|
|
@@ -174,6 +173,9 @@ class WOFResolver {
|
|
|
174
173
|
// Re-resolve foreign region/locality pairs blocked by locale scope.
|
|
175
174
|
await applyRegionCountryCoherence(newRoots, this.#backend, state.defaultCountry?.country ?? null);
|
|
176
175
|
await applyRegionCity(newRoots, this.#backend);
|
|
176
|
+
await applyCountryAsRegion(newRoots, this.#backend);
|
|
177
|
+
await applyVenueAsTown(newRoots, this.#backend);
|
|
178
|
+
await applyStreetAsTown(newRoots, this.#backend);
|
|
177
179
|
if (isCallerScope(state.defaultCountry)) {
|
|
178
180
|
await applyLocalityAsRegion(newRoots, this.#backend, state.defaultCountry.country);
|
|
179
181
|
}
|
|
@@ -182,6 +184,9 @@ class WOFResolver {
|
|
|
182
184
|
if (opts.landmarks) {
|
|
183
185
|
await applyLandmark(newRoots, opts.writtenInput ?? tree.raw, opts.landmarks);
|
|
184
186
|
}
|
|
187
|
+
if (opts.adminCoherence) {
|
|
188
|
+
await applyWholeInputPlace(newRoots, opts.writtenInput ?? tree.raw, this.#backend, isCallerScope(state.defaultCountry) ? state.defaultCountry.country : null);
|
|
189
|
+
}
|
|
185
190
|
// The postcode and rooftop passes hold a locality to the same distance from their point.
|
|
186
191
|
const consistencyThresholdKm = opts.postcodeConsistencyThresholdKm ?? 50;
|
|
187
192
|
// Postcode/locality consistency pass (default on).
|
|
@@ -193,7 +198,7 @@ class WOFResolver {
|
|
|
193
198
|
const ancestorsOf = this.#backend.ancestors?.bind(this.#backend);
|
|
194
199
|
// Optional exact address-point tier.
|
|
195
200
|
if (opts.addressPoints) {
|
|
196
|
-
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf);
|
|
201
|
+
await applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf);
|
|
197
202
|
}
|
|
198
203
|
// Optional interpolation tier after exact points.
|
|
199
204
|
if (opts.interpolation) {
|
|
@@ -210,7 +215,7 @@ class WOFResolver {
|
|
|
210
215
|
}
|
|
211
216
|
// Retry exact address-point tier once after span-rescore recovery.
|
|
212
217
|
if (opts.addressPoints) {
|
|
213
|
-
applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf);
|
|
218
|
+
await applyAddressPoint(newRoots, opts.addressPoints, opts.addressPointBboxFallback, ancestorsOf);
|
|
214
219
|
}
|
|
215
220
|
// A rooftop row keyed by locality name can lie in a namesake of the selected locality.
|
|
216
221
|
if (opts.addressPoints && opts.postcodeConsistency) {
|
|
@@ -220,6 +225,15 @@ class WOFResolver {
|
|
|
220
225
|
if (opts.streetCentroids) {
|
|
221
226
|
applyStreetCentroid(newRoots, tree.raw, opts.streetCentroids, opts.streetCountryHints ?? []);
|
|
222
227
|
}
|
|
228
|
+
// The walk's ancestors are read again for the place that replaced its pick.
|
|
229
|
+
if (this.#backend.ancestors) {
|
|
230
|
+
for (const node of walkedWithAncestors) {
|
|
231
|
+
const id = node.placeID?.startsWith("wof:") ? Number(node.placeID.slice(4)) : Number.NaN;
|
|
232
|
+
if (node.metadata?.["ancestors"] == null && Number.isInteger(id)) {
|
|
233
|
+
node.metadata = { ...node.metadata, ancestors: this.#backend.ancestors(id) };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
223
237
|
return { raw: tree.raw, roots: newRoots };
|
|
224
238
|
}
|
|
225
239
|
async #walk(node, parentResolved, state) {
|
|
@@ -257,6 +271,9 @@ class WOFResolver {
|
|
|
257
271
|
else if (decorated.metadata?.["bare_region_repick"]) {
|
|
258
272
|
decorated.tag = "region";
|
|
259
273
|
}
|
|
274
|
+
else if (decorated.metadata?.[COUNTY_DESIGNATOR_FALLBACK]) {
|
|
275
|
+
decorated.tag = "subregion";
|
|
276
|
+
}
|
|
260
277
|
// Optional lineage attachment.
|
|
261
278
|
if (state.includeAncestors && this.#backend.ancestors) {
|
|
262
279
|
decorated.metadata = { ...decorated.metadata, ancestors: this.#backend.ancestors(picked.top.id) };
|
|
@@ -296,6 +313,61 @@ class WOFResolver {
|
|
|
296
313
|
}
|
|
297
314
|
return found;
|
|
298
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* The county of a designated `locality` value's name in its region, when the locality
|
|
318
|
+
* lookup found no locality known to be inside that region.
|
|
319
|
+
*
|
|
320
|
+
* A candidate is known inside when the lookup was scoped by the region and kept it,
|
|
321
|
+
* or when the qualifier containment partition marked it contained.
|
|
322
|
+
* A candidate whose containment neither the region scope nor the partition
|
|
323
|
+
* computed keeps the locality pick.
|
|
324
|
+
*
|
|
325
|
+
* A `locality` lookup is region-scoped exactly when its resolved parent has
|
|
326
|
+
* a numeric id (`applyParentScope`).
|
|
327
|
+
*/
|
|
328
|
+
async #countyDesignatorPick(node, placetype, country, candidates, parentResolved, state, rec) {
|
|
329
|
+
if (!state.countyDesignatorFallback || placetype !== "locality" || node.tag !== "locality" || !country)
|
|
330
|
+
return null;
|
|
331
|
+
const regionScoped = typeof parentResolved?.id === "number";
|
|
332
|
+
const inRegion = candidates.some((c) => c.containedByQualifier === true || (regionScoped && c.regionScopeMiss !== true));
|
|
333
|
+
const unplaced = candidates.some((c) => c.containedByQualifier == null && !regionScoped);
|
|
334
|
+
if (inRegion || unplaced)
|
|
335
|
+
return null;
|
|
336
|
+
const parentIsRegion = parentResolved?.placetype === "region" || parentResolved?.placetype === "macroregion";
|
|
337
|
+
const county = await countyFromDesignatedLocality(this.#backend, node.value, {
|
|
338
|
+
country,
|
|
339
|
+
regionID: parentIsRegion && typeof parentResolved.id === "number" ? parentResolved.id : null,
|
|
340
|
+
regionQualifier: state.regionQualifier ?? null,
|
|
341
|
+
limit: state.candidatesPerLookup,
|
|
342
|
+
});
|
|
343
|
+
if (!county)
|
|
344
|
+
return null;
|
|
345
|
+
rec.check(COUNTY_DESIGNATOR_FALLBACK);
|
|
346
|
+
rec.emit({ id: county.top.id, name: county.top.name, source: COUNTY_DESIGNATOR_FALLBACK });
|
|
347
|
+
return { ...county, metadata: { [COUNTY_DESIGNATOR_FALLBACK]: true } };
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* The postcode-prefix prior's pick for a `postalcode` lookup that missed,
|
|
351
|
+
* when the prior is on and the prefix index holds the code's prefix.
|
|
352
|
+
*/
|
|
353
|
+
#postcodePrefixPick(node, placetype, country, candidates, state, rec) {
|
|
354
|
+
if (candidates.length || placetype !== "postalcode" || !state.postcodePrefixPrior || !state.postcodePrefixIndex) {
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
const probe = probePostcodePrefix(node.value, state.postcodePrefixIndex, country);
|
|
358
|
+
if (!probe)
|
|
359
|
+
return null;
|
|
360
|
+
const metadata = {
|
|
361
|
+
postcode_prefix: probe.prefix,
|
|
362
|
+
postcode_prefix_ancestors: probe.node.ancestors,
|
|
363
|
+
...(probe.node.radiusP95Km != null ? { postcode_prefix_radius_p95_km: probe.node.radiusP95Km } : {}),
|
|
364
|
+
...(probe.node.lat != null && probe.node.lon != null ? { coordinate_source: "postcode_prefix" } : {}),
|
|
365
|
+
};
|
|
366
|
+
rec.check("postcode_prefix_prior");
|
|
367
|
+
const prefixPlace = postcodePrefixResolvedPlace(probe.prefix, probe.node, state.postcodePrefixIndex);
|
|
368
|
+
rec.emit({ id: prefixPlace.id, name: prefixPlace.name, source: "postcode_prefix" });
|
|
369
|
+
return { top: prefixPlace, alternatives: [], metadata };
|
|
370
|
+
}
|
|
299
371
|
async #lookupAndPick(node, placetype, parentResolved, state) {
|
|
300
372
|
state.lookupsRemaining--;
|
|
301
373
|
const query = {
|
|
@@ -387,32 +459,17 @@ class WOFResolver {
|
|
|
387
459
|
}
|
|
388
460
|
}
|
|
389
461
|
catch (error) {
|
|
390
|
-
// Defensive: backend errors
|
|
462
|
+
// Defensive: backend errors shouldn't abort the whole tree walk.
|
|
391
463
|
rec.check(`backend_error: ${error.message}`);
|
|
392
464
|
rec.emit(null);
|
|
393
465
|
return null;
|
|
394
466
|
}
|
|
395
467
|
}
|
|
396
|
-
//
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
postcode_prefix: probe.prefix,
|
|
402
|
-
postcode_prefix_ancestors: probe.node.ancestors,
|
|
403
|
-
...(probe.node.radiusP95Km != null ? { postcode_prefix_radius_p95_km: probe.node.radiusP95Km } : {}),
|
|
404
|
-
...(probe.node.lat != null && probe.node.lon != null ? { coordinate_source: "postcode_prefix" } : {}),
|
|
405
|
-
};
|
|
406
|
-
rec.check("postcode_prefix_prior");
|
|
407
|
-
const prefixPlace = postcodePrefixResolvedPlace(probe.prefix, probe.node, state.postcodePrefixIndex);
|
|
408
|
-
rec.emit({ id: prefixPlace.id, name: prefixPlace.name, source: "postcode_prefix" });
|
|
409
|
-
return {
|
|
410
|
-
top: prefixPlace,
|
|
411
|
-
alternatives: [],
|
|
412
|
-
metadata,
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
}
|
|
468
|
+
// A designated county span resolves as its region's county, and a missed postcode takes the prefix prior.
|
|
469
|
+
const recovered = (await this.#countyDesignatorPick(node, placetype, query.country, candidates, parentResolved, state, rec)) ??
|
|
470
|
+
this.#postcodePrefixPick(node, placetype, query.country, candidates, state, rec);
|
|
471
|
+
if (recovered)
|
|
472
|
+
return recovered;
|
|
416
473
|
// Bare-toponym race: locality-tagged single spans also probe country/region.
|
|
417
474
|
const isBareRace = placetype === "locality" && node === state.bareLocalityNode;
|
|
418
475
|
if (isBareRace) {
|
|
@@ -438,20 +495,10 @@ class WOFResolver {
|
|
|
438
495
|
const anchorEligible = placetype === "region" || placetype === "locality";
|
|
439
496
|
let ranked = candidates;
|
|
440
497
|
if (state.anchorPosterior && anchorEligible && candidates.length > 1) {
|
|
441
|
-
|
|
442
|
-
const w = state.anchorWeight;
|
|
443
|
-
// Within-tier key uses prominence (fallback score) plus posterior boost.
|
|
444
|
-
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array. toSorted would double-allocate on a hot path
|
|
445
|
-
ranked = [...candidates].sort((a, b) => {
|
|
446
|
-
const tier = Number(b.exactMatch ?? false) - Number(a.exactMatch ?? false);
|
|
447
|
-
if (tier !== 0)
|
|
448
|
-
return tier;
|
|
449
|
-
// Unknown-country candidates get zero posterior mass.
|
|
450
|
-
const aKey = (a.prominence ?? a.score) + w * (a.country ? (post[a.country] ?? 0) : 0);
|
|
451
|
-
const bKey = (b.prominence ?? b.score) + w * (b.country ? (post[b.country] ?? 0) : 0);
|
|
452
|
-
return bKey - aKey || b.score - a.score;
|
|
453
|
-
});
|
|
498
|
+
ranked = rankByAnchorPosterior(candidates, state.anchorPosterior, state.anchorWeight);
|
|
454
499
|
rec.stage("anchor", ranked);
|
|
500
|
+
ranked = rankWithinPosteriorCountry(ranked, state.capitalLevel, node);
|
|
501
|
+
rec.stage("importance", ranked);
|
|
455
502
|
}
|
|
456
503
|
// Optional locale-country soft prior adds to candidate scores while retaining every candidate.
|
|
457
504
|
// Disabled when default country or anchor posterior is present.
|
|
@@ -459,7 +506,7 @@ class WOFResolver {
|
|
|
459
506
|
ranked = rankByCountryPrior(ranked, state.localeCountryPrior, state.localeCountryPriorWeight);
|
|
460
507
|
rec.stage("locale_prior", ranked);
|
|
461
508
|
}
|
|
462
|
-
// Importance-first rerank (
|
|
509
|
+
// Importance-first rerank (the posterior branch above runs its own, within the winning country).
|
|
463
510
|
if (!state.anchorPosterior) {
|
|
464
511
|
ranked = rankByImportance(ranked);
|
|
465
512
|
rec.stage("importance", ranked);
|
|
@@ -561,6 +608,37 @@ function containmentStamps(qualifierPartition, parentContainment, ranked, top) {
|
|
|
561
608
|
...(parentContainment && top.containedByQualifier === true ? { contained_by_parent: true } : {}),
|
|
562
609
|
};
|
|
563
610
|
}
|
|
611
|
+
/**
|
|
612
|
+
* Sorts candidates by the postcode country posterior, exact matches first.
|
|
613
|
+
*
|
|
614
|
+
* Within a match tier, the key is a candidate's prominence (its score when it has none)
|
|
615
|
+
* plus `weight` times its country's posterior mass.
|
|
616
|
+
* A candidate with no country gets no posterior mass.
|
|
617
|
+
*/
|
|
618
|
+
function rankByAnchorPosterior(candidates, posterior, weight) {
|
|
619
|
+
const key = (c) => (c.prominence ?? c.score) + weight * (c.country ? (posterior[c.country] ?? 0) : 0);
|
|
620
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array. toSorted would double-allocate on a hot path
|
|
621
|
+
return [...candidates].sort((a, b) => Number(b.exactMatch ?? false) - Number(a.exactMatch ?? false) || key(b) - key(a) || b.score - a.score);
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Reorders the posterior-ranked leader's peers, the candidates in its country and exact-match
|
|
625
|
+
* tier, by importance and capital promotion, as the path without a posterior ranks them.
|
|
626
|
+
*
|
|
627
|
+
* The posterior decides the country.
|
|
628
|
+
* Within that country this order makes a venue query (`Clayton Post Office, Clayton, WI`)
|
|
629
|
+
* pick the same locality as its bare form (`Clayton, WI`).
|
|
630
|
+
*/
|
|
631
|
+
function rankWithinPosteriorCountry(ranked, capitalLevel, node) {
|
|
632
|
+
const leader = ranked[0];
|
|
633
|
+
if (!leader?.country)
|
|
634
|
+
return [...ranked];
|
|
635
|
+
const isPeer = (c) => c.country === leader.country && (c.exactMatch === true) === (leader.exactMatch === true);
|
|
636
|
+
let peers = rankByImportance(ranked.filter(isPeer));
|
|
637
|
+
if (capitalLevel) {
|
|
638
|
+
peers = promoteCapitalsWithReceipt(peers, capitalLevel, node);
|
|
639
|
+
}
|
|
640
|
+
return [...peers, ...ranked.filter((c) => !isPeer(c))];
|
|
641
|
+
}
|
|
564
642
|
/**
|
|
565
643
|
* Promote capitals and stamp `capital_promotion` when the leader changes.
|
|
566
644
|
* Metadata only.
|