@mailwoman/resolver 9.1.0 → 9.3.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 +89 -0
- package/lib/admin/coherence-passes.ts +435 -0
- package/lib/admin/containment.ts +92 -0
- package/lib/admin/index.ts +10 -0
- package/lib/admin/jp-municipality.ts +97 -0
- package/lib/admin/winner.ts +189 -0
- package/lib/backend-capabilities.ts +88 -0
- package/lib/bare-toponym-race.ts +129 -0
- package/lib/decorate-node.ts +127 -0
- package/lib/fold-name.ts +36 -0
- package/{index.ts → lib/index.ts} +27 -18
- package/{plausibility.ts → lib/plausibility.ts} +5 -4
- package/{postcode-country-coherence.ts → lib/postcode/country-coherence.ts} +173 -86
- package/lib/postcode/index.ts +9 -0
- package/{postcode-prefix.ts → lib/postcode/prefix.ts} +7 -6
- package/{postcode-shape-coherence.ts → lib/postcode/shape-coherence.ts} +10 -24
- package/{remote-resolver.ts → lib/remote-resolver.ts} +13 -4
- package/{rerank.ts → lib/rerank.ts} +2 -2
- package/lib/resolve/index.ts +982 -0
- package/lib/resolve/passes.ts +544 -0
- package/{span-rescore.ts → lib/span-rescore.ts} +114 -99
- package/{street-evidence.ts → lib/street/evidence.ts} +40 -7
- package/lib/street/index.ts +8 -0
- package/{street-tier.ts → lib/street/tier.ts} +138 -99
- package/{toponym-prior.ts → lib/toponym-prior.ts} +97 -8
- package/out/admin/coherence-passes.d.ts +70 -0
- package/out/admin/coherence-passes.d.ts.map +1 -0
- package/out/admin/coherence-passes.js +327 -0
- package/out/admin/coherence-passes.js.map +1 -0
- package/out/admin/containment.d.ts +48 -0
- package/out/admin/containment.d.ts.map +1 -0
- package/out/admin/containment.js +76 -0
- package/out/admin/containment.js.map +1 -0
- package/out/admin/index.d.ts +10 -0
- package/out/admin/index.d.ts.map +1 -0
- package/out/admin/index.js +10 -0
- package/out/admin/index.js.map +1 -0
- package/out/admin/jp-municipality.d.ts +50 -0
- package/out/admin/jp-municipality.d.ts.map +1 -0
- package/out/admin/jp-municipality.js +54 -0
- package/out/admin/jp-municipality.js.map +1 -0
- package/out/admin/winner.d.ts +114 -0
- package/out/admin/winner.d.ts.map +1 -0
- package/out/admin/winner.js +130 -0
- package/out/admin/winner.js.map +1 -0
- package/out/backend-capabilities.d.ts +37 -0
- package/out/backend-capabilities.d.ts.map +1 -0
- package/out/backend-capabilities.js +75 -0
- package/out/backend-capabilities.js.map +1 -0
- package/out/bare-toponym-race.d.ts +55 -0
- package/out/bare-toponym-race.d.ts.map +1 -0
- package/out/bare-toponym-race.js +110 -0
- package/out/bare-toponym-race.js.map +1 -0
- package/out/decorate-node.d.ts +24 -0
- package/out/decorate-node.d.ts.map +1 -0
- package/out/decorate-node.js +103 -0
- package/out/decorate-node.js.map +1 -0
- package/out/fold-name.d.ts +18 -3
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +19 -3
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +15 -12
- package/out/index.d.ts.map +1 -1
- package/out/index.js +9 -7
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +5 -4
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +5 -4
- package/out/plausibility.js.map +1 -1
- package/out/{postcode-country-coherence.d.ts → postcode/country-coherence.d.ts} +46 -25
- package/out/postcode/country-coherence.d.ts.map +1 -0
- package/out/{postcode-country-coherence.js → postcode/country-coherence.js} +157 -77
- package/out/postcode/country-coherence.js.map +1 -0
- package/out/postcode/index.d.ts +9 -0
- package/out/postcode/index.d.ts.map +1 -0
- package/out/postcode/index.js +9 -0
- package/out/postcode/index.js.map +1 -0
- package/out/{postcode-prefix.d.ts → postcode/prefix.d.ts} +4 -4
- package/out/postcode/prefix.d.ts.map +1 -0
- package/out/{postcode-prefix.js → postcode/prefix.js} +7 -6
- package/out/postcode/prefix.js.map +1 -0
- package/out/{postcode-shape-coherence.d.ts → postcode/shape-coherence.d.ts} +3 -3
- package/out/postcode/shape-coherence.d.ts.map +1 -0
- package/out/{postcode-shape-coherence.js → postcode/shape-coherence.js} +11 -20
- package/out/postcode/shape-coherence.js.map +1 -0
- package/out/remote-resolver.d.ts +18 -3
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +2 -2
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +1 -1
- package/out/rerank.d.ts.map +1 -1
- package/out/rerank.js +2 -2
- package/out/rerank.js.map +1 -1
- package/out/{resolve.d.ts → resolve/index.d.ts} +2 -2
- package/out/resolve/index.d.ts.map +1 -0
- package/out/resolve/index.js +795 -0
- package/out/resolve/index.js.map +1 -0
- package/out/resolve/passes.d.ts +218 -0
- package/out/resolve/passes.d.ts.map +1 -0
- package/out/resolve/passes.js +333 -0
- package/out/resolve/passes.js.map +1 -0
- package/out/span-rescore.d.ts +16 -16
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +87 -71
- package/out/span-rescore.js.map +1 -1
- package/out/{street-evidence.d.ts → street/evidence.d.ts} +18 -3
- package/out/street/evidence.d.ts.map +1 -0
- package/out/{street-evidence.js → street/evidence.js} +23 -7
- package/out/street/evidence.js.map +1 -0
- package/out/street/index.d.ts +8 -0
- package/out/street/index.d.ts.map +1 -0
- package/out/street/index.js +8 -0
- package/out/street/index.js.map +1 -0
- package/out/{street-tier.d.ts → street/tier.d.ts} +14 -3
- package/out/street/tier.d.ts.map +1 -0
- package/out/{street-tier.js → street/tier.js} +127 -90
- package/out/street/tier.js.map +1 -0
- package/out/toponym-prior.d.ts +43 -19
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +76 -8
- package/out/toponym-prior.js.map +1 -1
- package/package.json +167 -19
- package/vitest.config.ts +5 -6
- package/fold-name.ts +0 -20
- package/out/postcode-country-coherence.d.ts.map +0 -1
- package/out/postcode-country-coherence.js.map +0 -1
- package/out/postcode-prefix.d.ts.map +0 -1
- package/out/postcode-prefix.js.map +0 -1
- package/out/postcode-shape-coherence.d.ts.map +0 -1
- package/out/postcode-shape-coherence.js.map +0 -1
- package/out/resolve.d.ts.map +0 -1
- package/out/resolve.js +0 -1049
- package/out/resolve.js.map +0 -1
- package/out/street-evidence.d.ts.map +0 -1
- package/out/street-evidence.js.map +0 -1
- package/out/street-tier.d.ts.map +0 -1
- package/out/street-tier.js.map +0 -1
- package/out/vitest.config.d.ts +0 -16
- package/out/vitest.config.d.ts.map +0 -1
- package/out/vitest.config.js +0 -36
- package/out/vitest.config.js.map +0 -1
- package/resolve.ts +0 -1369
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The three post-walk admin coherence passes (#263 / #822 / the region-country pass) — split from
|
|
7
|
+
* `resolve.ts` on the `bare-toponym-race.ts` precedent: the walk file holds the walk, this one
|
|
8
|
+
* holds the joint-consistency re-picks that run after it. Each pass's contract, triggers, and
|
|
9
|
+
* byte-stability guarantees are documented on the pass itself; `resolveTree` sequences them.
|
|
10
|
+
*/
|
|
11
|
+
import { matchCountry, matchSubdivision } from "@mailwoman/codex/country";
|
|
12
|
+
import { decorateNode, isResolvedWithCoord } from "#decorate-node";
|
|
13
|
+
/**
|
|
14
|
+
* Admin descendant-consistency (#263) — the joint-consistency resolve, scoped to the admin assignment. The greedy walk
|
|
15
|
+
* resolves a region on its own (name + population), so "ME" picks Messina (IT) over Maine, then scopes "Portland" to
|
|
16
|
+
* Messina's descendants, finds nothing, and the result falls back to the region centroid (Sicily). The region's
|
|
17
|
+
* same-named runner-ups (Maine, Missouri, …) were already captured as `alternatives`; this pass asks the question the
|
|
18
|
+
* greedy order skipped — _which "ME" has a "Portland" under it?_ — and re-picks the (region, locality) pair where a
|
|
19
|
+
* same-named locality descends from a same-named region candidate. Geography decides; no country prior, no list.
|
|
20
|
+
*
|
|
21
|
+
* Fires ONLY for a resolved region whose child locality fell through (the unresolved-locality signal), so a
|
|
22
|
+
* well-resolved tree ("Springfield, IL" → Illinois, Springfield) is byte-identical. Costs one unscoped locality lookup
|
|
23
|
+
* per triggering pair. Needs {@link ResolverBackend.ancestors}; no-op without it. See `ResolveOpts.adminCoherence`.
|
|
24
|
+
*/
|
|
25
|
+
export async function applyAdminCoherence(roots, backend) {
|
|
26
|
+
const visit = async (node, regionAncestor) => {
|
|
27
|
+
const regionHere = node.tag === "region" && isResolvedWithCoord(node) ? node : regionAncestor;
|
|
28
|
+
if (regionHere &&
|
|
29
|
+
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
30
|
+
!isResolvedWithCoord(node) &&
|
|
31
|
+
node.value.trim().length) {
|
|
32
|
+
await reconcileAdminPair(regionHere, node, backend);
|
|
33
|
+
}
|
|
34
|
+
for (const child of node.children) {
|
|
35
|
+
await visit(child, regionHere);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
for (const root of roots) {
|
|
39
|
+
await visit(root, null);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Re-pick a (region, locality) pair so the locality descends from the region. `alternatives` on the node are the
|
|
44
|
+
* `ResolvedPlace` runner-ups `decorateNode` attached (typed `unknown[]` in the decoder, which can't import resolver
|
|
45
|
+
* types — the cast is sound). Picks the FIRST same-named locality (already score-ordered) that descends from a
|
|
46
|
+
* same-named region candidate, then swaps both nodes. Leaves both untouched when no consistent pair exists (a genuinely
|
|
47
|
+
* un-gazetteered locality — "Portland, VT" with no Portland in Vermont — stays as the region centroid, not a foreign
|
|
48
|
+
* namesake).
|
|
49
|
+
*/
|
|
50
|
+
async function reconcileAdminPair(regionNode, localityNode, backend) {
|
|
51
|
+
// EXACT region matches only: the alternatives for a 2-letter token are loose ("ME" also surfaces
|
|
52
|
+
// Missouri/Michigan/Mississippi as fuzzy M-state runner-ups). Restricting to exact name/alias matches
|
|
53
|
+
// (Maine/Messina/Medway for "ME") keeps the join honest. `exactMatch` is stamped by exactMatchTiering.
|
|
54
|
+
const regionCands = (regionNode.alternatives ?? []).filter((r) => r.exactMatch);
|
|
55
|
+
// For each exact region candidate, ask the gazetteer directly: is there a same-named locality UNDER it?
|
|
56
|
+
// The `parentID` scope is the descendant test (over the #832-repaired ancestors table), and it finds the
|
|
57
|
+
// instance regardless of its global population rank — "Springfield, ME" reaches the small Springfield in
|
|
58
|
+
// Maine that an unscoped top-N window would drop. First region with an exact-named descendant wins; the
|
|
59
|
+
// region candidates are score-ordered, so a tie breaks toward the more prominent place.
|
|
60
|
+
for (const region of regionCands) {
|
|
61
|
+
const scoped = await backend.findPlace({
|
|
62
|
+
text: localityNode.value,
|
|
63
|
+
placetype: "locality",
|
|
64
|
+
parentID: region.id,
|
|
65
|
+
limit: 3,
|
|
66
|
+
});
|
|
67
|
+
const lc = scoped.find((l) => l.exactMatch && !(l.lat === 0 && l.lon === 0));
|
|
68
|
+
if (lc) {
|
|
69
|
+
decorateNode(regionNode, region, regionCands.filter((r) => r !== region));
|
|
70
|
+
regionNode.metadata = { ...regionNode.metadata, admin_coherence_repicked: true };
|
|
71
|
+
decorateNode(localityNode, lc, scoped.filter((l) => l !== lc));
|
|
72
|
+
localityNode.metadata = { ...localityNode.metadata, admin_coherence_repicked: true };
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// #267 follow-up: the token may name a COUNTRY whose namesake is a more-populous foreign region — "Tbilisi,
|
|
77
|
+
// Georgia" parses region("Georgia") → the US state, but Tbilisi descends from Georgia the COUNTRY. When no
|
|
78
|
+
// region candidate holds the locality, try same-named country candidates: a foreign capital under its
|
|
79
|
+
// country out-votes the state namesake. Needs the country + the locality's ancestry in the gazetteer (the
|
|
80
|
+
// #267 admin fold). The re-picked region node then carries the country place; the locality coordinate wins.
|
|
81
|
+
const countryCands = (await backend.findPlace({ text: regionNode.value, placetype: "country", limit: 3 })).filter((c) => c.exactMatch);
|
|
82
|
+
for (const country of countryCands) {
|
|
83
|
+
const scoped = await backend.findPlace({
|
|
84
|
+
text: localityNode.value,
|
|
85
|
+
placetype: "locality",
|
|
86
|
+
parentID: country.id,
|
|
87
|
+
limit: 3,
|
|
88
|
+
});
|
|
89
|
+
const lc = scoped.find((l) => l.exactMatch && !(l.lat === 0 && l.lon === 0));
|
|
90
|
+
if (lc) {
|
|
91
|
+
decorateNode(regionNode, country, regionCands);
|
|
92
|
+
regionNode.metadata = { ...regionNode.metadata, admin_coherence_repicked: true };
|
|
93
|
+
decorateNode(localityNode, lc, scoped.filter((l) => l !== lc));
|
|
94
|
+
localityNode.metadata = { ...localityNode.metadata, admin_coherence_repicked: true };
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// #1023: the admin gazetteer may carry NO `country`-placetype node for the token AND the same-named
|
|
99
|
+
// foreign locality may be ORPHANED (parent_id = -1) — the 2026-07-07 rebuild flattened Georgia's admin
|
|
100
|
+
// hierarchy to localities-only, so the country-node lookup above finds nothing and the `parentID`
|
|
101
|
+
// descendant test can never reach Tbilisi. Fall back to matchCountry: normalize the token to an
|
|
102
|
+
// ISO-3166 alpha-2 and scope the locality by the gazetteer's `country` COLUMN (set even on an orphaned
|
|
103
|
+
// row). Same primitive reconcileExplicitCountry (#822) uses, so the region-parsed namesake path
|
|
104
|
+
// ("Tbilisi, Georgia") converges with the country-parsed one ("Vienna, Austria"). matchCountry returns
|
|
105
|
+
// null for a US state name/abbrev ("Illinois" / "ME" / "IL"), so a real US (region, locality) pair
|
|
106
|
+
// never reaches here — this stays inert on the domestic path.
|
|
107
|
+
const mc = matchCountry(regionNode.value);
|
|
108
|
+
if (mc) {
|
|
109
|
+
const scoped = await backend.findPlace({
|
|
110
|
+
text: localityNode.value,
|
|
111
|
+
placetype: "locality",
|
|
112
|
+
country: mc.iso2,
|
|
113
|
+
limit: 3,
|
|
114
|
+
});
|
|
115
|
+
const lc = scoped.find((l) => l.exactMatch && !(l.lat === 0 && l.lon === 0));
|
|
116
|
+
if (lc) {
|
|
117
|
+
decorateNode(localityNode, lc, scoped.filter((l) => l !== lc));
|
|
118
|
+
localityNode.metadata = { ...localityNode.metadata, admin_coherence_repicked: true };
|
|
119
|
+
// The token named a foreign country the admin gazetteer has no node for, but the greedy walk had
|
|
120
|
+
// already decorated the region node with the US-state namesake. Revert that stale decoration so the
|
|
121
|
+
// node stops asserting the wrong-country coordinate + `resolver_country` (which would otherwise leak
|
|
122
|
+
// into the result's `countryCode`); the re-picked locality carries the winning coordinate, and the
|
|
123
|
+
// region node falls back to the parsed "Georgia" token, unresolved (the admin DB has nothing truer).
|
|
124
|
+
revertResolverDecoration(regionNode);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Undo a resolver decoration on a node: restore the classifier attribution {@link decorateNode} displaced into
|
|
130
|
+
* `metadata.classifier_source(_id)` and drop the resolver-supplied coordinate/identity/alternatives. Used by the #1023
|
|
131
|
+
* country fall-through when the region token turns out to name a foreign country the admin gazetteer holds no node for
|
|
132
|
+
* — the greedy walk had bound it to the US-state namesake, and that stale claim must not survive the locality re-pick.
|
|
133
|
+
*/
|
|
134
|
+
function revertResolverDecoration(node) {
|
|
135
|
+
const meta = { ...node.metadata };
|
|
136
|
+
const priorSource = meta["classifier_source"];
|
|
137
|
+
const priorSourceID = meta["classifier_source_id"];
|
|
138
|
+
node.source = typeof priorSource === "string" ? priorSource : undefined;
|
|
139
|
+
node.sourceID = typeof priorSourceID === "string" ? priorSourceID : undefined;
|
|
140
|
+
for (const key of [
|
|
141
|
+
"classifier_source",
|
|
142
|
+
"classifier_source_id",
|
|
143
|
+
"resolver_score",
|
|
144
|
+
"resolver_prominence",
|
|
145
|
+
"resolver_name",
|
|
146
|
+
"resolver_country",
|
|
147
|
+
"resolution_quality",
|
|
148
|
+
"postcode_city_mismatch",
|
|
149
|
+
]) {
|
|
150
|
+
// oxlint-disable-next-line typescript/no-dynamic-delete -- removing one key from a plain record; the object is not on a hot path
|
|
151
|
+
delete meta[key];
|
|
152
|
+
}
|
|
153
|
+
node.metadata = meta;
|
|
154
|
+
node.lat = undefined;
|
|
155
|
+
node.lon = undefined;
|
|
156
|
+
node.placeID = undefined;
|
|
157
|
+
node.alternatives = undefined;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Explicit-country coherence (#822) — the joint-consistency resolve keyed on the query's own EXPLICIT country token.
|
|
161
|
+
* The greedy walk resolves a locality on name + population alone, so "Vienna, Austria" picks the populous US namesake
|
|
162
|
+
* (Vienna WV) and IGNORES the "Austria" the address named. This pass asks the question the greedy order skipped —
|
|
163
|
+
* _which "Vienna" is in the country the address names?_ — and re-picks the locality to the same-named place under that
|
|
164
|
+
* country. The country code comes from the parser's OWN `country` emission via codex's ISO-3166 table (a name→code
|
|
165
|
+
* normalization of a token the model already classified, NOT a routing prior or safelist); the gazetteer's `country`
|
|
166
|
+
* column does the geographic confirmation. No pin, no list; generalizes to every country.
|
|
167
|
+
*
|
|
168
|
+
* Disjoint from {@link applyAdminCoherence} by the region guard: that pass owns the case where a REGION scopes the
|
|
169
|
+
* locality; this one fires only when the explicit country is the locality's nearest admin context (no region between),
|
|
170
|
+
* and then regardless of the locality's resolution state — so it covers both the resolved-but-foreign locality (Sydney
|
|
171
|
+
* → the greedy AU pick was wrong) and the unresolved locality the span-rescore tier would otherwise back-fill with the
|
|
172
|
+
* US namesake (Vienna → Vienna WV). Byte-stable when the locality already resolved in-country (the id guard) or the
|
|
173
|
+
* named country holds no same-named locality (the fail-safe — what also protects "Turkey, TX": no country token ⇒ no
|
|
174
|
+
* trigger; and an in-country lookup that finds nothing keeps the greedy result). Costs one country-scoped locality
|
|
175
|
+
* lookup per triggering pair. See `ResolveOpts.adminCoherence`.
|
|
176
|
+
*/
|
|
177
|
+
export async function applyExplicitCountryCoherence(roots, backend) {
|
|
178
|
+
const visit = async (node, countryToken, regionAbove) => {
|
|
179
|
+
const countryHere = node.tag === "country" && node.value.trim().length ? node : countryToken;
|
|
180
|
+
// A region suppresses the country re-pick only when it RESOLVED: the suppression's rationale is
|
|
181
|
+
// that applyAdminCoherence + the region's `parentID` scope already disambiguate the locality
|
|
182
|
+
// ("Springfield, IL" must not re-pick to the most populous US Springfield), and an UNRESOLVED
|
|
183
|
+
// region disambiguates nothing — 'NIC-38' (an ISO-3166-2 code the gazetteer holds no node for)
|
|
184
|
+
// silently swallowed the explicit 'Nicaragua', and El Sauce resolved a US namesake. This pass
|
|
185
|
+
// runs post-walk, so resolution state is known.
|
|
186
|
+
const regionHere = regionAbove || ((node.tag === "region" || node.tag === "subregion") && isResolvedWithCoord(node));
|
|
187
|
+
// Fire only when the explicit country is the locality's NEAREST RESOLVED admin context.
|
|
188
|
+
// Fires regardless of the locality's resolution state, so it PRE-EMPTS the span-rescore tier
|
|
189
|
+
// (which would otherwise back-fill the unresolved locality with the US namesake).
|
|
190
|
+
if (countryHere && !regionHere && (node.tag === "locality" || node.tag === "dependent_locality")) {
|
|
191
|
+
await reconcileExplicitCountry(countryHere, node, backend);
|
|
192
|
+
}
|
|
193
|
+
for (const child of node.children) {
|
|
194
|
+
await visit(child, countryHere, regionHere);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
for (const root of roots) {
|
|
198
|
+
await visit(root, null, false);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Re-pick a resolved locality to its same-named place UNDER the explicitly-named country. `matchCountry` turns the
|
|
203
|
+
* country token into an ISO-3166 alpha-2 (returns null for an unrecognized token → no-op); the backend then surfaces
|
|
204
|
+
* the in-country namesake the population-first unscoped window buried. Leaves the node untouched when the country is
|
|
205
|
+
* unrecognized, the named country has no exact same-named locality (the fail-safe), or the locality already resolved to
|
|
206
|
+
* that place (the id guard → byte-stable). The country node itself stays as the parser emitted it — the named
|
|
207
|
+
* well-covered countries carry no `country`-placetype row in the admin gazetteer, so there is nothing to decorate it
|
|
208
|
+
* with; the locality coordinate is what the re-pick fixes.
|
|
209
|
+
*/
|
|
210
|
+
async function reconcileExplicitCountry(countryNode, localityNode, backend) {
|
|
211
|
+
const mc = matchCountry(countryNode.value);
|
|
212
|
+
if (!mc)
|
|
213
|
+
return;
|
|
214
|
+
const scoped = await backend.findPlace({
|
|
215
|
+
text: localityNode.value,
|
|
216
|
+
placetype: "locality",
|
|
217
|
+
country: mc.iso2,
|
|
218
|
+
limit: 3,
|
|
219
|
+
});
|
|
220
|
+
const lc = scoped.find((l) => l.exactMatch && !(l.lat === 0 && l.lon === 0));
|
|
221
|
+
if (!lc)
|
|
222
|
+
return;
|
|
223
|
+
// Already the in-country place? (placeID encodes the WOF id.) Then the greedy walk was right — byte-stable.
|
|
224
|
+
if (localityNode.placeID === `wof:${lc.id}`)
|
|
225
|
+
return;
|
|
226
|
+
decorateNode(localityNode, lc, scoped.filter((l) => l !== lc));
|
|
227
|
+
localityNode.metadata = { ...localityNode.metadata, explicit_country_repicked: true };
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Region-country coherence — the joint-consistency resolve keyed on a REGION token the locale-inferred default-country
|
|
231
|
+
* filter could not resolve. Companion to {@link applyExplicitCountryCoherence} (which keys on an explicit COUNTRY token)
|
|
232
|
+
* and disjoint from {@link applyAdminCoherence} (which needs a region that DID resolve): this pass owns the mirror case,
|
|
233
|
+
* where the region qualifier is a foreign subdivision the default-country hard filter (`spr.country = ?`) discarded.
|
|
234
|
+
*
|
|
235
|
+
* "Montreal QC" under a US locale: the walk applies `defaultCountry="US"` as a hard candidate filter to every admin
|
|
236
|
+
* lookup, so the region "QC" (a Canadian subdivision) resolves to nothing and is dropped — the one signal that would
|
|
237
|
+
* redirect the country to CA — and the locality "Montreal" is force-matched to the populous US namesake (Montreal, WI).
|
|
238
|
+
* The greedy order threw away the evidence that could correct it.
|
|
239
|
+
*
|
|
240
|
+
* The fix expands the region token to its country via codex's ISO-3166-2 subdivision table (`matchSubdivision`: "QC" →
|
|
241
|
+
* `{ name: "Quebec", country: "CA" }`, handling the FTS index's missing "QC" alt-name code-side), then asks the two
|
|
242
|
+
* questions the greedy walk skipped: does that subdivision genuinely resolve UNDER its own country, and is there a
|
|
243
|
+
* same-named locality under it? Only when BOTH hold does it swap the region and locality to the in-country pair.
|
|
244
|
+
* Geography confirms; the subdivision table is a soft name→country prior, not a routing decision.
|
|
245
|
+
*
|
|
246
|
+
* Evidence-conditional to stay byte-stable on the domestic path. It fires ONLY when (a) a default country is in force,
|
|
247
|
+
* (b) the region node is UNRESOLVED (the default-country filter came up empty — a US region resolves fine under `US`,
|
|
248
|
+
* so a well-formed US query never trips this), (c) the token is a subdivision of a DIFFERENT country than the default,
|
|
249
|
+
* and (d) both the foreign region and a same-named foreign locality resolve. "Springfield, IL" / "Portland, ME": the
|
|
250
|
+
* region resolves under `US`, so check (b) fails and the tree is untouched. Costs one region + one locality lookup per
|
|
251
|
+
* triggering pair. See `ResolveOpts.adminCoherence`.
|
|
252
|
+
*/
|
|
253
|
+
export async function applyRegionCountryCoherence(roots, backend, defaultCountry) {
|
|
254
|
+
// No default country → no hard country filter was applied, so no region qualifier was discarded by one. The bug
|
|
255
|
+
// this pass corrects is specific to the locale-inferred default country; without it, there is nothing to rescue.
|
|
256
|
+
if (!defaultCountry)
|
|
257
|
+
return;
|
|
258
|
+
const visit = async (node, regionAncestor) => {
|
|
259
|
+
// Track the nearest region ancestor regardless of its resolution state (the trigger is an UNRESOLVED region).
|
|
260
|
+
const regionHere = node.tag === "region" || node.tag === "subregion" ? node : regionAncestor;
|
|
261
|
+
// Fire for an UNRESOLVED region (the default-country filter came up empty) whose companion locality node
|
|
262
|
+
// exists — regardless of the locality's resolution state, so it covers both the resolved-but-foreign namesake
|
|
263
|
+
// (Montreal → the greedy US pick, Montreal WI) and the unresolved locality the span-rescore tier would
|
|
264
|
+
// otherwise back-fill with a US namesake. The in-country lookups below are the evidence check.
|
|
265
|
+
if (regionHere &&
|
|
266
|
+
!isResolvedWithCoord(regionHere) &&
|
|
267
|
+
(node.tag === "locality" || node.tag === "dependent_locality") &&
|
|
268
|
+
node.value.trim().length) {
|
|
269
|
+
await reconcileRegionCountry(regionHere, node, backend, defaultCountry);
|
|
270
|
+
}
|
|
271
|
+
for (const child of node.children) {
|
|
272
|
+
await visit(child, regionHere);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
for (const root of roots) {
|
|
276
|
+
await visit(root, null);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Re-pick an (unresolved region, resolved-but-foreign-namesake locality) pair to the in-country instance the
|
|
281
|
+
* default-country filter hid. `matchSubdivision` turns the region token into `{ name, country }` (null for anything
|
|
282
|
+
* that isn't a US state or CA province → no-op); the region's full name then resolves it under that country (expanding
|
|
283
|
+
* the abbreviation the gazetteer FTS index lacks), and the locality is re-scoped to the same country. Leaves both nodes
|
|
284
|
+
* untouched unless every check holds — the subdivision names a different country than the default, the region resolves
|
|
285
|
+
* under it, and a same-named locality exists there — so the domestic path stays byte-identical.
|
|
286
|
+
*/
|
|
287
|
+
async function reconcileRegionCountry(regionNode, localityNode, backend, defaultCountry) {
|
|
288
|
+
const sub = matchSubdivision(regionNode.value);
|
|
289
|
+
if (!sub)
|
|
290
|
+
return;
|
|
291
|
+
// The subdivision must belong to a DIFFERENT country than the locale default. A US-state token under a US default
|
|
292
|
+
// (sub.country === defaultCountry) never reaches the swap — the pass is inert on the domestic path.
|
|
293
|
+
if (sub.country.toUpperCase() === defaultCountry.toUpperCase())
|
|
294
|
+
return;
|
|
295
|
+
// The locality already resolved in the subdivision's country? Then the greedy walk was already right — byte-stable.
|
|
296
|
+
const localityCountry = localityNode.metadata?.["resolver_country"]?.toUpperCase();
|
|
297
|
+
if (localityCountry === sub.country.toUpperCase())
|
|
298
|
+
return;
|
|
299
|
+
// Confirm the subdivision genuinely resolves under its own country, by its full name (expands "QC" → "Quebec", the
|
|
300
|
+
// form the FTS index carries). No resolvable region → no evidence the token is a real foreign subdivision; abstain.
|
|
301
|
+
const regionScoped = await backend.findPlace({
|
|
302
|
+
text: sub.name,
|
|
303
|
+
placetype: "region",
|
|
304
|
+
country: sub.country,
|
|
305
|
+
limit: 3,
|
|
306
|
+
});
|
|
307
|
+
const rc = regionScoped.find((r) => r.exactMatch && !(r.lat === 0 && r.lon === 0));
|
|
308
|
+
if (!rc)
|
|
309
|
+
return;
|
|
310
|
+
// Is there a same-named locality under that country? (the descendant test, by country column — the same primitive
|
|
311
|
+
// reconcileExplicitCountry uses.) No in-country namesake → keep the greedy result (fail-safe).
|
|
312
|
+
const scoped = await backend.findPlace({
|
|
313
|
+
text: localityNode.value,
|
|
314
|
+
placetype: "locality",
|
|
315
|
+
country: sub.country,
|
|
316
|
+
limit: 3,
|
|
317
|
+
});
|
|
318
|
+
const lc = scoped.find((l) => l.exactMatch && !(l.lat === 0 && l.lon === 0));
|
|
319
|
+
if (!lc)
|
|
320
|
+
return;
|
|
321
|
+
// Adopt the in-country pair: the region gets the foreign subdivision, the locality its same-named foreign instance.
|
|
322
|
+
decorateNode(regionNode, rc, regionScoped.filter((r) => r !== rc));
|
|
323
|
+
regionNode.metadata = { ...regionNode.metadata, region_country_repicked: true };
|
|
324
|
+
decorateNode(localityNode, lc, scoped.filter((l) => l !== lc));
|
|
325
|
+
localityNode.metadata = { ...localityNode.metadata, region_country_repicked: true };
|
|
326
|
+
}
|
|
327
|
+
//# sourceMappingURL=coherence-passes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coherence-passes.js","sourceRoot":"","sources":["../../lib/admin/coherence-passes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAIzE,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AAElE;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,KAA6B,EAAE,OAAwB;IAChG,MAAM,KAAK,GAAG,KAAK,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,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CACpC,CAAA;IAED,UAAU,CAAC,QAAQ,GAAG,EAAE,GAAG,UAAU,CAAC,QAAQ,EAAE,uBAAuB,EAAE,IAAI,EAAE,CAAA;IAE/E,YAAY,CACX,YAAY,EACZ,EAAE,EACF,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAC9B,CAAA;IAED,YAAY,CAAC,QAAQ,GAAG,EAAE,GAAG,YAAY,CAAC,QAAQ,EAAE,uBAAuB,EAAE,IAAI,EAAE,CAAA;AACpF,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The admin-containment partition (#1717 stage 2) — the ONE ordering function both deciding sites
|
|
7
|
+
* call, per the #861 rule (shared constants prove nothing; the #861 parity contract diverged at
|
|
8
|
+
* exactly the points a constant cannot express). The candidate backend partitions its row set
|
|
9
|
+
* before the limit window (so a contained candidate can reach the walk at all), and the resolver
|
|
10
|
+
* walk partitions AGAIN after its fame/anchor re-ranks (so `rankByImportance` cannot hand the top
|
|
11
|
+
* slot back to a more-famous uncontained namesake — Richmond VA outscores Richmond, North
|
|
12
|
+
* Yorkshire on importance, and without the second partition the change loses exactly where fame
|
|
13
|
+
* disagrees with the qualifier). Two call sites, one function, one ordering.
|
|
14
|
+
*/
|
|
15
|
+
import { type AddressNode } from "@mailwoman/core/decoder";
|
|
16
|
+
/**
|
|
17
|
+
* Find the first non-empty region-tagged span anywhere in a tree — the qualifier the walk threads onto locality
|
|
18
|
+
* lookups. Deliberately the same node the admin-coherence VERDICTS read (the first `region` node), so the change's
|
|
19
|
+
* firing population and the flag's `contradicted` population are the same rows. A `region` slot can hold a country name
|
|
20
|
+
* the parser mislabeled ("Moscow, Russia" parses region="Russia") — the backend's qualifier probe answers that too,
|
|
21
|
+
* because containment is containment whatever tier the container sits at.
|
|
22
|
+
*/
|
|
23
|
+
export declare function firstRegionQualifier(roots: readonly AddressNode[]): string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Stable, TIER-SAFE partition: within each match tier, candidates the containment source vouched for (`isContained`)
|
|
26
|
+
* move ahead of the rest, and BOTH groups keep their incoming relative order — the same house rules every soft ranking
|
|
27
|
+
* key here obeys (`toponym-prior.ts`): tier-safe (a contained partial match never outranks an exact one — `isExact`
|
|
28
|
+
* splits the population exactly as `rankWithinTier`'s tri-state rule does, only a stated exact tier earns the front),
|
|
29
|
+
* positive-evidence-only (only a stated `true` moves a row; `false` and "never evaluated" both hold their incoming
|
|
30
|
+
* rank, so an incapable backend is a no-op by construction), and stable (the backend's own ordering survives within
|
|
31
|
+
* each (tier, containment) cell).
|
|
32
|
+
*
|
|
33
|
+
* Implemented with the slot idiom (`reorderMeasured`'s mechanics): each tier's members permute only among the positions
|
|
34
|
+
* that tier already occupies, so a tier-interleaved list — the walk's no-importance path never regrouped tiers — cannot
|
|
35
|
+
* have a rest-tier row promoted across an exact-tier boundary by this partition.
|
|
36
|
+
*/
|
|
37
|
+
export declare function partitionByContainment<T>(rows: readonly T[], isContained: (row: T) => boolean, isExact: (row: T) => boolean): T[];
|
|
38
|
+
/**
|
|
39
|
+
* The trace verdict for one locality pick under the change — stamped as `metadata.admin_containment` so an inert
|
|
40
|
+
* mechanism is visible in the result rather than silently dead (#1719's rule). Derived from the candidates'
|
|
41
|
+
* `containedByQualifier` stamps: any `true` → `"contained"`; stamps present but none true → `"no_contained_candidate"`;
|
|
42
|
+
* no stamps at all → `"unavailable"` (the backend or artifact cannot answer — a pre-sidecar candidate.db, the FTS or
|
|
43
|
+
* browser backend, or a qualifier the probe never ran for).
|
|
44
|
+
*/
|
|
45
|
+
export declare function adminContainmentVerdict(candidates: ReadonlyArray<{
|
|
46
|
+
containedByQualifier?: boolean | undefined;
|
|
47
|
+
}>): "contained" | "no_contained_candidate" | "unavailable";
|
|
48
|
+
//# sourceMappingURL=containment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"containment.d.ts","sourceRoot":"","sources":["../../lib/admin/containment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAkB,KAAK,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAE1E;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,GAAG,SAAS,CAEtF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,EACvC,IAAI,EAAE,SAAS,CAAC,EAAE,EAClB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,EAChC,OAAO,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,GAC1B,CAAC,EAAE,CAuBL;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACtC,UAAU,EAAE,aAAa,CAAC;IAAE,oBAAoB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,CAAC,GACvE,WAAW,GAAG,wBAAwB,GAAG,aAAa,CAYxD"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The admin-containment partition (#1717 stage 2) — the ONE ordering function both deciding sites
|
|
7
|
+
* call, per the #861 rule (shared constants prove nothing; the #861 parity contract diverged at
|
|
8
|
+
* exactly the points a constant cannot express). The candidate backend partitions its row set
|
|
9
|
+
* before the limit window (so a contained candidate can reach the walk at all), and the resolver
|
|
10
|
+
* walk partitions AGAIN after its fame/anchor re-ranks (so `rankByImportance` cannot hand the top
|
|
11
|
+
* slot back to a more-famous uncontained namesake — Richmond VA outscores Richmond, North
|
|
12
|
+
* Yorkshire on importance, and without the second partition the change loses exactly where fame
|
|
13
|
+
* disagrees with the qualifier). Two call sites, one function, one ordering.
|
|
14
|
+
*/
|
|
15
|
+
import { firstNodeWhere } from "@mailwoman/core/decoder";
|
|
16
|
+
/**
|
|
17
|
+
* Find the first non-empty region-tagged span anywhere in a tree — the qualifier the walk threads onto locality
|
|
18
|
+
* lookups. Deliberately the same node the admin-coherence VERDICTS read (the first `region` node), so the change's
|
|
19
|
+
* firing population and the flag's `contradicted` population are the same rows. A `region` slot can hold a country name
|
|
20
|
+
* the parser mislabeled ("Moscow, Russia" parses region="Russia") — the backend's qualifier probe answers that too,
|
|
21
|
+
* because containment is containment whatever tier the container sits at.
|
|
22
|
+
*/
|
|
23
|
+
export function firstRegionQualifier(roots) {
|
|
24
|
+
return firstNodeWhere(roots, (n) => n.tag === "region" && n.value.trim().length > 0)?.value.trim();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Stable, TIER-SAFE partition: within each match tier, candidates the containment source vouched for (`isContained`)
|
|
28
|
+
* move ahead of the rest, and BOTH groups keep their incoming relative order — the same house rules every soft ranking
|
|
29
|
+
* key here obeys (`toponym-prior.ts`): tier-safe (a contained partial match never outranks an exact one — `isExact`
|
|
30
|
+
* splits the population exactly as `rankWithinTier`'s tri-state rule does, only a stated exact tier earns the front),
|
|
31
|
+
* positive-evidence-only (only a stated `true` moves a row; `false` and "never evaluated" both hold their incoming
|
|
32
|
+
* rank, so an incapable backend is a no-op by construction), and stable (the backend's own ordering survives within
|
|
33
|
+
* each (tier, containment) cell).
|
|
34
|
+
*
|
|
35
|
+
* Implemented with the slot idiom (`reorderMeasured`'s mechanics): each tier's members permute only among the positions
|
|
36
|
+
* that tier already occupies, so a tier-interleaved list — the walk's no-importance path never regrouped tiers — cannot
|
|
37
|
+
* have a rest-tier row promoted across an exact-tier boundary by this partition.
|
|
38
|
+
*/
|
|
39
|
+
export function partitionByContainment(rows, isContained, isExact) {
|
|
40
|
+
const out = [...rows];
|
|
41
|
+
for (const wantExact of [true, false]) {
|
|
42
|
+
const slots = [];
|
|
43
|
+
for (const [index, row] of out.entries()) {
|
|
44
|
+
if (isExact(row) === wantExact) {
|
|
45
|
+
slots.push(index);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (slots.length < 2)
|
|
49
|
+
continue;
|
|
50
|
+
const members = slots.map((slot) => out[slot]);
|
|
51
|
+
const reordered = [...members.filter((row) => isContained(row)), ...members.filter((row) => !isContained(row))];
|
|
52
|
+
for (const [k, slot] of slots.entries()) {
|
|
53
|
+
out[slot] = reordered[k];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The trace verdict for one locality pick under the change — stamped as `metadata.admin_containment` so an inert
|
|
60
|
+
* mechanism is visible in the result rather than silently dead (#1719's rule). Derived from the candidates'
|
|
61
|
+
* `containedByQualifier` stamps: any `true` → `"contained"`; stamps present but none true → `"no_contained_candidate"`;
|
|
62
|
+
* no stamps at all → `"unavailable"` (the backend or artifact cannot answer — a pre-sidecar candidate.db, the FTS or
|
|
63
|
+
* browser backend, or a qualifier the probe never ran for).
|
|
64
|
+
*/
|
|
65
|
+
export function adminContainmentVerdict(candidates) {
|
|
66
|
+
let evaluated = false;
|
|
67
|
+
for (const candidate of candidates) {
|
|
68
|
+
if (candidate.containedByQualifier === true)
|
|
69
|
+
return "contained";
|
|
70
|
+
if (candidate.containedByQualifier !== undefined) {
|
|
71
|
+
evaluated = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return evaluated ? "no_contained_candidate" : "unavailable";
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=containment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"containment.js","sourceRoot":"","sources":["../../lib/admin/containment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,cAAc,EA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,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;YACzC,GAAG,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC,CAAE,CAAA;QAC1B,CAAC;IACF,CAAC;IAED,OAAO,GAAG,CAAA;AACX,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CACtC,UAAyE;IAEzE,IAAI,SAAS,GAAG,KAAK,CAAA;IAErB,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACpC,IAAI,SAAS,CAAC,oBAAoB,KAAK,IAAI;YAAE,OAAO,WAAW,CAAA;QAE/D,IAAI,SAAS,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;YAClD,SAAS,GAAG,IAAI,CAAA;QACjB,CAAC;IACF,CAAC;IAED,OAAO,SAAS,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,aAAa,CAAA;AAC5D,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
export * from "#admin/coherence-passes";
|
|
7
|
+
export * from "#admin/containment";
|
|
8
|
+
export * from "#admin/jp-municipality";
|
|
9
|
+
export * from "#admin/winner";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/admin/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,yBAAyB,CAAA;AACvC,cAAc,oBAAoB,CAAA;AAClC,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
export * from "#admin/coherence-passes";
|
|
7
|
+
export * from "#admin/containment";
|
|
8
|
+
export * from "#admin/jp-municipality";
|
|
9
|
+
export * from "#admin/winner";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/admin/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,yBAAyB,CAAA;AACvC,cAAc,oBAAoB,CAAA;AAClC,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file A compound Japanese municipality split into the two units the gazetteer keys separately.
|
|
6
|
+
*
|
|
7
|
+
* The JP model tags `神戸市西区` (city + ward) and `猿島郡五霞町` (county + town) as ONE `municipality` span, which is
|
|
8
|
+
* the postal form, and WOF keys the city, the ward and the town — never the compound. Probed whole, the span misses
|
|
9
|
+
* and the coordinate falls to the prefecture centroid (25–52 km on the JP board). Probed as its trailing unit
|
|
10
|
+
* UNSCOPED it does worse: a bare `西区` resolves a namesake ward in another city (Kobe's answered Fukuoka's, 407 km;
|
|
11
|
+
* 251 of 300 rows against 271 whole). The split is therefore consumed by the resolver walk as a SCOPED pair: the
|
|
12
|
+
* head resolves first, the tail probes as its child, and a namesake outside the head is not admissible.
|
|
13
|
+
*/
|
|
14
|
+
export interface CompoundMunicipality {
|
|
15
|
+
/**
|
|
16
|
+
* The containing unit: the city (`神戸市`) or the county (`猿島郡`).
|
|
17
|
+
*/
|
|
18
|
+
head: string;
|
|
19
|
+
/**
|
|
20
|
+
* The contained unit: the ward (`西区`) or the town / village (`五霞町`).
|
|
21
|
+
*/
|
|
22
|
+
tail: string;
|
|
23
|
+
shape: "city_ward" | "county_town";
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The head and tail of a compound municipality, or null for a plain one (`都城市`, `千代田区`, `東村山市`).
|
|
27
|
+
*/
|
|
28
|
+
export declare function compoundMunicipality(value: string): CompoundMunicipality | null;
|
|
29
|
+
/**
|
|
30
|
+
* One probe of the gazetteer as the walk makes it: `value` under `scope`, with the parent fallback allowed or withheld.
|
|
31
|
+
*/
|
|
32
|
+
export type CompoundMunicipalityProbe<Place, Pick extends {
|
|
33
|
+
top: Place;
|
|
34
|
+
metadata?: Record<string, unknown>;
|
|
35
|
+
}> = (value: string, scope: Place | null, parentFallback: boolean) => Promise<Pick | null>;
|
|
36
|
+
/**
|
|
37
|
+
* A compound municipality probed as a SCOPED pair after the whole span missed: the head under the node's own parent
|
|
38
|
+
* with the walk's ordinary fallback, then the tail as the head's child with the parent fallback WITHHELD, so a namesake
|
|
39
|
+
* ward or town outside the head is never admissible — including one the backend re-admits through its own region-scope
|
|
40
|
+
* fallback (`regionScopeMiss`). A county head usually has no key; the tail then probes under the parent the walk
|
|
41
|
+
* already holds. Answers the tail when it hits, else the head, else null. Two probes at most, each drawn from the
|
|
42
|
+
* caller's budget (`hasBudget`).
|
|
43
|
+
*/
|
|
44
|
+
export declare function resolveCompoundMunicipality<Place extends {
|
|
45
|
+
regionScopeMiss?: boolean;
|
|
46
|
+
}, Pick extends {
|
|
47
|
+
top: Place;
|
|
48
|
+
metadata?: Record<string, unknown>;
|
|
49
|
+
}>(value: string, parent: Place | null, hasBudget: () => boolean, probe: CompoundMunicipalityProbe<Place, Pick>): Promise<Pick | null>;
|
|
50
|
+
//# sourceMappingURL=jp-municipality.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jp-municipality.d.ts","sourceRoot":"","sources":["../../lib/admin/jp-municipality.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,MAAM,WAAW,oBAAoB;IACpC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,WAAW,GAAG,aAAa,CAAA;CAClC;AAKD;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,oBAAoB,GAAG,IAAI,CAe/E;AAED;;GAEG;AACH,MAAM,MAAM,yBAAyB,CAAC,KAAK,EAAE,IAAI,SAAS;IAAE,GAAG,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,IAAI,CAC/G,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,KAAK,GAAG,IAAI,EACnB,cAAc,EAAE,OAAO,KACnB,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAA;AAUzB;;;;;;;GAOG;AACH,wBAAsB,2BAA2B,CAChD,KAAK,SAAS;IAAE,eAAe,CAAC,EAAE,OAAO,CAAA;CAAE,EAC3C,IAAI,SAAS;IAAE,GAAG,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,EAE/D,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,KAAK,GAAG,IAAI,EACpB,SAAS,EAAE,MAAM,OAAO,EACxB,KAAK,EAAE,yBAAyB,CAAC,KAAK,EAAE,IAAI,CAAC,GAC3C,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CActB"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file A compound Japanese municipality split into the two units the gazetteer keys separately.
|
|
6
|
+
*
|
|
7
|
+
* The JP model tags `神戸市西区` (city + ward) and `猿島郡五霞町` (county + town) as ONE `municipality` span, which is
|
|
8
|
+
* the postal form, and WOF keys the city, the ward and the town — never the compound. Probed whole, the span misses
|
|
9
|
+
* and the coordinate falls to the prefecture centroid (25–52 km on the JP board). Probed as its trailing unit
|
|
10
|
+
* UNSCOPED it does worse: a bare `西区` resolves a namesake ward in another city (Kobe's answered Fukuoka's, 407 km;
|
|
11
|
+
* 251 of 300 rows against 271 whole). The split is therefore consumed by the resolver walk as a SCOPED pair: the
|
|
12
|
+
* head resolves first, the tail probes as its child, and a namesake outside the head is not admissible.
|
|
13
|
+
*/
|
|
14
|
+
const CITY_WARD = /^(.+?市)(.+区)$/u;
|
|
15
|
+
const COUNTY_TOWN = /^(.+?郡)(.+[町村])$/u;
|
|
16
|
+
/**
|
|
17
|
+
* The head and tail of a compound municipality, or null for a plain one (`都城市`, `千代田区`, `東村山市`).
|
|
18
|
+
*/
|
|
19
|
+
export function compoundMunicipality(value) {
|
|
20
|
+
const trimmed = value.trim();
|
|
21
|
+
const cityWard = CITY_WARD.exec(trimmed);
|
|
22
|
+
if (cityWard) {
|
|
23
|
+
return { head: cityWard[1], tail: cityWard[2], shape: "city_ward" };
|
|
24
|
+
}
|
|
25
|
+
const countyTown = COUNTY_TOWN.exec(trimmed);
|
|
26
|
+
if (countyTown) {
|
|
27
|
+
return { head: countyTown[1], tail: countyTown[2], shape: "county_town" };
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
function stamp(pick, split, answered) {
|
|
32
|
+
return pick ? { ...pick, metadata: { ...pick.metadata, municipality_split: { ...split, answered } } } : null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A compound municipality probed as a SCOPED pair after the whole span missed: the head under the node's own parent
|
|
36
|
+
* with the walk's ordinary fallback, then the tail as the head's child with the parent fallback WITHHELD, so a namesake
|
|
37
|
+
* ward or town outside the head is never admissible — including one the backend re-admits through its own region-scope
|
|
38
|
+
* fallback (`regionScopeMiss`). A county head usually has no key; the tail then probes under the parent the walk
|
|
39
|
+
* already holds. Answers the tail when it hits, else the head, else null. Two probes at most, each drawn from the
|
|
40
|
+
* caller's budget (`hasBudget`).
|
|
41
|
+
*/
|
|
42
|
+
export async function resolveCompoundMunicipality(value, parent, hasBudget, probe) {
|
|
43
|
+
const split = compoundMunicipality(value);
|
|
44
|
+
if (!split || !hasBudget())
|
|
45
|
+
return null;
|
|
46
|
+
const head = await probe(split.head, parent, true);
|
|
47
|
+
if (!hasBudget())
|
|
48
|
+
return stamp(head, split, "head");
|
|
49
|
+
const tail = await probe(split.tail, head?.top ?? parent, false);
|
|
50
|
+
if (tail && !tail.top.regionScopeMiss)
|
|
51
|
+
return stamp(tail, split, "tail");
|
|
52
|
+
return stamp(head, split, "head");
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=jp-municipality.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jp-municipality.js","sourceRoot":"","sources":["../../lib/admin/jp-municipality.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAcH,MAAM,SAAS,GAAG,gBAAgB,CAAA;AAClC,MAAM,WAAW,GAAG,mBAAmB,CAAA;AAEvC;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAa;IACjD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;IAC5B,MAAM,QAAQ,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAExC,IAAI,QAAQ,EAAE,CAAC;QACd,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAE,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAE,EAAE,KAAK,EAAE,WAAW,EAAE,CAAA;IACtE,CAAC;IAED,MAAM,UAAU,GAAG,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAE5C,IAAI,UAAU,EAAE,CAAC;QAChB,OAAO,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAE,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAE,EAAE,KAAK,EAAE,aAAa,EAAE,CAAA;IAC5E,CAAC;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