@mailwoman/resolver-wof-sqlite 9.1.0 → 9.3.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 (501) hide show
  1. package/README.md +36 -20
  2. package/lib/address/index.ts +9 -0
  3. package/{address-point-interpolation.ts → lib/address/point-interpolation.ts} +40 -28
  4. package/{address-point-schema.ts → lib/address/point-schema.ts} +34 -8
  5. package/lib/address/point.ts +280 -0
  6. package/{ancestry-backfill.ts → lib/ancestry/backfill.ts} +14 -19
  7. package/{ancestry.ts → lib/ancestry/index.ts} +13 -8
  8. package/{build-candidate.ts → lib/build-candidate.ts} +243 -199
  9. package/{build-slim.ts → lib/build-slim.ts} +153 -168
  10. package/lib/candidate/alias-bags.ts +56 -0
  11. package/lib/candidate/ancestors-sidecar.ts +204 -0
  12. package/lib/candidate/country-display-names.ts +79 -0
  13. package/lib/candidate/extract-fold.ts +138 -0
  14. package/lib/candidate/name-roles.ts +238 -0
  15. package/lib/candidate/own-name.ts +146 -0
  16. package/lib/candidate/place-attrs.ts +44 -0
  17. package/lib/candidate-ancestors-schema.ts +195 -0
  18. package/{candidate-fts.ts → lib/candidate-fts.ts} +7 -5
  19. package/{candidate-importance.ts → lib/candidate-importance.ts} +54 -57
  20. package/lib/candidate-lookup.ts +976 -0
  21. package/{candidate-schema.ts → lib/candidate-schema.ts} +34 -6
  22. package/lib/candidate-scoring.ts +261 -0
  23. package/lib/capital-schema.ts +89 -0
  24. package/lib/capitals.ts +148 -0
  25. package/{coincident-roles.ts → lib/coincident-roles.ts} +75 -19
  26. package/{convention.ts → lib/convention/index.ts} +5 -5
  27. package/lib/convention/schema.ts +72 -0
  28. package/{coverage-manifest-schema.ts → lib/coverage-manifest-schema.ts} +15 -16
  29. package/lib/currency-backfill.ts +334 -0
  30. package/lib/env.ts +50 -0
  31. package/lib/exact-match.ts +113 -0
  32. package/{sharding.ts → lib/extracts.ts} +63 -60
  33. package/lib/fst/autocomplete.ts +176 -0
  34. package/{fst-builder.ts → lib/fst/builder.ts} +30 -27
  35. package/{fst-deserialize-web.ts → lib/fst/deserialize-web.ts} +27 -87
  36. package/lib/fst/format.ts +106 -0
  37. package/{fst-freshness.ts → lib/fst/freshness.ts} +42 -75
  38. package/lib/fst/index.ts +14 -0
  39. package/{fst-matcher.ts → lib/fst/matcher.ts} +1 -1
  40. package/{fst-serialize.ts → lib/fst/serialize.ts} +37 -115
  41. package/{fst-types.ts → lib/fst/types.ts} +4 -2
  42. package/{fts.ts → lib/fts/index.ts} +28 -23
  43. package/{fts-query.ts → lib/fts/query.ts} +2 -2
  44. package/{geonames-aliases.ts → lib/geonames/aliases.ts} +13 -13
  45. package/lib/geonames/index.ts +8 -0
  46. package/{geonames-postal.ts → lib/geonames/postal.ts} +16 -15
  47. package/{index.ts → lib/index.ts} +52 -48
  48. package/{interpolation.ts → lib/interpolation.ts} +137 -41
  49. package/lib/lookup.ts +861 -0
  50. package/{name-score.ts → lib/name-score.ts} +6 -4
  51. package/lib/nsul/index.ts +8 -0
  52. package/lib/nsul/lookup.ts +144 -0
  53. package/lib/nsul/schema.ts +130 -0
  54. package/{place-importance-schema.ts → lib/place-importance-schema.ts} +70 -22
  55. package/lib/poi/index.ts +8 -0
  56. package/{poi-lookup.ts → lib/poi/lookup.ts} +32 -39
  57. package/{poi-schema.ts → lib/poi/schema.ts} +10 -6
  58. package/lib/polygon-schema.ts +47 -0
  59. package/{postal-city-alias-lookup.ts → lib/postal/city-alias-lookup.ts} +15 -21
  60. package/{postal-city-candidate-schema.ts → lib/postal/city-candidate-schema.ts} +4 -2
  61. package/lib/postal/index.ts +9 -0
  62. package/{postcode-point-lookup.ts → lib/postcode-point-lookup.ts} +17 -15
  63. package/lib/primary-preference.ts +226 -0
  64. package/lib/proximity-rerank.ts +120 -0
  65. package/{ranking-weights.ts → lib/ranking-weights.ts} +30 -4
  66. package/lib/region-keys.ts +144 -0
  67. package/{reverse.ts → lib/reverse.ts} +45 -56
  68. package/{schema.ts → lib/schema.ts} +11 -1
  69. package/lib/search-fetch.ts +257 -0
  70. package/{sqlite-convention-source.ts → lib/sqlite-convention-source.ts} +9 -10
  71. package/lib/sqlite-utils.ts +83 -0
  72. package/{street-centroid-schema.ts → lib/street/centroid-schema.ts} +12 -6
  73. package/{street-centroid.ts → lib/street/centroid.ts} +26 -28
  74. package/lib/street/index.ts +13 -0
  75. package/{street-morphology-fst-builder.ts → lib/street/morphology-fst-builder.ts} +24 -14
  76. package/{street-morphology-fst-loader.ts → lib/street/morphology-fst-loader.ts} +14 -12
  77. package/{street-name-lookup.ts → lib/street/name-lookup.ts} +14 -30
  78. package/lib/street/normalize.ts +572 -0
  79. package/{street-segment-schema.ts → lib/street/segment-schema.ts} +13 -8
  80. package/{types.ts → lib/types.ts} +49 -9
  81. package/{unified-schema.ts → lib/unified-schema.ts} +21 -25
  82. package/lib/uprn/existence.ts +84 -0
  83. package/lib/uprn/index.ts +9 -0
  84. package/lib/uprn/lookup.ts +206 -0
  85. package/lib/uprn/schema.ts +124 -0
  86. package/lib/weights-overlay-linker.ts +878 -0
  87. package/out/address/index.d.ts +9 -0
  88. package/out/address/index.d.ts.map +1 -0
  89. package/out/address/index.js +9 -0
  90. package/out/address/index.js.map +1 -0
  91. package/out/{address-point-interpolation.d.ts → address/point-interpolation.d.ts} +11 -10
  92. package/out/address/point-interpolation.d.ts.map +1 -0
  93. package/out/{address-point-interpolation.js → address/point-interpolation.js} +29 -23
  94. package/out/address/point-interpolation.js.map +1 -0
  95. package/out/{address-point-schema.d.ts → address/point-schema.d.ts} +30 -10
  96. package/out/address/point-schema.d.ts.map +1 -0
  97. package/out/{address-point-schema.js → address/point-schema.js} +7 -3
  98. package/out/address/point-schema.js.map +1 -0
  99. package/out/{address-point.d.ts → address/point.d.ts} +16 -8
  100. package/out/address/point.d.ts.map +1 -0
  101. package/out/address/point.js +191 -0
  102. package/out/address/point.js.map +1 -0
  103. package/out/{ancestry-backfill.d.ts → ancestry/backfill.d.ts} +7 -5
  104. package/out/ancestry/backfill.d.ts.map +1 -0
  105. package/out/{ancestry-backfill.js → ancestry/backfill.js} +12 -16
  106. package/out/ancestry/backfill.js.map +1 -0
  107. package/out/{ancestry.d.ts → ancestry/index.d.ts} +6 -5
  108. package/out/ancestry/index.d.ts.map +1 -0
  109. package/out/{ancestry.js → ancestry/index.js} +7 -7
  110. package/out/ancestry/index.js.map +1 -0
  111. package/out/build-candidate.d.ts +88 -7
  112. package/out/build-candidate.d.ts.map +1 -1
  113. package/out/build-candidate.js +145 -149
  114. package/out/build-candidate.js.map +1 -1
  115. package/out/build-slim.d.ts +5 -5
  116. package/out/build-slim.d.ts.map +1 -1
  117. package/out/build-slim.js +127 -142
  118. package/out/build-slim.js.map +1 -1
  119. package/out/candidate/alias-bags.d.ts +19 -0
  120. package/out/candidate/alias-bags.d.ts.map +1 -0
  121. package/out/candidate/alias-bags.js +39 -0
  122. package/out/candidate/alias-bags.js.map +1 -0
  123. package/out/candidate/ancestors-sidecar.d.ts +32 -0
  124. package/out/candidate/ancestors-sidecar.d.ts.map +1 -0
  125. package/out/candidate/ancestors-sidecar.js +140 -0
  126. package/out/candidate/ancestors-sidecar.js.map +1 -0
  127. package/out/candidate/country-display-names.d.ts +35 -0
  128. package/out/candidate/country-display-names.d.ts.map +1 -0
  129. package/out/candidate/country-display-names.js +59 -0
  130. package/out/candidate/country-display-names.js.map +1 -0
  131. package/out/candidate/extract-fold.d.ts +32 -0
  132. package/out/candidate/extract-fold.d.ts.map +1 -0
  133. package/out/candidate/extract-fold.js +104 -0
  134. package/out/candidate/extract-fold.js.map +1 -0
  135. package/out/candidate/name-roles.d.ts +57 -0
  136. package/out/candidate/name-roles.d.ts.map +1 -0
  137. package/out/candidate/name-roles.js +166 -0
  138. package/out/candidate/name-roles.js.map +1 -0
  139. package/out/candidate/own-name.d.ts +50 -0
  140. package/out/candidate/own-name.d.ts.map +1 -0
  141. package/out/candidate/own-name.js +132 -0
  142. package/out/candidate/own-name.js.map +1 -0
  143. package/out/candidate/place-attrs.d.ts +43 -0
  144. package/out/candidate/place-attrs.d.ts.map +1 -0
  145. package/out/candidate/place-attrs.js +15 -0
  146. package/out/candidate/place-attrs.js.map +1 -0
  147. package/out/candidate-ancestors-schema.d.ts +150 -0
  148. package/out/candidate-ancestors-schema.d.ts.map +1 -0
  149. package/out/candidate-ancestors-schema.js +123 -0
  150. package/out/candidate-ancestors-schema.js.map +1 -0
  151. package/out/candidate-fts.d.ts +7 -5
  152. package/out/candidate-fts.d.ts.map +1 -1
  153. package/out/candidate-fts.js +5 -3
  154. package/out/candidate-fts.js.map +1 -1
  155. package/out/candidate-importance.d.ts +16 -16
  156. package/out/candidate-importance.d.ts.map +1 -1
  157. package/out/candidate-importance.js +47 -52
  158. package/out/candidate-importance.js.map +1 -1
  159. package/out/candidate-lookup.d.ts +31 -52
  160. package/out/candidate-lookup.d.ts.map +1 -1
  161. package/out/candidate-lookup.js +380 -172
  162. package/out/candidate-lookup.js.map +1 -1
  163. package/out/candidate-schema.d.ts +31 -7
  164. package/out/candidate-schema.d.ts.map +1 -1
  165. package/out/candidate-schema.js +3 -0
  166. package/out/candidate-schema.js.map +1 -1
  167. package/out/candidate-scoring.d.ts +34 -0
  168. package/out/candidate-scoring.d.ts.map +1 -0
  169. package/out/candidate-scoring.js +196 -0
  170. package/out/candidate-scoring.js.map +1 -0
  171. package/out/capital-schema.d.ts +51 -0
  172. package/out/capital-schema.d.ts.map +1 -0
  173. package/out/capital-schema.js +63 -0
  174. package/out/capital-schema.js.map +1 -0
  175. package/out/capitals.d.ts +69 -0
  176. package/out/capitals.d.ts.map +1 -0
  177. package/out/capitals.js +98 -0
  178. package/out/capitals.js.map +1 -0
  179. package/out/coincident-roles.d.ts +12 -4
  180. package/out/coincident-roles.d.ts.map +1 -1
  181. package/out/coincident-roles.js +43 -11
  182. package/out/coincident-roles.js.map +1 -1
  183. package/out/{convention.d.ts → convention/index.d.ts} +5 -5
  184. package/out/convention/index.d.ts.map +1 -0
  185. package/out/{convention.js → convention/index.js} +4 -4
  186. package/out/convention/index.js.map +1 -0
  187. package/out/convention/schema.d.ts +51 -0
  188. package/out/convention/schema.d.ts.map +1 -0
  189. package/out/convention/schema.js +34 -0
  190. package/out/convention/schema.js.map +1 -0
  191. package/out/coverage-manifest-schema.d.ts +8 -8
  192. package/out/coverage-manifest-schema.d.ts.map +1 -1
  193. package/out/coverage-manifest-schema.js +6 -10
  194. package/out/coverage-manifest-schema.js.map +1 -1
  195. package/out/currency-backfill.d.ts +90 -0
  196. package/out/currency-backfill.d.ts.map +1 -0
  197. package/out/currency-backfill.js +196 -0
  198. package/out/currency-backfill.js.map +1 -0
  199. package/out/env.d.ts +43 -0
  200. package/out/env.d.ts.map +1 -0
  201. package/out/env.js +48 -0
  202. package/out/env.js.map +1 -0
  203. package/out/exact-match.d.ts +25 -0
  204. package/out/exact-match.d.ts.map +1 -0
  205. package/out/exact-match.js +88 -0
  206. package/out/exact-match.js.map +1 -0
  207. package/out/extracts.d.ts +114 -0
  208. package/out/extracts.d.ts.map +1 -0
  209. package/out/{sharding.js → extracts.js} +38 -38
  210. package/out/extracts.js.map +1 -0
  211. package/out/{fst-autocomplete.d.ts → fst/autocomplete.d.ts} +13 -13
  212. package/out/fst/autocomplete.d.ts.map +1 -0
  213. package/out/fst/autocomplete.js +113 -0
  214. package/out/fst/autocomplete.js.map +1 -0
  215. package/out/{fst-builder.d.ts → fst/builder.d.ts} +5 -5
  216. package/out/fst/builder.d.ts.map +1 -0
  217. package/out/{fst-builder.js → fst/builder.js} +24 -24
  218. package/out/fst/builder.js.map +1 -0
  219. package/out/{fst-deserialize-web.d.ts → fst/deserialize-web.d.ts} +3 -3
  220. package/out/fst/deserialize-web.d.ts.map +1 -0
  221. package/out/{fst-deserialize-web.js → fst/deserialize-web.js} +10 -74
  222. package/out/fst/deserialize-web.js.map +1 -0
  223. package/out/fst/format.d.ts +80 -0
  224. package/out/fst/format.d.ts.map +1 -0
  225. package/out/fst/format.js +91 -0
  226. package/out/fst/format.js.map +1 -0
  227. package/out/{fst-freshness.d.ts → fst/freshness.d.ts} +11 -14
  228. package/out/fst/freshness.d.ts.map +1 -0
  229. package/out/{fst-freshness.js → fst/freshness.js} +42 -74
  230. package/out/fst/freshness.js.map +1 -0
  231. package/out/fst/index.d.ts +14 -0
  232. package/out/fst/index.d.ts.map +1 -0
  233. package/out/fst/index.js +14 -0
  234. package/out/fst/index.js.map +1 -0
  235. package/out/{fst-matcher.d.ts → fst/matcher.d.ts} +2 -2
  236. package/out/fst/matcher.d.ts.map +1 -0
  237. package/out/{fst-matcher.js → fst/matcher.js} +1 -1
  238. package/out/fst/matcher.js.map +1 -0
  239. package/out/{fst-serialize.d.ts → fst/serialize.d.ts} +5 -10
  240. package/out/fst/serialize.d.ts.map +1 -0
  241. package/out/{fst-serialize.js → fst/serialize.js} +18 -98
  242. package/out/fst/serialize.js.map +1 -0
  243. package/out/{fst-types.d.ts → fst/types.d.ts} +4 -3
  244. package/out/fst/types.d.ts.map +1 -0
  245. package/out/{fst-types.js → fst/types.js} +1 -1
  246. package/out/fst/types.js.map +1 -0
  247. package/out/{fts.d.ts → fts/index.d.ts} +20 -13
  248. package/out/fts/index.d.ts.map +1 -0
  249. package/out/{fts.js → fts/index.js} +20 -15
  250. package/out/fts/index.js.map +1 -0
  251. package/out/{fts-query.d.ts → fts/query.d.ts} +2 -2
  252. package/out/fts/query.d.ts.map +1 -0
  253. package/out/{fts-query.js → fts/query.js} +2 -2
  254. package/out/fts/query.js.map +1 -0
  255. package/out/{geonames-aliases.d.ts → geonames/aliases.d.ts} +8 -6
  256. package/out/geonames/aliases.d.ts.map +1 -0
  257. package/out/{geonames-aliases.js → geonames/aliases.js} +7 -7
  258. package/out/geonames/aliases.js.map +1 -0
  259. package/out/geonames/index.d.ts +8 -0
  260. package/out/geonames/index.d.ts.map +1 -0
  261. package/out/geonames/index.js +8 -0
  262. package/out/geonames/index.js.map +1 -0
  263. package/out/{geonames-postal.d.ts → geonames/postal.d.ts} +12 -10
  264. package/out/geonames/postal.d.ts.map +1 -0
  265. package/out/{geonames-postal.js → geonames/postal.js} +11 -11
  266. package/out/geonames/postal.js.map +1 -0
  267. package/out/index.d.ts +32 -31
  268. package/out/index.d.ts.map +1 -1
  269. package/out/index.js +24 -24
  270. package/out/index.js.map +1 -1
  271. package/out/interpolation.d.ts +18 -9
  272. package/out/interpolation.d.ts.map +1 -1
  273. package/out/interpolation.js +108 -36
  274. package/out/interpolation.js.map +1 -1
  275. package/out/lookup.d.ts +25 -25
  276. package/out/lookup.d.ts.map +1 -1
  277. package/out/lookup.js +174 -558
  278. package/out/lookup.js.map +1 -1
  279. package/out/name-score.d.ts +0 -10
  280. package/out/name-score.d.ts.map +1 -1
  281. package/out/name-score.js +6 -4
  282. package/out/name-score.js.map +1 -1
  283. package/out/nsul/index.d.ts +8 -0
  284. package/out/nsul/index.d.ts.map +1 -0
  285. package/out/nsul/index.js +8 -0
  286. package/out/nsul/index.js.map +1 -0
  287. package/out/nsul/lookup.d.ts +83 -0
  288. package/out/nsul/lookup.d.ts.map +1 -0
  289. package/out/nsul/lookup.js +80 -0
  290. package/out/nsul/lookup.js.map +1 -0
  291. package/out/nsul/schema.d.ts +100 -0
  292. package/out/nsul/schema.d.ts.map +1 -0
  293. package/out/nsul/schema.js +78 -0
  294. package/out/nsul/schema.js.map +1 -0
  295. package/out/place-importance-schema.d.ts +47 -10
  296. package/out/place-importance-schema.d.ts.map +1 -1
  297. package/out/place-importance-schema.js +56 -10
  298. package/out/place-importance-schema.js.map +1 -1
  299. package/out/poi/index.d.ts +8 -0
  300. package/out/poi/index.d.ts.map +1 -0
  301. package/out/poi/index.js +8 -0
  302. package/out/poi/index.js.map +1 -0
  303. package/out/{poi-lookup.d.ts → poi/lookup.d.ts} +8 -10
  304. package/out/poi/lookup.d.ts.map +1 -0
  305. package/out/{poi-lookup.js → poi/lookup.js} +24 -29
  306. package/out/poi/lookup.js.map +1 -0
  307. package/out/{poi-schema.d.ts → poi/schema.d.ts} +10 -6
  308. package/out/poi/schema.d.ts.map +1 -0
  309. package/out/{poi-schema.js → poi/schema.js} +1 -1
  310. package/out/poi/schema.js.map +1 -0
  311. package/out/polygon-schema.d.ts +37 -0
  312. package/out/polygon-schema.d.ts.map +1 -0
  313. package/out/polygon-schema.js +23 -0
  314. package/out/polygon-schema.js.map +1 -0
  315. package/out/{postal-city-alias-lookup.d.ts → postal/city-alias-lookup.d.ts} +7 -6
  316. package/out/postal/city-alias-lookup.d.ts.map +1 -0
  317. package/out/{postal-city-alias-lookup.js → postal/city-alias-lookup.js} +12 -16
  318. package/out/postal/city-alias-lookup.js.map +1 -0
  319. package/out/{postal-city-alias-schema.d.ts → postal/city-alias-schema.d.ts} +1 -1
  320. package/out/{postal-city-alias-schema.d.ts.map → postal/city-alias-schema.d.ts.map} +1 -1
  321. package/out/{postal-city-alias-schema.js → postal/city-alias-schema.js} +1 -1
  322. package/out/{postal-city-alias-schema.js.map → postal/city-alias-schema.js.map} +1 -1
  323. package/out/{postal-city-candidate-schema.d.ts → postal/city-candidate-schema.d.ts} +4 -3
  324. package/out/postal/city-candidate-schema.d.ts.map +1 -0
  325. package/out/{postal-city-candidate-schema.js → postal/city-candidate-schema.js} +2 -2
  326. package/out/{postal-city-candidate-schema.js.map → postal/city-candidate-schema.js.map} +1 -1
  327. package/out/postal/index.d.ts +9 -0
  328. package/out/postal/index.d.ts.map +1 -0
  329. package/out/postal/index.js +9 -0
  330. package/out/postal/index.js.map +1 -0
  331. package/out/postcode-point-lookup.d.ts +10 -10
  332. package/out/postcode-point-lookup.d.ts.map +1 -1
  333. package/out/postcode-point-lookup.js +13 -13
  334. package/out/postcode-point-lookup.js.map +1 -1
  335. package/out/primary-preference.d.ts +133 -0
  336. package/out/primary-preference.d.ts.map +1 -0
  337. package/out/primary-preference.js +146 -0
  338. package/out/primary-preference.js.map +1 -0
  339. package/out/proximity-rerank.d.ts +77 -0
  340. package/out/proximity-rerank.d.ts.map +1 -0
  341. package/out/proximity-rerank.js +86 -0
  342. package/out/proximity-rerank.js.map +1 -0
  343. package/out/ranking-weights.d.ts +15 -3
  344. package/out/ranking-weights.d.ts.map +1 -1
  345. package/out/ranking-weights.js +21 -3
  346. package/out/ranking-weights.js.map +1 -1
  347. package/out/region-keys.d.ts +47 -0
  348. package/out/region-keys.d.ts.map +1 -0
  349. package/out/region-keys.js +121 -0
  350. package/out/region-keys.js.map +1 -0
  351. package/out/reverse.d.ts +6 -6
  352. package/out/reverse.d.ts.map +1 -1
  353. package/out/reverse.js +23 -35
  354. package/out/reverse.js.map +1 -1
  355. package/out/schema.d.ts +10 -1
  356. package/out/schema.d.ts.map +1 -1
  357. package/out/schema.js.map +1 -1
  358. package/out/search-fetch.d.ts +59 -0
  359. package/out/search-fetch.d.ts.map +1 -0
  360. package/out/search-fetch.js +184 -0
  361. package/out/search-fetch.js.map +1 -0
  362. package/out/sqlite-convention-source.d.ts +7 -7
  363. package/out/sqlite-convention-source.d.ts.map +1 -1
  364. package/out/sqlite-convention-source.js +5 -5
  365. package/out/sqlite-convention-source.js.map +1 -1
  366. package/out/sqlite-utils.d.ts +24 -6
  367. package/out/sqlite-utils.d.ts.map +1 -1
  368. package/out/sqlite-utils.js +24 -7
  369. package/out/sqlite-utils.js.map +1 -1
  370. package/out/{street-centroid-schema.d.ts → street/centroid-schema.d.ts} +12 -7
  371. package/out/street/centroid-schema.d.ts.map +1 -0
  372. package/out/{street-centroid-schema.js → street/centroid-schema.js} +5 -5
  373. package/out/{street-centroid-schema.js.map → street/centroid-schema.js.map} +1 -1
  374. package/out/{street-centroid.d.ts → street/centroid.d.ts} +6 -6
  375. package/out/street/centroid.d.ts.map +1 -0
  376. package/out/{street-centroid.js → street/centroid.js} +18 -24
  377. package/out/street/centroid.js.map +1 -0
  378. package/out/street/index.d.ts +13 -0
  379. package/out/street/index.d.ts.map +1 -0
  380. package/out/street/index.js +13 -0
  381. package/out/street/index.js.map +1 -0
  382. package/out/{street-morphology-fst-builder.d.ts → street/morphology-fst-builder.d.ts} +4 -4
  383. package/out/street/morphology-fst-builder.d.ts.map +1 -0
  384. package/out/{street-morphology-fst-builder.js → street/morphology-fst-builder.js} +18 -13
  385. package/out/street/morphology-fst-builder.js.map +1 -0
  386. package/out/{street-morphology-fst-loader.d.ts → street/morphology-fst-loader.d.ts} +5 -5
  387. package/out/street/morphology-fst-loader.d.ts.map +1 -0
  388. package/out/{street-morphology-fst-loader.js → street/morphology-fst-loader.js} +11 -10
  389. package/out/street/morphology-fst-loader.js.map +1 -0
  390. package/out/{street-name-lookup.d.ts → street/name-lookup.d.ts} +4 -7
  391. package/out/street/name-lookup.d.ts.map +1 -0
  392. package/out/{street-name-lookup.js → street/name-lookup.js} +9 -23
  393. package/out/street/name-lookup.js.map +1 -0
  394. package/out/street/normalize.d.ts +179 -0
  395. package/out/street/normalize.d.ts.map +1 -0
  396. package/out/street/normalize.js +457 -0
  397. package/out/street/normalize.js.map +1 -0
  398. package/out/{street-segment-schema.d.ts → street/segment-schema.d.ts} +13 -9
  399. package/out/street/segment-schema.d.ts.map +1 -0
  400. package/out/{street-segment-schema.js → street/segment-schema.js} +4 -4
  401. package/out/{street-segment-schema.js.map → street/segment-schema.js.map} +1 -1
  402. package/out/types.d.ts +49 -9
  403. package/out/types.d.ts.map +1 -1
  404. package/out/types.js.map +1 -1
  405. package/out/unified-schema.d.ts +6 -5
  406. package/out/unified-schema.d.ts.map +1 -1
  407. package/out/unified-schema.js +16 -19
  408. package/out/unified-schema.js.map +1 -1
  409. package/out/uprn/existence.d.ts +53 -0
  410. package/out/uprn/existence.d.ts.map +1 -0
  411. package/out/uprn/existence.js +61 -0
  412. package/out/uprn/existence.js.map +1 -0
  413. package/out/uprn/index.d.ts +9 -0
  414. package/out/uprn/index.d.ts.map +1 -0
  415. package/out/uprn/index.js +9 -0
  416. package/out/uprn/index.js.map +1 -0
  417. package/out/uprn/lookup.d.ts +85 -0
  418. package/out/uprn/lookup.d.ts.map +1 -0
  419. package/out/uprn/lookup.js +149 -0
  420. package/out/uprn/lookup.js.map +1 -0
  421. package/out/uprn/schema.d.ts +93 -0
  422. package/out/uprn/schema.d.ts.map +1 -0
  423. package/out/uprn/schema.js +78 -0
  424. package/out/uprn/schema.js.map +1 -0
  425. package/out/weights-overlay-linker.d.ts +299 -0
  426. package/out/weights-overlay-linker.d.ts.map +1 -0
  427. package/out/weights-overlay-linker.js +511 -0
  428. package/out/weights-overlay-linker.js.map +1 -0
  429. package/package.json +622 -154
  430. package/address-point.ts +0 -125
  431. package/candidate-lookup.ts +0 -720
  432. package/fst-autocomplete.ts +0 -204
  433. package/geo.ts +0 -121
  434. package/lookup.ts +0 -1354
  435. package/out/address-point-interpolation.d.ts.map +0 -1
  436. package/out/address-point-interpolation.js.map +0 -1
  437. package/out/address-point-schema.d.ts.map +0 -1
  438. package/out/address-point-schema.js.map +0 -1
  439. package/out/address-point.d.ts.map +0 -1
  440. package/out/address-point.js +0 -84
  441. package/out/address-point.js.map +0 -1
  442. package/out/ancestry-backfill.d.ts.map +0 -1
  443. package/out/ancestry-backfill.js.map +0 -1
  444. package/out/ancestry.d.ts.map +0 -1
  445. package/out/ancestry.js.map +0 -1
  446. package/out/convention.d.ts.map +0 -1
  447. package/out/convention.js.map +0 -1
  448. package/out/fst-autocomplete.d.ts.map +0 -1
  449. package/out/fst-autocomplete.js +0 -130
  450. package/out/fst-autocomplete.js.map +0 -1
  451. package/out/fst-builder.d.ts.map +0 -1
  452. package/out/fst-builder.js.map +0 -1
  453. package/out/fst-deserialize-web.d.ts.map +0 -1
  454. package/out/fst-deserialize-web.js.map +0 -1
  455. package/out/fst-freshness.d.ts.map +0 -1
  456. package/out/fst-freshness.js.map +0 -1
  457. package/out/fst-matcher.d.ts.map +0 -1
  458. package/out/fst-matcher.js.map +0 -1
  459. package/out/fst-serialize.d.ts.map +0 -1
  460. package/out/fst-serialize.js.map +0 -1
  461. package/out/fst-types.d.ts.map +0 -1
  462. package/out/fst-types.js.map +0 -1
  463. package/out/fts-query.d.ts.map +0 -1
  464. package/out/fts-query.js.map +0 -1
  465. package/out/fts.d.ts.map +0 -1
  466. package/out/fts.js.map +0 -1
  467. package/out/geo.d.ts +0 -74
  468. package/out/geo.d.ts.map +0 -1
  469. package/out/geo.js +0 -71
  470. package/out/geo.js.map +0 -1
  471. package/out/geonames-aliases.d.ts.map +0 -1
  472. package/out/geonames-aliases.js.map +0 -1
  473. package/out/geonames-postal.d.ts.map +0 -1
  474. package/out/geonames-postal.js.map +0 -1
  475. package/out/poi-lookup.d.ts.map +0 -1
  476. package/out/poi-lookup.js.map +0 -1
  477. package/out/poi-schema.d.ts.map +0 -1
  478. package/out/poi-schema.js.map +0 -1
  479. package/out/postal-city-alias-lookup.d.ts.map +0 -1
  480. package/out/postal-city-alias-lookup.js.map +0 -1
  481. package/out/postal-city-candidate-schema.d.ts.map +0 -1
  482. package/out/sharding.d.ts +0 -114
  483. package/out/sharding.d.ts.map +0 -1
  484. package/out/sharding.js.map +0 -1
  485. package/out/street-centroid-schema.d.ts.map +0 -1
  486. package/out/street-centroid.d.ts.map +0 -1
  487. package/out/street-centroid.js.map +0 -1
  488. package/out/street-morphology-fst-builder.d.ts.map +0 -1
  489. package/out/street-morphology-fst-builder.js.map +0 -1
  490. package/out/street-morphology-fst-loader.d.ts.map +0 -1
  491. package/out/street-morphology-fst-loader.js.map +0 -1
  492. package/out/street-name-lookup.d.ts.map +0 -1
  493. package/out/street-name-lookup.js.map +0 -1
  494. package/out/street-normalize.d.ts +0 -95
  495. package/out/street-normalize.d.ts.map +0 -1
  496. package/out/street-normalize.js +0 -272
  497. package/out/street-normalize.js.map +0 -1
  498. package/out/street-segment-schema.d.ts.map +0 -1
  499. package/sqlite-utils.ts +0 -46
  500. package/street-normalize.ts +0 -318
  501. /package/{postal-city-alias-schema.ts → lib/postal/city-alias-schema.ts} +0 -0
@@ -0,0 +1,204 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ * @file Pass 3b of the candidate build — the containment sidecar (closure rows + interval labels).
6
+ */
7
+
8
+ import type { DatabaseClient } from "@mailwoman/sqlite/client"
9
+
10
+ import { placetypeDepth } from "#ancestry/index"
11
+ import {
12
+ CANDIDATE_ANCESTOR_COLUMNS,
13
+ CANDIDATE_ANCESTOR_TABLE,
14
+ CANDIDATE_INTERVAL_TABLE,
15
+ createCandidateAncestorTable,
16
+ createCandidateIntervalTable,
17
+ MAX_ANCESTOR_DEPTH,
18
+ } from "#candidate-ancestors-schema"
19
+ import type { CandidateDatabase } from "#candidate-schema"
20
+ import type { PlaceAttrs } from "#candidate/place-attrs"
21
+ import type { WOFDatabase } from "#schema"
22
+
23
+ /**
24
+ * Pass 3b — the ancestors sidecar: closure rows + interval labels (candidate-ancestors-schema.ts owns the encoding
25
+ * decision and the DAG/absence semantics). Reads the same source `ancestors` table the region stamp reads,
26
+ * denormalizing each edge with the parent's name/key from `attrs`, streamed `ORDER BY id` so the clustered `(spr_id,
27
+ * depth)` insert is sorted — the contiguous-leaves discipline of the candidate table itself.
28
+ *
29
+ * Excluded by policy: self rows, and placetypes outside the containment ladder (continent, empire, …: `placetypeDepth` 0)
30
+ * — they discriminate nothing a consumer of this sidecar checks. An edge to a parent with no current `spr` row has no
31
+ * name to denormalize; it is dropped and counted rather than stored blind.
32
+ */
33
+ export async function buildAncestorsSidecar(ctx: {
34
+ src: DatabaseClient<WOFDatabase>
35
+ out: DatabaseClient<CandidateDatabase>
36
+ attrs: Map<number, PlaceAttrs>
37
+ ptID: (pt: string | null) => number
38
+ progress: (phase: string, message: string) => void
39
+ }): Promise<{ ancestorRows: number; ancestorPlaces: number; intervalPlaces: number }> {
40
+ const { src, out, attrs, ptID, progress } = ctx
41
+
42
+ progress("ancestors", "building containment sidecar (closure rows + interval labels)")
43
+ await createCandidateAncestorTable(ctx.out)
44
+ await createCandidateIntervalTable(ctx.out)
45
+
46
+ const insAncestor = out.prepare(
47
+ `INSERT INTO ${CANDIDATE_ANCESTOR_TABLE} VALUES (${CANDIDATE_ANCESTOR_COLUMNS.map(() => "?").join(", ")})`
48
+ )
49
+
50
+ // The canonical-parent forest the interval labels are computed over. One parent per place — the
51
+ // depth-1 edge (finest containment tier, lowest ancestor id; the `regionOf` MIN-stability
52
+ // convention). ALL parents stay in the closure rows; only the interval tree canonicalizes.
53
+ const canonicalParentOf = new Map<number, number>()
54
+ const childrenOf = new Map<number, number[]>()
55
+ const forest = new Set<number>()
56
+
57
+ let ancestorRows = 0
58
+ let ancestorPlaces = 0
59
+ let droppedParents = 0
60
+
61
+ // Per-child edge buffer; the stream below is grouped by child id, so each flush owns one place.
62
+ let childID = -1
63
+ let edges: Array<{ aid: number; apt: string }> = []
64
+
65
+ const flush = (): void => {
66
+ if (childID < 0 || !edges.length) return
67
+
68
+ // Deterministic nearest-first: containment depth descending, then ancestor id ascending —
69
+ // the FTS backend's `ancestorLineage` ordering, made stable across rebuilds.
70
+ edges.sort((a, b) => placetypeDepth(b.apt) - placetypeDepth(a.apt) || a.aid - b.aid)
71
+
72
+ if (edges.length > MAX_ANCESTOR_DEPTH) {
73
+ edges = edges.slice(0, MAX_ANCESTOR_DEPTH)
74
+ }
75
+
76
+ ancestorPlaces++
77
+
78
+ for (const [i, edge] of edges.entries()) {
79
+ const parent = attrs.get(edge.aid)!
80
+
81
+ insAncestor.run(childID, i + 1, edge.aid, ptID(edge.apt), parent.name, parent.pkey)
82
+
83
+ ancestorRows++
84
+ }
85
+
86
+ const canonical = edges[0]!.aid
87
+
88
+ canonicalParentOf.set(childID, canonical)
89
+
90
+ const siblings = childrenOf.get(canonical)
91
+
92
+ if (siblings) {
93
+ siblings.push(childID)
94
+ } else {
95
+ childrenOf.set(canonical, [childID])
96
+ }
97
+
98
+ forest.add(childID)
99
+ forest.add(canonical)
100
+ }
101
+
102
+ out.exec("BEGIN")
103
+
104
+ for (const r of src
105
+ .prepare("SELECT id, ancestor_id, ancestor_placetype FROM ancestors WHERE ancestor_id != id ORDER BY id")
106
+ .iterate()) {
107
+ const id = Number(r.id)
108
+
109
+ if (id !== childID) {
110
+ flush()
111
+ childID = id
112
+ edges = []
113
+ }
114
+
115
+ if (!attrs.has(id)) continue
116
+
117
+ const apt = String(r.ancestor_placetype ?? "")
118
+
119
+ if (placetypeDepth(apt) === 0) continue
120
+
121
+ const aid = Number(r.ancestor_id)
122
+
123
+ if (!attrs.has(aid)) {
124
+ droppedParents++
125
+
126
+ continue
127
+ }
128
+
129
+ edges.push({ aid, apt })
130
+ }
131
+
132
+ flush()
133
+ out.exec("COMMIT")
134
+
135
+ // Interval labels: pre/post-order DFS over the canonical-parent forest. Root order and child
136
+ // order are id-ascending so the labels are stable across rebuilds of the same source.
137
+ const preOf = new Map<number, number>()
138
+ const postOf = new Map<number, number>()
139
+
140
+ for (const kids of childrenOf.values()) {
141
+ // oxlint-disable-next-line unicorn/no-array-sort -- sorts an array this pass just built
142
+ kids.sort((a, b) => a - b)
143
+ }
144
+
145
+ const roots = [...forest].filter((id) => !canonicalParentOf.has(id))
146
+
147
+ // oxlint-disable-next-line unicorn/no-array-sort -- sorts an array this pass just built
148
+ roots.sort((a, b) => a - b)
149
+
150
+ let counter = 0
151
+
152
+ for (const root of roots) {
153
+ preOf.set(root, counter++)
154
+ const stack: Array<{ id: number; next: number }> = [{ id: root, next: 0 }]
155
+
156
+ while (stack.length) {
157
+ const top = stack.at(-1)!
158
+ const kids = childrenOf.get(top.id)
159
+
160
+ if (kids && top.next < kids.length) {
161
+ const kid = kids[top.next++]!
162
+
163
+ // Each child holds exactly one canonical parent, so a labeled node here means the
164
+ // grouping upstream broke — skip rather than corrupt the numbering.
165
+ if (preOf.has(kid)) continue
166
+
167
+ preOf.set(kid, counter++)
168
+ stack.push({ id: kid, next: 0 })
169
+ } else {
170
+ postOf.set(top.id, counter++)
171
+ stack.pop()
172
+ }
173
+ }
174
+ }
175
+
176
+ // A canonical-parent CYCLE (corrupt source ancestry) leaves its members unreachable from any
177
+ // root: they simply receive no label, and containment against them reads unverifiable — the
178
+ // absence semantics the schema module states. Counted so a jump is visible across rebuilds.
179
+ const cycleSkipped = forest.size - preOf.size
180
+
181
+ const insInterval = out.prepare(`INSERT INTO ${CANDIDATE_INTERVAL_TABLE} VALUES (?, ?, ?)`)
182
+ const labeled = [...preOf.keys()]
183
+
184
+ // oxlint-disable-next-line unicorn/no-array-sort -- sorts an array this pass just built
185
+ labeled.sort((a, b) => a - b)
186
+
187
+ out.exec("BEGIN")
188
+
189
+ for (const id of labeled) {
190
+ insInterval.run(id, preOf.get(id)!, postOf.get(id)!)
191
+ }
192
+
193
+ out.exec("COMMIT")
194
+
195
+ progress(
196
+ "ancestors",
197
+ `${ancestorRows.toLocaleString()} closure rows across ${ancestorPlaces.toLocaleString()} places; ` +
198
+ `${preOf.size.toLocaleString()} interval labels` +
199
+ (droppedParents ? `; ${droppedParents.toLocaleString()} edges dropped (parent has no current spr row)` : "") +
200
+ (cycleSkipped ? `; ${cycleSkipped.toLocaleString()} places skipped (canonical-parent cycle)` : "")
201
+ )
202
+
203
+ return { ancestorRows, ancestorPlaces, intervalPlaces: preOf.size }
204
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ * @file Pass 1b of the candidate build — fold ICU's country display names onto the country rows.
6
+ */
7
+
8
+ import { enumerateCountryDisplayNames } from "@mailwoman/codex/country"
9
+
10
+ import type { PlaceAttrs, StageRow } from "#candidate/place-attrs"
11
+ import { normalizeLocalityForKey } from "#street/normalize"
12
+
13
+ /**
14
+ * Fold every country surface ICU knows onto that country's candidate row (#1678 thread 1).
15
+ *
16
+ * A bare `格鲁吉亚` (Georgia the country) resolved to NOTHING while `佐治亚州` (Georgia the US state) resolved correctly, and
17
+ * the model gave both the same wrong `locality` tag — so the tag was never the variable. Measured 2026-08-15: 140 of
18
+ * 237 country rows are synthetic and carry a canonical English name and nothing else; WOF holds no Chinese country
19
+ * names at all; and the GeoNames alias fold filters through a Latin-script regex, so neither existing source could ever
20
+ * supply them.
21
+ *
22
+ * `Intl.DisplayNames` already knows every one — ~280 regions, ~5,244 surfaces, from the same ICU the runtime uses for
23
+ * every other locale-sensitive operation. No download, no vendored corpus, no snapshot to drift.
24
+ *
25
+ * `is_primary = 0`: these are NAMES THE WORLD USES, not the country's canonical name. The display `name` stays whatever
26
+ * the gazetteer already had, so resolving `格鲁吉亚` answers with the Georgia country row rather than renaming it.
27
+ *
28
+ * Returns the row count so the caller can report it — a zero means ICU supplied nothing, which is a different fact from
29
+ * the pass not having run.
30
+ */
31
+ export function stageCountryDisplayNames(ctx: {
32
+ attrs: Map<number, PlaceAttrs>
33
+ iso2ByID: Map<number, string>
34
+ countryPtID: number
35
+ stageRow: StageRow
36
+ tx: { exec(sql: string): void }
37
+ }): number {
38
+ // One country row per ISO2. Where a code has several (historic rows surviving the is_current filter), the most
39
+ // populous wins — the same tiebreak the ranking uses everywhere else.
40
+ const countryByISO2 = new Map<string, { sid: number; a: PlaceAttrs }>()
41
+
42
+ for (const [sid, a] of ctx.attrs) {
43
+ if (a.ptid !== ctx.countryPtID) continue
44
+
45
+ const iso2 = ctx.iso2ByID.get(a.cid)
46
+
47
+ if (!iso2 || iso2 === "??") continue
48
+
49
+ const held = countryByISO2.get(iso2)
50
+
51
+ if (!held || a.pop > held.a.pop) {
52
+ countryByISO2.set(iso2, { sid, a })
53
+ }
54
+ }
55
+
56
+ let staged = 0
57
+
58
+ ctx.tx.exec("BEGIN")
59
+
60
+ for (const { iso2, name } of enumerateCountryDisplayNames()) {
61
+ const target = countryByISO2.get(iso2)
62
+
63
+ if (!target) continue
64
+
65
+ const k = normalizeLocalityForKey(name)
66
+
67
+ // The country's own key is already staged as its primary; INSERT OR IGNORE at materialization dedupes the
68
+ // rest, so this only skips the obvious self-alias.
69
+ if (!k || k === target.a.pkey) continue
70
+
71
+ ctx.stageRow(k, target.a, target.sid, 0)
72
+
73
+ staged++
74
+ }
75
+
76
+ ctx.tx.exec("COMMIT")
77
+
78
+ return staged
79
+ }
@@ -0,0 +1,138 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ * @file Pass 4 of the candidate build — fold a postcode or locality extract into the staging table.
6
+ */
7
+
8
+ import { DatabaseClient } from "@mailwoman/sqlite/client"
9
+ import { tableExists } from "@mailwoman/sqlite/introspection"
10
+
11
+ import type { CandidateDatabase } from "#candidate-schema"
12
+ import type { PlaceAttrs, StageRow } from "#candidate/place-attrs"
13
+ import type { WOFDatabase } from "#schema"
14
+ import { normalizeLocalityForKey } from "#street/normalize"
15
+
16
+ /**
17
+ * Fold ONE extract (`spr` rows at `extractPlacetype` carrying real coordinates) in, then pass 4b: the alias names
18
+ * hanging off that same extract's `names` table.
19
+ *
20
+ * Self-contained by construction — it shares only the staging statement and the code dictionaries with the admin passes
21
+ * above it, and nothing downstream reads anything it produces except the two counters it returns.
22
+ */
23
+ export function foldExtract(ctx: {
24
+ /**
25
+ * The staging connection. The extract itself is opened read-only here and closed before returning.
26
+ */
27
+ out: DatabaseClient<CandidateDatabase>
28
+ extractPath: string
29
+ extractPlacetype: "postalcode" | "locality"
30
+ ccID: (code: string | null) => number
31
+ ptID: (pt: string | null) => number
32
+ stageRow: StageRow
33
+ progress: (phase: string, message: string) => void
34
+ }): { primaries: number; aliases: number } {
35
+ const { out, extractPath, extractPlacetype, ccID, ptID, stageRow, progress } = ctx
36
+
37
+ progress(extractPlacetype === "postalcode" ? "postcodes" : "localities", `reading ${extractPath}`)
38
+
39
+ using pc = new DatabaseClient<WOFDatabase>(extractPath, { readOnly: true })
40
+ const pcPtid = ptID(extractPlacetype)
41
+ // Per-extract, not the admin `attrs` map: pass 1 only ever sees the admin DB, so the alias pass
42
+ // below has nothing to join against unless this primary loop records what it staged.
43
+ const pcAttrs = new Map<number, PlaceAttrs>()
44
+ let primaries = 0
45
+ let aliases = 0
46
+
47
+ out.exec("BEGIN")
48
+
49
+ for (const r of pc
50
+ .prepare(
51
+ `SELECT id, name, country, latitude, longitude,
52
+ min_latitude AS mnlat, min_longitude AS mnlon, max_latitude AS mxlat, max_longitude AS mxlon
53
+ FROM spr WHERE placetype = ? AND latitude != 0 AND longitude != 0`
54
+ )
55
+ .iterate(extractPlacetype)) {
56
+ const name = String(r.name ?? "")
57
+ const key = normalizeLocalityForKey(name)
58
+
59
+ if (!key) continue
60
+
61
+ const lat = r.latitude as number
62
+ const lon = r.longitude as number
63
+
64
+ // region_id 0 (a postcode is unique by name+country — no same-name disambiguation); neg_rank 0
65
+ // (no population). bbox = the postcode's own min/max (falls back to the centroid point).
66
+ const a: PlaceAttrs = {
67
+ cid: ccID(r.country as string | null),
68
+ rid: 0,
69
+ ptid: pcPtid,
70
+ name,
71
+ lat,
72
+ lon,
73
+ mnLat: (r.mnlat as number) || lat,
74
+ mnLon: (r.mnlon as number) || lon,
75
+ mxLat: (r.mxlat as number) || lat,
76
+ mxLon: (r.mxlon as number) || lon,
77
+ pop: 0,
78
+ neg: 0,
79
+ pkey: key,
80
+ // A postcode has no toponym fame — nobody writes an encyclopedia article about SW1A 2AA — and
81
+ // the score source carries no `postalcode` rows to join against anyway. NULL is the truthful
82
+ // value: unmeasured, so the ranking key leaves postcode rows exactly where they were.
83
+ imp: null,
84
+ }
85
+
86
+ pcAttrs.set(Number(r.id), a)
87
+ stageRow(key, a, Number(r.id), 1)
88
+
89
+ primaries++
90
+ }
91
+
92
+ out.exec("COMMIT")
93
+
94
+ // --- pass 4b: postcode ALIAS names (#1495) ---
95
+ //
96
+ // The delivery-city names GeoNames supplies for a ZIP ("Brooklyn" for 11201) are written into
97
+ // the extract's `names` table by `postcode/centroid-fills.ts`'s `geonamesNameFill`. Everything
98
+ // downstream of `names` picked them up EXCEPT this build: `fts.ts` unions `spr.name` with every
99
+ // `names` row into `place_search.alt_names`, so the FTS backend resolved "Brooklyn" → 11201
100
+ // while the candidate backend — whose every row IS an exact-tier row — had no key for it at
101
+ // all. Pass 2 does the equivalent fold for admin places, but reads the ADMIN `place_search`,
102
+ // and `attrs` holds admin ids only, so a postcode extract could never reach it.
103
+ //
104
+ // Same discipline as pass 2: `is_primary = 0` (so `rankByPrimaryPreference` treats it as an
105
+ // alias, not a canonical postcode name), the row stays denormalized onto the POSTCODE's own
106
+ // spr_id/coords/bbox, and the display `name` stays the postcode — resolving "brooklyn" answers
107
+ // with place 11201, it does not rename the place to its delivery city.
108
+ const hasNames = tableExists(pc, "names")
109
+
110
+ if (hasNames) {
111
+ out.exec("BEGIN")
112
+
113
+ for (const r of pc.prepare("SELECT id, name FROM names").iterate()) {
114
+ const a = pcAttrs.get(Number(r.id))
115
+
116
+ if (!a) continue
117
+
118
+ const k = normalizeLocalityForKey(String(r.name ?? ""))
119
+
120
+ // The postcode's own key is already staged as the primary; `INSERT OR IGNORE` at
121
+ // materialization dedupes repeats, so this only skips the obvious self-alias.
122
+ if (!k || k === a.pkey) continue
123
+
124
+ stageRow(k, a, Number(r.id), 0)
125
+
126
+ aliases++
127
+ }
128
+
129
+ out.exec("COMMIT")
130
+ } else {
131
+ // Never a silent zero: real extracts come from `createUnifiedSchema`, which always creates
132
+ // `names`. A extract without it has no alias surface to lose, but say so rather than reporting
133
+ // "0 aliases" from a table that was never read.
134
+ progress("postcode-aliases", `${extractPath} has no \`names\` table — no delivery-city aliases to fold`)
135
+ }
136
+
137
+ return { primaries, aliases }
138
+ }
@@ -0,0 +1,238 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ * @file Pass 3c of the candidate build — the `name_role` detectors and the thresholds they are judged by.
6
+ */
7
+
8
+ import { isOfficialLanguage } from "@mailwoman/codex/country"
9
+ import type { DatabaseClient } from "@mailwoman/sqlite/client"
10
+ import { tableExists } from "@mailwoman/sqlite/introspection"
11
+
12
+ import type { CandidateDatabase } from "#candidate-schema"
13
+ import { isOwnNameVariant } from "#candidate/own-name"
14
+ import type { PlaceAttrs } from "#candidate/place-attrs"
15
+ import type { WOFDatabase } from "#schema"
16
+ import { normalizeLocalityForKey } from "#street/normalize"
17
+
18
+ /**
19
+ * Key-count threshold for the gloss anomaly detector (#1730) — the sweep's own boundary: 4,000 places carried >= 50
20
+ * keys, and a legitimate famous place at that count (New York, 176 keys) is separated by the PROMINENCE check, never by
21
+ * this number alone.
22
+ */
23
+ export const GLOSS_KEY_THRESHOLD = 50
24
+
25
+ /**
26
+ * Placetypes the gloss detector never flags. A country or region legitimately carries a name in every language — that
27
+ * is what an exonym set IS — so key volume discriminates nothing there. The detector's population is the non-admin
28
+ * tail, where a place named by a common noun ("Poisson", "Sunday") accumulating 200+ translations is a
29
+ * machine-translated gloss set, not fame.
30
+ */
31
+ export const GLOSS_EXCLUDED_PLACETYPES: ReadonlySet<string> = new Set([
32
+ "country",
33
+ "dependency",
34
+ "disputed",
35
+ "empire",
36
+ "macroregion",
37
+ "region",
38
+ "macrocounty",
39
+ "county",
40
+ ])
41
+
42
+ /**
43
+ * Pass 3c — the #1730 name-role prototype: two independent detectors over the staged rows, WRITE-ONLY in this
44
+ * generation (no ranking consumer; the rank penalty is its own D-rule-conditional step with the `gloss_key` board as
45
+ * regression check). Both stamp `is_primary = 0` rows only — a place's canonical name and the `place_abbr` region
46
+ * abbreviations are never a gloss or a variant.
47
+ *
48
+ * - `gloss` is ANOMALY-based, and stamps only the certain core: key volume at/over the threshold + a non-admin placetype
49
+ *
50
+ * - NO measured prominence (population absent AND importance unmeasured). Provenance cannot separate a gloss from an
51
+ * exonym — WOF imported both as `x_preferred` — and prominence is what rescues New York/Paris.
52
+ * - `abbr` is PROVENANCE-based — the #936 signal: a WOF `variant` name in one of the country's official languages (or
53
+ * English), measured there at a 13× key-collision rate. A source without a `names` table (fixture-scale admin DBs)
54
+ * skips this detector loudly.
55
+ *
56
+ * Returns the stamp counts plus the census the prototype exists to report: how much of the ≥-threshold key tail carries
57
+ * any role.
58
+ */
59
+ export function stampNameRoles(ctx: {
60
+ src: DatabaseClient<WOFDatabase>
61
+ out: DatabaseClient<CandidateDatabase>
62
+ attrs: Map<number, PlaceAttrs>
63
+ keyCounts: Map<number, number>
64
+ glossThreshold: number
65
+ ptcodes: Map<string, number>
66
+ ccodes: Map<string, number>
67
+ progress: (phase: string, message: string) => void
68
+ }): { roleGloss: number; roleAbbr: number; roleVariant: number; keyTailPlaces: number; keyTailWithRole: number } {
69
+ const { src, out, attrs, keyCounts, glossThreshold, progress } = ctx
70
+ progress("roles", "stamping name roles (gloss anomaly + abbr provenance)")
71
+
72
+ const ptNameByID = new Map([...ctx.ptcodes].map(([placetype, id]) => [id, placetype]))
73
+ const iso2ByCID = new Map([...ctx.ccodes].map(([code, id]) => [id, code]))
74
+
75
+ let keyTailPlaces = 0
76
+ const glossSids: number[] = []
77
+
78
+ for (const [sid, count] of keyCounts) {
79
+ if (count < glossThreshold) continue
80
+
81
+ keyTailPlaces++
82
+ const a = attrs.get(sid)
83
+
84
+ if (!a) continue
85
+
86
+ if (GLOSS_EXCLUDED_PLACETYPES.has(ptNameByID.get(a.ptid) ?? "")) continue
87
+
88
+ if (a.pop > 0 || a.imp != null) continue
89
+ glossSids.push(sid)
90
+ }
91
+
92
+ out.exec(
93
+ "CREATE TEMP TABLE role_key (spr_id INTEGER NOT NULL, name_key TEXT NOT NULL, PRIMARY KEY (spr_id, name_key)) WITHOUT ROWID"
94
+ )
95
+
96
+ const insRoleKey = out.prepare("INSERT OR IGNORE INTO role_key VALUES (?, ?)")
97
+
98
+ // Zero abbr stamps from a skipped detector is a different fact from zero variants found.
99
+ const hasSourceNames = tableExists(src, "names")
100
+
101
+ if (hasSourceNames) {
102
+ out.exec("BEGIN")
103
+
104
+ // Two provenance routes into the same stamp: WOF's abbreviation/short name KINDS arrive in the
105
+ // LANGUAGE column ('abbr'/'short' — 280 rows, measured 2026-08-18, Toledo's 'TO' among them) and
106
+ // qualify by kind alone; everything else qualifies as a variant in an official language.
107
+ for (const r of src
108
+ .prepare("SELECT id, name, language FROM names WHERE privateuse = 'variant' OR language IN ('abbr', 'short')")
109
+ .iterate()) {
110
+ const a = attrs.get(Number(r.id))
111
+
112
+ if (!a) continue
113
+ const language = String(r.language ?? "")
114
+
115
+ if (language !== "abbr" && language !== "short") {
116
+ const iso2 = iso2ByCID.get(a.cid) ?? "??"
117
+
118
+ if (language !== "eng" && !isOfficialLanguage(iso2, language)) continue
119
+ }
120
+
121
+ const k = normalizeLocalityForKey(String(r.name ?? ""))
122
+
123
+ if (!k || k === a.pkey) continue
124
+ insRoleKey.run(Number(r.id), k)
125
+ }
126
+
127
+ out.exec("COMMIT")
128
+ } else {
129
+ progress("roles", "source carries no `names` table — abbr detector skipped (gloss still runs)")
130
+ }
131
+
132
+ // Stamp order is precedence: the provenance-based abbr first, then the own-name variant verdict,
133
+ // then gloss fills what neither claimed.
134
+ const roleAbbr = Number(
135
+ out
136
+ .prepare(
137
+ `UPDATE cand_stage SET name_role = 'abbr'
138
+ WHERE is_primary = 0 AND EXISTS (
139
+ SELECT 1 FROM role_key rk WHERE rk.spr_id = cand_stage.spr_id AND rk.name_key = cand_stage.name_key)`
140
+ )
141
+ .run().changes
142
+ )
143
+
144
+ // --- variant detector (#1882): the alias surface is the holder's OWN primary name in another
145
+ // orthography — romanization, spacing/diacritic variant, or abbreviation expansion. The verdict
146
+ // is per (alias key, primary key) pair, so it runs in JS over the still-unstamped alias rows;
147
+ // an uncovered script answers no-verdict and stamps nothing (own-name.ts owns the predicate and
148
+ // its measured threshold). Runs BEFORE gloss on purpose: a surface that IS the place's own name
149
+ // is not a translation, whatever the key volume says.
150
+ out.exec(
151
+ "CREATE TEMP TABLE variant_key (spr_id INTEGER NOT NULL, name_key TEXT NOT NULL, PRIMARY KEY (spr_id, name_key)) WITHOUT ROWID"
152
+ )
153
+
154
+ const insVariantKey = out.prepare("INSERT OR IGNORE INTO variant_key VALUES (?, ?)")
155
+ let variantScanned = 0
156
+
157
+ out.exec("BEGIN")
158
+
159
+ for (const r of out
160
+ .prepare("SELECT DISTINCT spr_id, name_key FROM cand_stage WHERE is_primary = 0 AND name_role IS NULL")
161
+ .iterate()) {
162
+ variantScanned++
163
+ const a = attrs.get(Number(r.spr_id))
164
+
165
+ if (!a?.pkey) continue
166
+
167
+ if (isOwnNameVariant(String(a.pkey), String(r.name_key))) {
168
+ insVariantKey.run(Number(r.spr_id), String(r.name_key))
169
+ }
170
+ }
171
+
172
+ out.exec("COMMIT")
173
+
174
+ const roleVariant = Number(
175
+ out
176
+ .prepare(
177
+ `UPDATE cand_stage SET name_role = 'variant'
178
+ WHERE is_primary = 0 AND name_role IS NULL AND EXISTS (
179
+ SELECT 1 FROM variant_key vk WHERE vk.spr_id = cand_stage.spr_id AND vk.name_key = cand_stage.name_key)`
180
+ )
181
+ .run().changes
182
+ )
183
+
184
+ out.exec("DROP TABLE variant_key")
185
+
186
+ progress(
187
+ "roles",
188
+ `variant: ${roleVariant.toLocaleString()} of ${variantScanned.toLocaleString()} unstamped alias keys are own-name variants`
189
+ )
190
+
191
+ out.exec("CREATE TEMP TABLE role_place (spr_id INTEGER PRIMARY KEY) WITHOUT ROWID")
192
+ out.exec("CREATE TEMP TABLE key_tail (spr_id INTEGER PRIMARY KEY) WITHOUT ROWID")
193
+ const insRolePlace = out.prepare("INSERT OR IGNORE INTO role_place VALUES (?)")
194
+ const insKeyTail = out.prepare("INSERT OR IGNORE INTO key_tail VALUES (?)")
195
+ out.exec("BEGIN")
196
+
197
+ for (const sid of glossSids) {
198
+ insRolePlace.run(sid)
199
+ }
200
+
201
+ for (const [sid, count] of keyCounts) {
202
+ if (count >= glossThreshold) {
203
+ insKeyTail.run(sid)
204
+ }
205
+ }
206
+
207
+ out.exec("COMMIT")
208
+
209
+ const roleGloss = Number(
210
+ out
211
+ .prepare(
212
+ `UPDATE cand_stage SET name_role = 'gloss'
213
+ WHERE is_primary = 0 AND name_role IS NULL AND spr_id IN (SELECT spr_id FROM role_place)`
214
+ )
215
+ .run().changes
216
+ )
217
+
218
+ const keyTailWithRole = Number(
219
+ out
220
+ .prepare(
221
+ `SELECT count(DISTINCT spr_id) AS n FROM cand_stage
222
+ WHERE name_role IS NOT NULL AND spr_id IN (SELECT spr_id FROM key_tail)`
223
+ )
224
+ .get()!["n"]
225
+ )
226
+
227
+ out.exec("DROP TABLE role_key")
228
+ out.exec("DROP TABLE role_place")
229
+ out.exec("DROP TABLE key_tail")
230
+
231
+ progress(
232
+ "roles",
233
+ `${roleAbbr.toLocaleString()} abbr + ${roleGloss.toLocaleString()} gloss rows stamped; ` +
234
+ `key tail (>= ${glossThreshold} keys): ${keyTailWithRole.toLocaleString()} of ${keyTailPlaces.toLocaleString()} places carry a role`
235
+ )
236
+
237
+ return { roleGloss, roleAbbr, roleVariant, keyTailPlaces, keyTailWithRole }
238
+ }