@mailwoman/corpus 11.1.0 → 11.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.
Files changed (474) hide show
  1. package/README.md +20 -14
  2. package/data/address-source-register.json +618 -13
  3. package/data/builds/v0.8.19-overlay/OVERLAY_PLAN.json +4778 -0
  4. package/data/builds/v0.8.19-rebuild/inputs.json +128 -0
  5. package/data/builds/v0.8.20-overlay/OVERLAY_PLAN.json +3595 -0
  6. package/data/builds/v0.8.20-rebuild/inputs.json +131 -0
  7. package/data/builds/v0.8.21-overlay/OVERLAY_PLAN.json +72 -0
  8. package/data/corpus-source-inputs.json +104 -2
  9. package/data/holdout/evaluation-groups.v0.8.18.json +90511 -0
  10. package/data/holdout/evaluation-groups.v0.8.19.json +100537 -0
  11. package/data/holdout/evaluation-groups.v0.8.20.json +129215 -0
  12. package/data/holdout/evaluation-groups.v0.8.21.json +173286 -0
  13. package/data/holdout/probe-text-collisions.v0.8.20.json +1478 -0
  14. package/data/license-decisions.json +393 -3
  15. package/data/licenses/PROVENANCE.md +15 -9
  16. package/data/licenses/au-acnc-cc-by-3.0-au.md +37 -0
  17. package/data/licenses/es-sanidad-aviso-legal.md +26 -0
  18. package/data/licenses/it-miur-iodl-2.0.md +41 -0
  19. package/data/licenses/nz-charities-cc-by-3.0-nz.md +36 -0
  20. package/data/region-slot-second-level.json +966 -0
  21. package/data/source-additions.json +126 -6
  22. package/data/source-resolutions.json +97 -0
  23. package/data/training-configs/v8.7.0-base-c6-90k-usgs-structures-s42.yaml +801 -0
  24. package/data/training-configs/v8.7.0-base-c6-90k-usgs-structures-s43.yaml +726 -0
  25. package/data/training-configs/v8.8.0-base-c6-90k-v0820-county-s42.yaml +944 -0
  26. package/data/training-configs/v8.9.0-base-c6-90k-v0821-s42.yaml +848 -0
  27. package/data/training-manifests/v0.8.19-retention-overlay.json +4553 -0
  28. package/data/training-manifests/v0.8.20-retention-overlay.json +4579 -0
  29. package/data/training-manifests/v0.8.21-retention-overlay.effective.json +2521 -0
  30. package/data/training-manifests/v0.8.21-retention-overlay.json +4579 -0
  31. package/lib/adapters/cordis/adapter.ts +10 -2
  32. package/lib/adapters/gleif/adapter.ts +23 -5
  33. package/lib/adapters/osm/adapter.ts +2 -2
  34. package/lib/adapters/street/repetition.ts +87 -0
  35. package/lib/adapters/street/unit.ts +14 -0
  36. package/lib/adapters/wof/admin/json/adapter.ts +34 -21
  37. package/lib/adapters/wof/admin/json/dual-role.ts +274 -0
  38. package/lib/adapters/wof/admin/json/explore.ts +4 -4
  39. package/lib/adapters/wof/admin/json/region-codes.ts +74 -3
  40. package/lib/adapters/wof/json-rows.ts +36 -21
  41. package/lib/adapters/wof/postalcode-json/adapter.ts +10 -28
  42. package/lib/adapters/wof/sample.ts +15 -3
  43. package/lib/adapters/wof/stored-records.ts +162 -0
  44. package/lib/adapters.ts +18 -6
  45. package/lib/au/adapters/acnc-register/adapter.ts +40 -0
  46. package/lib/au/evaluation/acnc-register.ts +144 -0
  47. package/lib/build/align/range.ts +9 -0
  48. package/lib/build/align/totals.ts +23 -0
  49. package/lib/build/align.ts +74 -4
  50. package/lib/build/budget.ts +11 -45
  51. package/lib/build/checkpoint.ts +11 -0
  52. package/lib/build/component-check.ts +99 -3
  53. package/lib/build/manifest.ts +4 -0
  54. package/lib/build/parallel.ts +8 -34
  55. package/lib/build/region-names.ts +609 -0
  56. package/lib/build/source-record.ts +7 -0
  57. package/lib/build/types.ts +20 -0
  58. package/lib/build/worker.ts +3 -1
  59. package/lib/build.ts +3 -6
  60. package/lib/de/recipes/locale.ts +3 -2
  61. package/lib/env.ts +0 -6
  62. package/lib/es/adapters/sanidad-centros-ap/adapter.ts +40 -0
  63. package/lib/es/evaluation/sanidad-centros-ap.ts +164 -0
  64. package/lib/evaluation/corpus-adapter.ts +153 -0
  65. package/lib/evaluation/source.ts +209 -0
  66. package/lib/evaluation/sources.ts +25 -0
  67. package/lib/fr/adapters/laposte-poincont/adapter.ts +381 -0
  68. package/lib/fr/recipes/unit-line-guideline.ts +9 -4
  69. package/lib/fr/structures/laposte.ts +139 -0
  70. package/lib/fr/tools/fetch/laposte-poincont.ts +234 -0
  71. package/lib/it/adapters/miur-scuole/adapter.ts +39 -0
  72. package/lib/it/evaluation/miur-scuole.ts +125 -0
  73. package/lib/jp/adapters/wof-admin/adapter.ts +4 -4
  74. package/lib/nc/adapters/opt-nc/adapter.ts +237 -0
  75. package/lib/nc/structures/opt-nc.ts +101 -0
  76. package/lib/nc/tools/fetch/opt-nc.ts +249 -0
  77. package/lib/nz/adapters/charities-register/adapter.ts +40 -0
  78. package/lib/nz/evaluation/charities-register.ts +149 -0
  79. package/lib/recipes/bare/postcode.ts +14 -11
  80. package/lib/recipes/deepseek/batches.ts +8 -8
  81. package/lib/recipes/po/box/cedex/recipe.ts +12 -1
  82. package/lib/recipes/po/box/cedex/us.ts +43 -2
  83. package/lib/recipes/scaffold.ts +1 -1
  84. package/lib/recipes/sources.ts +16 -0
  85. package/lib/recipes.ts +2 -0
  86. package/lib/registers.ts +28 -2
  87. package/lib/{post-offices → structures}/source.ts +49 -16
  88. package/lib/structures/sources.ts +26 -0
  89. package/lib/synthesizers/house-venue.ts +11 -6
  90. package/lib/synthesizers/tail.ts +27 -3
  91. package/lib/tools/audit.ts +1 -1
  92. package/lib/tools/fetch/storage.ts +18 -4
  93. package/lib/tools/fetch.ts +10 -2
  94. package/lib/tools/overlay/kryptonite.ts +6 -2
  95. package/lib/tools/overlay/split/attribute.ts +14 -2
  96. package/lib/tools/overlay/split/overlay.ts +36 -7
  97. package/lib/tools/postcode/triples.ts +3 -3
  98. package/lib/tools/region-slot-census.run.ts +184 -0
  99. package/lib/training/config/load.ts +19 -0
  100. package/lib/training/config/schema.ts +6 -0
  101. package/lib/tw/registers.ts +15 -4
  102. package/lib/tw/structures/chunghwa-post.ts +191 -0
  103. package/lib/tw/tools/fetch/chunghwa-post.ts +174 -0
  104. package/lib/us/adapters/state/ny-notaries/adapter.ts +7 -2
  105. package/lib/us/adapters/state/tx-notaries/adapter.ts +126 -21
  106. package/lib/us/adapters/usgov/nad/adapter.ts +111 -31
  107. package/lib/us/adapters/usgov/nppes/adapter.ts +8 -11
  108. package/lib/us/adapters/usgov/{usgs-post-offices → usgs-structures}/adapter.ts +65 -35
  109. package/lib/us/recipes/county.ts +474 -0
  110. package/lib/us/street-unit.ts +15 -5
  111. package/lib/us/structures/usgs/layers.ts +171 -0
  112. package/lib/us/structures/usgs/national-map.ts +95 -0
  113. package/lib/us/tools/fetch/usgs/gnis.ts +288 -0
  114. package/lib/us/tools/fetch/usgs/structures.ts +362 -0
  115. package/lib/utils/align.ts +2 -2
  116. package/lib/utils/evaluation-groups.ts +172 -0
  117. package/lib/utils/split.ts +40 -4
  118. package/lib/utils/tokenize.ts +7 -7
  119. package/lib/utils/validation-groups.ts +292 -0
  120. package/lib/utils/wof-json.ts +96 -134
  121. package/out/adapters/cordis/adapter.d.ts +3 -1
  122. package/out/adapters/cordis/adapter.d.ts.map +1 -1
  123. package/out/adapters/cordis/adapter.js +8 -2
  124. package/out/adapters/cordis/adapter.js.map +1 -1
  125. package/out/adapters/gleif/adapter.d.ts.map +1 -1
  126. package/out/adapters/gleif/adapter.js +19 -5
  127. package/out/adapters/gleif/adapter.js.map +1 -1
  128. package/out/adapters/osm/adapter.js +2 -2
  129. package/out/adapters/osm/adapter.js.map +1 -1
  130. package/out/adapters/street/repetition.d.ts +19 -0
  131. package/out/adapters/street/repetition.d.ts.map +1 -0
  132. package/out/adapters/street/repetition.js +71 -0
  133. package/out/adapters/street/repetition.js.map +1 -0
  134. package/out/adapters/street/unit.d.ts +10 -0
  135. package/out/adapters/street/unit.d.ts.map +1 -1
  136. package/out/adapters/street/unit.js +6 -0
  137. package/out/adapters/street/unit.js.map +1 -1
  138. package/out/adapters/wof/admin/json/adapter.d.ts +8 -0
  139. package/out/adapters/wof/admin/json/adapter.d.ts.map +1 -1
  140. package/out/adapters/wof/admin/json/adapter.js +26 -22
  141. package/out/adapters/wof/admin/json/adapter.js.map +1 -1
  142. package/out/adapters/wof/admin/json/dual-role.d.ts +83 -0
  143. package/out/adapters/wof/admin/json/dual-role.d.ts.map +1 -0
  144. package/out/adapters/wof/admin/json/dual-role.js +183 -0
  145. package/out/adapters/wof/admin/json/dual-role.js.map +1 -0
  146. package/out/adapters/wof/admin/json/explore.d.ts.map +1 -1
  147. package/out/adapters/wof/admin/json/explore.js +4 -4
  148. package/out/adapters/wof/admin/json/explore.js.map +1 -1
  149. package/out/adapters/wof/admin/json/region-codes.d.ts +16 -0
  150. package/out/adapters/wof/admin/json/region-codes.d.ts.map +1 -1
  151. package/out/adapters/wof/admin/json/region-codes.js +62 -3
  152. package/out/adapters/wof/admin/json/region-codes.js.map +1 -1
  153. package/out/adapters/wof/json-rows.d.ts +28 -6
  154. package/out/adapters/wof/json-rows.d.ts.map +1 -1
  155. package/out/adapters/wof/json-rows.js +9 -15
  156. package/out/adapters/wof/json-rows.js.map +1 -1
  157. package/out/adapters/wof/postalcode-json/adapter.d.ts +1 -1
  158. package/out/adapters/wof/postalcode-json/adapter.d.ts.map +1 -1
  159. package/out/adapters/wof/postalcode-json/adapter.js +9 -26
  160. package/out/adapters/wof/postalcode-json/adapter.js.map +1 -1
  161. package/out/adapters/wof/sample.d.ts +8 -1
  162. package/out/adapters/wof/sample.d.ts.map +1 -1
  163. package/out/adapters/wof/sample.js +12 -4
  164. package/out/adapters/wof/sample.js.map +1 -1
  165. package/out/adapters/wof/stored-records.d.ts +62 -0
  166. package/out/adapters/wof/stored-records.d.ts.map +1 -0
  167. package/out/adapters/wof/stored-records.js +91 -0
  168. package/out/adapters/wof/stored-records.js.map +1 -0
  169. package/out/adapters.d.ts +1 -1
  170. package/out/adapters.d.ts.map +1 -1
  171. package/out/adapters.js +15 -3
  172. package/out/adapters.js.map +1 -1
  173. package/out/au/adapters/acnc-register/adapter.d.ts +9 -0
  174. package/out/au/adapters/acnc-register/adapter.d.ts.map +1 -0
  175. package/out/au/adapters/acnc-register/adapter.js +37 -0
  176. package/out/au/adapters/acnc-register/adapter.js.map +1 -0
  177. package/out/au/evaluation/acnc-register.d.ts +56 -0
  178. package/out/au/evaluation/acnc-register.d.ts.map +1 -0
  179. package/out/au/evaluation/acnc-register.js +84 -0
  180. package/out/au/evaluation/acnc-register.js.map +1 -0
  181. package/out/build/align/range.d.ts.map +1 -1
  182. package/out/build/align/range.js +9 -0
  183. package/out/build/align/range.js.map +1 -1
  184. package/out/build/align/totals.d.ts +5 -0
  185. package/out/build/align/totals.d.ts.map +1 -1
  186. package/out/build/align/totals.js +15 -0
  187. package/out/build/align/totals.js.map +1 -1
  188. package/out/build/align.d.ts +26 -0
  189. package/out/build/align.d.ts.map +1 -1
  190. package/out/build/align.js +35 -2
  191. package/out/build/align.js.map +1 -1
  192. package/out/build/budget.d.ts +9 -26
  193. package/out/build/budget.d.ts.map +1 -1
  194. package/out/build/budget.js +10 -31
  195. package/out/build/budget.js.map +1 -1
  196. package/out/build/checkpoint.d.ts +11 -0
  197. package/out/build/checkpoint.d.ts.map +1 -1
  198. package/out/build/checkpoint.js.map +1 -1
  199. package/out/build/component-check.d.ts +55 -1
  200. package/out/build/component-check.d.ts.map +1 -1
  201. package/out/build/component-check.js +65 -3
  202. package/out/build/component-check.js.map +1 -1
  203. package/out/build/manifest.d.ts.map +1 -1
  204. package/out/build/manifest.js +4 -0
  205. package/out/build/manifest.js.map +1 -1
  206. package/out/build/parallel.d.ts.map +1 -1
  207. package/out/build/parallel.js +8 -24
  208. package/out/build/parallel.js.map +1 -1
  209. package/out/build/region-names.d.ts +120 -0
  210. package/out/build/region-names.d.ts.map +1 -0
  211. package/out/build/region-names.js +430 -0
  212. package/out/build/region-names.js.map +1 -0
  213. package/out/build/source-record.d.ts.map +1 -1
  214. package/out/build/source-record.js +4 -0
  215. package/out/build/source-record.js.map +1 -1
  216. package/out/build/types.d.ts +20 -0
  217. package/out/build/types.d.ts.map +1 -1
  218. package/out/build/worker.js +3 -1
  219. package/out/build/worker.js.map +1 -1
  220. package/out/build.d.ts +0 -1
  221. package/out/build.d.ts.map +1 -1
  222. package/out/build.js +3 -5
  223. package/out/build.js.map +1 -1
  224. package/out/de/recipes/locale.d.ts.map +1 -1
  225. package/out/de/recipes/locale.js +3 -2
  226. package/out/de/recipes/locale.js.map +1 -1
  227. package/out/env.d.ts +0 -2
  228. package/out/env.d.ts.map +1 -1
  229. package/out/env.js +0 -5
  230. package/out/env.js.map +1 -1
  231. package/out/es/adapters/sanidad-centros-ap/adapter.d.ts +9 -0
  232. package/out/es/adapters/sanidad-centros-ap/adapter.d.ts.map +1 -0
  233. package/out/es/adapters/sanidad-centros-ap/adapter.js +36 -0
  234. package/out/es/adapters/sanidad-centros-ap/adapter.js.map +1 -0
  235. package/out/es/evaluation/sanidad-centros-ap.d.ts +34 -0
  236. package/out/es/evaluation/sanidad-centros-ap.d.ts.map +1 -0
  237. package/out/es/evaluation/sanidad-centros-ap.js +128 -0
  238. package/out/es/evaluation/sanidad-centros-ap.js.map +1 -0
  239. package/out/evaluation/corpus-adapter.d.ts +60 -0
  240. package/out/evaluation/corpus-adapter.d.ts.map +1 -0
  241. package/out/evaluation/corpus-adapter.js +100 -0
  242. package/out/evaluation/corpus-adapter.js.map +1 -0
  243. package/out/evaluation/source.d.ts +134 -0
  244. package/out/evaluation/source.d.ts.map +1 -0
  245. package/out/evaluation/source.js +80 -0
  246. package/out/evaluation/source.js.map +1 -0
  247. package/out/evaluation/sources.d.ts +6 -0
  248. package/out/evaluation/sources.d.ts.map +1 -0
  249. package/out/evaluation/sources.js +22 -0
  250. package/out/evaluation/sources.js.map +1 -0
  251. package/out/fr/adapters/laposte-poincont/adapter.d.ts +110 -0
  252. package/out/fr/adapters/laposte-poincont/adapter.d.ts.map +1 -0
  253. package/out/fr/adapters/laposte-poincont/adapter.js +315 -0
  254. package/out/fr/adapters/laposte-poincont/adapter.js.map +1 -0
  255. package/out/fr/recipes/unit-line-guideline.d.ts.map +1 -1
  256. package/out/fr/recipes/unit-line-guideline.js +8 -4
  257. package/out/fr/recipes/unit-line-guideline.js.map +1 -1
  258. package/out/fr/structures/laposte.d.ts +27 -0
  259. package/out/fr/structures/laposte.d.ts.map +1 -0
  260. package/out/fr/structures/laposte.js +96 -0
  261. package/out/fr/structures/laposte.js.map +1 -0
  262. package/out/fr/tools/fetch/laposte-poincont.d.ts +95 -0
  263. package/out/fr/tools/fetch/laposte-poincont.d.ts.map +1 -0
  264. package/out/fr/tools/fetch/laposte-poincont.js +135 -0
  265. package/out/fr/tools/fetch/laposte-poincont.js.map +1 -0
  266. package/out/it/adapters/miur-scuole/adapter.d.ts +9 -0
  267. package/out/it/adapters/miur-scuole/adapter.d.ts.map +1 -0
  268. package/out/it/adapters/miur-scuole/adapter.js +35 -0
  269. package/out/it/adapters/miur-scuole/adapter.js.map +1 -0
  270. package/out/it/evaluation/miur-scuole.d.ts +53 -0
  271. package/out/it/evaluation/miur-scuole.d.ts.map +1 -0
  272. package/out/it/evaluation/miur-scuole.js +68 -0
  273. package/out/it/evaluation/miur-scuole.js.map +1 -0
  274. package/out/jp/adapters/wof-admin/adapter.js +4 -4
  275. package/out/nc/adapters/opt-nc/adapter.d.ts +84 -0
  276. package/out/nc/adapters/opt-nc/adapter.d.ts.map +1 -0
  277. package/out/nc/adapters/opt-nc/adapter.js +173 -0
  278. package/out/nc/adapters/opt-nc/adapter.js.map +1 -0
  279. package/out/nc/structures/opt-nc.d.ts +12 -0
  280. package/out/nc/structures/opt-nc.d.ts.map +1 -0
  281. package/out/nc/structures/opt-nc.js +64 -0
  282. package/out/nc/structures/opt-nc.js.map +1 -0
  283. package/out/nc/tools/fetch/opt-nc.d.ts +81 -0
  284. package/out/nc/tools/fetch/opt-nc.d.ts.map +1 -0
  285. package/out/nc/tools/fetch/opt-nc.js +162 -0
  286. package/out/nc/tools/fetch/opt-nc.js.map +1 -0
  287. package/out/nz/adapters/charities-register/adapter.d.ts +9 -0
  288. package/out/nz/adapters/charities-register/adapter.d.ts.map +1 -0
  289. package/out/nz/adapters/charities-register/adapter.js +36 -0
  290. package/out/nz/adapters/charities-register/adapter.js.map +1 -0
  291. package/out/nz/evaluation/charities-register.d.ts +60 -0
  292. package/out/nz/evaluation/charities-register.d.ts.map +1 -0
  293. package/out/nz/evaluation/charities-register.js +84 -0
  294. package/out/nz/evaluation/charities-register.js.map +1 -0
  295. package/out/recipes/bare/postcode.d.ts +2 -1
  296. package/out/recipes/bare/postcode.d.ts.map +1 -1
  297. package/out/recipes/bare/postcode.js +14 -11
  298. package/out/recipes/bare/postcode.js.map +1 -1
  299. package/out/recipes/deepseek/batches.d.ts +11 -1
  300. package/out/recipes/deepseek/batches.d.ts.map +1 -1
  301. package/out/recipes/deepseek/batches.js +8 -7
  302. package/out/recipes/deepseek/batches.js.map +1 -1
  303. package/out/recipes/po/box/cedex/recipe.d.ts.map +1 -1
  304. package/out/recipes/po/box/cedex/recipe.js +12 -1
  305. package/out/recipes/po/box/cedex/recipe.js.map +1 -1
  306. package/out/recipes/po/box/cedex/us.d.ts +12 -0
  307. package/out/recipes/po/box/cedex/us.d.ts.map +1 -1
  308. package/out/recipes/po/box/cedex/us.js +36 -2
  309. package/out/recipes/po/box/cedex/us.js.map +1 -1
  310. package/out/recipes/scaffold.d.ts +1 -1
  311. package/out/recipes/sources.d.ts.map +1 -1
  312. package/out/recipes/sources.js +16 -0
  313. package/out/recipes/sources.js.map +1 -1
  314. package/out/recipes.d.ts.map +1 -1
  315. package/out/recipes.js +2 -0
  316. package/out/recipes.js.map +1 -1
  317. package/out/registers.d.ts +28 -2
  318. package/out/registers.d.ts.map +1 -1
  319. package/out/registers.js +28 -2
  320. package/out/registers.js.map +1 -1
  321. package/out/{post-offices → structures}/source.d.ts +34 -16
  322. package/out/structures/source.d.ts.map +1 -0
  323. package/out/structures/source.js +39 -0
  324. package/out/structures/source.js.map +1 -0
  325. package/out/structures/sources.d.ts +6 -0
  326. package/out/structures/sources.d.ts.map +1 -0
  327. package/out/structures/sources.js +23 -0
  328. package/out/structures/sources.js.map +1 -0
  329. package/out/synthesizers/house-venue.d.ts.map +1 -1
  330. package/out/synthesizers/house-venue.js +2 -1
  331. package/out/synthesizers/house-venue.js.map +1 -1
  332. package/out/synthesizers/tail.d.ts +15 -1
  333. package/out/synthesizers/tail.d.ts.map +1 -1
  334. package/out/synthesizers/tail.js +8 -3
  335. package/out/synthesizers/tail.js.map +1 -1
  336. package/out/tools/audit.js +1 -1
  337. package/out/tools/audit.js.map +1 -1
  338. package/out/tools/fetch/storage.d.ts +10 -2
  339. package/out/tools/fetch/storage.d.ts.map +1 -1
  340. package/out/tools/fetch/storage.js +18 -4
  341. package/out/tools/fetch/storage.js.map +1 -1
  342. package/out/tools/fetch.d.ts +10 -2
  343. package/out/tools/fetch.d.ts.map +1 -1
  344. package/out/tools/fetch.js +10 -2
  345. package/out/tools/fetch.js.map +1 -1
  346. package/out/tools/overlay/kryptonite.d.ts.map +1 -1
  347. package/out/tools/overlay/kryptonite.js +5 -2
  348. package/out/tools/overlay/kryptonite.js.map +1 -1
  349. package/out/tools/overlay/split/attribute.d.ts +7 -1
  350. package/out/tools/overlay/split/attribute.d.ts.map +1 -1
  351. package/out/tools/overlay/split/attribute.js +4 -1
  352. package/out/tools/overlay/split/attribute.js.map +1 -1
  353. package/out/tools/overlay/split/overlay.d.ts +10 -0
  354. package/out/tools/overlay/split/overlay.d.ts.map +1 -1
  355. package/out/tools/overlay/split/overlay.js +17 -4
  356. package/out/tools/overlay/split/overlay.js.map +1 -1
  357. package/out/tools/postcode/triples.js +3 -3
  358. package/out/tools/postcode/triples.js.map +1 -1
  359. package/out/tools/region-slot-census.run.d.ts +2 -0
  360. package/out/tools/region-slot-census.run.d.ts.map +1 -0
  361. package/out/tools/region-slot-census.run.js +134 -0
  362. package/out/tools/region-slot-census.run.js.map +1 -0
  363. package/out/training/config/load.d.ts.map +1 -1
  364. package/out/training/config/load.js +10 -0
  365. package/out/training/config/load.js.map +1 -1
  366. package/out/training/config/schema.d.ts +13 -0
  367. package/out/training/config/schema.d.ts.map +1 -1
  368. package/out/training/config/schema.js +6 -0
  369. package/out/training/config/schema.js.map +1 -1
  370. package/out/tw/registers.d.ts +9 -0
  371. package/out/tw/registers.d.ts.map +1 -1
  372. package/out/tw/registers.js +11 -4
  373. package/out/tw/registers.js.map +1 -1
  374. package/out/tw/structures/chunghwa-post.d.ts +39 -0
  375. package/out/tw/structures/chunghwa-post.d.ts.map +1 -0
  376. package/out/tw/structures/chunghwa-post.js +150 -0
  377. package/out/tw/structures/chunghwa-post.js.map +1 -0
  378. package/out/tw/tools/fetch/chunghwa-post.d.ts +55 -0
  379. package/out/tw/tools/fetch/chunghwa-post.d.ts.map +1 -0
  380. package/out/tw/tools/fetch/chunghwa-post.js +110 -0
  381. package/out/tw/tools/fetch/chunghwa-post.js.map +1 -0
  382. package/out/us/adapters/state/ny-notaries/adapter.d.ts.map +1 -1
  383. package/out/us/adapters/state/ny-notaries/adapter.js +7 -2
  384. package/out/us/adapters/state/ny-notaries/adapter.js.map +1 -1
  385. package/out/us/adapters/state/tx-notaries/adapter.d.ts +42 -0
  386. package/out/us/adapters/state/tx-notaries/adapter.d.ts.map +1 -1
  387. package/out/us/adapters/state/tx-notaries/adapter.js +92 -19
  388. package/out/us/adapters/state/tx-notaries/adapter.js.map +1 -1
  389. package/out/us/adapters/usgov/nad/adapter.d.ts +35 -2
  390. package/out/us/adapters/usgov/nad/adapter.d.ts.map +1 -1
  391. package/out/us/adapters/usgov/nad/adapter.js +90 -27
  392. package/out/us/adapters/usgov/nad/adapter.js.map +1 -1
  393. package/out/us/adapters/usgov/nppes/adapter.d.ts.map +1 -1
  394. package/out/us/adapters/usgov/nppes/adapter.js +8 -11
  395. package/out/us/adapters/usgov/nppes/adapter.js.map +1 -1
  396. package/out/us/adapters/usgov/usgs-structures/adapter.d.ts +87 -0
  397. package/out/us/adapters/usgov/usgs-structures/adapter.d.ts.map +1 -0
  398. package/out/us/adapters/usgov/{usgs-post-offices → usgs-structures}/adapter.js +36 -27
  399. package/out/us/adapters/usgov/usgs-structures/adapter.js.map +1 -0
  400. package/out/us/recipes/county.d.ts +124 -0
  401. package/out/us/recipes/county.d.ts.map +1 -0
  402. package/out/us/recipes/county.js +295 -0
  403. package/out/us/recipes/county.js.map +1 -0
  404. package/out/us/street-unit.d.ts +10 -3
  405. package/out/us/street-unit.d.ts.map +1 -1
  406. package/out/us/street-unit.js +14 -6
  407. package/out/us/street-unit.js.map +1 -1
  408. package/out/us/structures/usgs/layers.d.ts +57 -0
  409. package/out/us/structures/usgs/layers.d.ts.map +1 -0
  410. package/out/us/structures/usgs/layers.js +121 -0
  411. package/out/us/structures/usgs/layers.js.map +1 -0
  412. package/out/us/structures/usgs/national-map.d.ts +14 -0
  413. package/out/us/structures/usgs/national-map.d.ts.map +1 -0
  414. package/out/us/{post-offices/usgs-national-map.js → structures/usgs/national-map.js} +16 -14
  415. package/out/us/structures/usgs/national-map.js.map +1 -0
  416. package/out/us/tools/fetch/usgs/gnis.d.ts +118 -0
  417. package/out/us/tools/fetch/usgs/gnis.d.ts.map +1 -0
  418. package/out/us/tools/fetch/usgs/gnis.js +177 -0
  419. package/out/us/tools/fetch/usgs/gnis.js.map +1 -0
  420. package/out/us/tools/fetch/usgs/structures.d.ts +99 -0
  421. package/out/us/tools/fetch/usgs/structures.d.ts.map +1 -0
  422. package/out/us/tools/fetch/usgs/structures.js +233 -0
  423. package/out/us/tools/fetch/usgs/structures.js.map +1 -0
  424. package/out/utils/align.js.map +1 -1
  425. package/out/utils/evaluation-groups.d.ts +98 -0
  426. package/out/utils/evaluation-groups.d.ts.map +1 -0
  427. package/out/utils/evaluation-groups.js +92 -0
  428. package/out/utils/evaluation-groups.js.map +1 -0
  429. package/out/utils/split.d.ts +26 -1
  430. package/out/utils/split.d.ts.map +1 -1
  431. package/out/utils/split.js +28 -3
  432. package/out/utils/split.js.map +1 -1
  433. package/out/utils/tokenize.d.ts +3 -3
  434. package/out/utils/tokenize.d.ts.map +1 -1
  435. package/out/utils/tokenize.js.map +1 -1
  436. package/out/utils/validation-groups.d.ts +168 -0
  437. package/out/utils/validation-groups.d.ts.map +1 -0
  438. package/out/utils/validation-groups.js +135 -0
  439. package/out/utils/validation-groups.js.map +1 -0
  440. package/out/utils/wof-json.d.ts +31 -57
  441. package/out/utils/wof-json.d.ts.map +1 -1
  442. package/out/utils/wof-json.js +81 -98
  443. package/out/utils/wof-json.js.map +1 -1
  444. package/package.json +18 -18
  445. package/lib/adapters/wof/record-cache.ts +0 -118
  446. package/lib/build/heap.ts +0 -60
  447. package/lib/post-offices/sources.ts +0 -18
  448. package/lib/us/post-offices/usgs-national-map.ts +0 -89
  449. package/lib/us/tools/fetch/usgs-post-offices.ts +0 -362
  450. package/out/adapters/wof/record-cache.d.ts +0 -22
  451. package/out/adapters/wof/record-cache.d.ts.map +0 -1
  452. package/out/adapters/wof/record-cache.js +0 -89
  453. package/out/adapters/wof/record-cache.js.map +0 -1
  454. package/out/build/heap.d.ts +0 -26
  455. package/out/build/heap.d.ts.map +0 -1
  456. package/out/build/heap.js +0 -52
  457. package/out/build/heap.js.map +0 -1
  458. package/out/post-offices/source.d.ts.map +0 -1
  459. package/out/post-offices/source.js +0 -21
  460. package/out/post-offices/source.js.map +0 -1
  461. package/out/post-offices/sources.d.ts +0 -13
  462. package/out/post-offices/sources.d.ts.map +0 -1
  463. package/out/post-offices/sources.js +0 -8
  464. package/out/post-offices/sources.js.map +0 -1
  465. package/out/us/adapters/usgov/usgs-post-offices/adapter.d.ts +0 -66
  466. package/out/us/adapters/usgov/usgs-post-offices/adapter.d.ts.map +0 -1
  467. package/out/us/adapters/usgov/usgs-post-offices/adapter.js.map +0 -1
  468. package/out/us/post-offices/usgs-national-map.d.ts +0 -14
  469. package/out/us/post-offices/usgs-national-map.d.ts.map +0 -1
  470. package/out/us/post-offices/usgs-national-map.js.map +0 -1
  471. package/out/us/tools/fetch/usgs-post-offices.d.ts +0 -99
  472. package/out/us/tools/fetch/usgs-post-offices.d.ts.map +0 -1
  473. package/out/us/tools/fetch/usgs-post-offices.js +0 -246
  474. package/out/us/tools/fetch/usgs-post-offices.js.map +0 -1
@@ -12,6 +12,7 @@ import { writeLocalJSONFile } from "@mailwoman/core/fs/writers"
12
12
  import type { PathBuilder } from "path-ts"
13
13
 
14
14
  import type { AlignTotals } from "#build/align/totals"
15
+ import { componentFindingsRecord, ENFORCED_COMPONENT_RULES } from "#build/component-check"
15
16
  import type { HoldoutCheckRecord } from "#build/holdout-check"
16
17
  import { TRAINING_MANIFEST_FILE, type BuildCorpusManifest, type BuildProfile, type BuildStage } from "#build/types"
17
18
  import type { ParquetManifest } from "#parquet/writers"
@@ -101,6 +102,9 @@ export async function writeBuildManifests(inputs: BuildManifestInputs): Promise<
101
102
  files_read: inputs.holdoutCheck.files_read,
102
103
  },
103
104
  quarantine_count: totals.quarantined,
105
+ component_findings_by_source: componentFindingsRecord(totals.componentFindingsBySource),
106
+ enforced_component_rules: [...ENFORCED_COMPONENT_RULES],
107
+ unchecked_component_rules: [...totals.uncheckedComponentRules],
104
108
  total_aligned_rows: totals.aligned,
105
109
  licenses: Object.fromEntries(licenseSummary),
106
110
  licenses_cover:
@@ -39,13 +39,7 @@ import { adapterRunInputs, reusableRun } from "#build/adapters"
39
39
  import { alignSettings } from "#build/align"
40
40
  import { planAlignRanges, reusableAlignRange, type AlignRangePlan } from "#build/align/range"
41
41
  import { mergeAlignTotals } from "#build/align/totals"
42
- import {
43
- adapterWorkerClass,
44
- WORKER_CLASSES,
45
- workerReservationBytes,
46
- type WorkerBudget,
47
- type WorkerClass,
48
- } from "#build/budget"
42
+ import { WORKER_CLASSES, workerReservationBytes, type WorkerBudget, type WorkerClass } from "#build/budget"
49
43
  import { readSourceDecisionTable } from "#build/eligibility"
50
44
  import { admitParquetManifest, UNCHECKED_PARQUET_MANIFEST_FILE } from "#build/holdout-check"
51
45
  import { writeBuildManifests } from "#build/manifest"
@@ -214,40 +208,20 @@ class WorkerPool {
214
208
  #admits(pending: PendingTask): boolean {
215
209
  if (this.#running.size >= this.#jobs) return false
216
210
 
217
- if (this.#reservedBytes() + workerReservationBytes(pending.workerClass) > this.#budgetBytes) return false
218
-
219
- if (pending.workerClass === "resident") {
220
- for (const { workerClass } of this.#running.values()) {
221
- if (workerClass === "resident") return false
222
- }
223
- }
224
-
225
- return true
211
+ return this.#reservedBytes() + workerReservationBytes(pending.workerClass) <= this.#budgetBytes
226
212
  }
227
213
 
228
214
  #dispatch(): void {
229
215
  if (this.#cancelled) return
230
216
 
231
217
  // Highest priority first.
232
- // A task that doesn't fit waits rather than letting a smaller one pass it, except that
233
- // a resident adapter waiting on another resident adapter doesn't hold up standard work.
218
+ // A task that doesn't fit waits rather than letting a smaller one pass it.
234
219
  const ordered = this.#queue.toSorted((a, b) => b.priority[0] - a.priority[0] || b.priority[1] - a.priority[1])
235
220
 
236
221
  for (const pending of ordered) {
237
- if (this.#running.size >= this.#jobs) return
238
-
239
- if (this.#admits(pending)) {
240
- this.#queue.splice(this.#queue.indexOf(pending), 1)
241
- void this.#start(pending)
242
-
243
- continue
244
- }
245
-
246
- const blockedOnResident =
247
- pending.workerClass === "resident" &&
248
- [...this.#running.values()].some((running) => running.workerClass === "resident")
249
-
250
- if (!blockedOnResident) return
222
+ if (this.#running.size >= this.#jobs || !this.#admits(pending)) return
223
+ this.#queue.splice(this.#queue.indexOf(pending), 1)
224
+ void this.#start(pending)
251
225
  }
252
226
  }
253
227
 
@@ -491,7 +465,7 @@ export async function runParallelBuild(ctx: ParallelBuildContext): Promise<Build
491
465
  kind: "adapter",
492
466
  id: `${source}/adapter`,
493
467
  source,
494
- resources: WORKER_CLASSES[adapterWorkerClass(source)],
468
+ resources: WORKER_CLASSES.standard,
495
469
  temporaryRoot: temporaryRoot("adapter"),
496
470
  adapterModule: derivationFor.modules.get(source)!,
497
471
  adapterOptions: { ...serializable, inputPath: options.inputPath.toString() },
@@ -499,7 +473,7 @@ export async function runParallelBuild(ctx: ParallelBuildContext): Promise<Build
499
473
  inputs,
500
474
  intermediateDir: intermediateDir.toString(),
501
475
  },
502
- adapterWorkerClass(source),
476
+ "standard",
503
477
  inputWeight
504
478
  )
505
479
 
@@ -0,0 +1,609 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * @file Every country's first-level division names, abbreviations and ISO 3166-2 codes from the WOF
6
+ * admin database and libaddressinput's subdivision keys, for the component check's `region:unknown` rule.
7
+ */
8
+
9
+ import { dependentTerritories, hasCountry } from "@mailwoman/codex/country/sets"
10
+ import { dataRootPath } from "@mailwoman/core/data-root"
11
+ import { readLocalJSONFile } from "@mailwoman/core/fs/readers"
12
+ import { pathExists } from "@mailwoman/core/fs/readers/stat"
13
+ import { resolveModulePath } from "@mailwoman/core/module/resolvers"
14
+ import { isPresent } from "@mailwoman/core/objects"
15
+ import { wofDatabasePath } from "@mailwoman/resolver-wof-sqlite/paths"
16
+ import { regionKeys } from "@mailwoman/resolver-wof-sqlite/region-keys"
17
+ import type { WOFDatabase } from "@mailwoman/resolver-wof-sqlite/schema"
18
+ import {
19
+ normalizeLocalityForKey,
20
+ withArabicLetterVariantsFolded,
21
+ type NameKey,
22
+ } from "@mailwoman/resolver-wof-sqlite/street/normalize"
23
+ import { DatabaseClient } from "@mailwoman/sqlite/client"
24
+ import { PathBuilder, type PathBuilderLike } from "path-ts"
25
+
26
+ import { loadAdmin1 } from "#adapters/geonames/adapter"
27
+ import { subdivisionWrittenForms } from "#adapters/wof/admin/json/region-codes"
28
+ import { codeCountry, SUBDIVISION_OF_SOURCES } from "#utils/wof-json"
29
+
30
+ /**
31
+ * The WOF placetypes a row's `region` may name: a region, or the macroregion above it where a country's
32
+ * addresses write that level (England, Scotland, Wales and Northern Ireland are GB macroregions).
33
+ */
34
+ const REGION_PLACETYPES = ["region", "macroregion"] as const
35
+
36
+ /**
37
+ * The WOF placetype of the second-level division a country's rows may write in
38
+ * the region slot, such as a Peruvian province.
39
+ */
40
+ const SECOND_LEVEL_PLACETYPES = ["county"] as const
41
+
42
+ /**
43
+ * The WOF concordances that hold a region's current ISO 3166-2 code, such as `CH-ZH`.
44
+ */
45
+ const ISO_SUBDIVISION_SOURCES = ["iso:id", "iso:code"] as const
46
+
47
+ /**
48
+ * The committed table of the countries whose rows write a second-level name in the region slot.
49
+ */
50
+ const SECOND_LEVEL_REGION_TABLE_PATH = resolveModulePath("@mailwoman/corpus/data/region-slot-second-level.json")
51
+
52
+ /**
53
+ * One country's counts in {@link SECOND_LEVEL_REGION_TABLE_PATH}.
54
+ */
55
+ interface SecondLevelRegionCount {
56
+ /**
57
+ * Base rows of the country with a region.
58
+ */
59
+ withRegion: number
60
+ /**
61
+ * Those whose region is a second-level name of the country and no region name of it.
62
+ */
63
+ secondLevelOnly: number
64
+ }
65
+
66
+ /**
67
+ * The census {@link SECOND_LEVEL_REGION_TABLE_PATH} holds.
68
+ */
69
+ export interface SecondLevelRegionTable {
70
+ $description: string
71
+ /**
72
+ * The corpus manifest the counts were read from.
73
+ */
74
+ manifest: string
75
+ countries: Record<string, SecondLevelRegionCount>
76
+ }
77
+
78
+ /**
79
+ * The least share of a country's rows with a region that must write a second-level name there
80
+ * before the rule admits that country's second-level names.
81
+ *
82
+ * Over v0.8.10 the shares fall in two groups: CD, PE, PR and PS at 7.5 % to 76 %,
83
+ * then Mali at 3.2 % on one value and every other country at 0.5 % or less.
84
+ */
85
+ const SECOND_LEVEL_MIN_SHARE = 0.05
86
+
87
+ /**
88
+ * The fewest such rows, so a country isn't admitted on a few rows.
89
+ */
90
+ const SECOND_LEVEL_MIN_ROWS = 100
91
+
92
+ /**
93
+ * The countries whose rows write a second-level name in the region slot at
94
+ * {@link SECOND_LEVEL_MIN_SHARE} or more, in {@link SECOND_LEVEL_MIN_ROWS} rows or more.
95
+ */
96
+ export function secondLevelAdmitted(table: Pick<SecondLevelRegionTable, "countries">): string[] {
97
+ return Object.entries(table.countries)
98
+ .filter(
99
+ ([, count]) =>
100
+ count.secondLevelOnly >= SECOND_LEVEL_MIN_ROWS &&
101
+ count.secondLevelOnly >= SECOND_LEVEL_MIN_SHARE * count.withRegion
102
+ )
103
+ .map(([country]) => country)
104
+ }
105
+
106
+ /**
107
+ * The part of an ISO 3166-2 code an address writes for `country`'s subdivision, `ZH` for
108
+ * `CH-ZH`, or `null` when the code belongs to another country or its part holds no letter.
109
+ *
110
+ * A numeric part, such as Thailand's `TH-10` or Latvia's `LV-088`, is a register number
111
+ * rather than a written form, and as a region it would admit a misplaced postcode or house number.
112
+ */
113
+ export function writtenISOSubdivisionCode(country: string, isoCode: string): string | null {
114
+ const prefix = `${country.toUpperCase()}-`
115
+
116
+ if (!isoCode.toUpperCase().startsWith(prefix)) return null
117
+
118
+ const code = isoCode.slice(prefix.length)
119
+
120
+ return /\p{L}/u.test(code) ? code : null
121
+ }
122
+
123
+ /**
124
+ * A word written as its leading letters and a full stop, `nov.` for `novads` or `obl.` for `oblast`.
125
+ */
126
+ const TRUNCATED_WORD = /^(\p{L}{2,})\.$/u
127
+
128
+ /**
129
+ * The placeholder that stands for the truncated word in an abbreviation key.
130
+ */
131
+ const TRUNCATED_SLOT = "\u0001"
132
+
133
+ /**
134
+ * The folded names and abbreviations of each country's regions.
135
+ */
136
+ export interface RegionNames {
137
+ /**
138
+ * The database the names were read from.
139
+ */
140
+ path: string
141
+ /**
142
+ * Whether `region` folds to a name or abbreviation of one of `country`'s regions, or `null`
143
+ * when neither source holds a region of `country` and the value cannot be judged.
144
+ */
145
+ holds(country: string, region: string): boolean | null
146
+ }
147
+
148
+ /**
149
+ * The fold the resolver keys its region lookups with, with Arabic letter variants folded together.
150
+ */
151
+ export const foldRegionName = (name: string): string => withArabicLetterVariantsFolded(normalizeLocalityForKey(name))
152
+
153
+ /**
154
+ * A folded region name with its spaces, dashes and format characters removed,
155
+ * `nakhonphanom` for `nakhon phanom`, `bariavungtau` for `ba ria - vung tau`,
156
+ * and `ชลบุรี` for `ชลบุรี` written with a trailing zero-width space.
157
+ *
158
+ * Over the WOF admin database of 2026-11-14, 11 of 5,095 regions and macroregions share a
159
+ * compact key with another region of their country that no spaced key of theirs shares.
160
+ */
161
+ export const compactRegionKey = (key: string): string => key.replaceAll(/[\s\p{Pd}\p{Cf}]/gu, "")
162
+
163
+ const LATIN_INITIAL = /^\p{Script=Latin}/u
164
+
165
+ /**
166
+ * The fewest words a name needs before all of it collapses to initials, so no initialism
167
+ * of a whole name is two letters long and meets a two-letter subdivision code.
168
+ */
169
+ const WHOLE_NAME_INITIALISM_MIN_WORDS = 3
170
+
171
+ /**
172
+ * The compact keys a folded name of Latin words gives when a run of two or more of its
173
+ * words is written as their initials: `ntb` and `ntbarat` for `nusa tenggara barat`,
174
+ * `diyogyakarta` for `daerah istimewa yogyakarta`.
175
+ *
176
+ * The whole name collapses only when it has three or more words, so no two-letter key comes of it.
177
+ * Over the WOF admin database of 2026-11-14 the 67,031 keys this gives include 143 that
178
+ * are another country's region key, such as `slp` for San Luis Potosí.
179
+ */
180
+ export function regionInitialisms(key: string): string[] {
181
+ const words = key.split(" ")
182
+
183
+ if (words.length < 2 || !words.every((word) => LATIN_INITIAL.test(word))) return []
184
+
185
+ const initialisms: string[] = []
186
+
187
+ for (let start = 0; start < words.length; start++) {
188
+ for (let end = start + 2; end <= words.length; end++) {
189
+ if (start === 0 && end === words.length && words.length < WHOLE_NAME_INITIALISM_MIN_WORDS) continue
190
+
191
+ const initials = words
192
+ .slice(start, end)
193
+ .map((word) => word[0])
194
+ .join("")
195
+
196
+ initialisms.push(compactRegionKey([...words.slice(0, start), initials, ...words.slice(end)].join("")))
197
+ }
198
+ }
199
+
200
+ return initialisms
201
+ }
202
+
203
+ /**
204
+ * The keys a row's region value is looked up under: the resolver's region-qualifier expansion
205
+ * ({@link regionKeys}, which strips the `County` and `Co.` prefixes and the `Province` suffix
206
+ * and adds the codex subdivision names), each with Arabic letter variants folded.
207
+ */
208
+ function lookupKeys(country: string, region: string): string[] {
209
+ return [...regionKeys(region, country)].map((key) => withArabicLetterVariantsFolded(key as NameKey)).filter(isPresent)
210
+ }
211
+
212
+ /**
213
+ * The names of the places `ids` lists, from `spr`, `names` and `place_abbr`.
214
+ */
215
+ async function readNamesOfIDs(db: DatabaseClient<WOFDatabase>, ids: readonly number[]) {
216
+ if (!ids.length) return []
217
+
218
+ return db
219
+ .selectFrom("spr")
220
+ .select(["id", "name"])
221
+ .where("id", "in", ids)
222
+ .unionAll(db.selectFrom("names").select(["id", "name"]).where("id", "in", ids))
223
+ .unionAll(db.selectFrom("place_abbr").select(["id", "place_abbr.abbr as name"]).where("id", "in", ids))
224
+ .execute()
225
+ }
226
+
227
+ /**
228
+ * The `country` and name of every current place of `placetypes`, from `spr`, `names`
229
+ * and `place_abbr`, limited to `countries` when given.
230
+ */
231
+ async function readAdminNames(
232
+ db: DatabaseClient<WOFDatabase>,
233
+ placetypes: readonly string[],
234
+ countries: readonly string[] | null
235
+ ) {
236
+ if (countries && !countries.length) return []
237
+
238
+ let places = db
239
+ .selectFrom("spr")
240
+ .select(["id", "country"])
241
+ .where("placetype", "in", placetypes)
242
+ .where("is_deprecated", "=", 0)
243
+
244
+ if (countries) {
245
+ places = places.where("country", "in", countries)
246
+ }
247
+
248
+ let own = db
249
+ .selectFrom("spr")
250
+ .select(["country", "name"])
251
+ .where("placetype", "in", placetypes)
252
+ .where("is_deprecated", "=", 0)
253
+
254
+ if (countries) {
255
+ own = own.where("country", "in", countries)
256
+ }
257
+
258
+ return own
259
+ .unionAll(
260
+ db
261
+ .selectFrom("names")
262
+ .innerJoin(places.as("p"), "p.id", "names.id")
263
+ .select(["p.country as country", "names.name as name"])
264
+ )
265
+ .unionAll(
266
+ db
267
+ .selectFrom("place_abbr")
268
+ .innerJoin(places.as("p"), "p.id", "place_abbr.id")
269
+ .select(["p.country as country", "place_abbr.abbr as name"])
270
+ )
271
+ .execute()
272
+ }
273
+
274
+ /**
275
+ * Folded names keyed by upper-case country code.
276
+ */
277
+ type NamesByCountry = Map<string, Set<string>>
278
+
279
+ function addName(byCountry: NamesByCountry, rawCountry: string | null, name: string | null): void {
280
+ const country = rawCountry?.toUpperCase()
281
+ const key = name ? foldRegionName(name) : ""
282
+
283
+ if (!country || !key) return
284
+
285
+ const names = byCountry.get(country) ?? new Set<string>()
286
+
287
+ byCountry.set(country, names)
288
+ names.add(key)
289
+ }
290
+
291
+ /**
292
+ * Whether a region value is held by a country's folded names: under one of its {@link lookupKeys},
293
+ * as a slash-joined pair of names (`Cataluña/Catalunya`), or with one word truncated to a full stop.
294
+ */
295
+ function createNameMatcher(namesOf: (country: string) => Set<string> | undefined) {
296
+ const truncationsByCountry = new Map<string, Map<string, Set<string>>>()
297
+
298
+ // Each name of two or more words, keyed once per word with that word replaced by the slot,
299
+ // so `rezeknes nov` finds `rezeknes novads` under `rezeknes \u0001`.
300
+ const truncationsOf = (country: string, names: Set<string>): Map<string, Set<string>> => {
301
+ const cached = truncationsByCountry.get(country)
302
+
303
+ if (cached) return cached
304
+
305
+ const truncations = new Map<string, Set<string>>()
306
+
307
+ for (const name of names) {
308
+ const words = name.split(" ")
309
+
310
+ if (words.length < 2) continue
311
+
312
+ for (const [i, word] of words.entries()) {
313
+ const key = words.with(i, TRUNCATED_SLOT).join(" ")
314
+ const full = truncations.get(key) ?? new Set<string>()
315
+
316
+ truncations.set(key, full)
317
+ full.add(word)
318
+ }
319
+ }
320
+
321
+ truncationsByCountry.set(country, truncations)
322
+
323
+ return truncations
324
+ }
325
+
326
+ // A region written with one word truncated to its leading letters and a full stop,
327
+ // `Rēzeknes nov.` for `Rēzeknes novads`, matches a name whose other words it writes in full.
328
+ const holdsTruncation = (country: string, names: Set<string>, region: string): boolean => {
329
+ const words = region.split(/\s+/u).filter(isPresent)
330
+
331
+ if (words.length < 2) return false
332
+
333
+ for (const [i, word] of words.entries()) {
334
+ const stem = TRUNCATED_WORD.exec(word)?.[1]
335
+
336
+ if (!stem) continue
337
+
338
+ const folded = words.map((other, j) => (j === i ? TRUNCATED_SLOT : foldRegionName(other)))
339
+
340
+ if (folded.some((other) => !other)) continue
341
+
342
+ const prefix = foldRegionName(stem)
343
+ const full = truncationsOf(country, names).get(folded.join(" "))
344
+
345
+ if (full && [...full].some((candidate) => candidate.length > prefix.length && candidate.startsWith(prefix))) {
346
+ return true
347
+ }
348
+ }
349
+
350
+ return false
351
+ }
352
+
353
+ const compactByCountry = new Map<string, Set<string>>()
354
+
355
+ // Each name with its spaces and dashes removed, and with each run of two or more
356
+ // of its words written as their initials, so `NakhonPhanom` finds `nakhon phanom`
357
+ // and `DIY` finds `daerah istimewa yogyakarta`.
358
+ const compactOf = (country: string, names: Set<string>): Set<string> => {
359
+ const cached = compactByCountry.get(country)
360
+
361
+ if (cached) return cached
362
+
363
+ const compact = new Set<string>()
364
+
365
+ for (const name of names) {
366
+ compact.add(compactRegionKey(name))
367
+
368
+ for (const initialism of regionInitialisms(name)) {
369
+ compact.add(initialism)
370
+ }
371
+ }
372
+
373
+ compactByCountry.set(country, compact)
374
+
375
+ return compact
376
+ }
377
+
378
+ const holdsValue = (country: string, names: Set<string>, region: string): boolean => {
379
+ const keys = lookupKeys(country, region)
380
+
381
+ if (keys.some((key) => names.has(key)) || holdsTruncation(country, names, region)) return true
382
+
383
+ const compact = compactOf(country, names)
384
+
385
+ return keys.some((key) => {
386
+ const folded = compactRegionKey(key)
387
+
388
+ return folded.length > 0 && compact.has(folded)
389
+ })
390
+ }
391
+
392
+ return (country: string, region: string): boolean | null => {
393
+ const code = country.toUpperCase()
394
+ const names = namesOf(code)
395
+
396
+ if (!names) return null
397
+
398
+ if (holdsValue(code, names, region)) return true
399
+
400
+ // A bilingual region written as both its names, `Cataluña/Catalunya`.
401
+ const parts = region.split("/")
402
+
403
+ return parts.length > 1 && parts.every((part) => part.trim() && holdsValue(code, names, part.trim()))
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Options for {@link readRegionNames}.
409
+ */
410
+ export interface ReadRegionNamesOptions {
411
+ /**
412
+ * The countries whose second-level (WOF `county`) names a row's region may also hold.
413
+ */
414
+ secondLevelCountries?: readonly string[]
415
+ /**
416
+ * The GeoNames directory whose `admin1CodesASCII.txt` names each country's first-level divisions.
417
+ *
418
+ * Those names widen the country's set with the names GeoNames-derived rows
419
+ * write (`Munster`, `Lesser Poland`).
420
+ */
421
+ geonamesDirectory?: PathBuilderLike
422
+ }
423
+
424
+ /**
425
+ * The GeoNames file of first-level division names, under a GeoNames directory.
426
+ */
427
+ const GEONAMES_ADMIN1_FILE = "admin1CodesASCII.txt"
428
+
429
+ /**
430
+ * Reads every current region's `spr` name, `names` rows, `place_abbr` abbreviations
431
+ * and the written part of its ISO 3166-2 code, per country.
432
+ *
433
+ * A dependent territory also holds the names of the regions whose ISO or HASC code is the territory's,
434
+ * as France's region `French Guiana` (ISO `GF`) is for `GF`, and of its own WOF country record.
435
+ * Those names and the second-level names of `secondLevelCountries` widen a country
436
+ * that already holds a region, and leave a country with none unjudged.
437
+ *
438
+ * @throws When the database holds no region at all, because an empty table would leave every row unjudged.
439
+ */
440
+ export async function readRegionNames(
441
+ path: PathBuilderLike,
442
+ options: ReadRegionNamesOptions = {}
443
+ ): Promise<RegionNames> {
444
+ using db = new DatabaseClient<WOFDatabase>(path, { readOnly: true })
445
+
446
+ const regions = db
447
+ .selectFrom("spr")
448
+ .select(["id", "country"])
449
+ .where("placetype", "in", REGION_PLACETYPES)
450
+ .where("is_deprecated", "=", 0)
451
+
452
+ const rows = await readAdminNames(db, REGION_PLACETYPES, null)
453
+
454
+ const codeRows = await db
455
+ .selectFrom("concordances")
456
+ .innerJoin(regions.as("r"), "r.id", "concordances.id")
457
+ .select([
458
+ "concordances.id as id",
459
+ "r.country as country",
460
+ "concordances.other_id as code",
461
+ "concordances.other_source as source",
462
+ ])
463
+ .where("concordances.other_source", "in", SUBDIVISION_OF_SOURCES)
464
+ .execute()
465
+
466
+ const byCountry: NamesByCountry = new Map()
467
+
468
+ for (const row of rows) {
469
+ addName(byCountry, row.country, row.name)
470
+ }
471
+
472
+ if (!byCountry.size) {
473
+ throw new Error(`${path.toString()} holds no region or macroregion row, so no region value can be judged`)
474
+ }
475
+
476
+ const dependents = dependentTerritories()
477
+ // The dependent territory whose code leads each region's code, where that differs from the region's own country.
478
+ const dependencyOf = new Map<number, Set<string>>()
479
+ // Names that widen a country's set but never make a country judged on their own: a dependency's
480
+ // names from its sovereign's region or its own record, and an admitted country's second-level names.
481
+ // Saint-Martin's (`MF`) one-name country record would otherwise refuse its other spellings.
482
+ const widening: NamesByCountry = new Map()
483
+
484
+ for (const row of codeRows) {
485
+ if (!row.country) continue
486
+
487
+ if ((ISO_SUBDIVISION_SOURCES as readonly string[]).includes(row.source)) {
488
+ addName(byCountry, row.country, writtenISOSubdivisionCode(row.country, row.code))
489
+ }
490
+
491
+ const named = codeCountry(row.code)
492
+
493
+ if (!named || named === row.country.toUpperCase() || !hasCountry(dependents, named)) continue
494
+
495
+ dependencyOf.set(row.id, (dependencyOf.get(row.id) ?? new Set()).add(named))
496
+ addName(widening, named, writtenISOSubdivisionCode(named, row.code))
497
+ }
498
+
499
+ const ownRecords = await db
500
+ .selectFrom("spr")
501
+ .select(["id", "country"])
502
+ .where("placetype", "in", ["country", "dependency"])
503
+ .where("is_deprecated", "=", 0)
504
+ .where("country", "in", [...dependents])
505
+ .execute()
506
+
507
+ for (const record of ownRecords) {
508
+ if (record.country) {
509
+ dependencyOf.set(record.id, (dependencyOf.get(record.id) ?? new Set()).add(record.country.toUpperCase()))
510
+ }
511
+ }
512
+
513
+ for (const row of await readNamesOfIDs(db, [...dependencyOf.keys()])) {
514
+ for (const country of dependencyOf.get(row.id) ?? []) {
515
+ addName(widening, country, row.name)
516
+ }
517
+ }
518
+
519
+ for (const row of await readAdminNames(db, SECOND_LEVEL_PLACETYPES, options.secondLevelCountries ?? [])) {
520
+ addName(widening, row.country, row.name)
521
+ }
522
+
523
+ if (options.geonamesDirectory) {
524
+ const directory = PathBuilder.from(options.geonamesDirectory)
525
+
526
+ if (!(await pathExists(directory(GEONAMES_ADMIN1_FILE)))) {
527
+ throw new Error(
528
+ `${directory(GEONAMES_ADMIN1_FILE).toString()} is missing, so no GeoNames region name can be read`
529
+ )
530
+ }
531
+
532
+ // Keyed `<CC>.<admin1 code>`.
533
+ for (const [code, name] of await loadAdmin1(directory)) {
534
+ addName(widening, code.split(".")[0] ?? null, name)
535
+ }
536
+ }
537
+
538
+ // WOF's abbreviations and ISO codes miss some postal codes addresses write
539
+ // for a subdivision (Italian provinces' `TO`, US `PR` and the military `AE`),
540
+ // so libaddressinput's keys and names join each country's set.
541
+ const merged = new Set<string>()
542
+
543
+ const namesOf = (country: string): Set<string> | undefined => {
544
+ if (!merged.has(country)) {
545
+ merged.add(country)
546
+
547
+ const forms = subdivisionWrittenForms(country).map(foldRegionName).filter(isPresent)
548
+
549
+ if (forms.length) {
550
+ byCountry.set(country, new Set([...(byCountry.get(country) ?? []), ...forms]))
551
+ }
552
+
553
+ const own = byCountry.get(country)
554
+
555
+ if (own) {
556
+ for (const name of widening.get(country) ?? []) {
557
+ own.add(name)
558
+ }
559
+ }
560
+ }
561
+
562
+ return byCountry.get(country)
563
+ }
564
+
565
+ return { path: path.toString(), holds: createNameMatcher(namesOf) }
566
+ }
567
+
568
+ /**
569
+ * Every country's second-level (WOF `county`) names, for counting the rows of
570
+ * each country that write one in the region slot.
571
+ *
572
+ * `holds` answers `null` for a country with no county in the database.
573
+ */
574
+ export async function readSecondLevelNames(path: PathBuilderLike): Promise<RegionNames> {
575
+ using db = new DatabaseClient<WOFDatabase>(path, { readOnly: true })
576
+
577
+ const byCountry: NamesByCountry = new Map()
578
+
579
+ for (const row of await readAdminNames(db, SECOND_LEVEL_PLACETYPES, null)) {
580
+ addName(byCountry, row.country, row.name)
581
+ }
582
+
583
+ return { path: path.toString(), holds: createNameMatcher((country) => byCountry.get(country)) }
584
+ }
585
+
586
+ /**
587
+ * The countries {@link SECOND_LEVEL_REGION_TABLE_PATH} admits.
588
+ */
589
+ async function readSecondLevelRegionCountries(): Promise<string[]> {
590
+ return secondLevelAdmitted(await readLocalJSONFile<SecondLevelRegionTable>(SECOND_LEVEL_REGION_TABLE_PATH))
591
+ }
592
+
593
+ /**
594
+ * The default WOF admin database's region names, with the second-level names of the
595
+ * countries {@link SECOND_LEVEL_REGION_TABLE_PATH} admits and the data root's GeoNames
596
+ * first-level names, or `null` when the database isn't on this host.
597
+ *
598
+ * @throws When the database is on this host and the GeoNames first-level file isn't.
599
+ */
600
+ export async function tryReadDefaultRegionNames(): Promise<RegionNames | null> {
601
+ const path = wofDatabasePath("admin-global-priority.db")
602
+
603
+ if (!(await pathExists(path))) return null
604
+
605
+ return readRegionNames(path, {
606
+ secondLevelCountries: await readSecondLevelRegionCountries(),
607
+ geonamesDirectory: dataRootPath("geonames"),
608
+ })
609
+ }