@mailwoman/resolver 10.0.0 → 10.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 +9 -7
- package/lib/admin/coherence-passes.ts +19 -160
- package/lib/admin/containment.ts +12 -30
- package/lib/admin/jp-municipality.ts +20 -13
- package/lib/admin/winner.ts +30 -62
- package/lib/backend-capabilities.ts +25 -24
- package/lib/bare-toponym-race.ts +69 -37
- package/lib/decorate-node.ts +17 -41
- package/lib/fold-name.ts +5 -18
- package/lib/index.ts +5 -9
- package/lib/plausibility.ts +35 -62
- package/lib/postcode/country-coherence.ts +64 -284
- package/lib/postcode/prefix.ts +20 -84
- package/lib/postcode/shape-coherence.ts +23 -113
- package/lib/remote-resolver.ts +32 -29
- package/lib/rerank.ts +34 -50
- package/lib/resolve/passes.ts +144 -147
- package/lib/resolve.ts +726 -0
- package/lib/span-rescore.ts +215 -172
- package/lib/street/evidence.ts +65 -73
- package/lib/street/tier.ts +33 -175
- package/lib/toponym-prior.ts +53 -179
- package/out/admin/coherence-passes.d.ts +17 -68
- package/out/admin/coherence-passes.d.ts.map +1 -1
- package/out/admin/coherence-passes.js +18 -160
- package/out/admin/coherence-passes.js.map +1 -1
- package/out/admin/containment.d.ts +11 -29
- package/out/admin/containment.d.ts.map +1 -1
- package/out/admin/containment.js +12 -30
- package/out/admin/containment.js.map +1 -1
- package/out/admin/jp-municipality.d.ts +20 -13
- package/out/admin/jp-municipality.d.ts.map +1 -1
- package/out/admin/jp-municipality.js +18 -12
- package/out/admin/jp-municipality.js.map +1 -1
- package/out/admin/winner.d.ts +30 -62
- package/out/admin/winner.d.ts.map +1 -1
- package/out/admin/winner.js +18 -51
- package/out/admin/winner.js.map +1 -1
- package/out/{admin/index.d.ts → admin.d.ts} +1 -1
- package/out/admin.d.ts.map +1 -0
- package/out/{admin/index.js → admin.js} +1 -1
- package/out/admin.js.map +1 -0
- package/out/backend-capabilities.d.ts +18 -19
- package/out/backend-capabilities.d.ts.map +1 -1
- package/out/backend-capabilities.js +25 -24
- package/out/backend-capabilities.js.map +1 -1
- package/out/bare-toponym-race.d.ts +54 -27
- package/out/bare-toponym-race.d.ts.map +1 -1
- package/out/bare-toponym-race.js +69 -37
- package/out/bare-toponym-race.js.map +1 -1
- package/out/decorate-node.d.ts +6 -8
- package/out/decorate-node.d.ts.map +1 -1
- package/out/decorate-node.js +17 -41
- package/out/decorate-node.js.map +1 -1
- package/out/fold-name.d.ts +5 -18
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +5 -18
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +5 -6
- package/out/index.d.ts.map +1 -1
- package/out/index.js +5 -8
- package/out/index.js.map +1 -1
- package/out/plausibility.d.ts +27 -57
- package/out/plausibility.d.ts.map +1 -1
- package/out/plausibility.js +16 -49
- package/out/plausibility.js.map +1 -1
- package/out/postcode/country-coherence.d.ts +48 -162
- package/out/postcode/country-coherence.d.ts.map +1 -1
- package/out/postcode/country-coherence.js +19 -252
- package/out/postcode/country-coherence.js.map +1 -1
- package/out/postcode/prefix.d.ts +17 -68
- package/out/postcode/prefix.d.ts.map +1 -1
- package/out/postcode/prefix.js +8 -77
- package/out/postcode/prefix.js.map +1 -1
- package/out/postcode/shape-coherence.d.ts +17 -84
- package/out/postcode/shape-coherence.d.ts.map +1 -1
- package/out/postcode/shape-coherence.js +9 -105
- package/out/postcode/shape-coherence.js.map +1 -1
- package/out/{postcode/index.d.ts → postcode.d.ts} +1 -1
- package/out/postcode.d.ts.map +1 -0
- package/out/{postcode/index.js → postcode.js} +1 -1
- package/out/postcode.js.map +1 -0
- package/out/remote-resolver.d.ts +27 -28
- package/out/remote-resolver.d.ts.map +1 -1
- package/out/remote-resolver.js +9 -19
- package/out/remote-resolver.js.map +1 -1
- package/out/rerank.d.ts +28 -45
- package/out/rerank.d.ts.map +1 -1
- package/out/rerank.js +13 -38
- package/out/rerank.js.map +1 -1
- package/out/resolve/passes.d.ts +95 -85
- package/out/resolve/passes.d.ts.map +1 -1
- package/out/resolve/passes.js +40 -105
- package/out/resolve/passes.js.map +1 -1
- package/out/resolve.d.ts +15 -0
- package/out/resolve.d.ts.map +1 -0
- package/out/resolve.js +543 -0
- package/out/resolve.js.map +1 -0
- package/out/span-rescore.d.ts +51 -64
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +132 -130
- package/out/span-rescore.js.map +1 -1
- package/out/street/evidence.d.ts +56 -61
- package/out/street/evidence.d.ts.map +1 -1
- package/out/street/evidence.js +22 -46
- package/out/street/evidence.js.map +1 -1
- package/out/street/tier.d.ts +17 -29
- package/out/street/tier.d.ts.map +1 -1
- package/out/street/tier.js +27 -175
- package/out/street/tier.js.map +1 -1
- package/out/{street/index.d.ts → street.d.ts} +1 -1
- package/out/street.d.ts.map +1 -0
- package/out/{street/index.js → street.js} +1 -1
- package/out/street.js.map +1 -0
- package/out/toponym-prior.d.ts +21 -109
- package/out/toponym-prior.d.ts.map +1 -1
- package/out/toponym-prior.js +41 -170
- package/out/toponym-prior.js.map +1 -1
- package/package.json +44 -147
- package/lib/resolve/index.ts +0 -989
- package/out/admin/index.d.ts.map +0 -1
- package/out/admin/index.js.map +0 -1
- package/out/postcode/index.d.ts.map +0 -1
- package/out/postcode/index.js.map +0 -1
- package/out/resolve/index.d.ts +0 -20
- package/out/resolve/index.d.ts.map +0 -1
- package/out/resolve/index.js +0 -808
- package/out/resolve/index.js.map +0 -1
- package/out/street/index.d.ts.map +0 -1
- package/out/street/index.js.map +0 -1
- package/vitest.config.ts +0 -38
- /package/lib/{admin/index.ts → admin.ts} +0 -0
- /package/lib/{postcode/index.ts → postcode.ts} +0 -0
- /package/lib/{street/index.ts → street.ts} +0 -0
package/out/span-rescore.d.ts
CHANGED
|
@@ -3,71 +3,70 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* the address comes back with no coordinate. But the whole word sits intact in the raw input — a
|
|
10
|
-
* whitespace tokenizer sees it where the model's subword tokenizer didn't.
|
|
11
|
-
*
|
|
12
|
-
* This module is the PURE, backend-agnostic core: enumerate contiguous raw-token spans, exact-match
|
|
13
|
-
* them against the same-country gazetteer, and return the best locality candidate. The resolver
|
|
14
|
-
* (`resolve.ts` → `applySpanRescore`) owns the integration: it runs this ONLY on an unresolved
|
|
15
|
-
* tree (the #685 brake — never second-guess a working coordinate) and injects the recovered
|
|
16
|
-
* locality as a resolved node. Default-ON (#370, promoted 2026-06-25 — same-harness EU+AU +1pp
|
|
17
|
-
* @25km, zero regressions); explicit opt-out via `ResolveOpts.spanRescore: false`, byte-stable then.
|
|
18
|
-
*
|
|
19
|
-
* The design + thresholds are validated on a 7-locale coordinate panel
|
|
20
|
-
* (`scripts/eval/span-rescore-validate.ts`, eval
|
|
21
|
-
* `docs/articles/evals/experiments/2026-06-23-370-span-rescore.mdx`): longest-exact-match-wins (the gold
|
|
22
|
-
* locality is usually the LONGER, more-specific name — shortest- wins grabs the ambiguous prefix
|
|
23
|
-
* "Tomaszów" of "Tomaszów Mazowiecki", 135 km off), and a postcode- consistency check that rejects
|
|
24
|
-
* a match far from where the postcode resolves (kills coverage-gap false-positives where the
|
|
25
|
-
* backend has postcode coverage).
|
|
6
|
+
* Recover a dropped or fragmented locality from the raw text when a parse fails to resolve, by
|
|
7
|
+
* matching contiguous raw-token spans against the same-country gazetteer. Default-on with an
|
|
8
|
+
* explicit `ResolveOpts.spanRescore: false` opt-out.
|
|
26
9
|
*/
|
|
27
10
|
import { type AddressNode } from "@mailwoman/core/decoder";
|
|
28
|
-
import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver";
|
|
11
|
+
import type { ResolvedPlace, ResolverBackend, WeakResolutionReading } from "@mailwoman/core/resolver";
|
|
29
12
|
export interface SpanRescoreOptions {
|
|
30
13
|
/**
|
|
31
14
|
* ISO-3166 alpha-2 country to constrain the gazetteer match (the parse's detected/ default country).
|
|
32
15
|
*/
|
|
33
16
|
country?: string;
|
|
34
17
|
/**
|
|
35
|
-
*
|
|
18
|
+
* The sibling postcode serves as the backend disambiguation hint and the consistency-check anchor.
|
|
36
19
|
*/
|
|
37
20
|
postcode?: string;
|
|
38
21
|
/**
|
|
39
|
-
* Reject a candidate whose coordinate is farther than this (km) from the postcode anchor.
|
|
40
|
-
*
|
|
41
|
-
*
|
|
22
|
+
* Reject a candidate whose coordinate is farther than this (km) from the postcode anchor.
|
|
23
|
+
*
|
|
24
|
+
* The check fires only when the postcode resolves to a point.
|
|
25
|
+
* A backend without postcode coverage receives no penalty.
|
|
26
|
+
*
|
|
27
|
+
* A value of 0 disables the check.
|
|
28
|
+
* Default 50.
|
|
42
29
|
*/
|
|
43
30
|
thresholdKm?: number;
|
|
44
31
|
/**
|
|
45
|
-
* Max contiguous raw tokens to treat as one locality span.
|
|
32
|
+
* Max contiguous raw tokens to treat as one locality span.
|
|
33
|
+
*
|
|
34
|
+
* Default 4.
|
|
46
35
|
*/
|
|
47
36
|
maxSpanTokens?: number;
|
|
48
37
|
/**
|
|
49
|
-
* Min confidence for a street/house_number/postcode node to count as a span-blocking constituent.
|
|
38
|
+
* Min confidence for a street/house_number/postcode node to count as a span-blocking constituent.
|
|
39
|
+
*
|
|
40
|
+
* Default 0.7.
|
|
50
41
|
*/
|
|
51
42
|
confidentThreshold?: number;
|
|
52
43
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
44
|
+
* When on, the anchor retries with the postcode's code-shaped token subset and an unresolved
|
|
45
|
+
* postcode node blocks only those code tokens, leaving the residual name tokens as span material.
|
|
46
|
+
*
|
|
47
|
+
* Street/affix blocking is untouched.
|
|
48
|
+
* Default false.
|
|
58
49
|
*/
|
|
59
50
|
postalCompoundRecovery?: boolean;
|
|
60
51
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
52
|
+
* When the span covers the whole unqualified input, treat {@link country} as an additive prior
|
|
53
|
+
* instead of a hard gazetteer filter; `false` restores the hard filter byte-for-byte.
|
|
54
|
+
* Default true.
|
|
64
55
|
*/
|
|
65
56
|
bareToponymSoftCountry?: boolean;
|
|
66
57
|
/**
|
|
67
|
-
* Weight of that prior, in log10-population units.
|
|
68
|
-
*
|
|
58
|
+
* Weight of that prior, in log10-population units.
|
|
59
|
+
*
|
|
60
|
+
* Default {@link DEFAULT_COUNTRY_PRIOR_WEIGHT} (2), and 0 removes the locale's say entirely.
|
|
69
61
|
*/
|
|
70
62
|
bareToponymCountryWeight?: number;
|
|
63
|
+
/**
|
|
64
|
+
* Admit a proper sub-span only when every token it leaves behind is a subdivision code or a number.
|
|
65
|
+
*
|
|
66
|
+
* See `remainderIsContext` for the rule.
|
|
67
|
+
* Default false.
|
|
68
|
+
*/
|
|
69
|
+
spanRescoreRequireContextRemainder?: boolean;
|
|
71
70
|
}
|
|
72
71
|
/**
|
|
73
72
|
* The recovered locality: the raw span and the gazetteer place it resolved to.
|
|
@@ -87,45 +86,33 @@ export interface RescoreCandidate {
|
|
|
87
86
|
*/
|
|
88
87
|
place: ResolvedPlace;
|
|
89
88
|
/**
|
|
90
|
-
* Whether the postcode-consistency check
|
|
91
|
-
* match was validated within `thresholdKm`
|
|
92
|
-
*
|
|
93
|
-
* case). The caller surfaces this as `metadata.rescore_postcode_verified` so a consumer can threshold on it WITHOUT a
|
|
94
|
-
* hidden per-country coverage map. Deliberately NOT folded into the calibrated `confidence` — that would break the
|
|
95
|
-
* isotonic guarantee (a true calibrated 0.83 must not be confused with a rescore plug-in estimate).
|
|
89
|
+
* Whether the postcode-consistency check fired — the postcode resolved to a point
|
|
90
|
+
* and the match was validated within `thresholdKm` — deliberately kept out of the
|
|
91
|
+
* calibrated `confidence` so the isotonic fit stays monotone.
|
|
96
92
|
*/
|
|
97
93
|
postcodeVerified: boolean;
|
|
98
94
|
/**
|
|
99
|
-
* The
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* #1537: these were being discarded. A name the model reads as a `street` ("Springfield", "Berlin", "Manchester",
|
|
104
|
-
* "Moscow", "Fulda") never reaches the admin walk, so the whole tree comes back unresolved and THIS tier is what
|
|
105
|
-
* recovers it — and it decorated the injected node with no alternatives at all. The result was that the geocode
|
|
106
|
-
* path's `candidates` array held one entry, and the top-1-vs-top-2 dominance margin that `declared_ambiguity` reads
|
|
107
|
-
* was uncomputable, for exactly the famous-homonym class that marker exists for. Measured on the shipped candidate
|
|
108
|
-
* backend, 2026-08-07: those five queries returned 1 candidate each while `Cambridge`/`Athens`/`Paris` — the same
|
|
109
|
-
* class, but parsed as `locality` — returned 4-5.
|
|
110
|
-
*
|
|
111
|
-
* The winner is unchanged by their presence: the pick is still the first check-passing exact match, and these are the
|
|
112
|
-
* ones that came after it. This costs no extra query — they were in the same `findPlace` response.
|
|
95
|
+
* The same-span namesake runner-ups this recovery's own lookup already returned,
|
|
96
|
+
* in backend rank order, minus the winner and anything the postcode check rejected,
|
|
97
|
+
* so the top-1-vs-top-2 dominance margin stays computable.
|
|
113
98
|
*/
|
|
114
99
|
alternatives: ResolvedPlace[];
|
|
115
100
|
}
|
|
116
101
|
/**
|
|
117
|
-
* The code-shaped (digit-
|
|
118
|
-
*
|
|
119
|
-
* Empty string when no token carries a digit.
|
|
102
|
+
* The code-shaped (digit-containing) token subset of a postcode string — "1382 Kožljek"
|
|
103
|
+
* → "1382" — and an empty string when no token contains a digit.
|
|
120
104
|
*/
|
|
121
105
|
export declare function postcodeCodeSubset(postcode: string): string;
|
|
122
106
|
/**
|
|
123
|
-
* True if any node in the tree already
|
|
107
|
+
* True if any node in the tree already has a resolved place id — the brake on span rescore.
|
|
108
|
+
* with `weakReading`, a node whose resolution is weak under that reading does not hold it.
|
|
124
109
|
*/
|
|
125
|
-
export declare function hasResolvedPlace(roots: readonly AddressNode[]): boolean;
|
|
110
|
+
export declare function hasResolvedPlace(roots: readonly AddressNode[], weakReading?: WeakResolutionReading | false): boolean;
|
|
126
111
|
/**
|
|
127
|
-
* Find the best locality the raw text
|
|
128
|
-
*
|
|
112
|
+
* Find the best locality that the raw text exactly matches in the gazetteer.
|
|
113
|
+
*
|
|
114
|
+
* Return `null` when no entry matches or the postcode check rejects every match.
|
|
115
|
+
* Callers test `hasResolvedPlace` first.
|
|
129
116
|
*/
|
|
130
117
|
export declare function findRescoreCandidate(raw: string, roots: readonly AddressNode[], backend: ResolverBackend, opts?: SpanRescoreOptions): Promise<RescoreCandidate | null>;
|
|
131
118
|
//# sourceMappingURL=span-rescore.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"span-rescore.d.ts","sourceRoot":"","sources":["../lib/span-rescore.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"span-rescore.d.ts","sourceRoot":"","sources":["../lib/span-rescore.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAA6B,KAAK,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrF,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AAQrG,MAAM,WAAW,kBAAkB;IAClC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;;;;;;OAQG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B;;;;;;OAMG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC;;;;OAIG;IACH,wBAAwB,CAAC,EAAE,MAAM,CAAA;IACjC;;;;;OAKG;IACH,kCAAkC,CAAC,EAAE,OAAO,CAAA;CAC5C;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;OAEG;IACH,KAAK,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,KAAK,EAAE,aAAa,CAAA;IACpB;;;;OAIG;IACH,gBAAgB,EAAE,OAAO,CAAA;IACzB;;;;OAIG;IACH,YAAY,EAAE,aAAa,EAAE,CAAA;CAC7B;AAuBD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAM3D;AA2CD;;;GAGG;AACH,wBAAgB,gBAAgB,CAC/B,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,WAAW,GAAE,qBAAqB,GAAG,KAAa,GAChD,OAAO,CAIT;AAgGD;;;;;GAKG;AACH,wBAAsB,oBAAoB,CACzC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,OAAO,EAAE,eAAe,EACxB,IAAI,GAAE,kBAAuB,GAC3B,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAuOlC"}
|
package/out/span-rescore.js
CHANGED
|
@@ -3,29 +3,14 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* the address comes back with no coordinate. But the whole word sits intact in the raw input — a
|
|
10
|
-
* whitespace tokenizer sees it where the model's subword tokenizer didn't.
|
|
11
|
-
*
|
|
12
|
-
* This module is the PURE, backend-agnostic core: enumerate contiguous raw-token spans, exact-match
|
|
13
|
-
* them against the same-country gazetteer, and return the best locality candidate. The resolver
|
|
14
|
-
* (`resolve.ts` → `applySpanRescore`) owns the integration: it runs this ONLY on an unresolved
|
|
15
|
-
* tree (the #685 brake — never second-guess a working coordinate) and injects the recovered
|
|
16
|
-
* locality as a resolved node. Default-ON (#370, promoted 2026-06-25 — same-harness EU+AU +1pp
|
|
17
|
-
* @25km, zero regressions); explicit opt-out via `ResolveOpts.spanRescore: false`, byte-stable then.
|
|
18
|
-
*
|
|
19
|
-
* The design + thresholds are validated on a 7-locale coordinate panel
|
|
20
|
-
* (`scripts/eval/span-rescore-validate.ts`, eval
|
|
21
|
-
* `docs/articles/evals/experiments/2026-06-23-370-span-rescore.mdx`): longest-exact-match-wins (the gold
|
|
22
|
-
* locality is usually the LONGER, more-specific name — shortest- wins grabs the ambiguous prefix
|
|
23
|
-
* "Tomaszów" of "Tomaszów Mazowiecki", 135 km off), and a postcode- consistency check that rejects
|
|
24
|
-
* a match far from where the postcode resolves (kills coverage-gap false-positives where the
|
|
25
|
-
* backend has postcode coverage).
|
|
6
|
+
* Recover a dropped or fragmented locality from the raw text when a parse fails to resolve, by
|
|
7
|
+
* matching contiguous raw-token spans against the same-country gazetteer. Default-on with an
|
|
8
|
+
* explicit `ResolveOpts.spanRescore: false` opt-out.
|
|
26
9
|
*/
|
|
27
10
|
import { firstNodeWhere, walkNodes } from "@mailwoman/core/decoder";
|
|
11
|
+
import { isRegionAbbreviationToken } from "@mailwoman/query-shape/region-abbreviations";
|
|
28
12
|
import { haversineKm } from "@mailwoman/spatial";
|
|
13
|
+
import { partitionByContainment } from "#admin/containment";
|
|
29
14
|
import { foldName } from "#fold-name";
|
|
30
15
|
import { DEFAULT_COUNTRY_PRIOR_WEIGHT, rankByCountryPrior, rankByImportance } from "#toponym-prior";
|
|
31
16
|
/**
|
|
@@ -41,9 +26,8 @@ function tokenizeRaw(raw) {
|
|
|
41
26
|
return toks;
|
|
42
27
|
}
|
|
43
28
|
/**
|
|
44
|
-
* The code-shaped (digit-
|
|
45
|
-
*
|
|
46
|
-
* Empty string when no token carries a digit.
|
|
29
|
+
* The code-shaped (digit-containing) token subset of a postcode string — "1382 Kožljek"
|
|
30
|
+
* → "1382" — and an empty string when no token contains a digit.
|
|
47
31
|
*/
|
|
48
32
|
export function postcodeCodeSubset(postcode) {
|
|
49
33
|
return postcode
|
|
@@ -53,16 +37,14 @@ export function postcodeCodeSubset(postcode) {
|
|
|
53
37
|
.trim();
|
|
54
38
|
}
|
|
55
39
|
/**
|
|
56
|
-
* Over-fetch for the
|
|
57
|
-
* often all in one country,
|
|
58
|
-
* five US rows ahead of Warwick, Warwickshire. The backend orders by the same population key either way, so a wider
|
|
59
|
-
* window only admits more of the same list.
|
|
40
|
+
* Over-fetch for the bare-toponym probe: without a country filter the top 5 worldwide bearers of a
|
|
41
|
+
* common name are often all in one country, with the candidate the soft prior promotes below the fold.
|
|
60
42
|
*/
|
|
61
43
|
const BARE_TOPONYM_FETCH = 20;
|
|
62
44
|
/**
|
|
63
|
-
* True when the tree names its
|
|
64
|
-
*
|
|
65
|
-
* the hard filter keeps its say
|
|
45
|
+
* True when the tree names its own admin context — a region, subregion, country,
|
|
46
|
+
* postcode or house number the parser read out of the input — so the soft-country
|
|
47
|
+
* prior stands down and the hard filter keeps its say.
|
|
66
48
|
*/
|
|
67
49
|
function hasAdminQualifier(roots) {
|
|
68
50
|
return (firstNodeWhere(roots, (n) => (n.tag === "region" ||
|
|
@@ -73,29 +55,32 @@ function hasAdminQualifier(roots) {
|
|
|
73
55
|
n.value.trim().length > 0) !== undefined);
|
|
74
56
|
}
|
|
75
57
|
/**
|
|
76
|
-
*
|
|
58
|
+
* Whether one node's resolution rests on evidence thin enough to re-open, under `reading`.
|
|
77
59
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
60
|
+
function resolvedWeakly(node, reading) {
|
|
61
|
+
const metadata = node.metadata ?? {};
|
|
62
|
+
const weakScore = metadata["resolver_score"] === 0;
|
|
63
|
+
const weakContainment = metadata["admin_containment"] === "no_contained_candidate";
|
|
64
|
+
if (reading === "score")
|
|
65
|
+
return weakScore;
|
|
66
|
+
if (reading === "containment")
|
|
67
|
+
return weakContainment;
|
|
68
|
+
return weakScore || weakContainment;
|
|
80
69
|
}
|
|
81
70
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* NY" is a street suffix, not a locality, and without this guard the recovery exact-matches it against a same-named
|
|
85
|
-
* place ("Ave", France) and injects a bogus locality.
|
|
71
|
+
* True if any node in the tree already has a resolved place id — the brake on span rescore.
|
|
72
|
+
* with `weakReading`, a node whose resolution is weak under that reading does not hold it.
|
|
86
73
|
*/
|
|
74
|
+
export function hasResolvedPlace(roots, weakReading = false) {
|
|
75
|
+
return (firstNodeWhere(roots, (n) => Boolean(n.placeID) && !(weakReading && resolvedWeakly(n, weakReading))) !== undefined);
|
|
76
|
+
}
|
|
87
77
|
/**
|
|
88
|
-
* Ranges of
|
|
89
|
-
*
|
|
78
|
+
* Ranges of multi-token `country` / `region` spans, used to block their interior
|
|
79
|
+
* tokens from being re-read as standalone places.
|
|
90
80
|
*
|
|
91
|
-
* The
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* Deliberately NOT confidence-conditioned, unlike {@link confidentRanges}. The country node in that case carries 0.68,
|
|
96
|
-
* under the 0.7 bar, and a low-confidence GROUPING is still a grouping: the tokens were read as one name either way,
|
|
97
|
-
* and the interior of a name the parse doubts is not thereby a better standalone candidate. Single-token spans are
|
|
98
|
-
* excluded — there is no interior to protect.
|
|
81
|
+
* The whole span stays probeable.
|
|
82
|
+
* Single-token spans are excluded.
|
|
83
|
+
* The guard does not depend on confidence.
|
|
99
84
|
*/
|
|
100
85
|
function multiTokenNameInteriors(roots, raw) {
|
|
101
86
|
const out = [];
|
|
@@ -109,8 +94,24 @@ function multiTokenNameInteriors(roots, raw) {
|
|
|
109
94
|
}
|
|
110
95
|
return out;
|
|
111
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* The character ranges the parse read as street material, context only
|
|
99
|
+
* when the range does not overlap the span under test.
|
|
100
|
+
*/
|
|
101
|
+
function streetRanges(roots) {
|
|
102
|
+
const out = [];
|
|
103
|
+
for (const n of walkNodes(roots)) {
|
|
104
|
+
if ((n.tag === "street" || n.tag === "street_prefix" || n.tag === "street_suffix") &&
|
|
105
|
+
Number.isFinite(n.start) &&
|
|
106
|
+
Number.isFinite(n.end)) {
|
|
107
|
+
out.push([n.start, n.end]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
112
|
function confidentRanges(roots, threshold, raw, postalCompoundRecovery) {
|
|
113
113
|
const out = [];
|
|
114
|
+
const affix = [];
|
|
114
115
|
for (const n of walkNodes(roots)) {
|
|
115
116
|
if ((n.tag === "postcode" ||
|
|
116
117
|
n.tag === "house_number" ||
|
|
@@ -120,9 +121,8 @@ function confidentRanges(roots, threshold, raw, postalCompoundRecovery) {
|
|
|
120
121
|
(n.confidence ?? 0) >= threshold &&
|
|
121
122
|
Number.isFinite(n.start) &&
|
|
122
123
|
Number.isFinite(n.end)) {
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
// street family keep the full-range block (the "Ave, France" guard).
|
|
124
|
+
// An unresolved postcode span blocks only its code-shaped tokens.
|
|
125
|
+
// Resolved postcodes and the street family keep the full-range block.
|
|
126
126
|
if (postalCompoundRecovery && n.tag === "postcode" && !n.placeID) {
|
|
127
127
|
for (const t of tokenizeRaw(raw.slice(n.start, n.end))) {
|
|
128
128
|
if (/\d/.test(t.text)) {
|
|
@@ -130,16 +130,21 @@ function confidentRanges(roots, threshold, raw, postalCompoundRecovery) {
|
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
|
+
else if (n.tag === "street_prefix" || n.tag === "street_suffix") {
|
|
134
|
+
affix.push([n.start, n.end]);
|
|
135
|
+
}
|
|
133
136
|
else {
|
|
134
137
|
out.push([n.start, n.end]);
|
|
135
138
|
}
|
|
136
139
|
}
|
|
137
140
|
}
|
|
138
|
-
return out;
|
|
141
|
+
return { hard: out, affix };
|
|
139
142
|
}
|
|
140
143
|
/**
|
|
141
|
-
* Find the best locality the raw text
|
|
142
|
-
*
|
|
144
|
+
* Find the best locality that the raw text exactly matches in the gazetteer.
|
|
145
|
+
*
|
|
146
|
+
* Return `null` when no entry matches or the postcode check rejects every match.
|
|
147
|
+
* Callers test `hasResolvedPlace` first.
|
|
143
148
|
*/
|
|
144
149
|
export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
145
150
|
const thresholdKm = opts.thresholdKm ?? 50;
|
|
@@ -147,21 +152,18 @@ export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
|
147
152
|
const threshold = opts.confidentThreshold ?? 0.7;
|
|
148
153
|
const country = opts.country;
|
|
149
154
|
const postcode = opts.postcode?.trim() || undefined;
|
|
150
|
-
//
|
|
151
|
-
//
|
|
155
|
+
// The postcode-consistency anchor.
|
|
156
|
+
// No candidate leaves it null, so the check cannot fire and the match is accepted.
|
|
152
157
|
let anchor = null;
|
|
153
158
|
if (postcode && thresholdKm > 0) {
|
|
154
|
-
//
|
|
155
|
-
//
|
|
156
|
-
// false anchor then EXCLUDES the true village. A typed miss leaves anchor=null → the match is
|
|
157
|
-
// accepted unrestricted, which is the correct degradation for an unverifiable code.
|
|
159
|
+
// Both anchor probes are `postalcode`-typed: an untyped truncated code fragment
|
|
160
|
+
// name-matches arbitrary places and the false anchor then excludes the true village.
|
|
158
161
|
const pcHits = await backend.findPlace({ text: postcode, country, placetype: "postalcode", limit: 2 });
|
|
159
162
|
const a = pcHits.find((h) => h.lat !== 0 || h.lon !== 0);
|
|
160
163
|
if (a) {
|
|
161
164
|
anchor = { lat: a.lat, lon: a.lon };
|
|
162
165
|
}
|
|
163
|
-
//
|
|
164
|
-
// the code-shaped token subset so the consistency check can validate the recovered city.
|
|
166
|
+
// The globbed compound matches no bare-code row, so retry the anchor with the code-shaped token subset.
|
|
165
167
|
if (!anchor && opts.postalCompoundRecovery) {
|
|
166
168
|
const code = postcodeCodeSubset(postcode);
|
|
167
169
|
if (code && code !== postcode) {
|
|
@@ -175,8 +177,14 @@ export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
|
175
177
|
}
|
|
176
178
|
const toks = tokenizeRaw(raw);
|
|
177
179
|
const avoid = confidentRanges(roots, threshold, raw, opts.postalCompoundRecovery ?? false);
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
+
const streets = streetRanges(roots);
|
|
181
|
+
const overlapsAvoid = (s, e) => avoid.hard.some(([as, ae]) => s < ae && as < e) ||
|
|
182
|
+
avoid.affix.some(([as, ae]) => {
|
|
183
|
+
if (e <= as || ae <= s)
|
|
184
|
+
return false; // disjoint — the affix has no say
|
|
185
|
+
const strictlyContains = s <= as && ae <= e && e - s > ae - as;
|
|
186
|
+
return !strictlyContains;
|
|
187
|
+
});
|
|
180
188
|
const nameInteriors = multiTokenNameInteriors(roots, raw);
|
|
181
189
|
const isNameInterior = (s, e) => nameInteriors.some(([ns, ne]) => s >= ns && e <= ne && !(s === ns && e === ne));
|
|
182
190
|
const spans = [];
|
|
@@ -188,51 +196,59 @@ export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
|
188
196
|
continue;
|
|
189
197
|
if (isNameInterior(start, end))
|
|
190
198
|
continue;
|
|
191
|
-
spans.push({ text: raw.slice(start, end), start, end
|
|
199
|
+
spans.push({ text: raw.slice(start, end), start, end });
|
|
192
200
|
}
|
|
193
201
|
}
|
|
194
|
-
spans.sort((a, b) => b.
|
|
195
|
-
//
|
|
196
|
-
//
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
202
|
+
spans.sort((a, b) => b.end - b.start - (a.end - a.start) || a.start - b.start);
|
|
203
|
+
// For a bare toponym the caller's `country` is a locale default rather than knowledge, so the hard
|
|
204
|
+
// filter is demoted to an additive prior and the span must cover the whole unqualified input.
|
|
205
|
+
/**
|
|
206
|
+
* The token a sub-span probe leaves behind when it reads as an administrative qualifier: one token
|
|
207
|
+
* of 2 or 3 uppercase ASCII letters, shaped as a subdivision code rather than looked up in a table.
|
|
208
|
+
*/
|
|
209
|
+
const qualifierRemainder = (span) => {
|
|
210
|
+
const outside = toks.filter((t) => t.end <= span.start || t.start >= span.end);
|
|
211
|
+
if (outside.length !== 1)
|
|
212
|
+
return undefined;
|
|
213
|
+
const token = outside[0].text;
|
|
214
|
+
return isRegionAbbreviationToken(token, { maxLetters: 3 }) ? token : undefined;
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* Whether every token a sub-span leaves behind is context rather than identity,
|
|
218
|
+
* testing the remainder's shape rather than the span being proper.
|
|
219
|
+
*/
|
|
220
|
+
const remainderIsContext = (span) => {
|
|
221
|
+
const outside = toks.filter((t) => t.end <= span.start || t.start >= span.end);
|
|
222
|
+
if (!outside.length)
|
|
223
|
+
return true;
|
|
224
|
+
const streetContext = streets.filter(([start, end]) => end <= span.start || start >= span.end);
|
|
225
|
+
return outside.every((t) => isRegionAbbreviationToken(t.text, { maxLetters: 3 }) ||
|
|
226
|
+
/^\d+[\d-]*$/.test(t.text) ||
|
|
227
|
+
streetContext.some(([start, end]) => t.start >= start && t.end <= end));
|
|
228
|
+
};
|
|
213
229
|
const softCountry = opts.bareToponymSoftCountry !== false;
|
|
214
230
|
const countryWeight = opts.bareToponymCountryWeight ?? DEFAULT_COUNTRY_PRIOR_WEIGHT;
|
|
215
231
|
const qualified = !!postcode || hasAdminQualifier(roots);
|
|
216
232
|
const wholeInput = toks.length ? { start: toks[0].start, end: toks.at(-1).end } : null;
|
|
217
|
-
// Everything the prior needs EXCEPT the span itself — hoisted so the per-span test is one comparison.
|
|
218
233
|
const softCountryEligible = softCountry && !!country && !qualified && !!wholeInput;
|
|
219
234
|
for (const sp of spans) {
|
|
220
235
|
const key = foldName(sp.text);
|
|
221
236
|
if (key.length < 2 || /^\d+$/.test(key))
|
|
222
237
|
continue; // skip bare numbers / empties
|
|
223
|
-
//
|
|
224
|
-
// needs an unqualified tree
|
|
225
|
-
// a scope-less bare "Riyadh"/"Frankfurt" is still a NAMING (their Latin surfaces live in alias
|
|
226
|
-
// rows: الرياض / Frankfurt am Main), and conflating the two checks cost exactly those rows on the
|
|
227
|
-
// 2026-08-15 board before this line split them.
|
|
238
|
+
// `wholeSpan` by itself decides the alias tier, while the soft-country prior
|
|
239
|
+
// also needs an unqualified tree and a caller country.
|
|
228
240
|
const wholeSpan = !!wholeInput && sp.start === wholeInput.start && sp.end === wholeInput.end;
|
|
241
|
+
// A sub-span that drops a word of the name is a corruption rather than a
|
|
242
|
+
// recovery. see `remainderIsContext`.
|
|
243
|
+
if (opts.spanRescoreRequireContextRemainder && !wholeSpan && !remainderIsContext(sp))
|
|
244
|
+
continue;
|
|
229
245
|
const bare = softCountryEligible && wholeSpan;
|
|
246
|
+
const qualifier = wholeSpan ? undefined : qualifierRemainder(sp);
|
|
230
247
|
const hits = bare
|
|
231
248
|
? rankByCountryPrior(await backend.findPlace({ text: sp.text, postcode, placetype: "locality", limit: BARE_TOPONYM_FETCH }), country, countryWeight)
|
|
232
249
|
: // A SUB-span probe is a RE-READING of a token the parse classified into a longer span — it
|
|
233
|
-
// never
|
|
234
|
-
//
|
|
235
|
-
// Москва/Riyadh-class exonym recall, #1546).
|
|
250
|
+
// never included an alias, so alias-keyed rows are off for it (`primaryOnly`); a whole-input
|
|
251
|
+
// span keeps the alias tier regardless of scope.
|
|
236
252
|
await backend.findPlace({
|
|
237
253
|
text: sp.text,
|
|
238
254
|
country,
|
|
@@ -240,32 +256,28 @@ export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
|
240
256
|
placetype: "locality",
|
|
241
257
|
limit: 5,
|
|
242
258
|
...(wholeSpan ? {} : { primaryOnly: true }),
|
|
259
|
+
// The qualifier the sub-span left behind.
|
|
260
|
+
// A backend without the ancestors sidecar ignores this qualifier.
|
|
261
|
+
// That matches a query without one.
|
|
262
|
+
...(qualifier === undefined ? {} : { regionQualifier: qualifier }),
|
|
243
263
|
});
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
// equals it. Re-comparing only the PRIMARY name folded to [a-z0-9 ] excluded exactly the
|
|
247
|
-
// non-Latin-primary class: Москва folds to "" and could never equal "moscow", so Moscow RU never
|
|
248
|
-
// entered the list and Moscow, Idaho won by default among the Latin-named bearers — population-
|
|
249
|
-
// first ranking starved, not violated. The alias surface is the recall; ranking then does its job.
|
|
250
|
-
// The postcode check below still applies to every admitted candidate, Moscow RU included.
|
|
264
|
+
// No primary-name re-check: `exactMatch` is name-or-alias, so re-comparing only
|
|
265
|
+
// the primary name would exclude the non-Latin-primary class.
|
|
251
266
|
//
|
|
252
|
-
//
|
|
253
|
-
//
|
|
254
|
-
|
|
255
|
-
//
|
|
256
|
-
//
|
|
257
|
-
|
|
258
|
-
|
|
267
|
+
// Importance-first within the admitted set.
|
|
268
|
+
// It abstains on an artifact predating the column and so changes no pick.
|
|
269
|
+
const ranked = rankByImportance(hits.filter((h) => h.exactMatch && (h.lat !== 0 || h.lon !== 0)));
|
|
270
|
+
// The same partition the walk applies: tier-safe, stable and positive-evidence-only,
|
|
271
|
+
// so a backend that ignored `regionQualifier` is byte-stable.
|
|
272
|
+
const exact = qualifier === undefined
|
|
273
|
+
? ranked
|
|
274
|
+
: partitionByContainment(ranked, (c) => c.containedByQualifier === true, (c) => c.exactMatch === true);
|
|
259
275
|
const withinThreshold = (p) => !anchor || thresholdKm <= 0 || haversineKm(anchor.lat, anchor.lon, p.lat, p.lon) <= thresholdKm;
|
|
260
276
|
for (const h of exact) {
|
|
261
277
|
if (!withinThreshold(h))
|
|
262
278
|
continue;
|
|
263
|
-
//
|
|
264
|
-
//
|
|
265
|
-
//
|
|
266
|
-
// #1537: carry the rest of the SAME lookup's exact matches as the namesake runner-ups. Check-filtered on the
|
|
267
|
-
// same rule as the winner — a candidate the postcode check rejected is not a namesake worth offering, it is a
|
|
268
|
-
// place the evidence already excluded. Rank order is the backend's, preserved by `filter`.
|
|
279
|
+
// `postcodeVerified` is true only when an anchor existed and validated this match within
|
|
280
|
+
// `thresholdKm`; otherwise the match is returned unrestricted but flagged lower-precision.
|
|
269
281
|
return {
|
|
270
282
|
text: sp.text,
|
|
271
283
|
start: sp.start,
|
|
@@ -276,15 +288,9 @@ export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
|
276
288
|
};
|
|
277
289
|
}
|
|
278
290
|
}
|
|
279
|
-
//
|
|
280
|
-
//
|
|
281
|
-
//
|
|
282
|
-
// scoped pass finds nothing and a postcode is present, re-probe the spans UNSCOPED (the admin
|
|
283
|
-
// gazetteer is one extract, all countries), then verify each exact candidate against the postcode's
|
|
284
|
-
// code subset resolved in the CANDIDATE's own country (postcode extracts route by country). A
|
|
285
|
-
// cross-country promotion is accepted ONLY postcode-verified within the radius — never unverified —
|
|
286
|
-
// so a US-shaped query can't wander abroad on a name coincidence (the 48026 guard: resolved
|
|
287
|
-
// trees never reach this code, and unresolved ones must pass the joint postcode check).
|
|
291
|
+
// Joint country recovery: when the scoped pass finds no match and a postcode
|
|
292
|
+
// is present, re-probe unscoped and accept a cross-country promotion only
|
|
293
|
+
// when its code subset resolves postcode-verified within the radius.
|
|
288
294
|
if (opts.postalCompoundRecovery && postcode && thresholdKm > 0) {
|
|
289
295
|
const code = postcodeCodeSubset(postcode) || postcode.trim();
|
|
290
296
|
for (const sp of spans) {
|
|
@@ -292,14 +298,12 @@ export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
|
292
298
|
if (key.length < 2 || /^\d+$/.test(key))
|
|
293
299
|
continue;
|
|
294
300
|
const hits = await backend.findPlace({ text: sp.text, placetype: "locality", limit: 5 });
|
|
295
|
-
//
|
|
296
|
-
//
|
|
297
|
-
// per-candidate postcode verification below remains the sole admission bar.
|
|
301
|
+
// Same alias-surface admission as the scoped pass.
|
|
302
|
+
// The per-candidate postcode verification below is the sole admission bar.
|
|
298
303
|
const exact = hits.filter((h) => h.exactMatch && (h.lat !== 0 || h.lon !== 0));
|
|
299
304
|
for (const h of exact) {
|
|
300
305
|
if (!h.country || h.country === country)
|
|
301
306
|
continue;
|
|
302
|
-
// the scoped pass already covered `country`
|
|
303
307
|
const pcHits = await backend.findPlace({
|
|
304
308
|
text: code,
|
|
305
309
|
country: h.country,
|
|
@@ -308,11 +312,9 @@ export async function findRescoreCandidate(raw, roots, backend, opts = {}) {
|
|
|
308
312
|
});
|
|
309
313
|
const verified = pcHits.find((p) => p.lat !== 0 || p.lon !== 0);
|
|
310
314
|
if (verified && haversineKm(verified.lat, verified.lon, h.lat, h.lon) <= thresholdKm) {
|
|
311
|
-
//
|
|
312
|
-
//
|
|
313
|
-
//
|
|
314
|
-
// ambiguous left to declare. The bare-toponym queries #1537 is about never reach this branch (it
|
|
315
|
-
// requires a postcode).
|
|
315
|
+
// Admission uses per-candidate postcode verification.
|
|
316
|
+
// The postcode already picked the country, so this pass has no remaining
|
|
317
|
+
// country ambiguity to report.
|
|
316
318
|
return { text: sp.text, start: sp.start, end: sp.end, place: h, postcodeVerified: true, alternatives: [] };
|
|
317
319
|
}
|
|
318
320
|
}
|