@mailwoman/resolver 8.1.0 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/span-rescore.ts CHANGED
@@ -44,7 +44,9 @@ export interface SpanRescoreOptions {
44
44
  * penalizes a backend without postcode coverage). 0 disables. Default 50.
45
45
  */
46
46
  gateKm?: number
47
- /** Max contiguous raw tokens to treat as one locality span. Default 4. */
47
+ /**
48
+ * Max contiguous raw tokens to treat as one locality span. Default 4.
49
+ */
48
50
  maxSpanTokens?: number
49
51
  /**
50
52
  * Min confidence for a street/house_number/postcode node to count as a span-blocking constituent. Default 0.7.
@@ -60,14 +62,22 @@ export interface SpanRescoreOptions {
60
62
  postalCompoundRecovery?: boolean
61
63
  }
62
64
 
63
- /** The recovered locality: the raw span and the gazetteer place it resolved to. */
65
+ /**
66
+ * The recovered locality: the raw span and the gazetteer place it resolved to.
67
+ */
64
68
  export interface RescoreCandidate {
65
- /** The raw text of the winning span. */
69
+ /**
70
+ * The raw text of the winning span.
71
+ */
66
72
  text: string
67
- /** Char offsets of the span in the raw input. */
73
+ /**
74
+ * Char offsets of the span in the raw input.
75
+ */
68
76
  start: number
69
77
  end: number
70
- /** The resolved gazetteer place (decorate a node with this). */
78
+ /**
79
+ * The resolved gazetteer place (decorate a node with this).
80
+ */
71
81
  place: ResolvedPlace
72
82
  /**
73
83
  * Whether the postcode-consistency gate FIRED for this recovery — i.e. the postcode resolved to a point and the match
@@ -80,13 +90,15 @@ export interface RescoreCandidate {
80
90
  gated: boolean
81
91
  }
82
92
 
83
- /** Normalize for exact comparison: lowercase, strip diacritics + punctuation, collapse whitespace. */
93
+ /**
94
+ * Normalize for exact comparison: lowercase, strip diacritics + punctuation, collapse whitespace.
95
+ */
84
96
  const norm = (s: string): string =>
85
97
  s
86
98
  .toLowerCase()
87
99
  .normalize("NFD")
88
- .replace(/[^a-z0-9 ]/g, " ")
89
- .replace(/\s+/g, " ")
100
+ .replaceAll(/[^a-z0-9 ]/g, " ")
101
+ .replaceAll(/\s+/g, " ")
90
102
  .trim()
91
103
 
92
104
  interface RawTok {
@@ -94,7 +106,10 @@ interface RawTok {
94
106
  start: number
95
107
  end: number
96
108
  }
97
- /** Whitespace/punctuation tokenization of the raw input, char offsets preserved, diacritics intact. */
109
+
110
+ /**
111
+ * Whitespace/punctuation tokenization of the raw input, char offsets preserved, diacritics intact.
112
+ */
98
113
  function tokenizeRaw(raw: string): RawTok[] {
99
114
  const toks: RawTok[] = []
100
115
  const re = /[^\s,;/]+/g
@@ -120,7 +135,9 @@ export function postcodeCodeSubset(postcode: string): string {
120
135
  .trim()
121
136
  }
122
137
 
123
- /** True if any node in the tree already carries a resolved place id — the #685 brake. */
138
+ /**
139
+ * True if any node in the tree already carries a resolved place id — the #685 brake.
140
+ */
124
141
  export function hasResolvedPlace(roots: readonly AddressNode[]): boolean {
125
142
  const stack: AddressNode[] = [...roots]
126
143
 
@@ -247,6 +264,7 @@ export async function findRescoreCandidate(
247
264
  end: number
248
265
  len: number
249
266
  }
267
+
250
268
  const spans: Span[] = []
251
269
 
252
270
  for (let len = Math.min(maxSpan, toks.length); len >= 1; len--) {
@@ -258,6 +276,7 @@ export async function findRescoreCandidate(
258
276
  spans.push({ text: raw.slice(start, end), start, end, len })
259
277
  }
260
278
  }
279
+
261
280
  spans.sort((a, b) => b.len - a.len)
262
281
 
263
282
  for (const sp of spans) {
@@ -296,13 +315,16 @@ export async function findRescoreCandidate(
296
315
  const exact = hits.filter((h) => h.exactMatch && norm(h.name) === key && (h.lat !== 0 || h.lon !== 0))
297
316
 
298
317
  for (const h of exact) {
299
- if (!h.country || h.country === country) continue // the scoped pass already covered `country`
318
+ if (!h.country || h.country === country) continue
319
+
320
+ // the scoped pass already covered `country`
300
321
  const pcHits = await backend.findPlace({
301
322
  text: code,
302
323
  country: h.country,
303
324
  placetype: "postalcode",
304
325
  limit: 2,
305
326
  })
327
+
306
328
  const verified = pcHits.find((p) => p.lat !== 0 || p.lon !== 0)
307
329
 
308
330
  if (verified && haversineKm(verified.lat, verified.lon, h.lat, h.lon) <= gateKm) {
@@ -41,7 +41,9 @@ export interface StreetLocalityEvidence {
41
41
  * {@link pickByStreetEvidence} fails open to the model's ranking. Absence is never a veto.
42
42
  */
43
43
  hasStreetName(streetSurface: string, scope?: StreetEvidenceScope): boolean
44
- /** ISO-2 (upper-case) countries this instance can answer for. Anything else → no evidence, never a veto. */
44
+ /**
45
+ * ISO-2 (upper-case) countries this instance can answer for. Anything else → no evidence, never a veto.
46
+ */
45
47
  readonly countries: ReadonlySet<string>
46
48
  }
47
49
 
@@ -58,10 +60,10 @@ export interface StreetEvidenceScope {
58
60
  export function foldStreetSurface(surface: string): string {
59
61
  return surface
60
62
  .normalize("NFD")
61
- .replace(/[̀-ͯ]/g, "")
63
+ .replaceAll(/[̀-ͯ]/g, "")
62
64
  .toLowerCase()
63
- .replace(/['’‐-―-]/g, " ")
64
- .replace(/\s+/g, " ")
65
+ .replaceAll(/['’‐-―-]/g, " ")
66
+ .replaceAll(/\s+/g, " ")
65
67
  .trim()
66
68
  }
67
69
 
@@ -107,27 +109,39 @@ const FR_STREET_TYPE_WORDS: ReadonlySet<string> = new Set([
107
109
  "sous",
108
110
  ])
109
111
 
110
- /** True when the folded surface contains no token outside {@link FR_STREET_TYPE_WORDS} — i.e. it is pure type/particle. */
112
+ /**
113
+ * True when the folded surface contains no token outside {@link FR_STREET_TYPE_WORDS} — i.e. it is pure type/particle.
114
+ */
111
115
  export function isPureTypeVocabulary(foldedSurface: string): boolean {
112
116
  const tokens = foldedSurface.split(" ").filter(Boolean)
113
117
 
114
- if (tokens.length === 0) return true
118
+ if (!tokens.length) return true
115
119
 
116
120
  return tokens.every((t) => FR_STREET_TYPE_WORDS.has(t))
117
121
  }
118
122
 
119
- /** One candidate parse for the street-evidence rerank — its street surface + its (within-input comparable) score. */
123
+ /**
124
+ * One candidate parse for the street-evidence rerank — its street surface + its (within-input comparable) score.
125
+ */
120
126
  export interface StreetCandidate<T = unknown> {
121
- /** The candidate's street surface (raw; folded internally). Empty string = no street parsed → never the evidence pick. */
127
+ /**
128
+ * The candidate's street surface (raw; folded internally). Empty string = no street parsed → never the evidence pick.
129
+ */
122
130
  streetSurface: string
123
- /** The parse score, comparable to its siblings from the SAME input. Higher is better. */
131
+ /**
132
+ * The parse score, comparable to its siblings from the SAME input. Higher is better.
133
+ */
124
134
  score: number
125
- /** Opaque caller payload carried through to the result (the segmentation, the tree, …). */
135
+ /**
136
+ * Opaque caller payload carried through to the result (the segmentation, the tree, …).
137
+ */
126
138
  payload?: T
127
139
  }
128
140
 
129
141
  export interface PickByStreetEvidenceOpts {
130
- /** Locality/postcode scope forwarded to {@link StreetLocalityEvidence.hasStreetName} (fragments usually carry none). */
142
+ /**
143
+ * Locality/postcode scope forwarded to {@link StreetLocalityEvidence.hasStreetName} (fragments usually carry none).
144
+ */
131
145
  scope?: StreetEvidenceScope
132
146
  /**
133
147
  * G2 — the margin cap. A candidate whose score is more than this far below rank-1 is never promoted by evidence
@@ -140,11 +154,17 @@ export interface PickByStreetEvidenceOpts {
140
154
  }
141
155
 
142
156
  export interface StreetEvidencePick<T = unknown> {
143
- /** The chosen candidate — the first evidence-passing sibling, or rank-1 when none passes (fail-open). */
157
+ /**
158
+ * The chosen candidate — the first evidence-passing sibling, or rank-1 when none passes (fail-open).
159
+ */
144
160
  candidate: StreetCandidate<T>
145
- /** Index of the chosen candidate in the input array. */
161
+ /**
162
+ * Index of the chosen candidate in the input array.
163
+ */
146
164
  index: number
147
- /** True when evidence MOVED the pick off rank-1 (a rank-2-beats-rank-1 correction — loggable training signal). */
165
+ /**
166
+ * True when evidence MOVED the pick off rank-1 (a rank-2-beats-rank-1 correction — loggable training signal).
167
+ */
148
168
  moved: boolean
149
169
  }
150
170
 
@@ -164,9 +184,10 @@ export function pickByStreetEvidence<T>(
164
184
  ): StreetEvidencePick<T> {
165
185
  const marginCap = opts.marginCap ?? 2.5
166
186
 
167
- if (candidates.length === 0) {
187
+ if (!candidates.length) {
168
188
  throw new Error("pickByStreetEvidence: no candidates")
169
189
  }
190
+
170
191
  const rank1 = candidates[0]!
171
192
  const topScore = rank1.score
172
193