@mailwoman/corpus 11.3.0 → 11.4.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 (336) hide show
  1. package/data/PROVENANCE.md +3 -4
  2. package/data/address-source-register.json +4 -4
  3. package/data/builds/v0.8.23-overlay/OVERLAY_PLAN.json +3656 -0
  4. package/data/builds/v0.8.23-prominent-overlay/OVERLAY_PLAN.json +42 -0
  5. package/data/builds/v0.8.23-rebuild/inputs.json +141 -0
  6. package/data/builds/v0.8.24-rebuild/inputs.json +111 -0
  7. package/data/corpus-source-inputs.json +95 -1
  8. package/data/holdout/evaluation-groups.v0.8.24.json +222158 -0
  9. package/data/license-decisions.json +5 -0
  10. package/data/region-slot-second-level.json +1 -1
  11. package/data/training-configs/v8.11.0-base-c6-90k-v0823-s42.yaml +920 -0
  12. package/data/training-configs/v8.12.0-base-c6-wofreps-v0823-s42.yaml +944 -0
  13. package/data/training-configs/v8.13.0-base-c6-wofreps-v0822-s42.yaml +918 -0
  14. package/lib/adapters/cordis/adapter.ts +3 -2
  15. package/lib/adapters/gleif/adapter.ts +123 -20
  16. package/lib/adapters/openaddresses/adapter.ts +2 -2
  17. package/lib/adapters/osm/adapter.ts +62 -4
  18. package/lib/adapters/overture/adapter.ts +97 -15
  19. package/lib/adapters/overture/address-key.ts +89 -0
  20. package/lib/adapters/overture/places-in/adapter.ts +258 -0
  21. package/lib/adapters/street/convention.ts +49 -0
  22. package/lib/adapters/street/country-line.ts +65 -0
  23. package/lib/adapters/wof/admin/json/adapter.ts +4 -4
  24. package/lib/adapters/wof/admin/json/region-codes.ts +98 -0
  25. package/lib/adapters/wof/json-rows.ts +2 -2
  26. package/lib/adapters.ts +2 -0
  27. package/lib/build/align/range.ts +21 -11
  28. package/lib/build/align/totals.ts +20 -0
  29. package/lib/build/align.ts +218 -20
  30. package/lib/build/budget.ts +55 -20
  31. package/lib/build/checkpoint.ts +9 -0
  32. package/lib/build/component-check.ts +97 -3
  33. package/lib/build/inputs.ts +122 -31
  34. package/lib/build/manifest.ts +1 -0
  35. package/lib/build/parallel.ts +140 -23
  36. package/lib/build/{region-names.ts → region/names.ts} +16 -4
  37. package/lib/build/region/recount.ts +153 -0
  38. package/lib/build/reuse.ts +112 -0
  39. package/lib/build/settings.ts +84 -3
  40. package/lib/build/source-record.ts +45 -8
  41. package/lib/build/types.ts +26 -0
  42. package/lib/build/worker.ts +75 -10
  43. package/lib/build.ts +2 -1
  44. package/lib/de/recipes/locale.ts +9 -3
  45. package/lib/evaluation/corpus-adapter.ts +2 -2
  46. package/lib/fi/adapters/ryhti/adapter.ts +2 -2
  47. package/lib/fo/adapters/adressur/adapter.ts +2 -2
  48. package/lib/fr/recipes/lieudit.ts +3 -4
  49. package/lib/in/street-line/segment.ts +370 -0
  50. package/lib/international/recipes/locale.ts +6 -3
  51. package/lib/li/adapters/llv/adapter.ts +2 -2
  52. package/lib/lu/adapters/bd-adresses/adapter.ts +2 -2
  53. package/lib/no/adapters/matrikkelen/adapter.ts +2 -2
  54. package/lib/parquet/streams.ts +18 -3
  55. package/lib/parquet/writers.ts +161 -29
  56. package/lib/recipes/country-balanced.ts +32 -18
  57. package/lib/recipes/fragment-assay/build.ts +8 -5
  58. package/lib/recipes/fragment-assay/rows.ts +4 -4
  59. package/lib/recipes/house/venue.ts +2 -1
  60. package/lib/recipes/postal/guideline/leg.ts +3 -2
  61. package/lib/recipes/prominent/places.ts +4 -4
  62. package/lib/runner.ts +139 -20
  63. package/lib/sg/recipes/register.ts +6 -1
  64. package/lib/source-register/row-license.ts +12 -2
  65. package/lib/surfaces/corpus-row.ts +84 -0
  66. package/lib/surfaces/locale.ts +20 -24
  67. package/lib/synthesizers/house-venue.ts +33 -40
  68. package/lib/synthesizers/no-street.ts +2 -2
  69. package/lib/synthesizers/po-box.ts +3 -3
  70. package/lib/synthesizers/tail.ts +3 -2
  71. package/lib/synthesizers/utils.ts +95 -56
  72. package/lib/test-kit.ts +50 -1
  73. package/lib/tools/fetch/overture-places-in.ts +118 -0
  74. package/lib/tools/fetch/storage.ts +3 -0
  75. package/lib/tools/fetch.ts +2 -0
  76. package/lib/tools/region-slot-census.run.ts +2 -2
  77. package/lib/types.ts +39 -0
  78. package/lib/us/adapters/state/tx-notaries/adapter.ts +2 -1
  79. package/lib/us/adapters/usgov/imls-pls/adapter.ts +2 -1
  80. package/lib/us/adapters/usgov/nad/adapter.ts +32 -4
  81. package/lib/us/adapters/usgov/nad/address-key.ts +8 -47
  82. package/lib/us/adapters/usgov/nppes/adapter.ts +2 -1
  83. package/lib/utils/address-key.ts +57 -0
  84. package/lib/utils/align.ts +22 -10
  85. package/lib/utils/evaluation-groups.ts +13 -6
  86. package/lib/utils/split.ts +24 -3
  87. package/out/adapters/cordis/adapter.d.ts.map +1 -1
  88. package/out/adapters/cordis/adapter.js +3 -2
  89. package/out/adapters/cordis/adapter.js.map +1 -1
  90. package/out/adapters/gleif/adapter.d.ts +4 -0
  91. package/out/adapters/gleif/adapter.d.ts.map +1 -1
  92. package/out/adapters/gleif/adapter.js +100 -20
  93. package/out/adapters/gleif/adapter.js.map +1 -1
  94. package/out/adapters/openaddresses/adapter.js +2 -2
  95. package/out/adapters/openaddresses/adapter.js.map +1 -1
  96. package/out/adapters/osm/adapter.d.ts +9 -0
  97. package/out/adapters/osm/adapter.d.ts.map +1 -1
  98. package/out/adapters/osm/adapter.js +55 -13
  99. package/out/adapters/osm/adapter.js.map +1 -1
  100. package/out/adapters/overture/adapter.d.ts +6 -0
  101. package/out/adapters/overture/adapter.d.ts.map +1 -1
  102. package/out/adapters/overture/adapter.js +72 -9
  103. package/out/adapters/overture/adapter.js.map +1 -1
  104. package/out/adapters/overture/address-key.d.ts +39 -0
  105. package/out/adapters/overture/address-key.d.ts.map +1 -0
  106. package/out/adapters/overture/address-key.js +75 -0
  107. package/out/adapters/overture/address-key.js.map +1 -0
  108. package/out/adapters/overture/places-in/adapter.d.ts +20 -0
  109. package/out/adapters/overture/places-in/adapter.d.ts.map +1 -0
  110. package/out/adapters/overture/places-in/adapter.js +191 -0
  111. package/out/adapters/overture/places-in/adapter.js.map +1 -0
  112. package/out/adapters/street/convention.d.ts +11 -0
  113. package/out/adapters/street/convention.d.ts.map +1 -0
  114. package/out/adapters/street/convention.js +40 -0
  115. package/out/adapters/street/convention.js.map +1 -0
  116. package/out/adapters/street/country-line.d.ts +31 -0
  117. package/out/adapters/street/country-line.d.ts.map +1 -0
  118. package/out/adapters/street/country-line.js +31 -0
  119. package/out/adapters/street/country-line.js.map +1 -0
  120. package/out/adapters/wof/admin/json/adapter.js +4 -4
  121. package/out/adapters/wof/admin/json/adapter.js.map +1 -1
  122. package/out/adapters/wof/admin/json/region-codes.d.ts +5 -0
  123. package/out/adapters/wof/admin/json/region-codes.d.ts.map +1 -1
  124. package/out/adapters/wof/admin/json/region-codes.js +83 -0
  125. package/out/adapters/wof/admin/json/region-codes.js.map +1 -1
  126. package/out/adapters/wof/json-rows.d.ts.map +1 -1
  127. package/out/adapters/wof/json-rows.js +2 -2
  128. package/out/adapters/wof/json-rows.js.map +1 -1
  129. package/out/adapters.d.ts.map +1 -1
  130. package/out/adapters.js +2 -0
  131. package/out/adapters.js.map +1 -1
  132. package/out/build/align/range.d.ts +7 -3
  133. package/out/build/align/range.d.ts.map +1 -1
  134. package/out/build/align/range.js +17 -9
  135. package/out/build/align/range.js.map +1 -1
  136. package/out/build/align/totals.d.ts +12 -0
  137. package/out/build/align/totals.d.ts.map +1 -1
  138. package/out/build/align/totals.js +8 -0
  139. package/out/build/align/totals.js.map +1 -1
  140. package/out/build/align.d.ts +60 -5
  141. package/out/build/align.d.ts.map +1 -1
  142. package/out/build/align.js +136 -12
  143. package/out/build/align.js.map +1 -1
  144. package/out/build/budget.d.ts +35 -11
  145. package/out/build/budget.d.ts.map +1 -1
  146. package/out/build/budget.js +52 -20
  147. package/out/build/budget.js.map +1 -1
  148. package/out/build/checkpoint.d.ts +9 -0
  149. package/out/build/checkpoint.d.ts.map +1 -1
  150. package/out/build/checkpoint.js.map +1 -1
  151. package/out/build/component-check.d.ts +29 -1
  152. package/out/build/component-check.d.ts.map +1 -1
  153. package/out/build/component-check.js +57 -2
  154. package/out/build/component-check.js.map +1 -1
  155. package/out/build/inputs.d.ts +58 -0
  156. package/out/build/inputs.d.ts.map +1 -1
  157. package/out/build/inputs.js +57 -20
  158. package/out/build/inputs.js.map +1 -1
  159. package/out/build/manifest.d.ts.map +1 -1
  160. package/out/build/manifest.js +1 -0
  161. package/out/build/manifest.js.map +1 -1
  162. package/out/build/parallel.d.ts +19 -1
  163. package/out/build/parallel.d.ts.map +1 -1
  164. package/out/build/parallel.js +92 -11
  165. package/out/build/parallel.js.map +1 -1
  166. package/out/build/{region-names.d.ts → region/names.d.ts} +10 -1
  167. package/out/build/region/names.d.ts.map +1 -0
  168. package/out/build/{region-names.js → region/names.js} +16 -5
  169. package/out/build/region/names.js.map +1 -0
  170. package/out/build/region/recount.d.ts +47 -0
  171. package/out/build/region/recount.d.ts.map +1 -0
  172. package/out/build/region/recount.js +105 -0
  173. package/out/build/region/recount.js.map +1 -0
  174. package/out/build/reuse.d.ts +31 -0
  175. package/out/build/reuse.d.ts.map +1 -0
  176. package/out/build/reuse.js +50 -0
  177. package/out/build/reuse.js.map +1 -0
  178. package/out/build/settings.d.ts +38 -0
  179. package/out/build/settings.d.ts.map +1 -1
  180. package/out/build/settings.js +67 -3
  181. package/out/build/settings.js.map +1 -1
  182. package/out/build/source-record.d.ts +32 -7
  183. package/out/build/source-record.d.ts.map +1 -1
  184. package/out/build/source-record.js +17 -4
  185. package/out/build/source-record.js.map +1 -1
  186. package/out/build/types.d.ts +24 -0
  187. package/out/build/types.d.ts.map +1 -1
  188. package/out/build/worker.d.ts +17 -0
  189. package/out/build/worker.d.ts.map +1 -1
  190. package/out/build/worker.js +44 -9
  191. package/out/build/worker.js.map +1 -1
  192. package/out/build.d.ts.map +1 -1
  193. package/out/build.js +2 -1
  194. package/out/build.js.map +1 -1
  195. package/out/de/recipes/locale.d.ts.map +1 -1
  196. package/out/de/recipes/locale.js +9 -3
  197. package/out/de/recipes/locale.js.map +1 -1
  198. package/out/evaluation/corpus-adapter.d.ts +14 -0
  199. package/out/evaluation/corpus-adapter.d.ts.map +1 -1
  200. package/out/evaluation/corpus-adapter.js +2 -16
  201. package/out/evaluation/corpus-adapter.js.map +1 -1
  202. package/out/fi/adapters/ryhti/adapter.js +2 -2
  203. package/out/fi/adapters/ryhti/adapter.js.map +1 -1
  204. package/out/fo/adapters/adressur/adapter.js +2 -2
  205. package/out/fo/adapters/adressur/adapter.js.map +1 -1
  206. package/out/fr/recipes/lieudit.d.ts.map +1 -1
  207. package/out/fr/recipes/lieudit.js +3 -3
  208. package/out/fr/recipes/lieudit.js.map +1 -1
  209. package/out/in/street-line/segment.d.ts +75 -0
  210. package/out/in/street-line/segment.d.ts.map +1 -0
  211. package/out/in/street-line/segment.js +298 -0
  212. package/out/in/street-line/segment.js.map +1 -0
  213. package/out/international/recipes/locale.d.ts.map +1 -1
  214. package/out/international/recipes/locale.js +6 -3
  215. package/out/international/recipes/locale.js.map +1 -1
  216. package/out/li/adapters/llv/adapter.js +2 -2
  217. package/out/li/adapters/llv/adapter.js.map +1 -1
  218. package/out/lu/adapters/bd-adresses/adapter.js +2 -2
  219. package/out/lu/adapters/bd-adresses/adapter.js.map +1 -1
  220. package/out/no/adapters/matrikkelen/adapter.js +2 -2
  221. package/out/no/adapters/matrikkelen/adapter.js.map +1 -1
  222. package/out/parquet/streams.d.ts +9 -0
  223. package/out/parquet/streams.d.ts.map +1 -1
  224. package/out/parquet/streams.js +7 -3
  225. package/out/parquet/streams.js.map +1 -1
  226. package/out/parquet/writers.d.ts +55 -2
  227. package/out/parquet/writers.d.ts.map +1 -1
  228. package/out/parquet/writers.js +109 -27
  229. package/out/parquet/writers.js.map +1 -1
  230. package/out/recipes/country-balanced.d.ts.map +1 -1
  231. package/out/recipes/country-balanced.js +32 -15
  232. package/out/recipes/country-balanced.js.map +1 -1
  233. package/out/recipes/fragment-assay/build.d.ts +5 -2
  234. package/out/recipes/fragment-assay/build.d.ts.map +1 -1
  235. package/out/recipes/fragment-assay/build.js +8 -5
  236. package/out/recipes/fragment-assay/build.js.map +1 -1
  237. package/out/recipes/fragment-assay/rows.d.ts +2 -2
  238. package/out/recipes/fragment-assay/rows.js +4 -4
  239. package/out/recipes/fragment-assay/rows.js.map +1 -1
  240. package/out/recipes/house/venue.d.ts.map +1 -1
  241. package/out/recipes/house/venue.js +2 -1
  242. package/out/recipes/house/venue.js.map +1 -1
  243. package/out/recipes/postal/guideline/leg.d.ts.map +1 -1
  244. package/out/recipes/postal/guideline/leg.js +2 -7
  245. package/out/recipes/postal/guideline/leg.js.map +1 -1
  246. package/out/recipes/prominent/places.js +4 -4
  247. package/out/recipes/prominent/places.js.map +1 -1
  248. package/out/runner.d.ts +28 -0
  249. package/out/runner.d.ts.map +1 -1
  250. package/out/runner.js +85 -18
  251. package/out/runner.js.map +1 -1
  252. package/out/sg/recipes/register.d.ts.map +1 -1
  253. package/out/sg/recipes/register.js +6 -1
  254. package/out/sg/recipes/register.js.map +1 -1
  255. package/out/source-register/row-license.d.ts +8 -0
  256. package/out/source-register/row-license.d.ts.map +1 -1
  257. package/out/source-register/row-license.js +10 -2
  258. package/out/source-register/row-license.js.map +1 -1
  259. package/out/surfaces/corpus-row.d.ts +32 -0
  260. package/out/surfaces/corpus-row.d.ts.map +1 -0
  261. package/out/surfaces/corpus-row.js +70 -0
  262. package/out/surfaces/corpus-row.js.map +1 -0
  263. package/out/surfaces/locale.d.ts.map +1 -1
  264. package/out/surfaces/locale.js +16 -22
  265. package/out/surfaces/locale.js.map +1 -1
  266. package/out/synthesizers/house-venue.d.ts +9 -4
  267. package/out/synthesizers/house-venue.d.ts.map +1 -1
  268. package/out/synthesizers/house-venue.js +25 -35
  269. package/out/synthesizers/house-venue.js.map +1 -1
  270. package/out/synthesizers/no-street.js +2 -9
  271. package/out/synthesizers/no-street.js.map +1 -1
  272. package/out/synthesizers/po-box.js +3 -7
  273. package/out/synthesizers/po-box.js.map +1 -1
  274. package/out/synthesizers/tail.d.ts.map +1 -1
  275. package/out/synthesizers/tail.js +2 -2
  276. package/out/synthesizers/tail.js.map +1 -1
  277. package/out/synthesizers/utils.d.ts +20 -3
  278. package/out/synthesizers/utils.d.ts.map +1 -1
  279. package/out/synthesizers/utils.js +74 -57
  280. package/out/synthesizers/utils.js.map +1 -1
  281. package/out/test-kit.d.ts +11 -0
  282. package/out/test-kit.d.ts.map +1 -1
  283. package/out/test-kit.js +30 -1
  284. package/out/test-kit.js.map +1 -1
  285. package/out/tools/fetch/overture-places-in.d.ts +14 -0
  286. package/out/tools/fetch/overture-places-in.d.ts.map +1 -0
  287. package/out/tools/fetch/overture-places-in.js +82 -0
  288. package/out/tools/fetch/overture-places-in.js.map +1 -0
  289. package/out/tools/fetch/storage.d.ts +2 -0
  290. package/out/tools/fetch/storage.d.ts.map +1 -1
  291. package/out/tools/fetch/storage.js +3 -0
  292. package/out/tools/fetch/storage.js.map +1 -1
  293. package/out/tools/fetch.d.ts +2 -0
  294. package/out/tools/fetch.d.ts.map +1 -1
  295. package/out/tools/fetch.js +2 -0
  296. package/out/tools/fetch.js.map +1 -1
  297. package/out/tools/region-slot-census.run.js +2 -2
  298. package/out/types.d.ts +41 -0
  299. package/out/types.d.ts.map +1 -1
  300. package/out/types.js.map +1 -1
  301. package/out/us/adapters/state/tx-notaries/adapter.d.ts.map +1 -1
  302. package/out/us/adapters/state/tx-notaries/adapter.js +2 -1
  303. package/out/us/adapters/state/tx-notaries/adapter.js.map +1 -1
  304. package/out/us/adapters/usgov/imls-pls/adapter.d.ts.map +1 -1
  305. package/out/us/adapters/usgov/imls-pls/adapter.js +2 -1
  306. package/out/us/adapters/usgov/imls-pls/adapter.js.map +1 -1
  307. package/out/us/adapters/usgov/nad/adapter.d.ts +15 -0
  308. package/out/us/adapters/usgov/nad/adapter.d.ts.map +1 -1
  309. package/out/us/adapters/usgov/nad/adapter.js +21 -3
  310. package/out/us/adapters/usgov/nad/adapter.js.map +1 -1
  311. package/out/us/adapters/usgov/nad/address-key.d.ts +1 -5
  312. package/out/us/adapters/usgov/nad/address-key.d.ts.map +1 -1
  313. package/out/us/adapters/usgov/nad/address-key.js +8 -33
  314. package/out/us/adapters/usgov/nad/address-key.js.map +1 -1
  315. package/out/us/adapters/usgov/nppes/adapter.d.ts.map +1 -1
  316. package/out/us/adapters/usgov/nppes/adapter.js +2 -1
  317. package/out/us/adapters/usgov/nppes/adapter.js.map +1 -1
  318. package/out/utils/address-key.d.ts +23 -0
  319. package/out/utils/address-key.d.ts.map +1 -0
  320. package/out/utils/address-key.js +43 -0
  321. package/out/utils/address-key.js.map +1 -0
  322. package/out/utils/align.d.ts +7 -0
  323. package/out/utils/align.d.ts.map +1 -1
  324. package/out/utils/align.js +18 -8
  325. package/out/utils/align.js.map +1 -1
  326. package/out/utils/evaluation-groups.d.ts +5 -1
  327. package/out/utils/evaluation-groups.d.ts.map +1 -1
  328. package/out/utils/evaluation-groups.js +12 -1
  329. package/out/utils/evaluation-groups.js.map +1 -1
  330. package/out/utils/split.d.ts +11 -0
  331. package/out/utils/split.d.ts.map +1 -1
  332. package/out/utils/split.js +18 -3
  333. package/out/utils/split.js.map +1 -1
  334. package/package.json +19 -19
  335. package/out/build/region-names.d.ts.map +0 -1
  336. package/out/build/region-names.js.map +0 -1
@@ -21,6 +21,7 @@ import { cliArguments } from "@mailwoman/core/scripting/arguments"
21
21
  import { heapLimitBytes } from "@mailwoman/core/utils/system"
22
22
  import type { LicensePolicy } from "@mailwoman/license/policy"
23
23
  import { PathBuilder } from "path-ts"
24
+ import { TextSpliterator } from "spliterator"
24
25
 
25
26
  import { alignSettings } from "#build/align"
26
27
  import {
@@ -36,7 +37,7 @@ import { mergeAlignTotals, serializeAlignTotals } from "#build/align/totals"
36
37
  import type { WorkerResources } from "#build/budget"
37
38
  import { loadAdapterFromModule } from "#build/code-hash"
38
39
  import { readSourceEligibility } from "#build/eligibility"
39
- import { streamJSONL } from "#build/rows"
40
+ import { regionCheckFingerprints } from "#build/settings"
40
41
  import {
41
42
  recordFile,
42
43
  SOURCE_RECORD_FILE,
@@ -46,9 +47,16 @@ import {
46
47
  import { BuildProfile } from "#build/types"
47
48
  import { resolveDerivation, type FileDerivation } from "#parquet/record"
48
49
  import { shuffleSplitRows, SPLIT_SHUFFLE_SALT } from "#parquet/shuffle"
49
- import { writeParquetSplits, type ParquetManifest } from "#parquet/writers"
50
- import { runAdapter, type AdapterRunManifest } from "#runner"
51
- import type { AdapterOptions, LabeledRow } from "#types"
50
+ import { decodeStagedRow, writeStagedParquetSplits, type ParquetManifest, type StagedRow } from "#parquet/writers"
51
+ import {
52
+ mergeAdapterPartitions,
53
+ runAdapter,
54
+ runAdapterPartition,
55
+ type AdapterPartitionRecord,
56
+ type AdapterRunManifest,
57
+ } from "#runner"
58
+ import { committedRowLicenses } from "#source-register/row-license"
59
+ import type { AdapterOptions } from "#types"
52
60
  import { SPLIT_NAMES, sortLines, type CountryHoldout, type SplitName } from "#utils/split"
53
61
 
54
62
  /**
@@ -61,6 +69,7 @@ export interface AlignTaskSettings {
61
69
  licensePolicy: LicensePolicy
62
70
  excludeLicenses: { source: string; flags: string }[]
63
71
  holdouts: Record<string, CountryHoldout>
72
+ trainShares: Record<string, number>
64
73
  }
65
74
 
66
75
  interface TaskBase {
@@ -93,6 +102,15 @@ export interface AdapterTask extends TaskBase {
93
102
  */
94
103
  inputs: string
95
104
  intermediateDir: string
105
+ /**
106
+ * Set for one partition of a partitioned run: the rows to read and the files
107
+ * to write them and their record to.
108
+ */
109
+ partition?: { rowRange: { start: number; end: number }; outputPath: string; recordPath: string } | null
110
+ /**
111
+ * Set for the task that merges a partitioned run: each partition's record, in input order.
112
+ */
113
+ mergeRecords?: string[] | null
96
114
  }
97
115
 
98
116
  export interface AlignTask extends TaskBase {
@@ -145,6 +163,7 @@ function alignInputs(settings: AlignTaskSettings) {
145
163
  licensePolicy: settings.licensePolicy,
146
164
  excludeLicenses: compileTaskLicensePatterns(settings),
147
165
  holdouts: settings.holdouts,
166
+ trainShares: settings.trainShares,
148
167
  }
149
168
  }
150
169
 
@@ -161,13 +180,46 @@ export function workerModulePath(): string {
161
180
  async function runAdapterTask(task: AdapterTask): Promise<void> {
162
181
  const adapter = await loadAdapterFromModule(task.adapterModule, task.source)
163
182
 
164
- await runAdapter({
183
+ if (task.partition) {
184
+ const record = await runAdapterPartition({
185
+ adapter,
186
+ adapterOptions: {
187
+ ...task.adapterOptions,
188
+ rowRange: task.partition.rowRange,
189
+ duckdb: { memoryLimitBytes: task.resources.duckdbBytes, threads: task.resources.duckdbThreads },
190
+ },
191
+ outputPath: task.partition.outputPath,
192
+ })
193
+
194
+ // The record is written last, so a record on disk means its partition file is complete.
195
+ await writeLocalJSONFile(record, task.partition.recordPath)
196
+
197
+ return
198
+ }
199
+
200
+ const options = {
165
201
  adapter,
166
202
  adapterOptions: task.adapterOptions,
167
203
  outputDir: task.intermediateDir,
168
204
  corpusVersion: task.corpusVersion,
169
205
  inputs: task.inputs,
170
- })
206
+ }
207
+
208
+ if (task.mergeRecords) {
209
+ const partitions = await Promise.all(
210
+ task.mergeRecords.map((path) => readLocalJSONFile<AdapterPartitionRecord>(path))
211
+ )
212
+
213
+ await mergeAdapterPartitions(options, partitions)
214
+
215
+ for (const partition of partitions) {
216
+ await removePathIfPresent(partition.path)
217
+ }
218
+
219
+ return
220
+ }
221
+
222
+ await runAdapter(options)
171
223
  }
172
224
 
173
225
  async function runAlignTask(task: AlignTask): Promise<void> {
@@ -201,9 +253,18 @@ async function finishedPartProblem(manifestPath: PathBuilder): Promise<string |
201
253
  return null
202
254
  }
203
255
 
204
- async function* concatenated<T>(paths: readonly PathBuilder[]): AsyncGenerator<T> {
256
+ /**
257
+ * The staged rows of one source's files, in order.
258
+ */
259
+ async function* concatenatedStagedRows(
260
+ paths: readonly PathBuilder[],
261
+ source: string,
262
+ license: string
263
+ ): AsyncGenerator<StagedRow> {
205
264
  for (const path of paths) {
206
- yield* streamJSONL<T>(path)
265
+ for await (const line of TextSpliterator.fromAsync(path)) {
266
+ yield decodeStagedRow(line, source, license)
267
+ }
207
268
  }
208
269
  }
209
270
 
@@ -254,12 +315,15 @@ async function runFinishTask(task: FinishTask, report: (message: string) => void
254
315
  report("writing parquet files")
255
316
 
256
317
  const derivation = task.derivation
318
+ // Every row of one source holds its register license id.
319
+ // The align range stamped that id on each staged line.
320
+ const license = (await committedRowLicenses()).require(task.source, `worker ${task.id}`)
257
321
 
258
- const parquet = await writeParquetSplits(
322
+ const parquet = await writeStagedParquetSplits(
259
323
  Object.fromEntries(
260
324
  SPLIT_NAMES.map((split) => [
261
325
  split,
262
- shuffleSplitRows(concatenated<LabeledRow>(files[split].labeled), {
326
+ shuffleSplitRows(concatenatedStagedRows(files[split].staged, task.source, license), {
263
327
  seed: task.buildSettings.shuffleSeed,
264
328
  salt: SPLIT_SHUFFLE_SALT[split],
265
329
  window: task.buildSettings.shuffleWindow,
@@ -302,6 +366,7 @@ async function runFinishTask(task: FinishTask, report: (message: string) => void
302
366
  task.plans.map((plan) => recordFile(alignRangeDirectory(sourceDir, plan)("quarantine.jsonl")))
303
367
  ),
304
368
  totals: serializeAlignTotals(mergeAlignTotals(ranges.map((range) => range.totals))),
369
+ regionCheck: await regionCheckFingerprints(),
305
370
  completed_at: new Date().toISOString(),
306
371
  }
307
372
 
package/lib/build.ts CHANGED
@@ -17,7 +17,7 @@ import { PathBuilder } from "path-ts"
17
17
 
18
18
  import { defaultAdapterRegistry } from "#adapters/registry"
19
19
  import { adapterRunInputs, runAdapterPhase } from "#build/adapters"
20
- import { runAlignPhase } from "#build/align"
20
+ import { runAlignPhase, trainSharesOf } from "#build/align"
21
21
  import { alignTotalsOf } from "#build/align/totals"
22
22
  import { planWorkerBudget } from "#build/budget"
23
23
  import { readSourceDecisionTable, readSourceEligibility } from "#build/eligibility"
@@ -164,6 +164,7 @@ export async function buildCorpus(opts: BuildCorpusOptions): Promise<BuildCorpus
164
164
  licensePolicy,
165
165
  excludeLicenses: settings.excludeLicenses,
166
166
  holdouts,
167
+ trainShares: trainSharesOf(opts.adapterInputs),
167
168
  eligibility: profile === BuildProfile.ReleaseEligible ? await readSourceEligibility() : null,
168
169
  labeledPaths,
169
170
  sourceIDPaths,
@@ -44,9 +44,15 @@ import { alignRow } from "#utils"
44
44
  /**
45
45
  * The share of rows that write the Bundesland as `region` after the postcode and locality.
46
46
  *
47
- * It is the share the retired international order printed in v0.7.10 (`--intl-fraction` 0.4).
47
+ * Deutsche Post prints no Land.
48
+ * Every example in its UPU description ends `<postcode> <TOWN>` (`Wacholderweg 52a / 26133 OLDENBURG`).
49
+ *
50
+ * A typed query still writes one (`04860 Torgau, Sachsen`).
51
+ * A minority of rows keep it, so the operator's form stays the majority.
52
+ *
53
+ * A Berlin row never takes one, because the Land is the city (see `renderLocaleRow`).
48
54
  */
49
- const BUNDESLAND_REGION_FRACTION = 0.4
55
+ const BUNDESLAND_REGION_FRACTION = 0.2
50
56
 
51
57
  /**
52
58
  * A German OA source (cached zip) + the Bundesland the file covers (OA's region column is empty for DE).
@@ -169,7 +175,7 @@ export const germanRecipe: CorpusRecipe = {
169
175
  },
170
176
  {
171
177
  flag: "--region-fraction <f>",
172
- description: "Fraction of rows that write the Bundesland as region after the locality. Default 0.4",
178
+ description: "Fraction of rows that write the Bundesland as region after the locality. Default 0.2",
173
179
  },
174
180
  {
175
181
  flag: "--ortsteil-fraction <f>",
@@ -16,12 +16,12 @@
16
16
  * under {@linkcode EvaluationAdapterRefusal.Duplicate}.
17
17
  */
18
18
 
19
- import { formatAddressRow } from "@mailwoman/codex/address/format"
20
19
  import type { PathBuilderLike } from "path-ts"
21
20
 
22
21
  import { UnsupportedCountryError } from "#adapters/errors"
23
22
  import { stableSourceID } from "#adapters/source-id"
24
23
  import type { EvaluationRecordReading, EvaluationSource } from "#evaluation/source"
24
+ import { formatCorpusRow } from "#surfaces/corpus-row"
25
25
  import { type AddressRole, type CanonicalRow, type CorpusAdapter, countDropped, SurfaceOrigin } from "#types"
26
26
 
27
27
  /**
@@ -78,7 +78,7 @@ export function evaluationRecordRow(
78
78
  if ("refused" in reading) return reading
79
79
 
80
80
  const { country } = spec.source
81
- const rendered = formatAddressRow(reading.components, country, { singleLine: true })
81
+ const rendered = formatCorpusRow(reading.components, country, { singleLine: true })
82
82
 
83
83
  if (!rendered || rendered.unplaced.length) return { refused: EvaluationAdapterRefusal.Unrenderable }
84
84
 
@@ -62,13 +62,13 @@
62
62
  * It honors `opts.limit`, `opts.signal` and `opts.country`.
63
63
  */
64
64
 
65
- import { formatAddressRow } from "@mailwoman/codex/address/format"
66
65
  import { CSVSpliterator } from "spliterator"
67
66
 
68
67
  import { UnsupportedCountryError } from "#adapters/errors"
69
68
  import { stableSourceID } from "#adapters/source-id"
70
69
  import { composeHouseNumber } from "#adapters/street/line"
71
70
  import { SourceRegister } from "#registers"
71
+ import { formatCorpusRow } from "#surfaces/corpus-row"
72
72
  import { AddressRole, type AdapterOptions, type CanonicalRow, type CorpusAdapter, SurfaceOrigin } from "#types"
73
73
 
74
74
  /**
@@ -286,7 +286,7 @@ export function createRyhtiAdapter(): CorpusAdapter {
286
286
  components.locality = surface.locality
287
287
  }
288
288
 
289
- const rendered = formatAddressRow(components, country, { singleLine: true })
289
+ const rendered = formatCorpusRow(components, country, { singleLine: true })
290
290
 
291
291
  if (!rendered) continue
292
292
 
@@ -29,7 +29,6 @@
29
29
  * Every row records those terms.
30
30
  */
31
31
 
32
- import { formatAddressRow } from "@mailwoman/codex/address/format"
33
32
  import { readLocalTextFile } from "@mailwoman/core/fs/readers"
34
33
  import { tryStat } from "@mailwoman/core/fs/readers/stat"
35
34
  import { parseJSONStrict } from "@mailwoman/core/values/json"
@@ -42,6 +41,7 @@ import { present } from "#adapters/present"
42
41
  import { stableSourceIDFromParts } from "#adapters/source-id"
43
42
  import { composeHouseNumber } from "#adapters/street/line"
44
43
  import { SourceRegister } from "#registers"
44
+ import { formatCorpusRow } from "#surfaces/corpus-row"
45
45
  import {
46
46
  AddressRole,
47
47
  type AdapterOptions,
@@ -195,7 +195,7 @@ export function createAdressurFaroeAdapter(): CorpusAdapter {
195
195
 
196
196
  if (!components) continue
197
197
 
198
- const rendered = formatAddressRow(components, "FO", {
198
+ const rendered = formatCorpusRow(components, "FO", {
199
199
  singleLine: true,
200
200
  })
201
201
 
@@ -30,7 +30,7 @@
30
30
  import { extractBANAddrPoints } from "@mailwoman/ban/sdk/extract"
31
31
  import { formatAddress } from "@mailwoman/codex/address/format"
32
32
  import type { ComponentTag } from "@mailwoman/codex/component"
33
- import { COUNTRY_SURFACE_FORMS } from "@mailwoman/codex/country"
33
+ import { countryRenderForms } from "@mailwoman/codex/country"
34
34
  import { currentSourcePath } from "@mailwoman/core/data-root"
35
35
  import { sample, shuffleWith } from "@mailwoman/core/math/random"
36
36
  import { mulberry32 as makeMulberry32 } from "@mailwoman/core/utils"
@@ -249,10 +249,9 @@ 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: readonly string[] | undefined =
253
- COUNTRY_SURFACE_FORMS[country as keyof typeof COUNTRY_SURFACE_FORMS]
252
+ const forms = countryRenderForms(country)
254
253
 
255
- if (forms) {
254
+ if (forms.length) {
256
255
  const form = sample(forms, random)
257
256
  raw = `${raw}, ${form}`
258
257
  components.country = form
@@ -0,0 +1,370 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * Reads the parts of an Indian street line written as one comma-separated field.
6
+ *
7
+ * A publisher that holds an Indian address as one free-text line
8
+ * writes it in the order India Post's UPU description shows
9
+ * (`https://www.upu.int/UPU/media/upu/PostalEntitiesFiles/addressingUnit/indEn.pdf`, 01/2013).
10
+ * The premise and the street come first, then the sub-localities from smallest to largest.
11
+ *
12
+ * Its examples are `4, Amrita Shergill Road`, `32/1, 2nd Main Road / 8th Block / Jayanagar`
13
+ * and `Flat 408, Tagore Road Hostel / Tagore Road`.
14
+ *
15
+ * {@linkcode segmentINStreetLine} labels a line only where every part it keeps has one reading:
16
+ * the premise number, a building name before the street, the street, and one sub-locality after it.
17
+ * Door, floor and shop designators and landmark phrases (`Near`, `Opp.`) are left out.
18
+ *
19
+ * The IN layout has no slot that prints them where the line wrote them.
20
+ * Any other part refuses the line.
21
+ */
22
+
23
+ import { foldName as fold } from "@mailwoman/codex/names/normalize"
24
+
25
+ /**
26
+ * The words that end an Indian thoroughfare's name, as the line writes them, folded to lower case.
27
+ *
28
+ * The English forms are libpostal's `en/street_types.txt` entries for road,
29
+ * street, lane, avenue, highway, bypass, cross and path, without the one-
30
+ * and two-letter forms (`r`, `l`, `la`) that end other words too.
31
+ * `marg` is libpostal's `hi/street_types.txt`.
32
+ *
33
+ * `main` (Bengaluru's `2nd Main Road` is also written `2nd Main`), `salai` (Tamil, as in `Anna Salai`)
34
+ * and `gali` (Hindi) are added here. libpostal's Hindi list also holds `nagar` and `bazar`.
35
+ * In Indian addresses those words end a neighborhood's name rather than a street's,
36
+ * so the table leaves them out.
37
+ */
38
+ const THOROUGHFARE_WORDS: ReadonlySet<string> = new Set([
39
+ "road",
40
+ "rd",
41
+ "roa",
42
+ "street",
43
+ "st",
44
+ "str",
45
+ "lane",
46
+ "ln",
47
+ "avenue",
48
+ "ave",
49
+ "highway",
50
+ "hwy",
51
+ "bypass",
52
+ "byepass",
53
+ "cross",
54
+ "path",
55
+ "marg",
56
+ "main",
57
+ "salai",
58
+ "gali",
59
+ "galli",
60
+ ])
61
+
62
+ /**
63
+ * The words that end a building or complex name, folded to lower case.
64
+ *
65
+ * The UPU description writes `Astro High Complex` and `Tagore Road Hostel`,
66
+ * and street lines write `Tower`, `Plaza` and `Bhavan`.
67
+ * A part before the street is read as a building only when it ends in one of them.
68
+ *
69
+ * Lines also write a village or a neighborhood there (`Kurumassery, Angamaly Road`).
70
+ * No word of such a part tells it from a building.
71
+ */
72
+ const BUILDING_WORDS: ReadonlySet<string> = new Set([
73
+ "apartment",
74
+ "apartments",
75
+ "arcade",
76
+ "bhavan",
77
+ "bhawan",
78
+ "building",
79
+ "buildings",
80
+ "centre",
81
+ "center",
82
+ "chambers",
83
+ "complex",
84
+ "court",
85
+ "heights",
86
+ "hostel",
87
+ "house",
88
+ "mall",
89
+ "mansion",
90
+ "plaza",
91
+ "residency",
92
+ "tower",
93
+ "towers",
94
+ ])
95
+
96
+ /**
97
+ * The fewest words a street or a building name holds, so a bare `Road` or `Complex` is neither.
98
+ */
99
+ const MIN_NAME_WORDS = 2
100
+
101
+ /**
102
+ * A national or state highway written by its number, `NH 17` or `SH-4`.
103
+ */
104
+ const NUMBERED_HIGHWAY = /^(?:NH|SH)[\s-]?\d+[A-Z]?$/i
105
+
106
+ /**
107
+ * A premise number, optionally after the label a line writes before it
108
+ * (`No.`, `Door No`, `Plot No`, `H.No`, `#`).
109
+ *
110
+ * The number is digits, optionally with a slash, a hyphen or a letter
111
+ * (`32/1`, `21/5794`, `2/A1`, `49-1`, `5B`).
112
+ * Group 1 is the number without the label.
113
+ */
114
+ const PREMISE_NUMBER =
115
+ /^(?:(?:no|door\s*no|d\.?\s*no|plot\s*no|h\.?\s*no|house\s*no|#)\.?\s*)?(\d+[A-Za-z]?(?:\s?[/-]\s?[0-9A-Za-z]{1,6}){0,2})$/i
116
+
117
+ /**
118
+ * A street part that opens with its premise number: `No 14 Muthuvelapa Street`, `318 DB Road`.
119
+ *
120
+ * Group 1 is the number and group 2 the street.
121
+ * A number followed by a measure belongs to the street's name (`100 Ft. Road`), so it isn't split off.
122
+ */
123
+ const NUMBER_THEN_STREET =
124
+ /^(?:(?:no|door\s*no|plot\s*no|h\.?\s*no|#)\.?\s*)?(\d+[A-Za-z]?(?:[/-][0-9A-Za-z]{1,6})?)\s+(?!(?:ft|feet|km|kms|mtrs?|met(?:er|re)s?)\b)(\S.*)$/i
125
+
126
+ /**
127
+ * A door, floor, shop or flat designator: `Ground Floor`, `1st Floor`, `Gr Flr`, `Shop No 70`, `Flat 408`.
128
+ */
129
+ const DESIGNATOR =
130
+ /^(?:(?:ground|gr\.?|grd\.?|upper|lower|first|second|third|fourth|fifth|top|\d+(?:st|nd|rd|th)?)\s*(?:floor|flr)\b|(?:shop|flat|office|unit|suite|room|stall|apartment|apt|door)\b)/i
131
+
132
+ /**
133
+ * A landmark phrase.
134
+ *
135
+ * It places the premise beside something else rather than giving its address.
136
+ */
137
+ const LANDMARK =
138
+ /^(?:opp|opposite|near|nr|behind|beside|besides|next\s+to|adjacent|adj|in\s+front\s+of|off|above|inside)\b/i
139
+
140
+ /**
141
+ * A landmark phrase inside a part, as in `318 DB Road Near Axis Bank`, which no one split of the part reads.
142
+ */
143
+ const LANDMARK_INSIDE = /\s(?:opp|opposite|near|nr|behind|beside)\.?\s/i
144
+
145
+ /**
146
+ * A sub-province or post-office designator: a district, taluk or tehsil,
147
+ * a hobli or mandal, or a post office.
148
+ *
149
+ * The UPU description writes these on their own lines (`Pennukonda Taluk / Anantpur District`).
150
+ * The IN layout has no slot for them, so a line that holds one is refused.
151
+ */
152
+ const SUB_PROVINCE =
153
+ /\b(?:dist|distt|district|sub-district|taluk|taluka|tehsil|tahsil|hobli|mandal|block|post|p\.?\s?o)\b\.?/i
154
+
155
+ /**
156
+ * A census sub-district numbered in Roman numerals, `Purulia-II`.
157
+ */
158
+ const NUMBERED_SUB_DISTRICT = /[\s-](?:I|II|III|IV|V|VI)$/u
159
+
160
+ /**
161
+ * Whether a value is a district, taluk, tehsil, hobli, mandal or post office,
162
+ * or a census sub-district numbered in Roman numerals, rather than a locality or a street.
163
+ */
164
+ export function isINSubProvince(value: string): boolean {
165
+ return SUB_PROVINCE.test(value) || NUMBERED_SUB_DISTRICT.test(value.trim())
166
+ }
167
+
168
+ /**
169
+ * A sub-locality as a line writes it after the street: a name of letters, at least three of them, with
170
+ * the spaces, dots, hyphens and apostrophes a name holds (`Jayanagar`, `R.S. Puram`, `Kalyan Nagar`).
171
+ */
172
+ const SUB_LOCALITY = /^[\p{L}\p{M}][\p{L}\p{M} .'-]{2,}$/u
173
+
174
+ /**
175
+ * The numbers a street's name holds: an ordinal (`2nd Main Road`), a numbered highway (`NH 17`),
176
+ * a measure (`100 Ft. Road`, `12th KM`) and a sector, phase or stage (`Sector 11 Road`).
177
+ */
178
+ const NUMBER_IN_STREET_NAME =
179
+ /\b\d+(?:st|nd|rd|th)\b|\b(?:NH|SH)[\s-]?\d+[A-Z]?\b|\b\d+\s*(?:ft|feet|km|kms|mtrs?|met(?:er|re)s?)\b|\b(?:sector|phase|stage)[\s-]?\d+[A-Z]?\b/giu
180
+
181
+ /**
182
+ * Whether a street holds a digit its name doesn't account for,
183
+ * as `MORARJEE VELJEE BLDGR 16 KOLBHAT LANE` holds a building's number.
184
+ */
185
+ function streetHoldsNumber(street: string): boolean {
186
+ return /\d/u.test(street.replaceAll(NUMBER_IN_STREET_NAME, ""))
187
+ }
188
+
189
+ /**
190
+ * A PIN code written into the street line: six digits, with an optional space after the third.
191
+ */
192
+ const PIN_IN_LINE = /(?<!\d)\d{3}\s?\d{3}(?!\d)/u
193
+
194
+ /**
195
+ * Why a line was not labeled.
196
+ */
197
+ export const INStreetLineRefusal = {
198
+ /**
199
+ * The line is empty once the parts that repeat the locality or the state are removed.
200
+ */
201
+ Empty: "empty",
202
+ /**
203
+ * The line writes a six-digit number: the PIN code, written into the street line.
204
+ */
205
+ PostcodeInLine: "postcode-in-line",
206
+ /**
207
+ * The line holds a district, taluk, tehsil, hobli, mandal or post office.
208
+ */
209
+ SubProvinceInLine: "sub-province-in-line",
210
+ /**
211
+ * A landmark phrase sits inside a part rather than in a part of its own.
212
+ */
213
+ LandmarkInsidePart: "landmark-inside-part",
214
+ /**
215
+ * No part ends in a thoroughfare word.
216
+ */
217
+ NoThoroughfare: "no-thoroughfare",
218
+ /**
219
+ * More than one part before the street remains once designators and the premise number are
220
+ * removed, or the one that remains isn't a building name (`RS No 514/4`, a village name).
221
+ */
222
+ PremiseUnread: "premise-unread",
223
+ /**
224
+ * The street holds a digit its name doesn't account for, such as a building's number inside it.
225
+ */
226
+ StreetHoldsNumber: "street-holds-number",
227
+ /**
228
+ * More than one part follows the street, or the part that does isn't a name of three letters or more.
229
+ */
230
+ SubLocalityUnread: "sub-locality-unread",
231
+ } as const
232
+
233
+ export type INStreetLineRefusal = (typeof INStreetLineRefusal)[keyof typeof INStreetLineRefusal]
234
+
235
+ /**
236
+ * The parts of an Indian street line, labeled.
237
+ */
238
+ export interface INStreetLineParts {
239
+ house_number?: string
240
+ /**
241
+ * A building or complex name written before the street, as `Tagore Road Hostel` is in the UPU description.
242
+ */
243
+ venue?: string
244
+ street: string
245
+ dependent_locality?: string
246
+ /**
247
+ * The count of designator and landmark parts left out.
248
+ */
249
+ omitted: number
250
+ }
251
+
252
+ /**
253
+ * The fields of the record beside the line.
254
+ * A line may repeat their values.
255
+ */
256
+ export interface INStreetLineContext {
257
+ locality?: string | null
258
+ region?: string | null
259
+ }
260
+
261
+ /**
262
+ * A part's words in lower case, with the punctuation at either end of each word removed (`Rd.` → `rd`).
263
+ */
264
+ function foldedWords(part: string): string[] {
265
+ return part
266
+ .toLowerCase()
267
+ .split(/\s+/u)
268
+ .map((word) => word.replaceAll(/^[\p{P}]+|[\p{P}]+$/gu, ""))
269
+ .filter((word) => word.length > 0)
270
+ }
271
+
272
+ /**
273
+ * Whether a part is a street: its last word is a thoroughfare word, or it is a numbered highway.
274
+ */
275
+ function isINThoroughfare(part: string): boolean {
276
+ if (NUMBERED_HIGHWAY.test(part.trim())) return true
277
+
278
+ const words = foldedWords(part)
279
+
280
+ return words.length >= MIN_NAME_WORDS && THOROUGHFARE_WORDS.has(words.at(-1)!)
281
+ }
282
+
283
+ /**
284
+ * Whether a part is a building's name: its last word a building word, and no digit.
285
+ */
286
+ function isINBuilding(part: string): boolean {
287
+ const words = foldedWords(part)
288
+
289
+ return words.length >= MIN_NAME_WORDS && BUILDING_WORDS.has(words.at(-1)!) && !/\d/u.test(part)
290
+ }
291
+
292
+ /**
293
+ * Label the parts of an Indian street line, or return the reason it can't be labeled.
294
+ */
295
+ export function segmentINStreetLine(
296
+ line: string,
297
+ context: INStreetLineContext = {}
298
+ ): INStreetLineParts | { refused: INStreetLineRefusal } {
299
+ const echoes = new Set(
300
+ [context.locality, context.region].filter((value) => value?.trim()).map((value) => fold(value!))
301
+ )
302
+
303
+ const parts = line
304
+ .replaceAll(/\s+/gu, " ")
305
+ .split(/\s*,\s*/u)
306
+ .map((part) => part.trim().replace(/[.;:-]+$/u, ""))
307
+ .filter((part) => part.length > 0 && !echoes.has(fold(part)))
308
+
309
+ if (!parts.length) return { refused: INStreetLineRefusal.Empty }
310
+
311
+ if (parts.some((part) => PIN_IN_LINE.test(part))) return { refused: INStreetLineRefusal.PostcodeInLine }
312
+
313
+ if (parts.some(isINSubProvince)) return { refused: INStreetLineRefusal.SubProvinceInLine }
314
+
315
+ if (parts.some((part) => LANDMARK_INSIDE.test(` ${part} `) && !LANDMARK.test(part))) {
316
+ return { refused: INStreetLineRefusal.LandmarkInsidePart }
317
+ }
318
+
319
+ let omitted = 0
320
+ const kept: string[] = []
321
+
322
+ for (const part of parts) {
323
+ if (DESIGNATOR.test(part) || LANDMARK.test(part)) {
324
+ omitted++
325
+ } else {
326
+ kept.push(part)
327
+ }
328
+ }
329
+
330
+ const streetAt = kept.findIndex(isINThoroughfare)
331
+
332
+ if (streetAt === -1) return { refused: INStreetLineRefusal.NoThoroughfare }
333
+
334
+ let street = kept[streetAt]!
335
+ let houseNumber: string | undefined
336
+ const before = kept.slice(0, streetAt)
337
+ const after = kept.slice(streetAt + 1)
338
+ const leading = NUMBER_THEN_STREET.exec(street)
339
+
340
+ if (leading && isINThoroughfare(leading[2]!)) {
341
+ houseNumber = leading[1]!
342
+ street = leading[2]!
343
+ } else {
344
+ // The premise number may stand before a building name (`No. 484/2, Rithvi Tower, Perundurai Rd`).
345
+ const at = before.findLastIndex((part) => PREMISE_NUMBER.test(part))
346
+
347
+ if (at !== -1) {
348
+ houseNumber = PREMISE_NUMBER.exec(before[at]!)![1]!
349
+ before.splice(at, 1)
350
+ }
351
+ }
352
+
353
+ if (streetHoldsNumber(street)) return { refused: INStreetLineRefusal.StreetHoldsNumber }
354
+
355
+ if (before.length > 1 || (before.length === 1 && !isINBuilding(before[0]!))) {
356
+ return { refused: INStreetLineRefusal.PremiseUnread }
357
+ }
358
+
359
+ if (after.length > 1 || (after.length === 1 && !SUB_LOCALITY.test(after[0]!))) {
360
+ return { refused: INStreetLineRefusal.SubLocalityUnread }
361
+ }
362
+
363
+ return {
364
+ ...(houseNumber ? { house_number: houseNumber.replaceAll(/\s+/gu, "") } : {}),
365
+ ...(before.length ? { venue: before[0]! } : {}),
366
+ street,
367
+ ...(after.length ? { dependent_locality: after[0]! } : {}),
368
+ omitted,
369
+ }
370
+ }