@mailwoman/resolver 9.4.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 +21 -161
- 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 +73 -39
- package/lib/decorate-node.ts +17 -41
- package/lib/fold-name.ts +5 -18
- package/lib/index.ts +5 -9
- package/lib/plausibility.ts +37 -63
- 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 +145 -148
- 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 +18 -69
- package/out/admin/coherence-passes.d.ts.map +1 -1
- package/out/admin/coherence-passes.js +19 -161
- 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 +56 -28
- 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 +29 -58
- 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 +97 -86
- 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 -995
- 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 -807
- 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/lib/rerank.ts
CHANGED
|
@@ -3,34 +3,13 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* resolver, not hand-weights. A rank-2 parse that resolves to a real place beats a rank-1 that
|
|
10
|
-
* resolves to a country centroid.
|
|
11
|
-
*
|
|
12
|
-
* The measured case for this existing at all: on the triaged parity corpus the shipped decode gets
|
|
13
|
-
* street 0.573 while the span decode's top-10 CONTAINS the right answer 0.775 of the time
|
|
14
|
-
* (oracle@5 0.723). Rank-1 cannot reach that headroom; only a reranker can.
|
|
15
|
-
*
|
|
16
|
-
* ## Why this is deliberately, almost embarrassingly small
|
|
17
|
-
*
|
|
18
|
-
* The failure mode we are avoiding is Pelias's: a ladder of per-class hand-tuned weights that
|
|
19
|
-
* nobody can re-derive (see `docs/articles/evals/competitive-parity/2026-07-15-pelias-dictionary-overrides.md`
|
|
20
|
-
* — 94 of 276 dictionary lines are hand-written deletions, each resolving a collision in favour of
|
|
21
|
-
* whoever complained loudest). The moment this file grows a per-hypothesis score blend, we have
|
|
22
|
-
* rebuilt that with extra steps.
|
|
23
|
-
*
|
|
24
|
-
* So the rule is ONE bit of evidence, not a score: **drop hypotheses whose resolution is
|
|
25
|
-
* implausible; otherwise keep the model's own ranking.** The parse scores already share a partition
|
|
26
|
-
* function and are comparable within an input — the reranker's job is not to re-score them, it is to
|
|
27
|
-
* veto the ones the world says are wrong.
|
|
28
|
-
*
|
|
29
|
-
* Adding a second signal here requires the same bar the first one cleared: a MEASURED win on the
|
|
30
|
-
* parity + Paris fixtures, stated in a verdict doc. Not a plausible story.
|
|
6
|
+
* Rerank a k-best parse list on resolution evidence with one bit — drop hypotheses whose resolution
|
|
7
|
+
* is implausible, otherwise keep the model's own ranking — because a second signal here needs a
|
|
8
|
+
* measured win on the parity + Paris fixtures.
|
|
31
9
|
*/
|
|
32
10
|
|
|
33
11
|
import type { AddressTree } from "@mailwoman/core/decoder"
|
|
12
|
+
import type { CountryBBoxFact } from "@mailwoman/core/resolver"
|
|
34
13
|
|
|
35
14
|
import { isImplausibleResolution } from "#plausibility"
|
|
36
15
|
|
|
@@ -39,15 +18,15 @@ import { isImplausibleResolution } from "#plausibility"
|
|
|
39
18
|
*/
|
|
40
19
|
export interface RerankCandidate<T = unknown> {
|
|
41
20
|
/**
|
|
42
|
-
* The parse's own score
|
|
21
|
+
* The parse's own score, comparable to its siblings from the same input and not across inputs.
|
|
43
22
|
*/
|
|
44
23
|
score: number
|
|
45
24
|
/**
|
|
46
|
-
* The parse tree,
|
|
25
|
+
* The parse tree, unresolved — `rerankByResolution` resolves it via the injected resolver.
|
|
47
26
|
*/
|
|
48
27
|
tree: AddressTree
|
|
49
28
|
/**
|
|
50
|
-
* Opaque caller payload
|
|
29
|
+
* Opaque caller payload passed through to the result (the segmentation, a surface string, …).
|
|
51
30
|
*/
|
|
52
31
|
payload?: T
|
|
53
32
|
}
|
|
@@ -69,45 +48,49 @@ export interface RerankedCandidate<T = unknown> extends RerankCandidate<T> {
|
|
|
69
48
|
|
|
70
49
|
export interface RerankResult<T = unknown> {
|
|
71
50
|
/**
|
|
72
|
-
* Candidates in
|
|
51
|
+
* Candidates in final order: plausible ones first (model order preserved), vetoed ones after.
|
|
73
52
|
*/
|
|
74
53
|
ranked: Array<RerankedCandidate<T>>
|
|
75
54
|
/**
|
|
76
|
-
* The winner — the first plausible candidate, or the model's rank-1 when
|
|
55
|
+
* The winner — the first plausible candidate, or the model's rank-1 when all were vetoed.
|
|
77
56
|
*/
|
|
78
57
|
best: RerankedCandidate<T>
|
|
79
58
|
/**
|
|
80
|
-
* True when
|
|
81
|
-
* metric the arc is judged on (`rank-2-beats-rank-1 rate`); log it, because a rerank that never fires is a rerank
|
|
82
|
-
* that is not earning its resolver calls.
|
|
59
|
+
* True when resolution evidence actually changed the answer from the model's rank-1.
|
|
83
60
|
*/
|
|
84
61
|
changed: boolean
|
|
85
62
|
}
|
|
86
63
|
|
|
87
64
|
/**
|
|
88
|
-
* Resolve a tree.
|
|
65
|
+
* Resolve a tree.
|
|
66
|
+
* Structural, so any `Resolver`-shaped thing satisfies it.
|
|
89
67
|
*/
|
|
90
68
|
export type ResolveTree = (tree: AddressTree) => Promise<AddressTree>
|
|
91
69
|
|
|
92
70
|
export interface RerankOpts {
|
|
93
71
|
/**
|
|
94
|
-
* Resolve at most this many candidates (default 5)
|
|
95
|
-
*
|
|
96
|
-
* — the last 5 candidates cost 2x the resolves for ~5pp of ceiling.
|
|
72
|
+
* Resolve at most this many candidates (default 5); each costs a resolver round-trip,
|
|
73
|
+
* so this is the latency knob.
|
|
97
74
|
*/
|
|
98
75
|
maxResolve?: number
|
|
76
|
+
/**
|
|
77
|
+
* ISO-2 country the resolutions are expected to land in, threaded to {@link isImplausibleResolution}
|
|
78
|
+
* as guard B so a coordinate outside that country's coarse box is vetoed.
|
|
79
|
+
* Omitted, only the bare-country-centroid guard runs.
|
|
80
|
+
*/
|
|
81
|
+
expectedCountry?: string
|
|
82
|
+
/**
|
|
83
|
+
* The loaded artifact's own guard-B boxes, replacing the built-in table wholesale when supplied.
|
|
84
|
+
*/
|
|
85
|
+
countryBBoxes?: ReadonlyMap<string, CountryBBoxFact>
|
|
99
86
|
}
|
|
100
87
|
|
|
101
88
|
/**
|
|
102
|
-
* Rerank a k-best parse list on resolution evidence
|
|
103
|
-
*
|
|
104
|
-
* Resolves up to `maxResolve` candidates IN MODEL ORDER and returns the first whose resolution is plausible. Candidates
|
|
105
|
-
* beyond `maxResolve` are never resolved (and never vetoed — they simply keep their model rank behind the resolved
|
|
106
|
-
* ones).
|
|
89
|
+
* Rerank a k-best parse list on resolution evidence: resolve up to `maxResolve`
|
|
90
|
+
* candidates in model order and return the first plausible one.
|
|
107
91
|
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* grounds to flag low confidence (the Phase-4b ambiguity check).
|
|
92
|
+
* Candidates beyond `maxResolve` are never resolved or vetoed. when every resolved
|
|
93
|
+
* candidate is implausible the model's rank-1 wins.
|
|
111
94
|
*/
|
|
112
95
|
export async function rerankByResolution<T>(
|
|
113
96
|
candidates: ReadonlyArray<RerankCandidate<T>>,
|
|
@@ -122,7 +105,6 @@ export async function rerankByResolution<T>(
|
|
|
122
105
|
const candidate = candidates[i]!
|
|
123
106
|
|
|
124
107
|
if (i >= maxResolve) {
|
|
125
|
-
// Beyond the resolve budget: carried through unresolved, never vetoed.
|
|
126
108
|
ranked.push({ ...candidate, resolved: null, implausible: false })
|
|
127
109
|
|
|
128
110
|
continue
|
|
@@ -133,14 +115,17 @@ export async function rerankByResolution<T>(
|
|
|
133
115
|
try {
|
|
134
116
|
resolved = await resolveTree(candidate.tree)
|
|
135
117
|
} catch {
|
|
136
|
-
// A resolver failure is
|
|
137
|
-
//
|
|
118
|
+
// A resolver failure is not evidence against the parse, so the model's rank stands
|
|
119
|
+
// rather than vetoing a possibly-correct hypothesis on an outage.
|
|
138
120
|
ranked.push({ ...candidate, resolved: null, implausible: false })
|
|
139
121
|
|
|
140
122
|
continue
|
|
141
123
|
}
|
|
142
124
|
|
|
143
|
-
const verdict = isImplausibleResolution(resolved
|
|
125
|
+
const verdict = isImplausibleResolution(resolved, {
|
|
126
|
+
...(opts.expectedCountry ? { expectedCountry: opts.expectedCountry } : {}),
|
|
127
|
+
...(opts.countryBBoxes ? { countryBBoxes: opts.countryBBoxes } : {}),
|
|
128
|
+
})
|
|
144
129
|
|
|
145
130
|
ranked.push({
|
|
146
131
|
...candidate,
|
|
@@ -150,7 +135,6 @@ export async function rerankByResolution<T>(
|
|
|
150
135
|
})
|
|
151
136
|
}
|
|
152
137
|
|
|
153
|
-
// Stable partition: plausible first (model order preserved within each group), vetoed after.
|
|
154
138
|
const plausible = ranked.filter((r) => !r.implausible)
|
|
155
139
|
const vetoed = ranked.filter((r) => r.implausible)
|
|
156
140
|
const finalOrder = [...plausible, ...vetoed]
|