@mailwoman/corpus 11.5.0 → 12.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 (232) hide show
  1. package/data/address-source-register.json +545 -11
  2. package/data/builds/v0.8.22-wofrank-overlay/OVERLAY_PLAN.json +51 -0
  3. package/data/builds/v0.8.24-everyplace/inputs.json +35 -0
  4. package/data/license-decisions.json +440 -9
  5. package/data/licenses/PROVENANCE.md +27 -18
  6. package/data/licenses/at-bev-adressregister-cc-by-4.0.md +25 -0
  7. package/data/licenses/br-ibge-cnefe-open-data.md +21 -0
  8. package/data/licenses/ca-qc-mrnf-cc-by-4.0.md +29 -0
  9. package/data/licenses/ca-statcan-open-licence.md +41 -0
  10. package/data/licenses/ca-yukon-ogl-2.0.md +19 -0
  11. package/data/licenses/dk-dmikg-cc-by-4.0.md +43 -0
  12. package/data/licenses/jp-mlit-isj-pdl-1.0.md +35 -0
  13. package/data/source-additions.json +90 -0
  14. package/data/source-resolutions.json +3 -2
  15. package/data/training-configs/v8.17.0-stage1-gazetteer-v0822-s42.yaml +557 -0
  16. package/data/training-configs/v8.17.0-stage2-corpus-v0822-s42.yaml +1008 -0
  17. package/data/training-configs/v8.18.0-gazetteer-then-mixture-v0822-s42.yaml +1225 -0
  18. package/data/training-configs/v8.20.0-wofrank-v0822-s42.yaml +863 -0
  19. package/data/training-configs/v8.21.0-outcomes-v0822-s42.yaml +873 -0
  20. package/data/training-configs/v8.22.0-stage1-everyplace-v0824-s42.yaml +374 -0
  21. package/data/training-configs/v8.22.0-stage2-corpus-v0824-s42.yaml +808 -0
  22. package/data/training-configs/v8.22.1-stage1-everyplace-v0824-s42.yaml +376 -0
  23. package/data/training-configs/v8.22.1-stage2-corpus-v0824-s42.yaml +793 -0
  24. package/data/training-configs/v9.0.0-bytestem-v090-s42.yaml +665 -0
  25. package/data/training-configs/v9.1.0-bytestem-global-v091-s42.yaml +710 -0
  26. package/data/training-configs/v9.2.0-bytestem-global-v092-s42.yaml +714 -0
  27. package/data/training-manifests/v0.8.22-wofrank-overlay.json +9000 -0
  28. package/data/training-manifests/v0.8.24-everyplace-overlay.json +9000 -0
  29. package/data/training-manifests/v0.9.0-compact.effective.json +2556 -0
  30. package/data/training-manifests/v0.9.0-compact.json +9000 -0
  31. package/data/training-manifests/v0.9.1-compact.json +9660 -0
  32. package/data/training-manifests/v0.9.2-compact.effective.json +2670 -0
  33. package/data/training-manifests/v0.9.2-compact.json +9660 -0
  34. package/lib/adapters/geonames/features/adapter.ts +29 -11
  35. package/lib/adapters/overture/adapter.ts +26 -6
  36. package/lib/adapters/street/line.ts +6 -5
  37. package/lib/adapters/street/unit.ts +7 -0
  38. package/lib/adapters/wof/admin/json/adapter.ts +38 -3
  39. package/lib/adapters/wof/json-rows.ts +84 -5
  40. package/lib/adapters/wof/sample.ts +42 -6
  41. package/lib/au/adapters/gnaf/source.ts +12 -0
  42. package/lib/build/code-hash.ts +1 -1
  43. package/lib/cjk/foreign-places.ts +1 -1
  44. package/lib/fr/adapters/ban/adapter.ts +57 -8
  45. package/lib/fr/recipes/admin-split.ts +14 -2
  46. package/lib/fr/recipes/lieudit.ts +1 -1
  47. package/lib/fr/recipes/order.ts +14 -5
  48. package/lib/international/recipes/locale.ts +1 -1
  49. package/lib/kr/adapters/localdata/permit-rows.ts +1 -1
  50. package/lib/parquet/schema.ts +7 -0
  51. package/lib/recipes/po/box/cedex/recipe.ts +1 -1
  52. package/lib/recipes/po/box/cedex/vocabulary.ts +4 -4
  53. package/lib/recipes/trailing-region.ts +147 -17
  54. package/lib/synthesizers/po-box.ts +51 -12
  55. package/lib/test-kit/corpus-recipe.ts +3 -0
  56. package/lib/tools/anthropic/batches.ts +221 -0
  57. package/lib/tools/anthropic/labeling.ts +263 -0
  58. package/lib/tools/compact-v9.ts +890 -0
  59. package/lib/tools/fetch/wikidata/vital-places/entities.ts +20 -2
  60. package/lib/tools/fetch/wikidata/vital-places/qrank.ts +5 -4
  61. package/lib/tools/fragment-assay.run.ts +2 -1
  62. package/lib/tools/name-roles/name-roles.run.ts +296 -0
  63. package/lib/tools/name-roles/prompt.ts +253 -0
  64. package/lib/tools/overlay/deletions.ts +1 -1
  65. package/lib/tools/overlay/draw-weight.ts +275 -0
  66. package/lib/tools/place-twins/place-twins.run.ts +528 -0
  67. package/lib/tools/place-twins/prompt.ts +276 -0
  68. package/lib/tools/prominent-shapes/prominent-shapes.run.ts +420 -0
  69. package/lib/tools/prominent-shapes/prompt.ts +189 -0
  70. package/lib/tools/prominent-shapes/read-seeds.ts +286 -0
  71. package/lib/tools/prominent-shapes/seeds.ts +281 -0
  72. package/lib/tools/prominent-shapes/validate.ts +563 -0
  73. package/lib/tools/source-register/build.ts +1 -0
  74. package/lib/training/config/admission.ts +9 -2
  75. package/lib/training/config/schema.ts +68 -1
  76. package/lib/types.ts +23 -0
  77. package/lib/us/adapters/usgov/usgs-structures/adapter.ts +103 -6
  78. package/lib/utils/split.ts +1 -1
  79. package/out/adapters/geonames/features/adapter.d.ts +2 -2
  80. package/out/adapters/geonames/features/adapter.d.ts.map +1 -1
  81. package/out/adapters/geonames/features/adapter.js +25 -11
  82. package/out/adapters/geonames/features/adapter.js.map +1 -1
  83. package/out/adapters/overture/adapter.d.ts.map +1 -1
  84. package/out/adapters/overture/adapter.js +21 -5
  85. package/out/adapters/overture/adapter.js.map +1 -1
  86. package/out/adapters/street/line.d.ts +4 -4
  87. package/out/adapters/street/line.d.ts.map +1 -1
  88. package/out/adapters/street/line.js +5 -5
  89. package/out/adapters/street/line.js.map +1 -1
  90. package/out/adapters/street/unit.d.ts +4 -0
  91. package/out/adapters/street/unit.d.ts.map +1 -1
  92. package/out/adapters/street/unit.js +3 -0
  93. package/out/adapters/street/unit.js.map +1 -1
  94. package/out/adapters/wof/admin/json/adapter.d.ts +7 -0
  95. package/out/adapters/wof/admin/json/adapter.d.ts.map +1 -1
  96. package/out/adapters/wof/admin/json/adapter.js +28 -3
  97. package/out/adapters/wof/admin/json/adapter.js.map +1 -1
  98. package/out/adapters/wof/json-rows.d.ts +8 -0
  99. package/out/adapters/wof/json-rows.d.ts.map +1 -1
  100. package/out/adapters/wof/json-rows.js +56 -4
  101. package/out/adapters/wof/json-rows.js.map +1 -1
  102. package/out/adapters/wof/sample.d.ts +12 -0
  103. package/out/adapters/wof/sample.d.ts.map +1 -1
  104. package/out/adapters/wof/sample.js +37 -6
  105. package/out/adapters/wof/sample.js.map +1 -1
  106. package/out/au/adapters/gnaf/source.d.ts +9 -0
  107. package/out/au/adapters/gnaf/source.d.ts.map +1 -1
  108. package/out/au/adapters/gnaf/source.js +9 -0
  109. package/out/au/adapters/gnaf/source.js.map +1 -1
  110. package/out/build/code-hash.js +1 -1
  111. package/out/build/code-hash.js.map +1 -1
  112. package/out/cjk/foreign-places.js +1 -1
  113. package/out/cjk/foreign-places.js.map +1 -1
  114. package/out/fr/adapters/ban/adapter.d.ts.map +1 -1
  115. package/out/fr/adapters/ban/adapter.js +49 -7
  116. package/out/fr/adapters/ban/adapter.js.map +1 -1
  117. package/out/fr/recipes/admin-split.d.ts.map +1 -1
  118. package/out/fr/recipes/admin-split.js +15 -2
  119. package/out/fr/recipes/admin-split.js.map +1 -1
  120. package/out/fr/recipes/lieudit.js +1 -1
  121. package/out/fr/recipes/lieudit.js.map +1 -1
  122. package/out/fr/recipes/order.d.ts.map +1 -1
  123. package/out/fr/recipes/order.js +16 -5
  124. package/out/fr/recipes/order.js.map +1 -1
  125. package/out/international/recipes/locale.js +1 -1
  126. package/out/international/recipes/locale.js.map +1 -1
  127. package/out/kr/adapters/localdata/permit-rows.js +1 -1
  128. package/out/kr/adapters/localdata/permit-rows.js.map +1 -1
  129. package/out/parquet/schema.d.ts +6 -0
  130. package/out/parquet/schema.d.ts.map +1 -1
  131. package/out/parquet/schema.js +6 -0
  132. package/out/parquet/schema.js.map +1 -1
  133. package/out/recipes/po/box/cedex/recipe.js +1 -1
  134. package/out/recipes/po/box/cedex/recipe.js.map +1 -1
  135. package/out/recipes/po/box/cedex/vocabulary.d.ts +1 -1
  136. package/out/recipes/po/box/cedex/vocabulary.d.ts.map +1 -1
  137. package/out/recipes/po/box/cedex/vocabulary.js +4 -4
  138. package/out/recipes/po/box/cedex/vocabulary.js.map +1 -1
  139. package/out/recipes/trailing-region.d.ts.map +1 -1
  140. package/out/recipes/trailing-region.js +122 -17
  141. package/out/recipes/trailing-region.js.map +1 -1
  142. package/out/synthesizers/po-box.d.ts +7 -0
  143. package/out/synthesizers/po-box.d.ts.map +1 -1
  144. package/out/synthesizers/po-box.js +36 -12
  145. package/out/synthesizers/po-box.js.map +1 -1
  146. package/out/test-kit/corpus-recipe.d.ts +3 -0
  147. package/out/test-kit/corpus-recipe.d.ts.map +1 -1
  148. package/out/test-kit/corpus-recipe.js.map +1 -1
  149. package/out/tools/anthropic/batches.d.ts +117 -0
  150. package/out/tools/anthropic/batches.d.ts.map +1 -0
  151. package/out/tools/anthropic/batches.js +141 -0
  152. package/out/tools/anthropic/batches.js.map +1 -0
  153. package/out/tools/anthropic/labeling.d.ts +97 -0
  154. package/out/tools/anthropic/labeling.d.ts.map +1 -0
  155. package/out/tools/anthropic/labeling.js +146 -0
  156. package/out/tools/anthropic/labeling.js.map +1 -0
  157. package/out/tools/compact-v9.d.ts +124 -0
  158. package/out/tools/compact-v9.d.ts.map +1 -0
  159. package/out/tools/compact-v9.js +507 -0
  160. package/out/tools/compact-v9.js.map +1 -0
  161. package/out/tools/fetch/wikidata/vital-places/entities.d.ts +3 -0
  162. package/out/tools/fetch/wikidata/vital-places/entities.d.ts.map +1 -1
  163. package/out/tools/fetch/wikidata/vital-places/entities.js +19 -1
  164. package/out/tools/fetch/wikidata/vital-places/entities.js.map +1 -1
  165. package/out/tools/fetch/wikidata/vital-places/qrank.d.ts.map +1 -1
  166. package/out/tools/fetch/wikidata/vital-places/qrank.js +4 -4
  167. package/out/tools/fetch/wikidata/vital-places/qrank.js.map +1 -1
  168. package/out/tools/fragment-assay.run.js +1 -1
  169. package/out/tools/fragment-assay.run.js.map +1 -1
  170. package/out/tools/name-roles/name-roles.run.d.ts +2 -0
  171. package/out/tools/name-roles/name-roles.run.d.ts.map +1 -0
  172. package/out/tools/name-roles/name-roles.run.js +230 -0
  173. package/out/tools/name-roles/name-roles.run.js.map +1 -0
  174. package/out/tools/name-roles/prompt.d.ts +166 -0
  175. package/out/tools/name-roles/prompt.d.ts.map +1 -0
  176. package/out/tools/name-roles/prompt.js +176 -0
  177. package/out/tools/name-roles/prompt.js.map +1 -0
  178. package/out/tools/overlay/deletions.js +1 -1
  179. package/out/tools/overlay/deletions.js.map +1 -1
  180. package/out/tools/overlay/draw-weight.d.ts +86 -0
  181. package/out/tools/overlay/draw-weight.d.ts.map +1 -0
  182. package/out/tools/overlay/draw-weight.js +131 -0
  183. package/out/tools/overlay/draw-weight.js.map +1 -0
  184. package/out/tools/place-twins/place-twins.run.d.ts +2 -0
  185. package/out/tools/place-twins/place-twins.run.d.ts.map +1 -0
  186. package/out/tools/place-twins/place-twins.run.js +385 -0
  187. package/out/tools/place-twins/place-twins.run.js.map +1 -0
  188. package/out/tools/place-twins/prompt.d.ts +169 -0
  189. package/out/tools/place-twins/prompt.d.ts.map +1 -0
  190. package/out/tools/place-twins/prompt.js +183 -0
  191. package/out/tools/place-twins/prompt.js.map +1 -0
  192. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts +2 -0
  193. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts.map +1 -0
  194. package/out/tools/prominent-shapes/prominent-shapes.run.js +302 -0
  195. package/out/tools/prominent-shapes/prominent-shapes.run.js.map +1 -0
  196. package/out/tools/prominent-shapes/prompt.d.ts +74 -0
  197. package/out/tools/prominent-shapes/prompt.d.ts.map +1 -0
  198. package/out/tools/prominent-shapes/prompt.js +156 -0
  199. package/out/tools/prominent-shapes/prompt.js.map +1 -0
  200. package/out/tools/prominent-shapes/read-seeds.d.ts +23 -0
  201. package/out/tools/prominent-shapes/read-seeds.d.ts.map +1 -0
  202. package/out/tools/prominent-shapes/read-seeds.js +219 -0
  203. package/out/tools/prominent-shapes/read-seeds.js.map +1 -0
  204. package/out/tools/prominent-shapes/seeds.d.ts +132 -0
  205. package/out/tools/prominent-shapes/seeds.d.ts.map +1 -0
  206. package/out/tools/prominent-shapes/seeds.js +178 -0
  207. package/out/tools/prominent-shapes/seeds.js.map +1 -0
  208. package/out/tools/prominent-shapes/validate.d.ts +152 -0
  209. package/out/tools/prominent-shapes/validate.d.ts.map +1 -0
  210. package/out/tools/prominent-shapes/validate.js +389 -0
  211. package/out/tools/prominent-shapes/validate.js.map +1 -0
  212. package/out/tools/source-register/build.d.ts.map +1 -1
  213. package/out/tools/source-register/build.js +1 -0
  214. package/out/tools/source-register/build.js.map +1 -1
  215. package/out/training/config/admission.d.ts +2 -1
  216. package/out/training/config/admission.d.ts.map +1 -1
  217. package/out/training/config/admission.js +9 -2
  218. package/out/training/config/admission.js.map +1 -1
  219. package/out/training/config/schema.d.ts +276 -1
  220. package/out/training/config/schema.d.ts.map +1 -1
  221. package/out/training/config/schema.js +64 -1
  222. package/out/training/config/schema.js.map +1 -1
  223. package/out/types.d.ts +21 -0
  224. package/out/types.d.ts.map +1 -1
  225. package/out/types.js +13 -0
  226. package/out/types.js.map +1 -1
  227. package/out/us/adapters/usgov/usgs-structures/adapter.d.ts.map +1 -1
  228. package/out/us/adapters/usgov/usgs-structures/adapter.js +78 -4
  229. package/out/us/adapters/usgov/usgs-structures/adapter.js.map +1 -1
  230. package/out/utils/split.js +1 -1
  231. package/out/utils/split.js.map +1 -1
  232. package/package.json +24 -24
@@ -249,7 +249,7 @@ export const frLieuditRecipe: CorpusRecipe = {
249
249
  // This keeps the byte-stream a function of `countryFraction` rather than of
250
250
  // the mixture of jurisdictions in the input files.
251
251
  if (countryFraction > 0 && random() < countryFraction) {
252
- const forms = countryRenderForms(country)
252
+ const forms = countryRenderForms(country, { afterPostcode: Boolean(components.postcode) })
253
253
 
254
254
  if (forms.length) {
255
255
  const form = sample(forms, random)
@@ -84,11 +84,14 @@ function renderCanonical(
84
84
  return { raw, components: { house_number: hn, street, postcode, locality } }
85
85
  }
86
86
 
87
- // Cumulative cutoffs for three reversed layouts.
88
- // Each moves the admin tail ahead of the street or splits it around the street.
87
+ // Cumulative cutoffs for five reversed layouts.
88
+ // Each moves the admin tail ahead of the street or splits it around the street, including comma-
89
+ // and dash-separated postcode-first shapes observed in French postcode misses.
89
90
  // The training-time `postcode_move` augmentation (#2750) moves the postcode across the locality.
90
- const REVERSED_VARIANT_A_CUTOFF = 1 / 3
91
- const REVERSED_VARIANT_B_CUTOFF = 2 / 3
91
+ const REVERSED_VARIANT_A_CUTOFF = 0.2
92
+ const REVERSED_VARIANT_B_CUTOFF = 0.4
93
+ const REVERSED_VARIANT_C_CUTOFF = 0.6
94
+ const REVERSED_VARIANT_D_CUTOFF = 0.8
92
95
 
93
96
  /**
94
97
  * Recipe registered with the corpus builder.
@@ -145,9 +148,15 @@ export const frOrderRecipe: CorpusRecipe = {
145
148
  } else if (variantRoll < REVERSED_VARIANT_B_CUTOFF) {
146
149
  // Locality, postcode, number and street.
147
150
  raw = `${locality}, ${postcode}, ${house_number} ${street}`
148
- } else {
151
+ } else if (variantRoll < REVERSED_VARIANT_C_CUTOFF) {
149
152
  // Postcode, number, street, then locality.
150
153
  raw = `${postcode}, ${house_number} ${street}, ${locality}`
154
+ } else if (variantRoll < REVERSED_VARIANT_D_CUTOFF) {
155
+ // Postcode, locality, then number and street.
156
+ raw = `${postcode}, ${locality}, ${house_number} ${street}`
157
+ } else {
158
+ // Dash-separated postcode, number and street, then locality.
159
+ raw = `${postcode} - ${house_number} ${street}, ${locality}`
151
160
  }
152
161
 
153
162
  rendered = { raw, components: { house_number, street, postcode, locality } }
@@ -549,7 +549,7 @@ export function applyCountryAppend(
549
549
  random: () => number
550
550
  ): void {
551
551
  if (countryFraction > 0 && random() < countryFraction) {
552
- const forms = countryRenderForms(country)
552
+ const forms = countryRenderForms(country, { afterPostcode: Boolean(synth.components.postcode) })
553
553
 
554
554
  if (!forms.length) {
555
555
  throw new Error(
@@ -96,7 +96,7 @@ function toNumber(value: string, field: string): number | null {
96
96
 
97
97
  const parsed = Number(value)
98
98
 
99
- if (!Number.isFinite(parsed)) throw new Error(`a permit row's ${field} is ${stringifyJSON(value)}, not a number`)
99
+ if (!Number.isFinite(parsed)) throw new TypeError(`a permit row's ${field} is ${stringifyJSON(value)}, not a number`)
100
100
 
101
101
  return parsed
102
102
  }
@@ -29,6 +29,13 @@ export const ROW_GROUP_SIZE = 50_000
29
29
  */
30
30
  export const ROWS_PER_FILE = 1_000_000
31
31
 
32
+ /**
33
+ * The optional per-row draw weight: a positive float that scales a row's chance of
34
+ * being drawn within its source, with mean 1 over the source's train rows.
35
+ * A file without the column reads 1.0 for every row.
36
+ */
37
+ export const DRAW_WEIGHT_COLUMN = "draw_weight"
38
+
32
39
  /**
33
40
  * Snappy is the codec selected for corpus parquet files.
34
41
  */
@@ -154,7 +154,7 @@ export const poBoxCedexRecipe: CorpusRecipe = {
154
154
  country = "NZ"
155
155
  locale = "en-NZ"
156
156
  } else if (kind === "po-box-us-military") {
157
- const military = synthesizeMilitaryPoBoxRow({ random })
157
+ const military = synthesizeMilitaryPoBoxRow({ random, varyCase: true })
158
158
  const { country: _country, ...components } = military.components
159
159
  rendered = { fmt: "po-box-military", raw: military.raw, components: components as Record<string, string> }
160
160
  country = "US"
@@ -51,15 +51,15 @@ export const AU_LEADERS_CURRENT = ["PO Box", "P.O. Box", "Post Office Box", "GPO
51
51
  /**
52
52
  * Recognized legacy Australian delivery-service leaders.
53
53
  */
54
- export const AU_LEADERS_LEGACY = ["RMB", "RSD", "CMB"]
54
+ export const AU_LEADERS_LEGACY = ["RMB", "RSD", "CMB", "Community Mail Bag"]
55
55
  /**
56
56
  * Common New Zealand delivery-service leaders.
57
57
  */
58
- export const NZ_LEADERS_COMMON = ["PO Box", "Private Bag"]
58
+ export const NZ_LEADERS_COMMON = ["PO Box", "Private Bag", "Private Box"]
59
59
  /**
60
- * Rare New Zealand delivery-service leader.
60
+ * Rare New Zealand delivery-service leaders.
61
61
  */
62
- export const NZ_LEADERS_RARE = ["CMB"]
62
+ export const NZ_LEADERS_RARE = ["CMB", "Response Bag", "Community Mail Box"]
63
63
 
64
64
  /**
65
65
  * Quebec first-letter choices from the Canadian FSA prior.
@@ -5,9 +5,16 @@
5
5
  * `trailing-region` — the `«locality», «region»[, «country»]` admin tail,
6
6
  * built from real `(locality, region)` WOF ancestor pairs.
7
7
  *
8
- * A tuple's `postcodePlacement` selects where the postcode sits: `leading`
9
- * (the default, so a tuples file written before the field existed is unchanged),
8
+ * A tuple's `postcodePlacement` selects where the postcode sits: `leading`,
10
9
  * `after_locality`, or `after_region`.
10
+ * When absent, placement is inferred from the country's codex layout and postcode format
11
+ * (defaulting to `leading` when unknown, so tuples files written before the field existed are unchanged).
12
+ *
13
+ * For `after_locality` tuples (for example `Barcelona 6001, Venezuela`
14
+ * and `Barcelona 6001, Anzoátegui, Venezuela`), the recipe emits rows both with
15
+ * and without a region, omits synthetic house numbers so the locality-postcode pair
16
+ * is never confused with a street-number tail, and adds anchor-varied contrast
17
+ * rows for non-US four-digit postcode countries (#1821).
11
18
  *
12
19
  * A bare `«region»` form is deliberately absent: teaching it as `region` would fight
13
20
  * the locality/region ambiguity the dominance race arbitrates.
@@ -15,9 +22,16 @@
15
22
  * row begins with the locality and teaches the model that the first segment is the locality.
16
23
  */
17
24
 
25
+ import { formatAddress } from "@mailwoman/codex/address/format"
18
26
  import { lookupCanadianProvince } from "@mailwoman/codex/ca"
19
- import { lookupUSState } from "@mailwoman/codex/us"
27
+ import { ISO2_TO_NAME } from "@mailwoman/codex/country"
28
+ import { lookupUSState, US_STATE_BY_ABBREVIATION } from "@mailwoman/codex/us"
20
29
  import { mulberry32 as makeMulberry32 } from "@mailwoman/core/utils"
30
+ import {
31
+ hasPostcodeForm,
32
+ postcodePlacement as codexPostcodePlacement,
33
+ postcodeSyntaxCountries,
34
+ } from "@mailwoman/postcode/country-formats"
21
35
 
22
36
  import {
23
37
  alignAndWrite,
@@ -30,6 +44,48 @@ import {
30
44
  import { defaultRecipeSource } from "#recipes/sources"
31
45
  import { SurfaceOrigin } from "#types"
32
46
 
47
+ const US_CONTRAST_REGIONS: readonly string[] = Object.values(US_STATE_BY_ABBREVIATION)
48
+ // country-branch: #1821 anchor contrasts test the US country frame independently of the region anchor.
49
+ const US_COUNTRY_NAME = ISO2_TO_NAME.get("US") ?? "United States"
50
+
51
+ /**
52
+ * Infers a country's postcode placement inside the admin tail from its codex
53
+ * address layout and libaddressinput postcode placement, defaulting to `leading`
54
+ * when the country is unknown or prints postcodes before the locality.
55
+ */
56
+ function inferPostcodePlacement(cc: string): PostcodePlacement {
57
+ const country = cc.trim().toUpperCase()
58
+
59
+ if (!country || codexPostcodePlacement(country) !== "after_locality") return "leading"
60
+
61
+ const probe = formatAddress({ locality: "__LOC__", region: "__REG__", postcode: "__PC__" }, country, {
62
+ singleLine: true,
63
+ })
64
+
65
+ if (probe) {
66
+ const regionAt = probe.indexOf("__REG__")
67
+ const postcodeAt = probe.indexOf("__PC__")
68
+
69
+ if (regionAt !== -1 && postcodeAt !== -1 && regionAt < postcodeAt) {
70
+ return "after_region"
71
+ }
72
+ }
73
+
74
+ return "after_locality"
75
+ }
76
+
77
+ /**
78
+ * Non-US countries whose postcode syntax accepts a four-digit numeric postcode,
79
+ * derived from `@mailwoman/postcode` rather than a static country list.
80
+ */
81
+ function nonUSFourDigitPostcodeCountries(): readonly string[] {
82
+ // country-branch: #1821 targets non-US four-digit postcode countries whose locality-postcode
83
+ // tails collapse to street + house_number outside the US country frame.
84
+ return postcodeSyntaxCountries().filter((cc) => cc !== "US" && hasPostcodeForm("6001", cc))
85
+ }
86
+
87
+ const FOUR_DIGIT_NON_US_COUNTRIES: ReadonlySet<string> = new Set(nonUSFourDigitPostcodeCountries())
88
+
33
89
  /**
34
90
  * The region code an address line in this country writes, or `null` where the region name is written out.
35
91
  *
@@ -78,28 +134,47 @@ export const trailingRegionRecipe: CorpusRecipe = {
78
134
  const region = String(t.region ?? "").trim()
79
135
  const country = String(t.country ?? "").trim()
80
136
  const dependentLocality = String(t.dependentLocality ?? "").trim()
137
+ const postcode = String(t.postcode ?? "").trim()
138
+
139
+ const cc = String(t.cc ?? "")
140
+ .trim()
141
+ .toUpperCase()
142
+
143
+ const placement = (t.postcodePlacement as PostcodePlacement | undefined) ?? inferPostcodePlacement(cc)
144
+
145
+ const allowRegionlessAfterLocality = placement === "after_locality" && postcode.length > 0 && country.length > 0
81
146
 
82
147
  // A pair whose region equals its locality provides no signal about the boundary this recipe teaches.
83
- if (!locality || !region || locality === region) {
148
+ // Region-less tuples are admitted only for `after_locality` postcode tails with a country
149
+ // (e.g. `Barcelona 6001, Venezuela`), where the postcode separates locality from country.
150
+ if (!locality || (region && locality === region) || (!region && !allowRegionlessAfterLocality)) {
84
151
  skipped++
85
152
 
86
153
  continue
87
154
  }
88
155
 
89
- const withCountry = read % 2 === 0 && country.length > 0
90
- const postcode = String(t.postcode ?? "").trim()
156
+ const withCountry = !region ? true : read % 2 === 0 && country.length > 0
157
+ // For `after_locality` rows with a region and country, alternate between
158
+ // `«locality» «postcode», «region», «country»` and `«locality» «postcode», «country»`.
159
+ const includeRegion = Boolean(region) && !(allowRegionlessAfterLocality && withCountry && read % 4 === 0)
91
160
 
92
- // The house number is the second trigger: the postcode discards the region,
93
- // the house number then displaces the locality.
94
- const withHouseNumber = postcode.length > 0 && read % 4 === 1
161
+ // The house number is the second trigger for `leading` / `after_region` rows:
162
+ // the postcode discards the region, and the house number then displaces the locality.
163
+ // `after_locality` rows omit synthetic house numbers so `«locality» «postcode»`
164
+ // never co-occurs with a house number on a streetless tail.
165
+ const withHouseNumber = postcode.length > 0 && placement !== "after_locality" && read % 4 === 1
95
166
  const houseNumber = withHouseNumber ? String((read % 97) + 1) : ""
96
167
 
97
- const components: Record<string, string> = { locality, region }
168
+ const components: Record<string, string> = { locality }
98
169
 
99
170
  if (dependentLocality && dependentLocality !== locality) {
100
171
  components["dependent_locality"] = dependentLocality
101
172
  }
102
173
 
174
+ if (includeRegion) {
175
+ components["region"] = region
176
+ }
177
+
103
178
  if (withCountry) {
104
179
  components["country"] = country
105
180
  }
@@ -112,25 +187,26 @@ export const trailingRegionRecipe: CorpusRecipe = {
112
187
  components["house_number"] = houseNumber
113
188
  }
114
189
 
115
- const placement = (t.postcodePlacement as PostcodePlacement | undefined) ?? "leading"
116
190
  const bareLocality = postcode && placement === "after_locality" ? `${locality} ${postcode}` : locality
117
191
 
118
192
  const localitySegment = components["dependent_locality"] ? `${dependentLocality}, ${bareLocality}` : bareLocality
119
193
 
120
194
  // The code alternates with the region name rather than replacing it:
121
195
  // both forms are posted and the resolver matches on the name.
122
- const regionCode = read % 3 === 2 ? regionCodeSurface(String(t.cc ?? ""), region) : null
196
+ const regionCode = includeRegion && read % 3 === 2 ? regionCodeSurface(cc, region) : null
123
197
  const regionSurface = regionCode ?? region
124
198
 
125
- if (regionCode) {
199
+ if (includeRegion && regionCode) {
126
200
  components["region"] = regionCode
127
201
  }
128
202
 
129
203
  const regionSegment = postcode && placement === "after_region" ? `${regionSurface} ${postcode}` : regionSurface
130
204
 
131
- const tail = withCountry
132
- ? `${localitySegment}, ${regionSegment}, ${country}`
133
- : `${localitySegment}, ${regionSegment}`
205
+ const tail = includeRegion
206
+ ? withCountry
207
+ ? `${localitySegment}, ${regionSegment}, ${country}`
208
+ : `${localitySegment}, ${regionSegment}`
209
+ : `${localitySegment}, ${country}`
134
210
 
135
211
  const leadingPostcode = postcode && placement === "leading" ? `${postcode} ` : ""
136
212
  const head = withHouseNumber ? `${houseNumber}, ${leadingPostcode}` : leadingPostcode
@@ -143,7 +219,7 @@ export const trailingRegionRecipe: CorpusRecipe = {
143
219
  const canonical = {
144
220
  raw,
145
221
  components,
146
- country: String(t.cc ?? "").trim() || "und",
222
+ country: cc || "und",
147
223
  locale: String(t.locale ?? "und"),
148
224
  source: sourceLabel,
149
225
  source_id,
@@ -163,6 +239,60 @@ export const trailingRegionRecipe: CorpusRecipe = {
163
239
  } else {
164
240
  skipped++
165
241
  }
242
+
243
+ // #1821 anchor contrasts: US competence on `«locality» «postcode», «region», «country»`
244
+ // requires both a US region and `United States`.
245
+ // These contrast pairs vary each anchor independently so neither anchor by
246
+ // itself decides the locality-postcode boundary.
247
+ if (
248
+ placement === "after_locality" &&
249
+ includeRegion &&
250
+ withCountry &&
251
+ read % 8 === 2 &&
252
+ FOUR_DIGIT_NON_US_COUNTRIES.has(cc) &&
253
+ /^\d{4}$/u.test(postcode)
254
+ ) {
255
+ const usRegion = US_CONTRAST_REGIONS[read % US_CONTRAST_REGIONS.length]!
256
+
257
+ const contrastRows: Array<{ suffix: string; regionValue: string; countryValue: string }> = [
258
+ { suffix: "contrast-us-region", regionValue: usRegion, countryValue: country },
259
+ { suffix: "contrast-us-country", regionValue: regionSurface, countryValue: US_COUNTRY_NAME },
260
+ ]
261
+
262
+ for (const contrast of contrastRows) {
263
+ const contrastComponents: Record<string, string> = {
264
+ locality,
265
+ postcode,
266
+ region: contrast.regionValue,
267
+ country: contrast.countryValue,
268
+ }
269
+
270
+ if (components["dependent_locality"]) {
271
+ contrastComponents["dependent_locality"] = components["dependent_locality"]
272
+ }
273
+
274
+ const contrastCanonical = {
275
+ raw: `${localitySegment}, ${contrast.regionValue}, ${contrast.countryValue}`,
276
+ components: contrastComponents,
277
+ country: cc,
278
+ locale: String(t.locale ?? "und"),
279
+ source: sourceLabel,
280
+ source_id: recipeSourceID(sourceLabel, { ...contrastComponents, v: `${read}-${contrast.suffix}` }),
281
+ license: canonical.license,
282
+ }
283
+
284
+ if (
285
+ alignAndWrite(write, contrastCanonical, `trailing-region:${contrast.suffix}`, {
286
+ ...TRAILING_REGION_PROVENANCE,
287
+ baseSourceID: t.sourceID ?? null,
288
+ })
289
+ ) {
290
+ emitted++
291
+ } else {
292
+ skipped++
293
+ }
294
+ }
295
+ }
166
296
  }
167
297
 
168
298
  return { read, emitted, skipped }
@@ -8,6 +8,7 @@
8
8
 
9
9
  import type { ComponentDict } from "@mailwoman/codex/address/render"
10
10
  import { countryCodeForTable } from "@mailwoman/codex/country"
11
+ import { US_MILITARY_POST_OFFICE_CODES, US_MILITARY_UNIT_DESIGNATORS } from "@mailwoman/codex/us"
11
12
  import { sample } from "@mailwoman/core/math/random"
12
13
 
13
14
  import { formatCorpusRow } from "#surfaces/corpus-row"
@@ -69,11 +70,19 @@ export const PO_BOX_LOCALE_TEMPLATES: ReadonlyArray<LocaleTemplate> = [
69
70
  },
70
71
  {
71
72
  locale: "en-AU",
72
- leaders: ["PO Box", "P.O. Box", "Post Office Box", "GPO Box", "Locked Bag"],
73
+ leaders: ["PO Box", "P.O. Box", "Post Office Box", "GPO Box", "Locked Bag", "Private Bag", "Community Mail Bag"],
73
74
  },
74
75
  {
75
76
  locale: "en-NZ",
76
- leaders: ["PO Box", "P.O. Box", "Post Office Box", "Private Bag", "Private Box"],
77
+ leaders: [
78
+ "PO Box",
79
+ "P.O. Box",
80
+ "Post Office Box",
81
+ "Private Bag",
82
+ "Private Box",
83
+ "Response Bag",
84
+ "Community Mail Box",
85
+ ],
77
86
  },
78
87
  {
79
88
  locale: "fr-FR",
@@ -157,6 +166,13 @@ export interface PoBoxSynthesisOpts {
157
166
  * @defaultValue `0`
158
167
  */
159
168
  pmbRatio?: number
169
+ /**
170
+ * Vary casing of US military PO-box lines across title, uppercase (`UNIT 2050 BOX 4190`),
171
+ * and lowercase (`psc 802 box 74, apo ae 09499`).
172
+ *
173
+ * @defaultValue `false`
174
+ */
175
+ varyCase?: boolean
160
176
  }
161
177
 
162
178
  function defaultPickNumber(random: () => number): string {
@@ -237,13 +253,14 @@ export function synthesizePoBoxRow(
237
253
  }
238
254
  }
239
255
 
240
- const MIL_UNITS: ReadonlyArray<{ code: string; boxRequired: boolean }> = [
241
- { code: "PSC", boxRequired: true },
242
- { code: "CMR", boxRequired: true },
243
- { code: "Unit", boxRequired: false },
244
- ]
256
+ const MIL_UNITS: ReadonlyArray<{ code: string; boxRequired: boolean }> = US_MILITARY_UNIT_DESIGNATORS.map(
257
+ ({ code, requiresBox }) => ({
258
+ code: code === "UNIT" ? "Unit" : code,
259
+ boxRequired: requiresBox,
260
+ })
261
+ )
245
262
 
246
- const MIL_PO_CODES = ["APO", "FPO", "DPO"] as const
263
+ const MIL_PO_CODES: ReadonlyArray<string> = US_MILITARY_POST_OFFICE_CODES.map(({ code }) => code)
247
264
 
248
265
  // Each armed-forces region has its own ZIP range.
249
266
  const MIL_REGION_ZIP: ReadonlyArray<{ region: string; zip: (r: () => number) => string }> = [
@@ -263,11 +280,33 @@ export function synthesizeMilitaryPoBoxRow(opts: PoBoxSynthesisOpts = {}): Synth
263
280
  const random = opts.random ?? Math.random
264
281
  const unit = sample(MIL_UNITS, random)
265
282
  const unitID = String(1 + Math.floor(random() * 9999))
266
- const { region, zip } = sample(MIL_REGION_ZIP, random)
283
+ const { region: baseRegion, zip } = sample(MIL_REGION_ZIP, random)
267
284
  const zipStr = zip(random)
268
- const po = sample(MIL_PO_CODES, random)
269
- const hasBox = unit.boxRequired || random() < 0.5
270
- const unitLine = hasBox ? `${unit.code} ${unitID} Box ${1 + Math.floor(random() * 9999)}` : `${unit.code} ${unitID}`
285
+ const basePo = sample(MIL_PO_CODES, random)
286
+ const hasBox = unit.boxRequired || random() < 0.75
287
+ const rawBoxNumber = 1 + Math.floor(random() * 9999)
288
+
289
+ const boxNumber =
290
+ rawBoxNumber < 1000 && random() < 0.15 ? String(rawBoxNumber).padStart(4, "0") : String(rawBoxNumber)
291
+
292
+ const baseUnitLine = hasBox ? `${unit.code} ${unitID} Box ${boxNumber}` : `${unit.code} ${unitID}`
293
+
294
+ let unitLine = baseUnitLine
295
+ let po = basePo
296
+ let region = baseRegion
297
+
298
+ if (opts.varyCase) {
299
+ const caseDraw = random()
300
+
301
+ if (caseDraw < 0.2) {
302
+ unitLine = baseUnitLine.toUpperCase()
303
+ } else if (caseDraw < 0.35) {
304
+ unitLine = baseUnitLine.toLowerCase()
305
+ po = basePo.toLowerCase()
306
+ region = baseRegion.toLowerCase()
307
+ }
308
+ }
309
+
271
310
  const raw = `${unitLine}, ${po} ${region} ${zipStr}`
272
311
 
273
312
  return {
@@ -31,6 +31,9 @@ export interface RecipeRow {
31
31
  components?: Partial<Record<string, string>>
32
32
  labels?: string[]
33
33
  tokens?: string[]
34
+ span_tags?: string[]
35
+ span_starts?: number[]
36
+ span_ends?: number[]
34
37
  }
35
38
 
36
39
  /**