@mailwoman/resolver-wof-sqlite 11.1.0 → 11.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 (467) hide show
  1. package/README.md +5 -5
  2. package/lib/address/duplicate.ts +1 -1
  3. package/lib/address/keys/attribution.ts +6 -1
  4. package/lib/address/keys/build.ts +1 -1
  5. package/lib/address/keys/layout.ts +27 -1
  6. package/lib/address/keys/lookup.ts +2 -2
  7. package/lib/address/keys/point-lookup.ts +2 -2
  8. package/lib/address/number-forms.ts +4 -6
  9. package/lib/address/point/interpolation.ts +11 -3
  10. package/lib/address/point/schema.ts +1 -1
  11. package/lib/address/point.ts +15 -14
  12. package/lib/address/record.ts +1 -1
  13. package/lib/ancestry/backfill.ts +13 -43
  14. package/lib/build/candidate.ts +171 -35
  15. package/lib/build/slim.ts +4 -1
  16. package/lib/build/sources.ts +50 -0
  17. package/lib/candidate/alias-bags.ts +74 -20
  18. package/lib/candidate/ancestors/schema.ts +1 -1
  19. package/lib/candidate/ancestors/sidecar.ts +13 -2
  20. package/lib/candidate/bare-admin-names.ts +1 -1
  21. package/lib/{capital-schema.ts → candidate/capital-schema.ts} +3 -3
  22. package/lib/{capitals.ts → candidate/capitals.ts} +1 -1
  23. package/lib/candidate/country-display-names.ts +1 -1
  24. package/lib/candidate/{extract-fold.ts → extract-rows.ts} +7 -4
  25. package/lib/candidate/importance.ts +90 -11
  26. package/lib/candidate/key-fold.ts +132 -0
  27. package/lib/candidate/lookup.ts +101 -60
  28. package/lib/candidate/name-roles.ts +1 -1
  29. package/lib/candidate/nickname-namesakes.ts +147 -0
  30. package/lib/candidate/overture-twins.ts +83 -0
  31. package/lib/candidate/place-attrs.ts +2 -2
  32. package/lib/candidate/postcode-browse.ts +173 -0
  33. package/lib/candidate/schema.ts +43 -2
  34. package/lib/candidate/scoring.ts +4 -4
  35. package/lib/convention.ts +1 -1
  36. package/lib/env.ts +15 -3
  37. package/lib/fst/binary-matcher.ts +26 -41
  38. package/lib/fst/builder.ts +8 -6
  39. package/lib/fst/deserialize-web.ts +1 -1
  40. package/lib/fst/format.ts +144 -7
  41. package/lib/fst/freshness.ts +2 -2
  42. package/lib/fst/serialize.ts +93 -64
  43. package/lib/fts/query.ts +1 -1
  44. package/lib/{search-fetch.ts → fts/search-fetch.ts} +2 -2
  45. package/lib/geonames/aliases.ts +42 -45
  46. package/lib/geonames/postal.ts +3 -3
  47. package/lib/index.ts +20 -17
  48. package/lib/{postal/city → labels/locality/postal-city}/alias/lookup.ts +1 -1
  49. package/lib/{postal/city → labels/locality/postal-city}/candidate-schema.ts +1 -1
  50. package/lib/labels/locality.ts +6 -0
  51. package/lib/{region-keys.ts → labels/region.ts} +5 -2
  52. package/lib/{street → labels/street}/centroid/schema.ts +1 -1
  53. package/lib/{street → labels/street}/centroid.ts +7 -7
  54. package/lib/{interpolation.ts → labels/street/interpolation.ts} +12 -4
  55. package/lib/labels/street/locale.ts +117 -0
  56. package/lib/{street → labels/street}/morphology/fst/loader.ts +1 -1
  57. package/lib/{street → labels/street}/name-lookup.ts +2 -0
  58. package/lib/{street → labels/street}/normalize.ts +284 -133
  59. package/lib/{street → labels/street}/segment-schema.ts +1 -1
  60. package/lib/{street → labels/street}/table.ts +1 -1
  61. package/lib/labels/street.ts +11 -0
  62. package/lib/landmark/rows.ts +1 -1
  63. package/lib/lookup/browse.ts +304 -0
  64. package/lib/{extracts.ts → lookup/extracts.ts} +1 -1
  65. package/lib/lookup/former-divisions.ts +183 -0
  66. package/lib/{postcode-point-lookup.ts → lookup/postcode-point.ts} +5 -0
  67. package/lib/{reverse.ts → lookup/reverse.ts} +4 -4
  68. package/lib/{types.ts → lookup/types.ts} +10 -0
  69. package/lib/lookup.ts +21 -31
  70. package/lib/poi/schema.ts +1 -1
  71. package/lib/{primary-preference.ts → ranking/primary-preference.ts} +45 -2
  72. package/lib/schema/importance-source.ts +210 -0
  73. package/lib/schema/overture-match.ts +79 -0
  74. package/lib/{place-importance-schema.ts → schema/place-importance.ts} +86 -0
  75. package/lib/schema.ts +3 -3
  76. package/lib/sqlite/convention-source.ts +1 -1
  77. package/lib/uprn/existence.ts +2 -0
  78. package/lib/{weights-overlay-linker.ts → weights/overlay-linker.ts} +244 -103
  79. package/out/address/duplicate.js +1 -1
  80. package/out/address/duplicate.js.map +1 -1
  81. package/out/address/keys/attribution.d.ts +4 -0
  82. package/out/address/keys/attribution.d.ts.map +1 -1
  83. package/out/address/keys/attribution.js +5 -1
  84. package/out/address/keys/attribution.js.map +1 -1
  85. package/out/address/keys/build.d.ts.map +1 -1
  86. package/out/address/keys/build.js +1 -1
  87. package/out/address/keys/build.js.map +1 -1
  88. package/out/address/keys/layout.d.ts +18 -1
  89. package/out/address/keys/layout.d.ts.map +1 -1
  90. package/out/address/keys/layout.js +23 -1
  91. package/out/address/keys/layout.js.map +1 -1
  92. package/out/address/keys/lookup.js +2 -2
  93. package/out/address/keys/lookup.js.map +1 -1
  94. package/out/address/keys/point-lookup.d.ts.map +1 -1
  95. package/out/address/keys/point-lookup.js +2 -2
  96. package/out/address/keys/point-lookup.js.map +1 -1
  97. package/out/address/number-forms.d.ts +1 -1
  98. package/out/address/number-forms.d.ts.map +1 -1
  99. package/out/address/number-forms.js +4 -5
  100. package/out/address/number-forms.js.map +1 -1
  101. package/out/address/point/interpolation.d.ts +1 -1
  102. package/out/address/point/interpolation.d.ts.map +1 -1
  103. package/out/address/point/interpolation.js +9 -2
  104. package/out/address/point/interpolation.js.map +1 -1
  105. package/out/address/point/schema.d.ts +1 -1
  106. package/out/address/point/schema.d.ts.map +1 -1
  107. package/out/address/point.d.ts +4 -5
  108. package/out/address/point.d.ts.map +1 -1
  109. package/out/address/point.js +11 -11
  110. package/out/address/point.js.map +1 -1
  111. package/out/address/record.d.ts +1 -1
  112. package/out/address/record.d.ts.map +1 -1
  113. package/out/address/record.js +1 -1
  114. package/out/address/record.js.map +1 -1
  115. package/out/ancestry/backfill.d.ts +14 -9
  116. package/out/ancestry/backfill.d.ts.map +1 -1
  117. package/out/ancestry/backfill.js +4 -42
  118. package/out/ancestry/backfill.js.map +1 -1
  119. package/out/build/candidate.d.ts +46 -1
  120. package/out/build/candidate.d.ts.map +1 -1
  121. package/out/build/candidate.js +82 -31
  122. package/out/build/candidate.js.map +1 -1
  123. package/out/build/slim.d.ts.map +1 -1
  124. package/out/build/slim.js +4 -1
  125. package/out/build/slim.js.map +1 -1
  126. package/out/build/sources.d.ts +32 -0
  127. package/out/build/sources.d.ts.map +1 -0
  128. package/out/build/sources.js +35 -0
  129. package/out/build/sources.js.map +1 -0
  130. package/out/candidate/alias-bags.d.ts +8 -3
  131. package/out/candidate/alias-bags.d.ts.map +1 -1
  132. package/out/candidate/alias-bags.js +63 -17
  133. package/out/candidate/alias-bags.js.map +1 -1
  134. package/out/candidate/ancestors/schema.d.ts +1 -1
  135. package/out/candidate/ancestors/schema.d.ts.map +1 -1
  136. package/out/candidate/ancestors/sidecar.d.ts +0 -3
  137. package/out/candidate/ancestors/sidecar.d.ts.map +1 -1
  138. package/out/candidate/ancestors/sidecar.js +16 -2
  139. package/out/candidate/ancestors/sidecar.js.map +1 -1
  140. package/out/candidate/bare-admin-names.js +1 -1
  141. package/out/candidate/bare-admin-names.js.map +1 -1
  142. package/out/{capital-schema.d.ts → candidate/capital-schema.d.ts} +2 -2
  143. package/out/candidate/capital-schema.d.ts.map +1 -0
  144. package/out/{capital-schema.js → candidate/capital-schema.js} +2 -2
  145. package/out/candidate/capital-schema.js.map +1 -0
  146. package/out/candidate/capitals.d.ts.map +1 -0
  147. package/out/{capitals.js → candidate/capitals.js} +1 -1
  148. package/out/candidate/capitals.js.map +1 -0
  149. package/out/candidate/country-display-names.js +1 -1
  150. package/out/candidate/country-display-names.js.map +1 -1
  151. package/out/{coverage-manifest-schema.d.ts → candidate/coverage-manifest.d.ts} +1 -1
  152. package/out/candidate/coverage-manifest.d.ts.map +1 -0
  153. package/out/{coverage-manifest-schema.js → candidate/coverage-manifest.js} +1 -1
  154. package/out/candidate/coverage-manifest.js.map +1 -0
  155. package/out/candidate/{extract-fold.d.ts → extract-rows.d.ts} +2 -2
  156. package/out/candidate/extract-rows.d.ts.map +1 -0
  157. package/out/candidate/{extract-fold.js → extract-rows.js} +8 -5
  158. package/out/candidate/extract-rows.js.map +1 -0
  159. package/out/candidate/importance.d.ts +34 -3
  160. package/out/candidate/importance.d.ts.map +1 -1
  161. package/out/candidate/importance.js +57 -11
  162. package/out/candidate/importance.js.map +1 -1
  163. package/out/candidate/key-fold.d.ts +55 -0
  164. package/out/candidate/key-fold.d.ts.map +1 -0
  165. package/out/candidate/key-fold.js +80 -0
  166. package/out/candidate/key-fold.js.map +1 -0
  167. package/out/candidate/lookup.d.ts +5 -1
  168. package/out/candidate/lookup.d.ts.map +1 -1
  169. package/out/candidate/lookup.js +80 -54
  170. package/out/candidate/lookup.js.map +1 -1
  171. package/out/candidate/name-roles.d.ts.map +1 -1
  172. package/out/candidate/name-roles.js +1 -1
  173. package/out/candidate/name-roles.js.map +1 -1
  174. package/out/candidate/nickname-namesakes.d.ts +28 -0
  175. package/out/candidate/nickname-namesakes.d.ts.map +1 -0
  176. package/out/candidate/nickname-namesakes.js +109 -0
  177. package/out/candidate/nickname-namesakes.js.map +1 -0
  178. package/out/candidate/overture-twins.d.ts +30 -0
  179. package/out/candidate/overture-twins.d.ts.map +1 -0
  180. package/out/candidate/overture-twins.js +40 -0
  181. package/out/candidate/overture-twins.js.map +1 -0
  182. package/out/candidate/place-attrs.d.ts +2 -2
  183. package/out/candidate/postcode-browse.d.ts +36 -0
  184. package/out/candidate/postcode-browse.d.ts.map +1 -0
  185. package/out/candidate/postcode-browse.js +119 -0
  186. package/out/candidate/postcode-browse.js.map +1 -0
  187. package/out/candidate/schema.d.ts +30 -2
  188. package/out/candidate/schema.d.ts.map +1 -1
  189. package/out/candidate/schema.js +29 -0
  190. package/out/candidate/schema.js.map +1 -1
  191. package/out/candidate/scoring.d.ts +3 -3
  192. package/out/candidate/scoring.d.ts.map +1 -1
  193. package/out/candidate/scoring.js +2 -2
  194. package/out/candidate/scoring.js.map +1 -1
  195. package/out/convention.d.ts +1 -1
  196. package/out/convention.d.ts.map +1 -1
  197. package/out/env.d.ts +3 -0
  198. package/out/env.d.ts.map +1 -1
  199. package/out/env.js +14 -3
  200. package/out/env.js.map +1 -1
  201. package/out/fst/binary-matcher.d.ts.map +1 -1
  202. package/out/fst/binary-matcher.js +9 -30
  203. package/out/fst/binary-matcher.js.map +1 -1
  204. package/out/fst/builder.d.ts.map +1 -1
  205. package/out/fst/builder.js +8 -6
  206. package/out/fst/builder.js.map +1 -1
  207. package/out/fst/deserialize-web.js +1 -1
  208. package/out/fst/deserialize-web.js.map +1 -1
  209. package/out/fst/format.d.ts +60 -9
  210. package/out/fst/format.d.ts.map +1 -1
  211. package/out/fst/format.js +97 -6
  212. package/out/fst/format.js.map +1 -1
  213. package/out/fst/freshness.js +2 -2
  214. package/out/fst/freshness.js.map +1 -1
  215. package/out/fst/serialize.d.ts.map +1 -1
  216. package/out/fst/serialize.js +67 -55
  217. package/out/fst/serialize.js.map +1 -1
  218. package/out/fts/query.d.ts +1 -1
  219. package/out/fts/query.d.ts.map +1 -1
  220. package/out/{search-fetch.d.ts → fts/search-fetch.d.ts} +2 -2
  221. package/out/fts/search-fetch.d.ts.map +1 -0
  222. package/out/fts/search-fetch.js.map +1 -0
  223. package/out/geonames/aliases.d.ts +18 -18
  224. package/out/geonames/aliases.d.ts.map +1 -1
  225. package/out/geonames/aliases.js +20 -24
  226. package/out/geonames/aliases.js.map +1 -1
  227. package/out/geonames/postal.d.ts +1 -1
  228. package/out/geonames/postal.js +3 -3
  229. package/out/index.d.ts +17 -17
  230. package/out/index.d.ts.map +1 -1
  231. package/out/index.js +12 -12
  232. package/out/index.js.map +1 -1
  233. package/out/{postal/city → labels/locality/postal-city}/alias/lookup.d.ts +1 -1
  234. package/out/labels/locality/postal-city/alias/lookup.d.ts.map +1 -0
  235. package/out/labels/locality/postal-city/alias/lookup.js.map +1 -0
  236. package/out/labels/locality/postal-city/alias/schema.d.ts.map +1 -0
  237. package/out/labels/locality/postal-city/alias/schema.js.map +1 -0
  238. package/out/{postal/city → labels/locality/postal-city}/candidate-schema.d.ts +1 -1
  239. package/out/labels/locality/postal-city/candidate-schema.d.ts.map +1 -0
  240. package/out/labels/locality/postal-city/candidate-schema.js.map +1 -0
  241. package/out/labels/locality.d.ts +4 -0
  242. package/out/labels/locality.d.ts.map +1 -0
  243. package/out/labels/locality.js +6 -0
  244. package/out/labels/locality.js.map +1 -0
  245. package/out/{region-keys.d.ts → labels/region.d.ts} +1 -1
  246. package/out/{region-keys.d.ts.map → labels/region.d.ts.map} +1 -1
  247. package/out/{region-keys.js → labels/region.js} +6 -3
  248. package/out/labels/region.js.map +1 -0
  249. package/out/{street → labels/street}/centroid/schema.d.ts +1 -1
  250. package/out/labels/street/centroid/schema.d.ts.map +1 -0
  251. package/out/labels/street/centroid/schema.js.map +1 -0
  252. package/out/{street → labels/street}/centroid.d.ts +5 -5
  253. package/out/labels/street/centroid.d.ts.map +1 -0
  254. package/out/{street → labels/street}/centroid.js +5 -5
  255. package/out/labels/street/centroid.js.map +1 -0
  256. package/out/{interpolation.d.ts → labels/street/interpolation.d.ts} +6 -1
  257. package/out/labels/street/interpolation.d.ts.map +1 -0
  258. package/out/{interpolation.js → labels/street/interpolation.js} +10 -8
  259. package/out/labels/street/interpolation.js.map +1 -0
  260. package/out/labels/street/locale.d.ts +26 -0
  261. package/out/labels/street/locale.d.ts.map +1 -0
  262. package/out/labels/street/locale.js +98 -0
  263. package/out/labels/street/locale.js.map +1 -0
  264. package/out/labels/street/morphology/fst/builder.d.ts.map +1 -0
  265. package/out/labels/street/morphology/fst/builder.js.map +1 -0
  266. package/out/labels/street/morphology/fst/loader.d.ts.map +1 -0
  267. package/out/{street → labels/street}/morphology/fst/loader.js +1 -1
  268. package/out/labels/street/morphology/fst/loader.js.map +1 -0
  269. package/out/labels/street/name-lookup.d.ts.map +1 -0
  270. package/out/{street → labels/street}/name-lookup.js +2 -0
  271. package/out/labels/street/name-lookup.js.map +1 -0
  272. package/out/labels/street/normalize.d.ts +188 -0
  273. package/out/labels/street/normalize.d.ts.map +1 -0
  274. package/out/{street → labels/street}/normalize.js +200 -119
  275. package/out/labels/street/normalize.js.map +1 -0
  276. package/out/{street → labels/street}/segment-schema.d.ts +1 -1
  277. package/out/labels/street/segment-schema.d.ts.map +1 -0
  278. package/out/labels/street/segment-schema.js.map +1 -0
  279. package/out/labels/street/table.d.ts.map +1 -0
  280. package/out/{street → labels/street}/table.js +1 -1
  281. package/out/labels/street/table.js.map +1 -0
  282. package/out/labels/street.d.ts +9 -0
  283. package/out/labels/street.d.ts.map +1 -0
  284. package/out/labels/street.js +11 -0
  285. package/out/labels/street.js.map +1 -0
  286. package/out/landmark/rows.js +1 -1
  287. package/out/landmark/rows.js.map +1 -1
  288. package/out/lookup/browse.d.ts +79 -0
  289. package/out/lookup/browse.d.ts.map +1 -0
  290. package/out/lookup/browse.js +194 -0
  291. package/out/lookup/browse.js.map +1 -0
  292. package/out/lookup/exact-match.d.ts.map +1 -0
  293. package/out/lookup/exact-match.js.map +1 -0
  294. package/out/lookup/extracts.d.ts.map +1 -0
  295. package/out/{extracts.js → lookup/extracts.js} +1 -1
  296. package/out/lookup/extracts.js.map +1 -0
  297. package/out/lookup/former-divisions.d.ts +99 -0
  298. package/out/lookup/former-divisions.d.ts.map +1 -0
  299. package/out/lookup/former-divisions.js +98 -0
  300. package/out/lookup/former-divisions.js.map +1 -0
  301. package/out/{postcode-point-lookup.d.ts → lookup/postcode-point.d.ts} +5 -1
  302. package/out/lookup/postcode-point.d.ts.map +1 -0
  303. package/out/{postcode-point-lookup.js → lookup/postcode-point.js} +1 -1
  304. package/out/lookup/postcode-point.js.map +1 -0
  305. package/out/{reverse.d.ts → lookup/reverse.d.ts} +1 -1
  306. package/out/lookup/reverse.d.ts.map +1 -0
  307. package/out/{reverse.js → lookup/reverse.js} +3 -3
  308. package/out/lookup/reverse.js.map +1 -0
  309. package/out/{types.d.ts → lookup/types.d.ts} +9 -0
  310. package/out/lookup/types.d.ts.map +1 -0
  311. package/out/lookup/types.js.map +1 -0
  312. package/out/lookup.d.ts +4 -4
  313. package/out/lookup.d.ts.map +1 -1
  314. package/out/lookup.js +14 -20
  315. package/out/lookup.js.map +1 -1
  316. package/out/poi/schema.d.ts +1 -1
  317. package/out/poi/schema.d.ts.map +1 -1
  318. package/out/ranking/name-score.d.ts.map +1 -0
  319. package/out/ranking/name-score.js.map +1 -0
  320. package/out/{primary-preference.d.ts → ranking/primary-preference.d.ts} +20 -1
  321. package/out/ranking/primary-preference.d.ts.map +1 -0
  322. package/out/{primary-preference.js → ranking/primary-preference.js} +35 -1
  323. package/out/ranking/primary-preference.js.map +1 -0
  324. package/out/ranking/proximity-rerank.d.ts.map +1 -0
  325. package/out/ranking/proximity-rerank.js.map +1 -0
  326. package/out/{ranking-weights.d.ts → ranking/weights.d.ts} +1 -1
  327. package/out/ranking/weights.d.ts.map +1 -0
  328. package/out/{ranking-weights.js → ranking/weights.js} +1 -1
  329. package/out/ranking/weights.js.map +1 -0
  330. package/out/schema/importance-source.d.ts +90 -0
  331. package/out/schema/importance-source.d.ts.map +1 -0
  332. package/out/schema/importance-source.js +133 -0
  333. package/out/schema/importance-source.js.map +1 -0
  334. package/out/schema/overture-match.d.ts +53 -0
  335. package/out/schema/overture-match.d.ts.map +1 -0
  336. package/out/schema/overture-match.js +33 -0
  337. package/out/schema/overture-match.js.map +1 -0
  338. package/out/{place-importance-schema.d.ts → schema/place-importance.d.ts} +50 -1
  339. package/out/schema/place-importance.d.ts.map +1 -0
  340. package/out/{place-importance-schema.js → schema/place-importance.js} +34 -1
  341. package/out/schema/place-importance.js.map +1 -0
  342. package/out/{polygon-schema.d.ts → schema/polygon.d.ts} +1 -1
  343. package/out/schema/polygon.d.ts.map +1 -0
  344. package/out/{polygon-schema.js → schema/polygon.js} +1 -1
  345. package/out/schema/polygon.js.map +1 -0
  346. package/out/{unified-schema.d.ts → schema/unified.d.ts} +1 -1
  347. package/out/schema/unified.d.ts.map +1 -0
  348. package/out/{unified-schema.js → schema/unified.js} +1 -1
  349. package/out/schema/unified.js.map +1 -0
  350. package/out/schema.d.ts +3 -3
  351. package/out/sqlite/convention-source.js +1 -1
  352. package/out/sqlite/convention-source.js.map +1 -1
  353. package/out/uprn/existence.d.ts.map +1 -1
  354. package/out/uprn/existence.js +2 -0
  355. package/out/uprn/existence.js.map +1 -1
  356. package/out/{weights-overlay-linker.d.ts → weights/overlay-linker.d.ts} +43 -24
  357. package/out/weights/overlay-linker.d.ts.map +1 -0
  358. package/out/{weights-overlay-linker.js → weights/overlay-linker.js} +182 -66
  359. package/out/weights/overlay-linker.js.map +1 -0
  360. package/package.json +12 -32
  361. package/lib/postal.ts +0 -6
  362. package/lib/street/locale.ts +0 -64
  363. package/lib/street.ts +0 -11
  364. package/out/candidate/extract-fold.d.ts.map +0 -1
  365. package/out/candidate/extract-fold.js.map +0 -1
  366. package/out/capital-schema.d.ts.map +0 -1
  367. package/out/capital-schema.js.map +0 -1
  368. package/out/capitals.d.ts.map +0 -1
  369. package/out/capitals.js.map +0 -1
  370. package/out/coverage-manifest-schema.d.ts.map +0 -1
  371. package/out/coverage-manifest-schema.js.map +0 -1
  372. package/out/exact-match.d.ts.map +0 -1
  373. package/out/exact-match.js.map +0 -1
  374. package/out/extracts.d.ts.map +0 -1
  375. package/out/extracts.js.map +0 -1
  376. package/out/interpolation.d.ts.map +0 -1
  377. package/out/interpolation.js.map +0 -1
  378. package/out/name-score.d.ts.map +0 -1
  379. package/out/name-score.js.map +0 -1
  380. package/out/place-importance-schema.d.ts.map +0 -1
  381. package/out/place-importance-schema.js.map +0 -1
  382. package/out/polygon-schema.d.ts.map +0 -1
  383. package/out/polygon-schema.js.map +0 -1
  384. package/out/postal/city/alias/lookup.d.ts.map +0 -1
  385. package/out/postal/city/alias/lookup.js.map +0 -1
  386. package/out/postal/city/alias/schema.d.ts.map +0 -1
  387. package/out/postal/city/alias/schema.js.map +0 -1
  388. package/out/postal/city/candidate-schema.d.ts.map +0 -1
  389. package/out/postal/city/candidate-schema.js.map +0 -1
  390. package/out/postal.d.ts +0 -4
  391. package/out/postal.d.ts.map +0 -1
  392. package/out/postal.js +0 -6
  393. package/out/postal.js.map +0 -1
  394. package/out/postcode-point-lookup.d.ts.map +0 -1
  395. package/out/postcode-point-lookup.js.map +0 -1
  396. package/out/primary-preference.d.ts.map +0 -1
  397. package/out/primary-preference.js.map +0 -1
  398. package/out/proximity-rerank.d.ts.map +0 -1
  399. package/out/proximity-rerank.js.map +0 -1
  400. package/out/ranking-weights.d.ts.map +0 -1
  401. package/out/ranking-weights.js.map +0 -1
  402. package/out/region-keys.js.map +0 -1
  403. package/out/reverse.d.ts.map +0 -1
  404. package/out/reverse.js.map +0 -1
  405. package/out/search-fetch.d.ts.map +0 -1
  406. package/out/search-fetch.js.map +0 -1
  407. package/out/street/centroid/schema.d.ts.map +0 -1
  408. package/out/street/centroid/schema.js.map +0 -1
  409. package/out/street/centroid.d.ts.map +0 -1
  410. package/out/street/centroid.js.map +0 -1
  411. package/out/street/locale.d.ts +0 -12
  412. package/out/street/locale.d.ts.map +0 -1
  413. package/out/street/locale.js +0 -54
  414. package/out/street/locale.js.map +0 -1
  415. package/out/street/morphology/fst/builder.d.ts.map +0 -1
  416. package/out/street/morphology/fst/builder.js.map +0 -1
  417. package/out/street/morphology/fst/loader.d.ts.map +0 -1
  418. package/out/street/morphology/fst/loader.js.map +0 -1
  419. package/out/street/name-lookup.d.ts.map +0 -1
  420. package/out/street/name-lookup.js.map +0 -1
  421. package/out/street/normalize.d.ts +0 -111
  422. package/out/street/normalize.d.ts.map +0 -1
  423. package/out/street/normalize.js.map +0 -1
  424. package/out/street/segment-schema.d.ts.map +0 -1
  425. package/out/street/segment-schema.js.map +0 -1
  426. package/out/street/table.d.ts.map +0 -1
  427. package/out/street/table.js.map +0 -1
  428. package/out/street.d.ts +0 -9
  429. package/out/street.d.ts.map +0 -1
  430. package/out/street.js +0 -11
  431. package/out/street.js.map +0 -1
  432. package/out/types.d.ts.map +0 -1
  433. package/out/types.js.map +0 -1
  434. package/out/unified-schema.d.ts.map +0 -1
  435. package/out/unified-schema.js.map +0 -1
  436. package/out/weights-overlay-linker.d.ts.map +0 -1
  437. package/out/weights-overlay-linker.js.map +0 -1
  438. /package/lib/{coverage-manifest-schema.ts → candidate/coverage-manifest.ts} +0 -0
  439. /package/lib/{postal/city → labels/locality/postal-city}/alias/schema.ts +0 -0
  440. /package/lib/{street → labels/street}/morphology/fst/builder.ts +0 -0
  441. /package/lib/{exact-match.ts → lookup/exact-match.ts} +0 -0
  442. /package/lib/{name-score.ts → ranking/name-score.ts} +0 -0
  443. /package/lib/{proximity-rerank.ts → ranking/proximity-rerank.ts} +0 -0
  444. /package/lib/{ranking-weights.ts → ranking/weights.ts} +0 -0
  445. /package/lib/{polygon-schema.ts → schema/polygon.ts} +0 -0
  446. /package/lib/{unified-schema.ts → schema/unified.ts} +0 -0
  447. /package/out/{capitals.d.ts → candidate/capitals.d.ts} +0 -0
  448. /package/out/{search-fetch.js → fts/search-fetch.js} +0 -0
  449. /package/out/{postal/city → labels/locality/postal-city}/alias/lookup.js +0 -0
  450. /package/out/{postal/city → labels/locality/postal-city}/alias/schema.d.ts +0 -0
  451. /package/out/{postal/city → labels/locality/postal-city}/alias/schema.js +0 -0
  452. /package/out/{postal/city → labels/locality/postal-city}/candidate-schema.js +0 -0
  453. /package/out/{street → labels/street}/centroid/schema.js +0 -0
  454. /package/out/{street → labels/street}/morphology/fst/builder.d.ts +0 -0
  455. /package/out/{street → labels/street}/morphology/fst/builder.js +0 -0
  456. /package/out/{street → labels/street}/morphology/fst/loader.d.ts +0 -0
  457. /package/out/{street → labels/street}/name-lookup.d.ts +0 -0
  458. /package/out/{street → labels/street}/segment-schema.js +0 -0
  459. /package/out/{street → labels/street}/table.d.ts +0 -0
  460. /package/out/{exact-match.d.ts → lookup/exact-match.d.ts} +0 -0
  461. /package/out/{exact-match.js → lookup/exact-match.js} +0 -0
  462. /package/out/{extracts.d.ts → lookup/extracts.d.ts} +0 -0
  463. /package/out/{types.js → lookup/types.js} +0 -0
  464. /package/out/{name-score.d.ts → ranking/name-score.d.ts} +0 -0
  465. /package/out/{name-score.js → ranking/name-score.js} +0 -0
  466. /package/out/{proximity-rerank.d.ts → ranking/proximity-rerank.d.ts} +0 -0
  467. /package/out/{proximity-rerank.js → ranking/proximity-rerank.js} +0 -0
@@ -14,11 +14,11 @@
14
14
  * keeps the coordinate radius from promoting a capital's same-name neighbors (`capitals.ts`).
15
15
  */
16
16
 
17
- import { tryParsingJSON } from "@mailwoman/core/json"
17
+ import { tryParsingJSON } from "@mailwoman/core/values/json"
18
18
  import type { DatabaseClient } from "@mailwoman/sqlite/client"
19
19
  import type { Kysely } from "kysely"
20
20
 
21
- import type { CapitalPoint } from "#capitals"
21
+ import type { CapitalPoint } from "#candidate/capitals"
22
22
  import { hasTable } from "#sqlite-utils"
23
23
 
24
24
  /**
@@ -47,7 +47,7 @@ export interface CapitalTable {
47
47
  /**
48
48
  * Create the table on a build in progress.
49
49
  *
50
- * Async because Kysely's schema-builder is.
50
+ * The function is async because Kysely's schema builder is asynchronous.
51
51
  * Called from the candidate build's DDL phase alongside the other typed builders.
52
52
  */
53
53
  export async function createCapitalTable<DB extends { capital: CapitalTable }>(db: Kysely<DB>): Promise<void> {
@@ -20,7 +20,7 @@
20
20
 
21
21
  import { haversine } from "@mailwoman/spatial/geometries/point/distance"
22
22
 
23
- import { normalizeLocalityForKey } from "#street/normalize"
23
+ import { normalizeLocalityForKey } from "#labels/street/normalize"
24
24
 
25
25
  /**
26
26
  * Capital status of one candidate: national capital, admin-1 seat, or neither.
@@ -4,7 +4,7 @@
4
4
  import { enumerateCountryDisplayNames } from "@mailwoman/codex/country"
5
5
 
6
6
  import type { PlaceAttrs, StageRow } from "#candidate/place-attrs"
7
- import { normalizeLocalityForKey } from "#street/normalize"
7
+ import { normalizeLocalityForKey } from "#labels/street/normalize"
8
8
 
9
9
  /**
10
10
  * Stages each country name that `Intl.DisplayNames` renders in the codex display-name
@@ -8,8 +8,8 @@ import type { PathBuilderLike } from "path-ts"
8
8
  import { CANDIDATE_ANCESTOR_COLUMNS, CANDIDATE_ANCESTOR_TABLE } from "#candidate/ancestors/schema"
9
9
  import type { PlaceAttrs, StageRow } from "#candidate/place-attrs"
10
10
  import type { CandidateDatabase } from "#candidate/schema"
11
+ import { normalizeLocalityForKey } from "#labels/street/normalize"
11
12
  import type { WOFDatabase } from "#schema"
12
- import { normalizeLocalityForKey } from "#street/normalize"
13
13
 
14
14
  function extractRegionAncestry(pc: DatabaseClient<WOFDatabase>, attrs: Map<number, PlaceAttrs>): Map<number, number> {
15
15
  const regionOf = new Map<number, number>()
@@ -41,7 +41,7 @@ function extractRegionAncestry(pc: DatabaseClient<WOFDatabase>, attrs: Map<numbe
41
41
  * the row also gets that region's scope and the region's ancestor chain.
42
42
  * The stored ancestors then agree with the row's scope.
43
43
  */
44
- export function foldExtract(ctx: {
44
+ export function stageExtractRows(ctx: {
45
45
  out: DatabaseClient<CandidateDatabase>
46
46
  extractPath: PathBuilderLike
47
47
  extractPlacetype: "postalcode" | "locality"
@@ -77,11 +77,14 @@ export function foldExtract(ctx: {
77
77
 
78
78
  out.exec("BEGIN")
79
79
 
80
+ // A deprecated record is one its source withdrew as wrong, and it stays out whatever point it holds.
81
+ // A record that only ceased (`is_current = 0`) stays in: a terminated postcode still
82
+ // appears in written addresses, and the extract keeps its last point.
80
83
  for (const r of pc
81
84
  .prepare(
82
85
  `SELECT id, name, country, latitude, longitude,
83
86
  min_latitude AS mnlat, min_longitude AS mnlon, max_latitude AS mxlat, max_longitude AS mxlon
84
- FROM spr WHERE placetype = ? AND latitude != 0 AND longitude != 0`
87
+ FROM spr WHERE placetype = ? AND latitude != 0 AND longitude != 0 AND is_deprecated = 0`
85
88
  )
86
89
  .iterate(extractPlacetype)) {
87
90
  const name = String(r.name ?? "")
@@ -164,7 +167,7 @@ export function foldExtract(ctx: {
164
167
 
165
168
  out.exec("COMMIT")
166
169
  } else {
167
- progress("postcode-aliases", `${extractPath} has no \`names\` table — no delivery-city aliases to fold`)
170
+ progress("postcode-aliases", `${extractPath} has no \`names\` table — no delivery-city aliases to add`)
168
171
  }
169
172
 
170
173
  return { primaries, aliases, scoped, ancestorRows }
@@ -8,10 +8,11 @@
8
8
  *
9
9
  * The join key is `(name_key, country, placetype)`, the same {@link normalizeLocalityForKey}
10
10
  * the candidate build uses for its probe key, so the two sides fold identically.
11
- * An id join is wrong here, because the score source (`admin-global-priority-importance.db`)
12
- * and the candidate build's admin source are different snapshots whose ids disagree.
11
+ * The score source is the importance build the candidate build matched to its
12
+ * admin database by md5 (`importance-source.ts`).
13
13
  *
14
- * An id join silently drops rows and leaves the ranking inert on the queries the prior exists for.
14
+ * The join stays on the name key rather than the id, the join every candidate build has used,
15
+ * so matching the two files changes which scores are read and leaves the join unchanged.
15
16
  *
16
17
  * The key by itself isn't enough.
17
18
  * `(warwick, US, locality)` covers eleven different places.
@@ -34,12 +35,14 @@
34
35
  * at the position population assigned it.
35
36
  */
36
37
 
38
+ import { isOverturePartID } from "@mailwoman/core/resolver/synthetic-id-ranges"
37
39
  import { haversine } from "@mailwoman/spatial/geometries/point/distance"
38
40
  import { DatabaseClient } from "@mailwoman/sqlite/client"
39
41
  import type { PathBuilderLike } from "path-ts"
40
42
 
41
43
  import type { CandidateDatabase } from "#candidate/schema"
42
- import { normalizeLocalityForKey } from "#street/normalize"
44
+ import { normalizeLocalityForKey } from "#labels/street/normalize"
45
+ import { loadPopulationEstimates, type PlacePopulationEstimateDatabase } from "#schema/place-importance"
43
46
 
44
47
  /**
45
48
  * How far apart two artifacts may put the same place's centroid and still be read as the same place.
@@ -63,6 +66,10 @@ interface ScoredPlace {
63
66
  lat: number
64
67
  lon: number
65
68
  importance: number
69
+ /**
70
+ * Whether the scored place is a row of the Overture Divisions part.
71
+ */
72
+ overture: boolean
66
73
  }
67
74
 
68
75
  /**
@@ -85,6 +92,11 @@ export interface ImportanceIndexStats {
85
92
  * (non-Latin punctuation-only names, blanks).
86
93
  */
87
94
  unkeyable: number
95
+ /**
96
+ * Population estimates read from `place_population_estimate`, or null
97
+ * when the loader wasn't asked for them.
98
+ */
99
+ estimates: number | null
88
100
  }
89
101
 
90
102
  /**
@@ -119,10 +131,36 @@ export class ImportanceIndex {
119
131
  * It doesn't mean the radius is too tight.
120
132
  */
121
133
  refused = 0
134
+ /**
135
+ * Places {@link estimatedPopulation} returned an estimate for.
136
+ */
137
+ estimated = 0
122
138
 
123
- constructor(groups: Map<string, ScoredPlace[]>, stats: ImportanceIndexStats) {
139
+ readonly #estimates: ReadonlyMap<number, number>
140
+
141
+ constructor(
142
+ groups: Map<string, ScoredPlace[]>,
143
+ stats: ImportanceIndexStats,
144
+ estimates: ReadonlyMap<number, number> = new Map()
145
+ ) {
124
146
  this.#groups = groups
125
147
  this.stats = stats
148
+ this.#estimates = estimates
149
+ }
150
+
151
+ /**
152
+ * The ranking population the importance build estimated for the place with WOF
153
+ * id `id`, or undefined when it holds none.
154
+ * Ask only for a place without a measured population.
155
+ */
156
+ estimatedPopulation(id: number): number | undefined {
157
+ const estimate = this.#estimates.get(id)
158
+
159
+ if (typeof estimate === "number") {
160
+ this.estimated++
161
+ }
162
+
163
+ return estimate
126
164
  }
127
165
 
128
166
  /**
@@ -135,8 +173,20 @@ export class ImportanceIndex {
135
173
  *
136
174
  * The source column already contains a population-derived fallback where one exists.
137
175
  * A second fallback would make an absence indistinguishable from a measurement.
176
+ *
177
+ * The gazetteer is a Collective Database whose Overture Divisions part is ODbL.
178
+ * A row outside that part (`overtureRow` false) reads only scores of places outside it,
179
+ * so an Overture division's Wikidata-joined score never reaches a WOF or GeoNames row.
180
+ * An Overture row may read any score.
138
181
  */
139
- find(name: string, country: string | null, placetype: string | null, lat: number, lon: number): number | null {
182
+ find(
183
+ name: string,
184
+ country: string | null,
185
+ placetype: string | null,
186
+ lat: number,
187
+ lon: number,
188
+ overtureRow = false
189
+ ): number | null {
140
190
  const nameKey = normalizeLocalityForKey(name)
141
191
 
142
192
  if (!nameKey) return null
@@ -147,8 +197,13 @@ export class ImportanceIndex {
147
197
 
148
198
  let best: ScoredPlace | null = null
149
199
  let bestKm = Infinity
200
+ let eligible = false
150
201
 
151
202
  for (const place of group) {
203
+ if (place.overture && !overtureRow) continue
204
+
205
+ eligible = true
206
+
152
207
  const km = haversine(lat, lon, place.lat, place.lon)
153
208
 
154
209
  if (km < bestKm) {
@@ -157,6 +212,9 @@ export class ImportanceIndex {
157
212
  }
158
213
  }
159
214
 
215
+ // A row outside the Overture part reads a group of only Overture scores as an absent group.
216
+ if (!eligible) return null
217
+
160
218
  if (!best || bestKm > IMPORTANCE_JOIN_RADIUS_KM) {
161
219
  this.refused++
162
220
 
@@ -182,9 +240,19 @@ export class ImportanceIndex {
182
240
  *
183
241
  * The whole table is held in memory on purpose, because the build probes it once for every place
184
242
  * and the alternative is a prepared statement per place against a multi-gigabyte database.
243
+ *
244
+ * With `populationEstimates`, it also reads `place_population_estimate`, keyed by WOF id
245
+ * because the importance database is a copy of the admin database the candidate build reads.
246
+ *
247
+ * @throws With `populationEstimates`, when the database holds no estimates table.
185
248
  */
186
- export function loadImportanceIndex(databasePath: PathBuilderLike): ImportanceIndex {
187
- using db = new DatabaseClient<CandidateDatabase>(databasePath, { readOnly: true })
249
+ export function loadImportanceIndex(
250
+ databasePath: PathBuilderLike,
251
+ options: { populationEstimates?: boolean } = {}
252
+ ): ImportanceIndex {
253
+ using db = new DatabaseClient<CandidateDatabase & PlacePopulationEstimateDatabase>(databasePath, {
254
+ readOnly: true,
255
+ })
188
256
 
189
257
  const groups = new Map<string, ScoredPlace[]>()
190
258
  let places = 0
@@ -192,7 +260,7 @@ export function loadImportanceIndex(databasePath: PathBuilderLike): ImportanceIn
192
260
 
193
261
  for (const row of db
194
262
  .prepare(
195
- `SELECT s.name AS name, s.country AS country, s.placetype AS placetype,
263
+ `SELECT s.id AS id, s.name AS name, s.country AS country, s.placetype AS placetype,
196
264
  s.latitude AS latitude, s.longitude AS longitude, i.importance AS importance
197
265
  FROM place_importance i JOIN spr s ON s.id = i.id
198
266
  WHERE s.is_current != 0 AND s.is_deprecated = 0`
@@ -217,10 +285,21 @@ export function loadImportanceIndex(databasePath: PathBuilderLike): ImportanceIn
217
285
  groups.set(key, group)
218
286
  }
219
287
 
220
- group.push({ lat: Number(row.latitude), lon: Number(row.longitude), importance })
288
+ group.push({
289
+ lat: Number(row.latitude),
290
+ lon: Number(row.longitude),
291
+ importance,
292
+ overture: isOverturePartID(Number(row.id)),
293
+ })
221
294
 
222
295
  places++
223
296
  }
224
297
 
225
- return new ImportanceIndex(groups, { places, keys: groups.size, unkeyable })
298
+ const estimates = options.populationEstimates ? loadPopulationEstimates(db) : null
299
+
300
+ return new ImportanceIndex(
301
+ groups,
302
+ { places, keys: groups.size, unkeyable, estimates: estimates ? estimates.size : null },
303
+ estimates ?? undefined
304
+ )
226
305
  }
@@ -0,0 +1,132 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * @file Extra folds a `candidate.db` applies to every `name_key`, recorded in the artifact so its reader
6
+ * folds each query key the same way.
7
+ *
8
+ * A fold changes which keys exist, so the build and the reader must agree on it. The build applies it
9
+ * as a post-pass over the materialized `candidate` table, and the reader reads the recorded folds once.
10
+ */
11
+
12
+ import type { DatabaseClient } from "@mailwoman/sqlite/client"
13
+
14
+ import type { CandidateDatabase } from "#candidate/schema"
15
+ import { type NameKey, withArabicLetterVariantsFolded } from "#labels/street/normalize"
16
+ import { hasTable } from "#sqlite-utils"
17
+
18
+ /**
19
+ * The table that lists the folds applied to the artifact's `name_key` column, one row per fold.
20
+ * An artifact without it applies none.
21
+ */
22
+ export const CANDIDATE_KEY_FOLD_TABLE = "candidate_key_fold"
23
+
24
+ /**
25
+ * The folds a candidate build can apply on top of `normalizeLocalityForKey`.
26
+ */
27
+ export const CandidateKeyFold = {
28
+ /**
29
+ * {@link withArabicLetterVariantsFolded}: hamza, madda and vowel marks dropped,
30
+ * `ى` written `ي`, `ة` written `ه`.
31
+ */
32
+ ArabicLetterVariants: "arabic-letter-variants",
33
+ } as const
34
+
35
+ export type CandidateKeyFold = (typeof CandidateKeyFold)[keyof typeof CandidateKeyFold]
36
+
37
+ /**
38
+ * The key function of each fold, and the GLOB that selects the keys it can change.
39
+ */
40
+ const FOLDS: Readonly<Record<CandidateKeyFold, { fold: (key: NameKey) => NameKey; glob: string }>> = {
41
+ [CandidateKeyFold.ArabicLetterVariants]: { fold: withArabicLetterVariantsFolded, glob: "*[؀-ۿ]*" },
42
+ }
43
+
44
+ /**
45
+ * The counts {@link applyCandidateKeyFold} reports.
46
+ */
47
+ export interface CandidateKeyFoldResult {
48
+ /**
49
+ * Rows whose key the fold changed.
50
+ */
51
+ rekeyed: number
52
+ /**
53
+ * Rekeyed rows dropped because the folded key already held the same place under the same scope.
54
+ */
55
+ merged: number
56
+ }
57
+
58
+ /**
59
+ * Rewrites every `candidate.name_key` the fold changes and records the fold
60
+ * in {@link CANDIDATE_KEY_FOLD_TABLE}.
61
+ *
62
+ * A row whose folded key already exists for the same place, scope, placetype and rank is merged into it.
63
+ * It runs on a writable copy or inside a build, never on a published artifact.
64
+ */
65
+ export async function applyCandidateKeyFold(
66
+ db: DatabaseClient<CandidateDatabase>,
67
+ foldName: CandidateKeyFold
68
+ ): Promise<CandidateKeyFoldResult> {
69
+ const { fold, glob } = FOLDS[foldName]
70
+ const sqlName = `mw_key_fold_${foldName.replaceAll("-", "_")}`
71
+
72
+ db.function(sqlName, { deterministic: true }, (key) => fold(String(key) as NameKey))
73
+
74
+ const changed = `name_key GLOB '${glob}' AND ${sqlName}(name_key) <> name_key`
75
+ const { n: rekeyed } = db.prepare(`SELECT count(*) AS n FROM candidate WHERE ${changed}`).get() as { n: number }
76
+
77
+ db.exec("BEGIN")
78
+
79
+ const inserted = db
80
+ .prepare(
81
+ "INSERT OR IGNORE INTO candidate SELECT " +
82
+ `${sqlName}(name_key), country_id, region_id, placetype_id, neg_rank, spr_id, name, latitude, longitude, ` +
83
+ "min_lat, min_lon, max_lat, max_lon, population, is_primary, importance, name_role " +
84
+ `FROM candidate WHERE ${changed}`
85
+ )
86
+ .run()
87
+
88
+ db.prepare(`DELETE FROM candidate WHERE ${changed}`).run()
89
+
90
+ await db.schema
91
+ .createTable(CANDIDATE_KEY_FOLD_TABLE)
92
+ .ifNotExists()
93
+ .addColumn("fold", "text", (c) => c.primaryKey())
94
+ .execute()
95
+
96
+ db.prepare(`INSERT OR IGNORE INTO ${CANDIDATE_KEY_FOLD_TABLE} (fold) VALUES (?)`).run(foldName)
97
+ db.exec("COMMIT")
98
+
99
+ return { rekeyed, merged: rekeyed - Number(inserted.changes) }
100
+ }
101
+
102
+ /**
103
+ * The query-key function an artifact's recorded folds call for: identity when it records none.
104
+ *
105
+ * A fold name the reader doesn't know raises, because a key that misses that
106
+ * fold would miss every row the fold rewrote.
107
+ */
108
+ export function candidateQueryKeyFold(db: DatabaseClient<CandidateDatabase>): (key: NameKey) => NameKey {
109
+ if (!hasTable(db, CANDIDATE_KEY_FOLD_TABLE)) return (key) => key
110
+
111
+ const names = (
112
+ db.prepare(`SELECT fold FROM ${CANDIDATE_KEY_FOLD_TABLE} ORDER BY fold`).all() as { fold: string }[]
113
+ ).map((row) => row.fold)
114
+
115
+ const folds = names.map((name) => {
116
+ const known = FOLDS[name as CandidateKeyFold]
117
+
118
+ if (!known) throw new Error(`candidate.db records the key fold ${name}, which this reader doesn't implement`)
119
+
120
+ return known.fold
121
+ })
122
+
123
+ return (key) => {
124
+ let folded = key
125
+
126
+ for (const fold of folds) {
127
+ folded = fold(folded)
128
+ }
129
+
130
+ return folded
131
+ }
132
+ }
@@ -1,9 +1,14 @@
1
1
  // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
2
  // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
3
 
4
- import { expandPlacetypeFilter } from "@mailwoman/codex/placetype-map"
4
+ import { expandPlacetypeFilter } from "@mailwoman/codex/labels/placetype-map"
5
5
  import type { VariantAliasExemption } from "@mailwoman/core/geocode"
6
- import { type Ancestor, type GazetteerArtifactCoverage, referentialFromPopulation } from "@mailwoman/core/resolver"
6
+ import {
7
+ type Ancestor,
8
+ type GazetteerArtifactCoverage,
9
+ populationScoreUnmeasured,
10
+ referentialFromPopulation,
11
+ } from "@mailwoman/core/resolver"
7
12
  import { allRows } from "@mailwoman/core/utils"
8
13
  import { jaroWinkler, levenshteinSimilarity } from "@mailwoman/match/comparators"
9
14
  import { partitionByContainment } from "@mailwoman/resolver"
@@ -18,16 +23,20 @@ import {
18
23
  type CandidateAncestorTable,
19
24
  type IntervalLabel,
20
25
  } from "#candidate/ancestors/schema"
26
+ import { readGazetteerCoverageManifest } from "#candidate/coverage-manifest"
21
27
  import { CANDIDATE_FTS_TABLE } from "#candidate/fts"
28
+ import { candidateQueryKeyFold } from "#candidate/key-fold"
22
29
  import type { CandidateDatabase, CandidateTable, CountryCodeTable, PlacetypeCodeTable } from "#candidate/schema"
23
- import { readGazetteerCoverageManifest } from "#coverage-manifest-schema"
24
- import { POSTAL_CITY_CANDIDATE_TABLE, type PostalCityCandidateTable } from "#postal/city/candidate-schema"
25
- import { rankByPrimaryPreference, type RankedRow, RERANK_FETCH } from "#primary-preference"
26
- import { applyProximityRerank } from "#proximity-rerank"
27
- import { REGION_CLASS_PLACETYPES, regionQualifierProbeKeys } from "#region-keys"
30
+ import {
31
+ POSTAL_CITY_CANDIDATE_TABLE,
32
+ type PostalCityCandidateTable,
33
+ } from "#labels/locality/postal-city/candidate-schema"
34
+ import { REGION_CLASS_PLACETYPES, regionQualifierProbeKeys } from "#labels/region"
35
+ import { type NameKey, normalizeLocalityForKey, stripLocalityQualifier } from "#labels/street/normalize"
36
+ import type { FindPlaceQuery, PlaceCandidate, PlaceLookup, WOFPlacetype } from "#lookup/types"
37
+ import { rankByPrimaryPreference, type RankedRow, regionAliasMargin, RERANK_FETCH } from "#ranking/primary-preference"
38
+ import { applyProximityRerank } from "#ranking/proximity-rerank"
28
39
  import { hasColumn, hasTable } from "#sqlite-utils"
29
- import { type NameKey, normalizeLocalityForKey, stripLocalityQualifier } from "#street/normalize"
30
- import type { FindPlaceQuery, PlaceCandidate, PlaceLookup, WOFPlacetype } from "#types"
31
40
 
32
41
  /**
33
42
  * Where a {@link WOFCandidateTableLookup} reads from
@@ -133,6 +142,12 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
133
142
  readonly #intervalCache = new Map<number, IntervalLabel | null>()
134
143
 
135
144
  readonly #qualifierProbe: ReturnType<DatabaseClient["prepare"]> | undefined
145
+
146
+ #placeProbe: ReturnType<DatabaseClient["prepare"]> | undefined
147
+ /**
148
+ * The folds the artifact recorded for its `name_key` column, applied to every query key.
149
+ */
150
+ readonly #queryKeyFold: (key: NameKey) => NameKey
136
151
  /**
137
152
  * Ancestor lineage accessor, nearest-first, present only when the artifact supports it.
138
153
  */
@@ -141,6 +156,8 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
141
156
  constructor(opts: WOFCandidateTableLookupOpts) {
142
157
  super(opts)
143
158
 
159
+ this.#queryKeyFold = candidateQueryKeyFold(this.database)
160
+
144
161
  for (const r of allRows<CountryCodeTable>(this.database.prepare("SELECT id, code FROM country_codes"))) {
145
162
  const code = String(r.code).toUpperCase()
146
163
  this.#countryToID.set(code, Number(r.id))
@@ -246,7 +263,7 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
246
263
  for (const key of regionQualifierProbeKeys(qualifier, country)) {
247
264
  if (!key) continue
248
265
 
249
- for (const row of allRows<{ spr_id: number }>(this.#qualifierProbe, key)) {
266
+ for (const row of allRows<{ spr_id: number }>(this.#qualifierProbe, this.#queryKeyFold(key as NameKey))) {
250
267
  ids.add(Number(row.spr_id))
251
268
  }
252
269
  }
@@ -425,7 +442,8 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
425
442
  text = text.replaceAll(/\s+/g, "")
426
443
  }
427
444
 
428
- const nameKey = normalizeLocalityForKey(text)
445
+ const nameKey = this.#queryKeyFold(normalizeLocalityForKey(text))
446
+ const strippedNameKey = this.#queryKeyFold(normalizeLocalityForKey(stripLocalityQualifier(text)))
429
447
 
430
448
  if (!nameKey) return []
431
449
 
@@ -535,19 +553,19 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
535
553
 
536
554
  const fetched = allRows<CandidateRow>(this.database.prepare(sql), ...params, Math.max(limit, RERANK_FETCH))
537
555
 
538
- return rankByPrimaryPreference(fetched, limit, undefined, this.#idToPlacetype, this.#variantAliasExemption)
556
+ // Inside a stated region, an alias yields to a different place whose primary name equals
557
+ // the query, including an own-name variant the exemption spares from the worldwide penalty.
558
+ return regionID === null
559
+ ? rankByPrimaryPreference(fetched, limit, undefined, this.#idToPlacetype, this.#variantAliasExemption)
560
+ : rankByPrimaryPreference(fetched, limit, regionAliasMargin, this.#idToPlacetype)
539
561
  }
540
562
 
541
563
  const cascade = (regionID: number | null): Array<RankedRow<CandidateRow>> => {
542
564
  let rows = probe(nameKey, regionID)
543
565
 
544
- if (!rows.length) {
545
- const strippedKey = normalizeLocalityForKey(stripLocalityQualifier(text))
546
-
547
- if (strippedKey && strippedKey !== nameKey) {
548
- // Stripped probes only accept primary rows to avoid alias-scrape matches.
549
- rows = probe(strippedKey, regionID).filter((r) => r.is_primary === 1)
550
- }
566
+ // Stripped probes only accept primary rows to avoid alias-scrape matches.
567
+ if (!rows.length && strippedNameKey && strippedNameKey !== nameKey) {
568
+ rows = probe(strippedNameKey, regionID).filter((r) => r.is_primary === 1)
551
569
  }
552
570
 
553
571
  // Typo-tolerant fallback for true name misses only, skipping postcodes.
@@ -573,7 +591,9 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
573
591
  const hits = allRows<{ name_key: string }>(this.#ftsProbe, match, FUZZY_FETCH)
574
592
 
575
593
  const ranked = hits
576
- .map((h) => ({ nk: String(h.name_key), s: wordFuzzySimilarity(nameKey, String(h.name_key)) }))
594
+ // An index built before the artifact's key folds holds unfolded keys, so each hit is folded too.
595
+ .map((h) => this.#queryKeyFold(String(h.name_key) as NameKey))
596
+ .map((nk) => ({ nk, s: wordFuzzySimilarity(nameKey, nk) }))
577
597
  .filter((h) => h.s >= WORD_FUZZY_MIN && (hasCountryEvidence || h.nk[0] === nameKey[0]))
578
598
  // oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array. toSorted would double-allocate on a hot path
579
599
  .sort((a, b) => b.s - a.s)
@@ -638,7 +658,7 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
638
658
  // Admin-containment re-rank runs last.
639
659
  const containmentOptions: ContainmentOptions = {
640
660
  nameKey,
641
- strippedKey: normalizeLocalityForKey(stripLocalityQualifier(text)),
661
+ strippedKey: strippedNameKey,
642
662
  shapeFilters,
643
663
  shapeParams,
644
664
  limit,
@@ -655,58 +675,79 @@ export class WOFCandidateTableLookup extends SQLiteLookup<CandidateDatabase> imp
655
675
  rows = this.#applyContainment(rows, new Set([query.parentQualifierID]), containmentOptions)
656
676
  }
657
677
 
658
- const candidates = rows.map((row): PlaceCandidate => {
659
- const hasBbox = row.min_lat != null && row.max_lat != null && row.min_lon != null && row.max_lon != null
660
- const parent = this.#ancestorLineage(Number(row.spr_id))[0]
661
- const hasPopulation = row.population !== null && row.population > 0
678
+ const candidates = rows.map((row) => this.#toCandidate(row, regionScopeMiss))
679
+
680
+ if (query.bias && query.bias.length) {
681
+ applyProximityRerank(candidates, query.bias)
682
+ }
683
+
684
+ return candidates
685
+ }
686
+
687
+ /**
688
+ * One place by its `spr_id`, read from its primary-name row, or null when the table holds no row for it.
689
+ */
690
+ place(id: number): PlaceCandidate | null {
691
+ if (!Number.isInteger(id)) return null
662
692
 
663
- return {
664
- id: Number(row.spr_id),
665
- name: String(row.name ?? ""),
666
- placetype: (this.#idToPlacetype.get(Number(row.placetype_id)) ?? "") as WOFPlacetype,
693
+ this.#placeProbe ??= this.database.prepare(
694
+ "SELECT spr_id, name, country_id, placetype_id, latitude, longitude, min_lat, min_lon, max_lat, max_lon, neg_rank, is_primary, population" +
695
+ `${this.#importanceSelect} FROM candidate WHERE spr_id = ? ORDER BY is_primary DESC LIMIT 1`
696
+ )
667
697
 
668
- country: this.#idToCountry.get(Number(row.country_id)) ?? "",
669
- lat: Number(row.latitude),
670
- lon: Number(row.longitude),
698
+ const row = this.#placeProbe.get(id) as CandidateRow | undefined
671
699
 
672
- parent_id: parent ? Number(parent.id) : null,
700
+ return row ? this.#toCandidate({ ...row, effectiveNegRank: row.neg_rank, demoted: false }, false) : null
701
+ }
673
702
 
674
- score: -Number(row.neg_rank),
675
- distanceKm: null,
703
+ #toCandidate(row: RankedRow<CandidateRow>, regionScopeMiss: boolean): PlaceCandidate {
704
+ const hasBbox = row.min_lat != null && row.max_lat != null && row.min_lon != null && row.max_lon != null
705
+ const parent = this.#ancestorLineage(Number(row.spr_id))[0]
706
+ const hasPopulation = row.population !== null && row.population > 0
676
707
 
677
- prominence: -Number(row.effectiveNegRank),
708
+ return {
709
+ id: Number(row.spr_id),
710
+ name: String(row.name ?? ""),
711
+ placetype: (this.#idToPlacetype.get(Number(row.placetype_id)) ?? "") as WOFPlacetype,
678
712
 
679
- exactMatch: !row.demoted && !row.fuzzy,
713
+ country: this.#idToCountry.get(Number(row.country_id)) ?? "",
714
+ lat: Number(row.latitude),
715
+ lon: Number(row.longitude),
680
716
 
681
- regionScopeMiss: regionScopeMiss ? true : null,
717
+ parent_id: parent ? Number(parent.id) : null,
682
718
 
683
- containedByQualifier: row.containedByQualifier ?? null,
719
+ score: -Number(row.neg_rank),
720
+ // `neg_rank` is -log10(population + 1), so a place without a population ranks at
721
+ // 0 unmeasured, unless the build ranked it by a population estimate.
722
+ scoreUnmeasured: populationScoreUnmeasured(row.population, Number(row.neg_rank)),
723
+ distanceKm: null,
684
724
 
685
- variantAliasExempted: row.variantExempted ? true : null,
725
+ prominence: -Number(row.effectiveNegRank),
686
726
 
687
- matchedAlias: row.is_primary === 0 ? true : null,
727
+ exactMatch: !row.demoted && !row.fuzzy,
688
728
 
689
- population: hasPopulation ? row.population : null,
690
- referential: hasPopulation ? referentialFromPopulation(row.population) : null,
691
- encyclopedic: null,
729
+ regionScopeMiss: regionScopeMiss ? true : null,
692
730
 
693
- importance: typeof row.importance === "number" && Number.isFinite(row.importance) ? row.importance : null,
694
- bbox: hasBbox
695
- ? {
696
- minLat: Number(row.min_lat),
697
- maxLat: Number(row.max_lat),
698
- minLon: Number(row.min_lon),
699
- maxLon: Number(row.max_lon),
700
- }
701
- : null,
702
- mismatch: null,
703
- }
704
- })
731
+ containedByQualifier: row.containedByQualifier ?? null,
705
732
 
706
- if (query.bias && query.bias.length) {
707
- applyProximityRerank(candidates, query.bias)
708
- }
733
+ variantAliasExempted: row.variantExempted ? true : null,
709
734
 
710
- return candidates
735
+ matchedAlias: row.is_primary === 0 ? true : null,
736
+
737
+ population: hasPopulation ? row.population : null,
738
+ referential: hasPopulation ? referentialFromPopulation(row.population) : null,
739
+ encyclopedic: null,
740
+
741
+ importance: typeof row.importance === "number" && Number.isFinite(row.importance) ? row.importance : null,
742
+ bbox: hasBbox
743
+ ? {
744
+ minLat: Number(row.min_lat),
745
+ maxLat: Number(row.max_lat),
746
+ minLon: Number(row.min_lon),
747
+ maxLon: Number(row.max_lon),
748
+ }
749
+ : null,
750
+ mismatch: null,
751
+ }
711
752
  }
712
753
  }
@@ -12,8 +12,8 @@ import { tableExists } from "@mailwoman/sqlite/introspection"
12
12
  import { isOwnNameVariant } from "#candidate/own-name"
13
13
  import type { PlaceAttrs } from "#candidate/place-attrs"
14
14
  import type { CandidateDatabase } from "#candidate/schema"
15
+ import { normalizeLocalityForKey } from "#labels/street/normalize"
15
16
  import type { WOFDatabase } from "#schema"
16
- import { normalizeLocalityForKey } from "#street/normalize"
17
17
 
18
18
  /**
19
19
  * Key-count threshold for the gloss anomaly detector.