@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.
Files changed (134) hide show
  1. package/README.md +9 -7
  2. package/lib/admin/coherence-passes.ts +21 -161
  3. package/lib/admin/containment.ts +12 -30
  4. package/lib/admin/jp-municipality.ts +20 -13
  5. package/lib/admin/winner.ts +30 -62
  6. package/lib/backend-capabilities.ts +25 -24
  7. package/lib/bare-toponym-race.ts +73 -39
  8. package/lib/decorate-node.ts +17 -41
  9. package/lib/fold-name.ts +5 -18
  10. package/lib/index.ts +5 -9
  11. package/lib/plausibility.ts +37 -63
  12. package/lib/postcode/country-coherence.ts +64 -284
  13. package/lib/postcode/prefix.ts +20 -84
  14. package/lib/postcode/shape-coherence.ts +23 -113
  15. package/lib/remote-resolver.ts +32 -29
  16. package/lib/rerank.ts +34 -50
  17. package/lib/resolve/passes.ts +145 -148
  18. package/lib/resolve.ts +726 -0
  19. package/lib/span-rescore.ts +215 -172
  20. package/lib/street/evidence.ts +65 -73
  21. package/lib/street/tier.ts +33 -175
  22. package/lib/toponym-prior.ts +53 -179
  23. package/out/admin/coherence-passes.d.ts +18 -69
  24. package/out/admin/coherence-passes.d.ts.map +1 -1
  25. package/out/admin/coherence-passes.js +19 -161
  26. package/out/admin/coherence-passes.js.map +1 -1
  27. package/out/admin/containment.d.ts +11 -29
  28. package/out/admin/containment.d.ts.map +1 -1
  29. package/out/admin/containment.js +12 -30
  30. package/out/admin/containment.js.map +1 -1
  31. package/out/admin/jp-municipality.d.ts +20 -13
  32. package/out/admin/jp-municipality.d.ts.map +1 -1
  33. package/out/admin/jp-municipality.js +18 -12
  34. package/out/admin/jp-municipality.js.map +1 -1
  35. package/out/admin/winner.d.ts +30 -62
  36. package/out/admin/winner.d.ts.map +1 -1
  37. package/out/admin/winner.js +18 -51
  38. package/out/admin/winner.js.map +1 -1
  39. package/out/{admin/index.d.ts → admin.d.ts} +1 -1
  40. package/out/admin.d.ts.map +1 -0
  41. package/out/{admin/index.js → admin.js} +1 -1
  42. package/out/admin.js.map +1 -0
  43. package/out/backend-capabilities.d.ts +18 -19
  44. package/out/backend-capabilities.d.ts.map +1 -1
  45. package/out/backend-capabilities.js +25 -24
  46. package/out/backend-capabilities.js.map +1 -1
  47. package/out/bare-toponym-race.d.ts +56 -28
  48. package/out/bare-toponym-race.d.ts.map +1 -1
  49. package/out/bare-toponym-race.js +69 -37
  50. package/out/bare-toponym-race.js.map +1 -1
  51. package/out/decorate-node.d.ts +6 -8
  52. package/out/decorate-node.d.ts.map +1 -1
  53. package/out/decorate-node.js +17 -41
  54. package/out/decorate-node.js.map +1 -1
  55. package/out/fold-name.d.ts +5 -18
  56. package/out/fold-name.d.ts.map +1 -1
  57. package/out/fold-name.js +5 -18
  58. package/out/fold-name.js.map +1 -1
  59. package/out/index.d.ts +5 -6
  60. package/out/index.d.ts.map +1 -1
  61. package/out/index.js +5 -8
  62. package/out/index.js.map +1 -1
  63. package/out/plausibility.d.ts +29 -58
  64. package/out/plausibility.d.ts.map +1 -1
  65. package/out/plausibility.js +16 -49
  66. package/out/plausibility.js.map +1 -1
  67. package/out/postcode/country-coherence.d.ts +48 -162
  68. package/out/postcode/country-coherence.d.ts.map +1 -1
  69. package/out/postcode/country-coherence.js +19 -252
  70. package/out/postcode/country-coherence.js.map +1 -1
  71. package/out/postcode/prefix.d.ts +17 -68
  72. package/out/postcode/prefix.d.ts.map +1 -1
  73. package/out/postcode/prefix.js +8 -77
  74. package/out/postcode/prefix.js.map +1 -1
  75. package/out/postcode/shape-coherence.d.ts +17 -84
  76. package/out/postcode/shape-coherence.d.ts.map +1 -1
  77. package/out/postcode/shape-coherence.js +9 -105
  78. package/out/postcode/shape-coherence.js.map +1 -1
  79. package/out/{postcode/index.d.ts → postcode.d.ts} +1 -1
  80. package/out/postcode.d.ts.map +1 -0
  81. package/out/{postcode/index.js → postcode.js} +1 -1
  82. package/out/postcode.js.map +1 -0
  83. package/out/remote-resolver.d.ts +27 -28
  84. package/out/remote-resolver.d.ts.map +1 -1
  85. package/out/remote-resolver.js +9 -19
  86. package/out/remote-resolver.js.map +1 -1
  87. package/out/rerank.d.ts +28 -45
  88. package/out/rerank.d.ts.map +1 -1
  89. package/out/rerank.js +13 -38
  90. package/out/rerank.js.map +1 -1
  91. package/out/resolve/passes.d.ts +97 -86
  92. package/out/resolve/passes.d.ts.map +1 -1
  93. package/out/resolve/passes.js +40 -105
  94. package/out/resolve/passes.js.map +1 -1
  95. package/out/resolve.d.ts +15 -0
  96. package/out/resolve.d.ts.map +1 -0
  97. package/out/resolve.js +543 -0
  98. package/out/resolve.js.map +1 -0
  99. package/out/span-rescore.d.ts +51 -64
  100. package/out/span-rescore.d.ts.map +1 -1
  101. package/out/span-rescore.js +132 -130
  102. package/out/span-rescore.js.map +1 -1
  103. package/out/street/evidence.d.ts +56 -61
  104. package/out/street/evidence.d.ts.map +1 -1
  105. package/out/street/evidence.js +22 -46
  106. package/out/street/evidence.js.map +1 -1
  107. package/out/street/tier.d.ts +17 -29
  108. package/out/street/tier.d.ts.map +1 -1
  109. package/out/street/tier.js +27 -175
  110. package/out/street/tier.js.map +1 -1
  111. package/out/{street/index.d.ts → street.d.ts} +1 -1
  112. package/out/street.d.ts.map +1 -0
  113. package/out/{street/index.js → street.js} +1 -1
  114. package/out/street.js.map +1 -0
  115. package/out/toponym-prior.d.ts +21 -109
  116. package/out/toponym-prior.d.ts.map +1 -1
  117. package/out/toponym-prior.js +41 -170
  118. package/out/toponym-prior.js.map +1 -1
  119. package/package.json +44 -147
  120. package/lib/resolve/index.ts +0 -995
  121. package/out/admin/index.d.ts.map +0 -1
  122. package/out/admin/index.js.map +0 -1
  123. package/out/postcode/index.d.ts.map +0 -1
  124. package/out/postcode/index.js.map +0 -1
  125. package/out/resolve/index.d.ts +0 -20
  126. package/out/resolve/index.d.ts.map +0 -1
  127. package/out/resolve/index.js +0 -807
  128. package/out/resolve/index.js.map +0 -1
  129. package/out/street/index.d.ts.map +0 -1
  130. package/out/street/index.js.map +0 -1
  131. package/vitest.config.ts +0 -38
  132. /package/lib/{admin/index.ts → admin.ts} +0 -0
  133. /package/lib/{postcode/index.ts → postcode.ts} +0 -0
  134. /package/lib/{street/index.ts → street.ts} +0 -0
@@ -2,15 +2,10 @@
2
2
  * @copyright Sister Software
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- * @file Which resolved admin place answers the query — the one ordering whose postcode rung is not a constant.
5
+ * @file Which resolved admin place answers the query: the one ordering whose postcode rung is not a constant.
6
6
  *
7
- * `postalcode` is the only placetype whose specificity depends on the hit rather than the name. An NL PC6 or a GB unit
8
- * postcode covers ~8 and ~15 addresses, categorically tighter than any locality centroid; a US ZIP, a French code
9
- * postal or a German PLZ covers a delivery AREA. `PLACETYPE_SPECIFICITY` answers "which placetype covers less ground"
10
- * and cannot express that split, so a consumer that sorts by it alone promotes every postcode.
11
- *
12
- * Two consumers need the split in two shapes, and both live here so a divergence is a test failure rather than a
13
- * silent disagreement: result assembly walks a TAG ladder, and the eval harnesses sort resolved nodes by placetype.
7
+ * `postalcode` is the only placetype whose specificity depends on the resolved hit.
8
+ * `PLACETYPE_SPECIFICITY` cannot express that split.
14
9
  */
15
10
 
16
11
  import { areaPostcodeLeadsLocality, isUnitGradePostcodeHit } from "@mailwoman/codex"
@@ -18,16 +13,8 @@ import type { AddressNode } from "@mailwoman/core/decoder"
18
13
  import { PLACETYPE_SPECIFICITY } from "@mailwoman/core/resources/whosonfirst/specificity"
19
14
 
20
15
  /**
21
- * The admin fallback order when the postcode leads — a unit-grade exact hit, or an address system whose area-grade code
22
- * is still finer than its locality ({@link areaPostcodeLeadsLocality}).
23
- *
24
- * The JP rungs (`municipality`, `district`, `prefecture`) sit beside their Latin counterparts, and `municipality`
25
- * stands ABOVE `district` on purpose. `subregion` (a county, a Korean 시군구) sits between the postcode and `region`:
26
- * finer than the region it belongs to, coarser than any postcode. Absent from the ladder it was never the answer, and
27
- * `부산광역시 해운대구 반송로 910-1` resolved both nodes and reported Busan's point, 3.3 km from the address, over Haeundae's. It
28
- * stands ABOVE `district` on purpose: a district (大字 / 町名) resolves without its municipality as a parent more often
29
- * than not, and the unscoped namesake it then picks can be another prefecture's (`市原市大作` → 921 km). Measured on 300 JP
30
- * board rows @15 km: district-first 202 accepted, municipality-first 271.
16
+ * The admin fallback order when the postcode leads, with the JP rungs beside their
17
+ * Latin counterparts and `municipality` above `district`.
31
18
  */
32
19
  export const ADMIN_LADDER_POSTCODE_FIRST: ReadonlyArray<string> = [
33
20
  "postcode",
@@ -42,10 +29,8 @@ export const ADMIN_LADDER_POSTCODE_FIRST: ReadonlyArray<string> = [
42
29
  ]
43
30
 
44
31
  /**
45
- * The admin fallback order everywhere else: the locality tiers lead and the postcode sits between them and `region`.
46
- *
47
- * This is the #945 epoch convention and the DEFAULT rather than the universal answer — which address systems leave it
48
- * is `AREA_POSTCODE_FINER_THAN_LOCALITY`'s question, and that table carries the per-country measurement.
32
+ * The admin fallback order everywhere else: the locality tiers lead
33
+ * and the postcode sits between them and `region`.
49
34
  */
50
35
  export const ADMIN_LADDER_LOCALITY_FIRST: ReadonlyArray<string> = [
51
36
  "locality",
@@ -60,29 +45,27 @@ export const ADMIN_LADDER_LOCALITY_FIRST: ReadonlyArray<string> = [
60
45
  ]
61
46
 
62
47
  /**
63
- * The resolved postcode a ladder decision reads: the PARSED span, the resolver's own hit, and the country the resolver
64
- * placed it in.
48
+ * The resolved postcode a ladder decision reads.
65
49
  *
66
- * The first two are needed together because a full unit shape the resolver answered with a coarser stem is area-grade,
67
- * whatever the user typed. `country` is separate evidence and answers a different question — not how tight this code
68
- * is, but whether this address system's codes are tighter than its localities at all.
50
+ * It contains the parsed span and the resolver hit.
51
+ * It also contains the country where the resolver placed the postcode.
69
52
  */
70
53
  export interface ResolvedPostcodeHit {
71
54
  value: string
72
55
  resolverName: string | undefined
73
56
  /**
74
- * ISO-3166 alpha-2 the RESOLVER placed the postcode in, not a caller's requested scope. Absent when the postcode did
75
- * not resolve to a country, which reads as the locality-first default.
57
+ * ISO-3166 alpha-2 code where the resolver placed the postcode.
58
+ *
59
+ * The code is independent of the caller's requested scope.
60
+ * It is absent when the postcode did not resolve to a country.
61
+ * The ladder then defaults to locality-first.
76
62
  */
77
63
  country?: string
78
64
  }
79
65
 
80
66
  /**
81
- * Pick the admin fallback order for one resolved tree.
82
- *
83
- * Two independent routes to postcode-first: the code itself is unit-grade, or the address system's area-grade codes are
84
- * finer than its localities. Either one is sufficient and the second is what makes this per-country rather than
85
- * global.
67
+ * Pick the admin fallback order for one resolved tree: postcode-first when the code is
68
+ * unit-grade or the address system's area-grade codes are finer than its localities.
86
69
  */
87
70
  export function adminLadderFor(postcode: ResolvedPostcodeHit | undefined): ReadonlyArray<string> {
88
71
  if (postcode === undefined) return ADMIN_LADDER_LOCALITY_FIRST
@@ -94,12 +77,8 @@ export function adminLadderFor(postcode: ResolvedPostcodeHit | undefined): Reado
94
77
  }
95
78
 
96
79
  /**
97
- * The ladder for a flat list of resolved nodes — the shape result assembly holds.
98
- *
99
- * Which node counts, and which of its fields the decision reads, is part of the ordering rather than the caller's
100
- * business: `country` is the one the RESOLVER placed the code in, never a caller's requested scope, because the ladder
101
- * is asking which address system this code belongs to and a scope is a filter on the answer rather than evidence about
102
- * it. Getting that wrong at a call site would be invisible.
80
+ * The ladder for a flat list of resolved nodes, reading `country` from the resolver's
81
+ * placement rather than a caller's requested scope.
103
82
  */
104
83
  export function adminLadderForNodes(nodes: readonly AddressNode[]): ReadonlyArray<string> {
105
84
  const node = nodes.find((n) => n.tag === "postcode" && n.lat != null && n.lon != null)
@@ -116,15 +95,8 @@ export function adminLadderForNodes(nodes: readonly AddressNode[]): ReadonlyArra
116
95
  }
117
96
 
118
97
  /**
119
- * Where an AREA-grade postal code sits on `PLACETYPE_SPECIFICITY`: below the whole locality tier, above `county` (2).
120
- *
121
- * The tier is `PLACETYPE_FILTER_GROUPS.locality` — `locality` (4), `borough` (5) and `localadmin` (3) — not the
122
- * `locality` placetype alone, because that is what the resolver's own `locality` tag expands to. New England civil
123
- * towns are `localadmin` in WOF, so a value between `locality` and `localadmin` would rank a US ZIP above the town it
124
- * sits in and reproduce the defect on exactly the rows that motivated the group.
125
- *
126
- * Deliberately not an integer. The scale's rungs are placetypes and this is not one — it is the position
127
- * {@link ADMIN_LADDER_LOCALITY_FIRST} puts a postcode in, expressed on the scale the sorting consumers already use.
98
+ * Deliberately non-integer position for an area-grade postal code on `PLACETYPE_SPECIFICITY`:
99
+ * below the whole locality tier and above `county` (2).
128
100
  */
129
101
  export const AREA_GRADE_POSTALCODE_SPECIFICITY = 2.5
130
102
 
@@ -134,25 +106,23 @@ export const AREA_GRADE_POSTALCODE_SPECIFICITY = 2.5
134
106
  export interface ResolvedSpecificityInput {
135
107
  placetype: string
136
108
  /**
137
- * ISO-3166 alpha-2 the resolver placed this candidate in. Read only for a `postalcode`, where it decides whether the
138
- * address system's area-grade codes outrank its localities.
109
+ * ISO-3166 alpha-2 the resolver placed this candidate in, read only for a `postalcode`.
139
110
  */
140
111
  country?: string
141
112
  /**
142
- * The parsed span. Absent for a non-postcode candidate, where nothing conditional applies.
113
+ * The parsed span.
114
+ * Absent for a non-postcode candidate.
143
115
  */
144
116
  value?: string
145
117
  /**
146
- * The resolver's own hit name, as `resolver_name` metadata carries it.
118
+ * The resolver's own hit name, as recorded in `resolver_name` metadata.
147
119
  */
148
120
  resolverName?: string
149
121
  }
150
122
 
151
123
  /**
152
- * Rank a resolved place for "whose coordinate answers the query" — `PLACETYPE_SPECIFICITY`, except that a `postalcode`
153
- * is ranked by its hit.
154
- *
155
- * An unranked placetype returns `-Infinity` so it wins only when nothing else resolved.
124
+ * Rank a resolved place for "whose coordinate answers the query": `PLACETYPE_SPECIFICITY`,
125
+ * except that a `postalcode` is ranked by its hit and an unranked placetype returns `-Infinity`.
156
126
  */
157
127
  export function resolvedSpecificity(candidate: ResolvedSpecificityInput): number {
158
128
  if (candidate.placetype !== "postalcode") {
@@ -167,12 +137,10 @@ export function resolvedSpecificity(candidate: ResolvedSpecificityInput): number
167
137
  }
168
138
 
169
139
  /**
170
- * The best of a resolved set under {@link resolvedSpecificity}, or `null` when the set is empty. Ties keep the first,
171
- * which is document order.
140
+ * The best of a resolved set under {@link resolvedSpecificity}, or `null` when the set is empty.
172
141
  *
173
- * `toInput` is explicit rather than structural because each consumer spells the resolver's hit differently — the eval
174
- * harnesses carry it as `name`, result assembly reads it off `resolver_name` metadata — and a projection at the call
175
- * site is where that mapping is visible.
142
+ * Ties keep the first.
143
+ * `toInput` is explicit because each consumer formats the resolver's hit differently.
176
144
  */
177
145
  export function mostSpecificResolved<T>(
178
146
  candidates: readonly T[],
@@ -3,26 +3,20 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * Which optional {@link ResolverBackend} methods the loaded backend does not implement, and what each absence
7
- * silently costs.
6
+ * Lists optional {@link ResolverBackend} methods that the loaded backend lacks and the cost of each omission.
8
7
  *
9
- * `ancestors` and `coincidentLocalitiesFor` are optional on the backend contract, so a backend that omits them is
10
- * valid — but the resolver passes that lack through as a guard that simply returns. The option those guards serve
11
- * (`hierarchyCompletion`) defaults to ON, so a caller who never touched it gets a feature that reports success while
12
- * doing nothing. That is absence read as a negative answer, and the rule is that a magnitude never carries its own
13
- * absence: the gap has to be stated somewhere a caller can read it.
14
- *
15
- * This module states it. `describeCapabilityGaps` is pure and cheap enough to run at construction; the resolver
16
- * exposes the result on `Resolver.capabilityGaps` and warns once so the gap appears in a log without a caller having
17
- * to ask. It changes no resolution behavior — a gap that was silent is now legible, nothing more.
8
+ * Optional backend methods can make default-on resolver features no-op. This module exposes and
9
+ * reports those gaps without changing resolution behavior.
18
10
  */
19
11
 
20
12
  import type { BackendCapabilityGap, ResolverBackend } from "@mailwoman/core/resolver"
21
13
 
22
14
  /**
23
- * The optional backend methods whose absence degrades a passing default, paired with what stops working. A method whose
24
- * absence is inert (or whose option is opt-in and therefore a deliberate caller choice) does not belong here — the list
25
- * exists to name silent degradation, not to inventory the interface.
15
+ * The optional backend methods whose absence degrades a passing default, paired with what stops working.
16
+ *
17
+ * A method whose absence is inert (or whose option is opt-in and therefore a
18
+ * deliberate caller choice) does not belong here.
19
+ * The list exists to name silent degradation rather than to inventory the interface.
26
20
  */
27
21
  const CONSEQUENTIAL_CAPABILITIES: ReadonlyArray<Omit<BackendCapabilityGap, "backend">> = [
28
22
  {
@@ -42,8 +36,9 @@ const CONSEQUENTIAL_CAPABILITIES: ReadonlyArray<Omit<BackendCapabilityGap, "back
42
36
  ]
43
37
 
44
38
  /**
45
- * Enumerate the consequential capabilities this backend does not implement. Empty means every default-ON option the
46
- * resolver offers has the backend support it needs.
39
+ * Enumerate the consequential capabilities this backend does not implement.
40
+ *
41
+ * Empty means every default-on option the resolver offers has the backend support it needs.
47
42
  */
48
43
  export function describeCapabilityGaps(backend: ResolverBackend): readonly BackendCapabilityGap[] {
49
44
  const name = backend.constructor?.name || "backend"
@@ -54,9 +49,14 @@ export function describeCapabilityGaps(backend: ResolverBackend): readonly Backe
54
49
  }
55
50
 
56
51
  /**
57
- * One log line for a backend's whole set of gaps. Deliberately short: what is missing, which option that costs, and
58
- * where the consequences are written down. The per-gap `degrades` prose stays on the data for a consumer that wants it,
59
- * because a line long enough to carry it is a line an operator learns to skip.
52
+ * One log line for a backend's whole set of gaps.
53
+ *
54
+ * Keep the log line short.
55
+ * It states the missing methods and affected options.
56
+ *
57
+ * A reference points to the document that describes their consequences.
58
+ * Each gap's `degrades` field retains details for consumers.
59
+ * Long log lines are harder for operators to scan.
60
60
  */
61
61
  export function formatCapabilityGaps(gaps: readonly BackendCapabilityGap[]): string {
62
62
  const [first] = gaps
@@ -68,15 +68,16 @@ export function formatCapabilityGaps(gaps: readonly BackendCapabilityGap[]): str
68
68
  }
69
69
 
70
70
  /**
71
- * Backends already reported, so a process that builds many resolvers over one artifact — the eval harness, the
72
- * gauntlet, a batch worker — states the gap once rather than once per construction.
71
+ * Backends already reported, so a process that builds many resolvers over one artifact — the eval
72
+ * harness, the gauntlet, a batch worker — states the gap once rather than once per construction.
73
73
  */
74
74
  const reported = new Set<string>()
75
75
 
76
76
  /**
77
- * Report a backend's gaps to stderr the first time this process sees them. The gaps are data on
78
- * `Resolver.capabilityGaps` regardless; this exists so they also reach an operator who never thought to look. The line
79
- * disappears on its own once the backend implements the methods, so it cannot become permanent wallpaper.
77
+ * Report a backend's gaps to stderr the first time this process sees them.
78
+ *
79
+ * The gaps are data on `Resolver.capabilityGaps` regardless.
80
+ * This also reports them to an operator.
80
81
  */
81
82
  export function reportCapabilityGaps(gaps: readonly BackendCapabilityGap[]): void {
82
83
  const [first] = gaps
@@ -3,43 +3,54 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * The bare-toponym ADMIN race — the policy for a query that is one bare name ("Japan", "Georgia").
7
- * The locality placetype filter makes country/region namesakes unreachable at any rank, so the
8
- * resolver's lookup runs these side races for the tree's lone locality-tagged span and promotes an
9
- * admin namesake under the rules each prober documents. Split from `resolve.ts` so the walk file
10
- * holds the walk and this one holds the race policy.
6
+ * The bare-toponym admin race. This is the policy for a query that is one bare name ("Japan",
7
+ * "Georgia"). The locality placetype filter makes country/region namesakes unreachable at any
8
+ * rank, so the resolver's lookup runs these side races for the tree's lone locality-tagged span
9
+ * and promotes an admin namesake under the rules each prober documents. Split from `resolve.ts`
10
+ * so the walk file holds the walk and this one holds the race policy.
11
11
  */
12
12
 
13
- import type { AddressNode, AddressTree, ComponentTag } from "@mailwoman/core/decoder"
14
- import { loneValueBearingNode } from "@mailwoman/core/decoder"
15
- import type { PlacetypeMap, ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
13
+ import type { ComponentTag } from "@mailwoman/codex/component"
14
+ import type { PlacetypeMap } from "@mailwoman/codex/placetype-map"
15
+ import type { AddressNode, AddressTree } from "@mailwoman/core/decoder"
16
+ import { loneValueNode } from "@mailwoman/core/decoder"
17
+ import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
16
18
 
17
19
  /**
18
- * The tree's single value-bearing node when it is locality-tagged, else null — the bare-toponym shape whose
19
- * country-placetype sibling race `#lookupAndPick` runs. A bare name the parser tagged `locality` can name a country
20
- * ("Japan", "China" — single country names are out of the parser's training distribution), and the locality placetype
21
- * filter makes the country row unreachable regardless of how the ranking would order it. Any second value-bearing node
22
- * makes the input address-shaped and the race stays off; `dependent_locality` maps to the same placetype but is an
23
- * address-interior tag, so only a literal `locality` qualifies.
20
+ * The tree's single value-containing node when it is locality-tagged, else null.
21
+ *
22
+ * This is the bare-toponym shape whose country-placetype sibling race `#lookupAndPick` runs.
23
+ *
24
+ * A bare name the parser tagged `locality` can refer to a country ("Japan", "China", because
25
+ * single country toponyms are out of the parser's training distribution), and the locality
26
+ * placetype filter makes the country row unreachable regardless of how the ranking would order it.
27
+ * Any second value-containing node makes the input address-shaped and the race stays off.
28
+ *
29
+ * `dependent_locality` maps to the same placetype but is an address-interior tag,
30
+ * so only a literal `locality` qualifies.
24
31
  */
25
32
  export function loneBareLocalityNode(tree: AddressTree, placetypeMap: PlacetypeMap): AddressNode | null {
26
33
  if (placetypeMap["locality" as ComponentTag] !== "locality") return null
27
- const lone = loneValueBearingNode(tree)
34
+ const lone = loneValueNode(tree)
28
35
 
29
36
  return lone?.tag === "locality" ? lone : null
30
37
  }
31
38
 
32
39
  /**
33
- * The log-population dominance a bare REGION namesake must hold over the locality winner before the bare-toponym race
34
- * promotes it. The value is DECISIVE_MARGIN_LOG10 from the ablation-expectation model (0.5 — below it the top-ranked
35
- * place is the intended one 52.4% of the time, above it 89.1%); restated here because the resolver package cannot
36
- * import the eval harness. If that measured value moves, move this with it.
40
+ * The log-population dominance a bare region namesake must hold over the locality winner
41
+ * before the bare-toponym race promotes it.
42
+ *
43
+ * The value is DECISIVE_MARGIN_LOG10 from the ablation-expectation model, restated here
44
+ * because the resolver package cannot import the eval harness.
45
+ * If that measured value moves, move this with it.
37
46
  */
38
47
  export const BARE_REGION_DOMINANCE_LOG10 = 0.5
39
48
 
40
49
  /**
41
- * Log10(population + 1), 0 when the backend carries none — the saturation-free magnitude the bare-region dominance rule
42
- * compares (prominence caps at the backend's populationBoost and erases exactly the margins this rule needs).
50
+ * Log10(population + 1), 0 when the backend has no population value.
51
+ *
52
+ * The saturation-free magnitude the bare-region dominance rule compares
53
+ * (prominence caps at the backend's populationBoost and erases exactly the margins this rule needs).
43
54
  */
44
55
  export function logPopulation(place: ResolvedPlace): number {
45
56
  return Math.log10((place.population ?? 0) + 1)
@@ -57,19 +68,30 @@ export function pickLargerAdmin(country: ResolvedPlace | null, region: ResolvedP
57
68
  }
58
69
 
59
70
  /**
60
- * Alias roles the side races refuse to answer through (#1730): a lone bare token that only reaches a place via an
61
- * ABBREVIATION row ("Tó" folds onto Toledo's "TO") or a translation-gloss row did not name that place — while the
62
- * role-NULL exonym tier stays open, which is what lets 格鲁吉亚 win the country race through its display-name alias. An
63
- * artifact without the role column ignores the exclusion and the races behave as before.
71
+ * Alias roles the side races refuse to answer through.
72
+ *
73
+ * A lone bare token that only reaches a place via an abbreviation row ("Tó" folds onto Toledo's "TO")
74
+ * or a translation-gloss row did not identify that place.
75
+ * The role-NULL exonym tier stays open, so 格鲁吉亚 can win the country race through its display-name alias.
76
+ *
77
+ * An artifact without the role column ignores the exclusion and the races behave as before.
64
78
  */
65
79
  const BARE_RACE_EXCLUDED_NAME_ROLES: readonly string[] = ["abbr", "gloss"]
66
80
 
67
81
  /**
68
- * The best `country`-placetype row for a bare toponym span, or null. `scopedCountry` is the same hard filter the
69
- * locality query ran under — an EXPLICIT caller scope therefore bounds this race too (a foreign country row cannot
70
- * outrank inside an explicit scope), while the bare-locality posture's withheld scope leaves it worldwide. Exact
71
- * matches only: the fuzzy tier exists for typo recovery on address spans, and a fuzzy country is a guess this race must
72
- * never promote. Abbreviation/gloss alias rows never enter ({@link BARE_RACE_EXCLUDED_NAME_ROLES}).
82
+ * The best `country`-placetype row for a bare toponym span, or null.
83
+ *
84
+ * `scopedCountry` is the same hard filter the locality query ran under.
85
+ * An explicit caller scope therefore bounds this race too
86
+ * (a foreign country row cannot outrank inside an explicit scope), while the
87
+ * bare-locality posture's withheld scope leaves it worldwide.
88
+ *
89
+ * This race uses exact matches only.
90
+ * The fuzzy tier recovers typos in address spans.
91
+ *
92
+ * A fuzzy country match is a guess this race must never promote.
93
+ *
94
+ * Abbreviation/gloss alias rows never enter ({@link BARE_RACE_EXCLUDED_NAME_ROLES}).
73
95
  */
74
96
  export async function bareCountryCandidate(
75
97
  backend: ResolverBackend,
@@ -87,22 +109,34 @@ export async function bareCountryCandidate(
87
109
 
88
110
  const top = hits[0]
89
111
 
90
- // The placetype check is required, not paranoia: a backend that ignores the filter
91
- // (several test stubs, and any future partial implementation) would otherwise hand this
92
- // race a LOCALITY row wearing a country costume, and the repick would demote the real pick.
112
+ // The placetype check is required.
113
+ // A backend may ignore the filter, as test stubs or a partial implementation could.
114
+ // It could then hand this race a locality row wearing a country costume,
115
+ // The repick would then demote the real pick.
93
116
  return top && top.placetype === "country" && top.exactMatch !== false ? top : null
94
117
  } catch {
95
- // A failed side race must never abort the primary lookup — the locality answer stands.
118
+ // A failed side race must never abort the primary lookup.
119
+ // The locality answer stands.
96
120
  return null
97
121
  }
98
122
  }
99
123
 
100
124
  /**
101
- * The best `region`-placetype row for a bare toponym span, or null — the sibling of {@link #bareCountryCandidate} for
102
- * the US-state class (bare "Georgia"/"Texas" the parser tags `locality`). Same contract: the locality query's own
103
- * country filter bounds it, exact matches only with abbreviation/gloss rows excluded, and the placetype check guards
104
- * partial backends. (The `place_abbr`-staged region abbreviations — bare "TX"/"CA" — are role-NULL primaries and stay
105
- * fully reachable; the exclusion removes only the names-table abbreviation ALIASES like Toledo's "TO".)
125
+ * The best `region`-placetype row for a bare toponym span, or null.
126
+ *
127
+ * The sibling of {@link #bareCountryCandidate} for the US-state class
128
+ * (bare "Georgia"/"Texas" the parser tags `locality`).
129
+ *
130
+ * Uses the same interface.
131
+ * The locality query's country filter bounds it.
132
+ *
133
+ * It accepts exact matches only with abbreviation and gloss rows excluded.
134
+ * The placetype check guards partial backends.
135
+ *
136
+ * The `place_abbr`-staged region abbreviations (bare "TX"/"CA") are role-NULL
137
+ * primaries and stay fully reachable.
138
+ *
139
+ * The exclusion removes only abbreviation aliases like Toledo's "TO".
106
140
  */
107
141
  export async function bareRegionCandidate(
108
142
  backend: ResolverBackend,
@@ -3,9 +3,8 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * Node decoration shared by the resolver walk and its post-walk passes — split from `resolve.ts`
7
- * (the `bare-toponym-race.ts` precedent: the walk file holds the walk) so the coherence passes can
8
- * live in their own module without an import cycle back into the walk.
6
+ * Node decoration shared by the resolver walk and its post-walk passes, in its own module so the
7
+ * coherence passes do not import the walk.
9
8
  */
10
9
 
11
10
  import type { AddressNode } from "@mailwoman/core/decoder"
@@ -14,17 +13,16 @@ import type { ResolvedPlace } from "@mailwoman/core/resolver"
14
13
  import type { CoordinateOptionalPlace } from "#postcode/prefix"
15
14
 
16
15
  /**
17
- * A resolved node carries a real coordinate (placeID set + non-zero lat/lon).
16
+ * A resolved node has a real coordinate (placeID set + non-zero lat/lon).
18
17
  */
19
18
  export function isResolvedWithCoord(n: AddressNode): boolean {
20
19
  return !!(n.placeID && typeof n.lat === "number" && typeof n.lon === "number" && (n.lat !== 0 || n.lon !== 0))
21
20
  }
22
21
 
23
22
  /**
24
- * Stamp a node with resolver-supplied attribution. Displaces any prior classifier `source` / `sourceID` into
25
- * `metadata.classifier_source` / `metadata.classifier_source_id` so debugging tools can still see who made the original
26
- * assertion. Surfaces the runner-up candidates on `alternatives` so callers can disambiguate (Springfield-class
27
- * failures, [#8 in the failure catalogue]).
23
+ * Stamp a node with resolver-supplied attribution, displacing any prior classifier `source`
24
+ * / `sourceID` into `metadata.classifier_source` / `metadata.classifier_source_id`
25
+ * and surfacing runner-up candidates on `alternatives`.
28
26
  */
29
27
  export function decorateNode(
30
28
  node: AddressNode,
@@ -48,14 +46,8 @@ export function decorateNode(
48
46
  node.source = "resolver"
49
47
  node.sourceID = `${resolved.placetype}:${resolved.id}`
50
48
 
51
- // `0,0` is the gazetteer's UNLOCATED sentinel, not a location in the Gulf of Guinea. Extracts carry a lot of it —
52
- // 48,216 of 142,604 JP postcodes, 86,377 GB, 9,708 intl, 414 US — and stamping it produces a node that answers
53
- // "yes" to every `lat != null` guard downstream, including the admin ladder's. Absence is the representable form
54
- // (`AddressNode.lat` is optional and {@link isResolvedWithCoord} already reads the sentinel this way), so the
55
- // place resolves and identifies itself while stating it cannot say where it is.
56
- //
57
- // Both are cleared together rather than left stale: a coordinate from a previously-decorated place beside this
58
- // one's `placeID` would be a worse answer than none.
49
+ // `0,0` is the gazetteer's unlocated sentinel, so an unlocated place gets both coordinates
50
+ // cleared rather than a coordinate that satisfies every `lat != null` guard downstream.
59
51
  const located = resolved.lat !== undefined && resolved.lon !== undefined && (resolved.lat !== 0 || resolved.lon !== 0)
60
52
 
61
53
  if (located) {
@@ -66,35 +58,21 @@ export function decorateNode(
66
58
  delete node.lon
67
59
  }
68
60
 
69
- node.placeID = `wof:${resolved.id}` // v1: only WOF resolvers; the URI scheme stays this simple
70
- // Record the resolver's ranking score AND the resolved place's CANONICAL name. The name is the
71
- // gazetteer's truth for the place we picked — distinct from `node.value` (the raw input span). It
72
- // lets consumers display the canonical name and lets the end-to-end eval check the resolver chose
73
- // the right PLACE (gazetteer-name vs ground-truth) rather than merely echoing the parser's text.
61
+ node.placeID = `wof:${resolved.id}` // v1: only WOF resolvers. the URI scheme stays this simple
74
62
  node.metadata = { ...node.metadata, resolver_score: resolved.score, resolver_name: resolved.name }
75
63
 
76
- // The winner's PROMINENCE, when the backend computed one. `alternatives` below are full `ResolvedPlace`s and
77
- // already carry theirs; without this stamp the WINNER's is the one value in the ranked list that gets dropped,
78
- // which makes a top-1-vs-top-2 margin uncomputable from the tree — and that margin is what
79
- // `mailwoman/query-intent.ts` reads to decide whether a bare-toponym answer was a clear win. Additive metadata
80
- // only; nothing in the resolve reads it back.
64
+ // Additive metadata only.
65
+ // No part of the resolve reads it back.
81
66
  if (resolved.prominence !== undefined) {
82
67
  node.metadata["resolver_prominence"] = resolved.prominence
83
68
  }
84
69
 
85
- // The resolved place's ISO-3166 alpha-2 country (from the gazetteer/candidate row), when known. #1014: lets a
86
- // forward consumer fill country/countrycode without an ancestry walk — the candidate backend carries this even
87
- // though it has no `ancestors()` table.
88
70
  if (resolved.country) {
89
71
  node.metadata["resolver_country"] = resolved.country
90
72
  }
91
73
 
92
- // The score-channel carries (ROAD_TO_V9 §2 + #28). Written ONLY when the backend actually has a
93
- // value: an absent score means "unmeasured" or "pre-split gazetteer", and a `resolver_*: 0` on the
94
- // node would assert a measurement nobody made. Nothing in the resolve path reads these keys back —
95
- // they exist for annotation / API surfaces downstream. `resolver_importance` is the blended #28
96
- // prior (the value the ranking consulted); `resolver_encyclopedic` is the strict channel, reserved
97
- // until a strict-channel source ships.
74
+ // Written only when the backend supplies a value, because `resolver_*: 0`
75
+ // would assert an unmeasured value.
98
76
  if (resolved.referential !== undefined) {
99
77
  node.metadata["resolver_referential"] = resolved.referential
100
78
  }
@@ -107,16 +85,14 @@ export function decorateNode(
107
85
  node.metadata["resolver_importance"] = resolved.importance
108
86
  }
109
87
 
110
- // The postcode/locality conflict flag (the falsehood differentiator): the postcode pointed to a
111
- // geographically different place than the parsed city name. Surface it so callers can warn rather
112
- // than silently trust the resolved point.
88
+ // The postcode pointed to a geographically different place than the parsed city name,
89
+ // surfaced so callers can warn rather than silently trust the resolved point.
113
90
  if (resolved.mismatch) {
114
91
  node.metadata["postcode_city_mismatch"] = true
115
92
  }
116
93
 
117
- // Fallback-observability (#718): a broader admin tier (macroregion/macrocounty) stood in for the
118
- // true region/county because no exact-type candidate existed. Additive annotation only — the
119
- // resolved coordinate/identity above is untouched; this just lets a consumer / QA pass see it.
94
+ // A broader admin tier stood in for the true region/county because no exact-type candidate existed.
95
+ // Additive annotation only.
120
96
  if (resolved.resolutionQuality) {
121
97
  node.metadata["resolution_quality"] = resolved.resolutionQuality
122
98
  }
package/lib/fold-name.ts CHANGED
@@ -3,27 +3,14 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * One shared name fold, used by the resolver's commune reconciliation, by the street tier's
7
- * locality comparison and by span-rescore's key guards. Its own module because those would
8
- * otherwise import each other for it.
9
- *
10
- * `@mailwoman/codex`'s `foldName` (`packages/codex/lib/normalize.ts`) is a near twin and is also
11
- * correct; it differs in collapsing every non-alphanumeric RUN, whitespace included. The two are
12
- * equivalent on the inputs either sees today. They are kept apart because this one answers to a
13
- * comparison contract the resolver owns, not because the codex copy is wrong.
6
+ * One shared name fold for the resolver's commune reconciliation, the street tier's locality
7
+ * comparison and span-rescore's key guards. Its own module because those would otherwise import
8
+ * each other for it.
14
9
  */
15
10
 
16
11
  /**
17
- * Case/diacritic-insensitive fold for commune-name comparison (#1058).
18
- *
19
- * Marks are DELETED, never spaced. `[^a-z0-9 ]` alone maps the combining cedilla that NFD has just produced to a SPACE,
20
- * so `Besançon` keys as `besanc on` and matches nothing it should. The gazetteer side of the same comparison deletes
21
- * them — 0 of 32,539 distinct `locality_base` keys in `street-centroids-fr.db` carry a mark — so spacing puts the two
22
- * halves of one comparison on different keys. Order is required: after the class filter the strip is a no-op.
23
- *
24
- * Not fixed here, and worth knowing before trusting the fold on a non-Latin-1 name: a letter with no decomposition is
25
- * DROPPED rather than folded (`Łódź` → `odz`, `Đà Nẵng` → `a nang`). That needs a transliteration table, not a regex,
26
- * and it is shared with every other fold in the repo.
12
+ * Case/diacritic-insensitive fold for commune-name comparison: marks are deleted, never spaced, so
13
+ * `Besançon` does not key as `besanc on`; a letter with no decomposition is dropped rather than folded.
27
14
  */
28
15
  export function foldName(s: string): string {
29
16
  return s
package/lib/index.ts CHANGED
@@ -3,12 +3,11 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * `@mailwoman/resolver` — the address resolver implementation, lifted out of `@mailwoman/core`
7
- * (#215) so it can depend on `@mailwoman/spatial` (haversine) + `@mailwoman/codex` (USPS
8
- * directionals) instead of reinventing them. The TYPE contract stays in
9
- * `@mailwoman/core/resolver` (so the `core/pipeline` composes the resolver structurally without a
10
- * package cycle); this barrel re-exports it, so `@mailwoman/resolver` is a complete drop-in for
11
- * what used to be `@mailwoman/core/resolver`.
6
+ * `@mailwoman/resolver` is the address resolver implementation. It depends on
7
+ * `@mailwoman/spatial` (haversine) and `@mailwoman/codex` (USPS directionals) instead of
8
+ * reinventing them. The type interface stays in `@mailwoman/core/resolver`, so `core/pipeline`
9
+ * composes the resolver structurally without a package cycle. This barrel re-exports it, so
10
+ * `@mailwoman/resolver` is a complete drop-in for `@mailwoman/core/resolver`.
12
11
  */
13
12
 
14
13
  export { RemoteResolver, serializableResolveOpts } from "#remote-resolver"
@@ -60,7 +59,4 @@ export {
60
59
 
61
60
  export type { ResolvedPostcodeHit, ResolvedSpecificityInput } from "#admin/winner"
62
61
 
63
- // The type contract + placetype helpers live in core (pure types, keep core a leaf). Re-export so
64
- // consumers get the whole surface from `@mailwoman/resolver`.
65
-
66
62
  export * from "#rerank"