@mailwoman/corpus 7.2.0 → 7.2.1

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 (103) hide show
  1. package/package.json +59 -27
  2. package/src/adapter.ts +153 -0
  3. package/src/adapters/ban/adapter.ts +189 -0
  4. package/src/adapters/ban/street-decompose.ts +98 -0
  5. package/src/adapters/fcc-bdc/adapter.ts +188 -0
  6. package/src/adapters/geonames/adapter.ts +185 -0
  7. package/src/adapters/geonames-postal/adapter.ts +111 -0
  8. package/src/adapters/gnaf/adapter.ts +139 -0
  9. package/src/adapters/gnaf/assemble.ts +200 -0
  10. package/src/adapters/index.ts +135 -0
  11. package/src/adapters/openaddresses/adapter.ts +237 -0
  12. package/src/adapters/overture/adapter.ts +149 -0
  13. package/src/adapters/state-hi-schools/adapter.ts +171 -0
  14. package/src/adapters/state-ia-contractors/adapter.ts +148 -0
  15. package/src/adapters/state-ny-notaries/adapter.ts +169 -0
  16. package/src/adapters/state-tx-notaries/adapter.ts +160 -0
  17. package/src/adapters/synth-po-box/adapter.ts +197 -0
  18. package/src/adapters/tiger/adapter.ts +232 -0
  19. package/src/adapters/tiger/street-decompose.ts +120 -0
  20. package/src/adapters/usgov-hrsa-fqhc/adapter.ts +189 -0
  21. package/src/adapters/usgov-imls-pls/adapter.ts +152 -0
  22. package/src/adapters/usgov-irs-bmf/adapter.ts +155 -0
  23. package/src/adapters/usgov-nad/adapter.ts +338 -0
  24. package/src/adapters/usgov-nppes/adapter.ts +169 -0
  25. package/src/adapters/usgov-samhsa-treatment-locator/adapter.ts +211 -0
  26. package/src/adapters/wof-admin-jp/adapter.ts +176 -0
  27. package/src/adapters/wof-admin-json/adapter.ts +280 -0
  28. package/src/adapters/wof-postalcode-json/adapter.ts +209 -0
  29. package/src/align.ts +317 -0
  30. package/src/build.ts +348 -0
  31. package/src/codex/us-fips-state.ts +109 -0
  32. package/src/format.ts +14 -0
  33. package/src/golden.ts +147 -0
  34. package/src/index.ts +28 -0
  35. package/src/license.ts +48 -0
  36. package/src/parquet-wrapper/index.ts +9 -0
  37. package/src/parquet-wrapper/reader.ts +67 -0
  38. package/src/parquet-wrapper/schema.ts +80 -0
  39. package/src/parquet-wrapper/writer.ts +102 -0
  40. package/src/parquet.ts +360 -0
  41. package/src/runner.ts +283 -0
  42. package/src/shard-recipes/anchor-absorption.ts +86 -0
  43. package/src/shard-recipes/boundary-stress.ts +87 -0
  44. package/src/shard-recipes/country-balanced.ts +471 -0
  45. package/src/shard-recipes/cz-pcfirst-preposition.ts +83 -0
  46. package/src/shard-recipes/fr-admin-split.ts +212 -0
  47. package/src/shard-recipes/fr-bare-street.ts +85 -0
  48. package/src/shard-recipes/fr-fragment.ts +304 -0
  49. package/src/shard-recipes/fr-order.ts +283 -0
  50. package/src/shard-recipes/german.ts +206 -0
  51. package/src/shard-recipes/house-venue.ts +78 -0
  52. package/src/shard-recipes/index.ts +76 -0
  53. package/src/shard-recipes/intersection.ts +617 -0
  54. package/src/shard-recipes/locale.ts +415 -0
  55. package/src/shard-recipes/nl-postcode.ts +105 -0
  56. package/src/shard-recipes/no-fragment.ts +206 -0
  57. package/src/shard-recipes/no-street-led.ts +130 -0
  58. package/src/shard-recipes/no-street.ts +78 -0
  59. package/src/shard-recipes/po-box-cedex.ts +853 -0
  60. package/src/shard-recipes/po-box.ts +111 -0
  61. package/src/shard-recipes/scaffold.ts +187 -0
  62. package/src/shard-recipes/si-bare-village.ts +88 -0
  63. package/src/shard-recipes/street-affix.ts +549 -0
  64. package/src/shard-recipes/street-bare.ts +90 -0
  65. package/src/shard-recipes/street.ts +74 -0
  66. package/src/shard-recipes/unit.ts +315 -0
  67. package/src/split.ts +251 -0
  68. package/src/synthesize-anchor-absorption.ts +263 -0
  69. package/src/synthesize-boundary-stress.ts +504 -0
  70. package/src/synthesize-german.ts +227 -0
  71. package/src/synthesize-house-venue.ts +192 -0
  72. package/src/synthesize-intersection.ts +205 -0
  73. package/src/synthesize-no-street.ts +341 -0
  74. package/src/synthesize-po-box.ts +307 -0
  75. package/src/synthesize-street.ts +264 -0
  76. package/src/synthesize.ts +872 -0
  77. package/src/tokenize.ts +68 -0
  78. package/src/tools/align-shard.ts +67 -0
  79. package/src/tools/audit.ts +352 -0
  80. package/src/tools/corpus-stats.ts +183 -0
  81. package/src/tools/fetch/ban.ts +288 -0
  82. package/src/tools/fetch/download.ts +119 -0
  83. package/src/tools/fetch/hrsa.ts +65 -0
  84. package/src/tools/fetch/imls-pls.ts +162 -0
  85. package/src/tools/fetch/index.ts +121 -0
  86. package/src/tools/fetch/nad.ts +304 -0
  87. package/src/tools/fetch/nppes.ts +177 -0
  88. package/src/tools/fetch/openaddresses.ts +380 -0
  89. package/src/tools/fetch/state-hi-schools.ts +217 -0
  90. package/src/tools/fetch/state-sources.ts +142 -0
  91. package/src/tools/fetch/tiger-full.ts +318 -0
  92. package/src/tools/golden-expand.ts +524 -0
  93. package/src/tools/golden-promote.ts +304 -0
  94. package/src/tools/index.ts +24 -0
  95. package/src/tools/ingest-csv.ts +420 -0
  96. package/src/tools/jsonl-to-parquet.ts +200 -0
  97. package/src/tools/lint-shard-vocab.ts +357 -0
  98. package/src/tools/lint-shard.ts +502 -0
  99. package/src/tools/overlay-manifest.ts +141 -0
  100. package/src/tools/shard-kryptonite.ts +131 -0
  101. package/src/tools/shard-translit.ts +244 -0
  102. package/src/types.ts +213 -0
  103. package/src/wof-json.ts +236 -0
@@ -0,0 +1,504 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Boundary-instability synthesizer (#375 — the highest-impact parser lever). The failure taxonomy
7
+ *
8
+ * The within-token-punctuation decomposition (#702) found one failure FAMILY surfacing under many
9
+ * names: the model mis-places token boundaries between adjacent components when the boundary is
10
+ * ambiguous or unmarked. This generator emits diverse BIO-labeled rows that put the gold boundary
11
+ * exactly where the model wobbles, so a retrain learns the boundary from context, not the
12
+ * lexeme.
13
+ *
14
+ * The four token-aligned stress shapes, all in BASE LOCALES (US/FR/DE) so the shard never
15
+ * introduces tokens the base corpus lacks (the #511 base-consistency lint flagged an earlier
16
+ * AU-bearing draft: AU 4-digit postcodes collide with US house numbers, and AU localities are
17
+ * absent from the US/FR/DE base — a real contradiction). Each component is a whitespace-separated
18
+ * token run, so `alignRow` labels it cleanly:
19
+ *
20
+ * 1. `street-eats-affix` — multi-word street + suffix (`Country Club Rd` → street + street_suffix),
21
+ * the #1 wobble: the model keeps the suffix in the street.
22
+ * 2. `comma-less-city-state` — no comma between street / locality / region (`100 Main St Springfield
23
+ * IL 62701`), the #694 family: concatenated input loses the segmentation cue. US-only (US
24
+ * zips are base-consistent; the boundary is locale-agnostic).
25
+ * 3. `fr-prefix` — FR street-type prefix split from the name (`Rue Jean-Baptiste Lebas` →
26
+ * street_prefix
27
+ *
28
+ * - Street), postcode-first order.
29
+ * 4. `house-number-after-street` — FR/DE number-follows-street (`Neuve-des-Capucines 5` → street +
30
+ * house_number), the model absorbs the number into the street.
31
+ *
32
+ * Two BALANCING shapes (added 2026-06-18 after the v1.6.0 probes). The first pass at weight 1.0
33
+ * lifted the boundaries but over-fit a NARROW distribution — every row was a clean, full,
34
+ * structured address — so the model regressed on out-of-distribution real rows (held-out US
35
+ * locality 66.3→58.2%). These two widen the distribution the shard teaches over, per the
36
+ * diagnosis (scripts/eval/locality-regression-probe): 5. `bare-locality` — locality with NO
37
+ * street (`Public Library, Lisbon ND`, `75003 Paris`), the ship-blocker: 84% of the v1.6.0
38
+ * locality regression was DROPPED locality on bare "City, STATE" rows, because every other shape
39
+ * placed a street before the city. Bare / comma-less / postcode'd / venue-prefixed forms, US +
40
+ * FR. 6. `house-number-before-street` — the confounding mirror of #4 (same FR vocab, number
41
+ * BEFORE the street). A balanced before:after mix breaks the positional shortcut behind the #4
42
+ * order-bias.
43
+ *
44
+ * EXCLUDED: the region+postcode glue (`NY14201` — sub-token, no punctuation to split) and the
45
+ * AU/NZ/UK slash unit-convention (`4/2A` → unit+house_number). The slash labels cleanly (the
46
+ * tokenizer splits `/`) and is the worst within-token class — but it inherently requires non-base
47
+ * AU/NZ/UK locales, which contradict the US/FR/DE base (the lint catch). It belongs in a
48
+ * separately-scoped AU/NZ/UK boundary-coverage shard that ALSO adds AU base coverage, not in this
49
+ * base-locale shard. `synthesize-boundary-stress.test.ts` proves the alignments.
50
+ */
51
+
52
+ import type { CanonicalRow } from "./types.ts"
53
+
54
+ export type BoundaryStressTemplate =
55
+ | "street-eats-affix"
56
+ | "comma-less-city-state"
57
+ | "fr-prefix"
58
+ | "house-number-after-street"
59
+ // Added 2026-06-18 after the v1.6.0 probes (the shard's NARROW distribution over-fit "full structured
60
+ // address" and regressed OOD). These two re-balance the contexts the model actually sees:
61
+ | "bare-locality" // the ship-blocker fix: locality with NO street (the 84%-dropped "City, STATE" rows)
62
+ | "house-number-before-street" // the confounding mirror of house-number-after-street (number position)
63
+
64
+ export interface BoundaryStressBaseTuple {
65
+ locality: string
66
+ region: string
67
+ postcode: string
68
+ country: string
69
+ }
70
+
71
+ export interface BoundaryStressSynthesisOpts {
72
+ random?: () => number
73
+ /** Force a specific shape (tests + balanced shard composition). */
74
+ forceTemplate?: BoundaryStressTemplate
75
+ }
76
+
77
+ export interface SynthesizedBoundaryStressRow {
78
+ raw: string
79
+ components: CanonicalRow["components"]
80
+ locale: string
81
+ template: BoundaryStressTemplate
82
+ }
83
+
84
+ function pick<T>(arr: ReadonlyArray<T>, random: () => number): T {
85
+ return arr[Math.floor(random() * arr.length)]!
86
+ }
87
+
88
+ // Multi-word street names — the suffix boundary only bites when "Club" could be read as part of the
89
+ // name. Single-word names alone teach nothing about the suffix edge.
90
+ // Multi-word names are what make the suffix boundary BITE (the model must not read the trailing
91
+ // suffix word as part of the name). Kept diverse so the shard teaches the boundary, not the lexeme.
92
+ const MULTIWORD_STREETS = [
93
+ "Country Club",
94
+ "Martin Luther King",
95
+ "Forest Hill",
96
+ "Lake View",
97
+ "Spring Valley",
98
+ "Cedar Ridge",
99
+ "Old Mill",
100
+ "Sunset Park",
101
+ "Maple Grove",
102
+ "Stone Creek",
103
+ "Glen Cove",
104
+ "Pine Bluff",
105
+ "Fox Hollow",
106
+ "Briar Patch",
107
+ "West End",
108
+ "College Station",
109
+ "Quail Hollow",
110
+ "Eagle Ridge",
111
+ "Deer Run",
112
+ "Bear Creek",
113
+ "Willow Bend",
114
+ "Cypress Point",
115
+ "Laurel Oak",
116
+ "Magnolia Park",
117
+ "Cherry Hill",
118
+ "Walnut Grove",
119
+ "Birch Hollow",
120
+ "Aspen Grove",
121
+ "Juniper Ridge",
122
+ "Hidden Valley",
123
+ "Rolling Hills",
124
+ "Tanglewood",
125
+ "Meadow Brook",
126
+ "Clover Field",
127
+ "Sunrise Point",
128
+ "Harbor View",
129
+ "Bay Shore",
130
+ "Ocean Breeze",
131
+ "Mountain View",
132
+ "Valley Forge",
133
+ "Liberty Square",
134
+ "Washington Crossing",
135
+ "Kings Highway",
136
+ "Queens Gate",
137
+ "Princeton Junction",
138
+ ] as const
139
+ const SINGLE_STREETS = [
140
+ "Main",
141
+ "Oak",
142
+ "Maple",
143
+ "Park",
144
+ "Washington",
145
+ "Lincoln",
146
+ "Church",
147
+ "River",
148
+ "Pine",
149
+ "Cedar",
150
+ "Elm",
151
+ "Jefferson",
152
+ "Madison",
153
+ "Adams",
154
+ "Jackson",
155
+ "Franklin",
156
+ "Highland",
157
+ "Sunset",
158
+ "Lakeview",
159
+ "Hillcrest",
160
+ "Cambridge",
161
+ "Devonshire",
162
+ "Sherwood",
163
+ "Kingston",
164
+ "Berkshire",
165
+ "Aberdeen",
166
+ "Belmont",
167
+ "Carlisle",
168
+ "Dover",
169
+ "Easton",
170
+ "Fairfax",
171
+ "Greenwood",
172
+ "1st",
173
+ "2nd",
174
+ "3rd",
175
+ "4th",
176
+ "5th",
177
+ "12th",
178
+ "42nd",
179
+ ] as const
180
+ const SUFFIXES = [
181
+ "St",
182
+ "Street",
183
+ "Ave",
184
+ "Avenue",
185
+ "Rd",
186
+ "Road",
187
+ "Blvd",
188
+ "Boulevard",
189
+ "Ln",
190
+ "Lane",
191
+ "Dr",
192
+ "Drive",
193
+ "Pkwy",
194
+ "Parkway",
195
+ "Way",
196
+ "Ct",
197
+ "Court",
198
+ "Pl",
199
+ "Place",
200
+ "Cir",
201
+ "Circle",
202
+ "Ter",
203
+ "Terrace",
204
+ "Hwy",
205
+ "Trail",
206
+ "Loop",
207
+ "Cres",
208
+ "Crescent",
209
+ "Row",
210
+ "Walk",
211
+ ] as const
212
+ const DIRECTIONALS = ["N", "S", "E", "W", "NE", "NW", "SE", "SW"] as const
213
+
214
+ // FR street-type prefixes + hyphenated honorific street names (the hyphen is incidental; the boundary
215
+ // stress is the prefix↔name split + the number-after-street order).
216
+ const FR_PREFIXES = [
217
+ "Rue",
218
+ "Avenue",
219
+ "Boulevard",
220
+ "Place",
221
+ "Impasse",
222
+ "Chemin",
223
+ "Quai",
224
+ "Cours",
225
+ "Allée",
226
+ "Passage",
227
+ "Square",
228
+ "Villa",
229
+ "Sentier",
230
+ "Promenade",
231
+ ] as const
232
+ const FR_NAMES = [
233
+ "Jean-Baptiste Lebas",
234
+ "Neuve-des-Capucines",
235
+ "Charles-de-Gaulle",
236
+ "du Général-Leclerc",
237
+ "de la République",
238
+ "des Trois-Frères",
239
+ "Victor-Hugo",
240
+ "Jean-Jaurès",
241
+ "de l'Abreuvoir",
242
+ "Émile-Zola",
243
+ "Gambetta",
244
+ "Jean-Moulin",
245
+ "des Martyrs-de-la-Résistance",
246
+ "du Maréchal-Foch",
247
+ "Pierre-et-Marie-Curie",
248
+ "Antoine-de-Saint-Exupéry",
249
+ "de la Liberté",
250
+ "des Quatre-Vents",
251
+ "du Faubourg-Saint-Antoine",
252
+ "Saint-Honoré",
253
+ "de la Pompe",
254
+ "des Petits-Champs",
255
+ "Léon-Blum",
256
+ "Aristide-Briand",
257
+ ] as const
258
+
259
+ // Org/venue prefixes for the bare-locality shape — the v1.6.0 locality drop hit org-PREFIXED real rows
260
+ // hardest ("LISBON PUBLIC LIBRARY, …, Lisbon ND"; "Alburg Health Center"). Teaching the locality WITH a
261
+ // leading venue keeps the model emitting it on facility-style addresses (NPPES/HRSA shapes). `venue` is a
262
+ // base ComponentTag.
263
+ //
264
+ // #511-LINTED 2026-06-18 (scripts/lint-venue-vocab — scan of nppes/hrsa/tiger/nad/wof-admin): every token
265
+ // here is venue-DOMINANT in the base, so the shard agrees with it. The first draft was naive — 9 terms
266
+ // were dropped because their tokens are dominantly street/locality and would CONTRADICT the base the way
267
+ // Madison-as-street did (#511): "Fire" 93% street, "Veterans" 94% street, "City" 68% locality, "Hall" 63%
268
+ // street, "Memorial"/"Hospital" 62-63% street, "Recreation" 79% street, "Town" 48% locality, "Library" 51%
269
+ // street, "County" 68% street, "Arts"/"Courthouse"/"Municipal" dependent_locality. Kept tokens: Clinic 98%,
270
+ // Practice 98%, Dental 100%, Health 99%, Medical 88%, Community 92%, Department 90%, Group 87%, Center 65%,
271
+ // School 70%, Public 89%, Elementary/Family 97% (all venue).
272
+ const VENUES = [
273
+ "Community Center",
274
+ "Health Center",
275
+ "Medical Center",
276
+ "Medical Clinic",
277
+ "Family Clinic",
278
+ "Community Clinic",
279
+ "Dental Clinic",
280
+ "Family Practice",
281
+ "Medical Practice",
282
+ "Dental Group",
283
+ "Medical Group",
284
+ "Health Department",
285
+ "Elementary School",
286
+ "Public School",
287
+ ] as const
288
+
289
+ // Localities DERIVED from the base corpus (#511): every name here is verified locality-DOMINANT in the
290
+ // training data (B-locality ≫ I-street), so the shard agrees with the base instead of fighting it. The
291
+ // night's targeted scan caught the prior vocab (Madison, Portland, Springfield IL…) at 92–100% STREET
292
+ // in the base ("Madison Ave"), the "5th Avenue Theatre" #511 trap. See 2026-06-17-locality-vocab-fix.
293
+ const US_TUPLES: ReadonlyArray<BoundaryStressBaseTuple> = [
294
+ { locality: "Albuquerque", region: "NM", postcode: "87102", country: "US" },
295
+ { locality: "Indianapolis", region: "IN", postcode: "46203", country: "US" },
296
+ { locality: "Sacramento", region: "CA", postcode: "95823", country: "US" },
297
+ { locality: "Rochester", region: "NY", postcode: "14606", country: "US" },
298
+ { locality: "Jacksonville", region: "FL", postcode: "32209", country: "US" },
299
+ { locality: "Portsmouth", region: "VA", postcode: "23704", country: "US" },
300
+ { locality: "Merced", region: "CA", postcode: "95340", country: "US" },
301
+ { locality: "Miami", region: "FL", postcode: "33125", country: "US" },
302
+ { locality: "Tampa", region: "FL", postcode: "33624", country: "US" },
303
+ { locality: "Orlando", region: "FL", postcode: "32827", country: "US" },
304
+ { locality: "Tulsa", region: "OK", postcode: "74133", country: "US" },
305
+ { locality: "Louisville", region: "KY", postcode: "40203", country: "US" },
306
+ { locality: "Nashville", region: "TN", postcode: "37207", country: "US" },
307
+ { locality: "Spokane", region: "WA", postcode: "99202", country: "US" },
308
+ { locality: "Akron", region: "OH", postcode: "44313", country: "US" },
309
+ { locality: "Fairbanks", region: "AK", postcode: "99701", country: "US" },
310
+ { locality: "Plano", region: "TX", postcode: "75024", country: "US" },
311
+ { locality: "Shreveport", region: "LA", postcode: "71103", country: "US" },
312
+ { locality: "Southfield", region: "MI", postcode: "48034", country: "US" },
313
+ { locality: "Glendale", region: "CA", postcode: "91203", country: "US" },
314
+ { locality: "Philadelphia", region: "PA", postcode: "19104", country: "US" },
315
+ { locality: "Brooklyn", region: "NY", postcode: "11230", country: "US" },
316
+ { locality: "Bronx", region: "NY", postcode: "10461", country: "US" },
317
+ { locality: "Fairport", region: "NY", postcode: "14450", country: "US" },
318
+ { locality: "Syracuse", region: "NE", postcode: "68446", country: "US" },
319
+ { locality: "Marion", region: "AR", postcode: "72364", country: "US" },
320
+ { locality: "Chicago", region: "IL", postcode: "60625", country: "US" },
321
+ { locality: "Springfield", region: "MA", postcode: "01108", country: "US" },
322
+ ]
323
+ // FR localities DERIVED from the FR (ban) shards specifically — where these famous cities are 95–99%
324
+ // locality-DOMINANT (Paris 515605/24789, Marseille 247014/1752, Lyon 106239/3114). NB: the all-shard
325
+ // scan falsely flagged them street-dominant by undersampling the FR block (parts 180–209) and mixing in
326
+ // US street-contexts; the FR-block scan is the honest distribution. Dept-diverse (28 depts), region
327
+ // empty (French addresses carry no region token; the generator's region-optional path handles it).
328
+ const FR_TUPLES: ReadonlyArray<BoundaryStressBaseTuple> = [
329
+ { locality: "Paris", region: "", postcode: "75003", country: "FR" },
330
+ { locality: "Marseille", region: "", postcode: "13016", country: "FR" },
331
+ { locality: "Lyon", region: "", postcode: "69009", country: "FR" },
332
+ { locality: "Perpignan", region: "", postcode: "66000", country: "FR" },
333
+ { locality: "Toulon", region: "", postcode: "83100", country: "FR" },
334
+ { locality: "Avignon", region: "", postcode: "84140", country: "FR" },
335
+ { locality: "Poitiers", region: "", postcode: "86000", country: "FR" },
336
+ { locality: "Arles", region: "", postcode: "13280", country: "FR" },
337
+ { locality: "Annecy", region: "", postcode: "74940", country: "FR" },
338
+ { locality: "Mulhouse", region: "", postcode: "68200", country: "FR" },
339
+ { locality: "Carpentras", region: "", postcode: "84200", country: "FR" },
340
+ { locality: "Antony", region: "", postcode: "92160", country: "FR" },
341
+ { locality: "Sartrouville", region: "", postcode: "78500", country: "FR" },
342
+ { locality: "Épinal", region: "", postcode: "88000", country: "FR" },
343
+ { locality: "Meyzieu", region: "", postcode: "69330", country: "FR" },
344
+ { locality: "Sens", region: "", postcode: "89100", country: "FR" },
345
+ { locality: "Brunoy", region: "", postcode: "91800", country: "FR" },
346
+ { locality: "Rambouillet", region: "", postcode: "78120", country: "FR" },
347
+ ]
348
+ // NB: no DE_TUPLES — German cities are street-dominated too ("Berliner Straße"), and the base yielded
349
+ // zero locality-dominant DE towns in the scan, so house-number-after-street is FR-only here. DE's
350
+ // native-order number-after-street is covered by the dedicated synth-german shard.
351
+ const houseNumber = (random: () => number): string => String(1 + Math.floor(random() * 4999))
352
+ const localeFor: Record<string, string> = { US: "en-US", FR: "fr-FR", DE: "de-DE" }
353
+
354
+ const ALL_TEMPLATES: readonly BoundaryStressTemplate[] = [
355
+ "street-eats-affix",
356
+ "comma-less-city-state",
357
+ "fr-prefix",
358
+ "house-number-after-street",
359
+ "bare-locality",
360
+ "house-number-before-street",
361
+ ]
362
+
363
+ /**
364
+ * Synthesize one boundary-stress row. `base` is optional — when omitted, a locale-appropriate tuple is drawn from the
365
+ * internal pools (so the generator is self-contained; a build script can pass real tuples for scale + diversity). Every
366
+ * component value is a verbatim substring of `raw`, so `alignRow` locates + BIO-labels it.
367
+ */
368
+ export function synthesizeBoundaryStressRow(
369
+ base: BoundaryStressBaseTuple | undefined,
370
+ opts: BoundaryStressSynthesisOpts = {}
371
+ ): SynthesizedBoundaryStressRow {
372
+ const random = opts.random ?? Math.random
373
+ const template = opts.forceTemplate ?? pick(ALL_TEMPLATES, random)
374
+
375
+ if (template === "bare-locality") {
376
+ // The v1.6.0 ship-blocker fix: locality was DROPPED on bare/short "City, STATE" rows (84% of the
377
+ // regression) because every prior shape placed a street before the city, so the model learned "the
378
+ // city follows a street" and stopped emitting locality without one. Teach the locality with NO street,
379
+ // across the forms real data carries it — bare, comma-LESS, postcode'd, and venue/org-prefixed.
380
+ const b = base ?? (random() < 0.3 ? pick(FR_TUPLES, random) : pick(US_TUPLES, random))
381
+ const venue = random() < 0.45 ? pick(VENUES, random) : ""
382
+ // ~12% carry a trailing country token — the v1.7.1 country patch (DeepSeek 2026-06-18). The pure
383
+ // "City, STATE" bare rows carry NO country token, which cost ~4pp on us.country_homograph in v1.7.0;
384
+ // teaching "…, USA"/"…, France" recovers it as a single-variable additive without diluting locality.
385
+ const withCountry = random() < 0.12
386
+
387
+ if (b.country === "FR") {
388
+ // FR carries no region token; "{postcode} {locality}" is the bare FR form.
389
+ const core = `${b.postcode} ${b.locality}${withCountry ? ", France" : ""}`
390
+
391
+ return {
392
+ raw: venue ? `${venue}, ${core}` : core,
393
+ components: {
394
+ ...(venue ? { venue } : {}),
395
+ postcode: b.postcode,
396
+ locality: b.locality,
397
+ ...(withCountry ? { country: "France" } : {}),
398
+ },
399
+ locale: "fr-FR",
400
+ template,
401
+ }
402
+ }
403
+ const withZip = random() < 0.5
404
+ const comma = random() < 0.6 ? "," : "" // include the comma-LESS "City STATE" form too
405
+ // "United States" (United 98% / States 98% country in the base), NOT "USA" — the #511 lint found
406
+ // "USA" is locality-DOMINANT (75%, only 6% country) in the base; labeling it country would contradict.
407
+ const countryName = "United States"
408
+ const core = `${b.locality}${comma} ${b.region}${withZip ? ` ${b.postcode}` : ""}${withCountry ? `, ${countryName}` : ""}`
409
+
410
+ return {
411
+ raw: venue ? `${venue}, ${core}` : core,
412
+ components: {
413
+ ...(venue ? { venue } : {}),
414
+ locality: b.locality,
415
+ region: b.region,
416
+ ...(withZip ? { postcode: b.postcode } : {}),
417
+ ...(withCountry ? { country: countryName } : {}),
418
+ },
419
+ locale: "en-US",
420
+ template,
421
+ }
422
+ }
423
+
424
+ if (
425
+ template === "fr-prefix" ||
426
+ template === "house-number-after-street" ||
427
+ template === "house-number-before-street"
428
+ ) {
429
+ // FR-only (no base-consistent DE locality vocab; see the DE_TUPLES note above).
430
+ const b = base ?? pick(FR_TUPLES, random)
431
+ const name = pick(FR_NAMES, random)
432
+ const hn = houseNumber(random)
433
+
434
+ if (template === "house-number-before-street") {
435
+ // The confounding MIRROR of house-number-after-street: the SAME FR street vocab with the number
436
+ // BEFORE the name. A balanced before:after mix (the build/recipe sets the ratio, ~7:3 to keep US
437
+ // house_number 99.8% safe) teaches the model a street-adjacent number is a house_number by FORM,
438
+ // not position — the probe found v1.6.0 confidently absorbs the TRAILING number into street (I-street
439
+ // P=0.96), the order-bias.
440
+ const raw = `${hn} ${name}, ${b.postcode} ${b.locality}`
441
+
442
+ return {
443
+ raw,
444
+ components: { house_number: hn, street: name, postcode: b.postcode, locality: b.locality },
445
+ locale: localeFor[b.country] ?? "fr-FR",
446
+ template,
447
+ }
448
+ }
449
+
450
+ if (template === "fr-prefix") {
451
+ const prefix = pick(FR_PREFIXES, random)
452
+ // "{hn} {prefix} {name}, {postcode} {locality}" — postcode-first, prefix split from the name.
453
+ const raw = `${hn} ${prefix} ${name}, ${b.postcode} ${b.locality}`
454
+
455
+ return {
456
+ raw,
457
+ components: {
458
+ house_number: hn,
459
+ street_prefix: prefix,
460
+ street: name,
461
+ postcode: b.postcode,
462
+ locality: b.locality,
463
+ },
464
+ locale: localeFor[b.country] ?? "fr-FR",
465
+ template,
466
+ }
467
+ }
468
+ // house-number-after-street: "{name} {hn}, {postcode} {locality}" — number FOLLOWS the street.
469
+ const raw = `${name} ${hn}, ${b.postcode} ${b.locality}`
470
+
471
+ return {
472
+ raw,
473
+ components: { street: name, house_number: hn, postcode: b.postcode, locality: b.locality },
474
+ locale: localeFor[b.country] ?? "fr-FR",
475
+ template,
476
+ }
477
+ }
478
+
479
+ // en-US street shapes (street-eats-affix + comma-less). US-only — US zips are base-consistent and
480
+ // the boundary these teach is locale-agnostic; no need to introduce a non-base locale.
481
+ const b = base ?? pick(US_TUPLES, random)
482
+ const hn = houseNumber(random)
483
+ const dir = random() < 0.4 ? pick(DIRECTIONALS, random) : ""
484
+ const name = random() < 0.7 ? pick(MULTIWORD_STREETS, random) : pick(SINGLE_STREETS, random)
485
+ const suffix = pick(SUFFIXES, random)
486
+ const streetCore = `${dir ? `${dir} ` : ""}${name} ${suffix}`
487
+ const components: CanonicalRow["components"] = {
488
+ house_number: hn,
489
+ ...(dir ? { street_prefix: dir } : {}),
490
+ street: name,
491
+ street_suffix: suffix,
492
+ locality: b.locality,
493
+ region: b.region,
494
+ postcode: b.postcode,
495
+ }
496
+ const raw =
497
+ template === "comma-less-city-state"
498
+ ? // no commas anywhere — the segmentation cue is gone
499
+ `${hn} ${streetCore} ${b.locality} ${b.region} ${b.postcode}`
500
+ : // standard delimited, multi-word street stresses the suffix boundary
501
+ `${hn} ${streetCore}, ${b.locality}, ${b.region} ${b.postcode}`
502
+
503
+ return { raw, components, locale: localeFor[b.country] ?? "en-US", template }
504
+ }