@mailwoman/resolver-wof-sqlite 9.1.0 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (278) hide show
  1. package/README.md +28 -9
  2. package/address-point-interpolation.ts +18 -8
  3. package/address-point-schema.ts +18 -6
  4. package/address-point.ts +111 -18
  5. package/ancestry.ts +9 -6
  6. package/build-candidate.ts +200 -163
  7. package/build-slim.ts +3 -3
  8. package/candidate/alias-bags.ts +54 -0
  9. package/candidate/ancestors-sidecar.ts +206 -0
  10. package/candidate/country-display-names.ts +79 -0
  11. package/candidate/name-roles.ts +237 -0
  12. package/candidate/own-name.ts +146 -0
  13. package/candidate/place-attrs.ts +44 -0
  14. package/candidate/shard-fold.ts +137 -0
  15. package/candidate-ancestors-schema.ts +195 -0
  16. package/candidate-fts.ts +4 -2
  17. package/candidate-importance.ts +2 -1
  18. package/candidate-lookup.ts +439 -186
  19. package/candidate-schema.ts +33 -5
  20. package/candidate-scoring.ts +268 -0
  21. package/capital-schema.ts +90 -0
  22. package/capitals.ts +148 -0
  23. package/coincident-roles.ts +69 -10
  24. package/convention-schema.ts +72 -0
  25. package/convention.ts +2 -2
  26. package/coverage-manifest-schema.ts +7 -7
  27. package/currency-backfill.ts +249 -0
  28. package/exact-match.ts +104 -0
  29. package/fst-autocomplete.ts +91 -119
  30. package/fst-builder.ts +14 -12
  31. package/fst-freshness.ts +2 -2
  32. package/fts-query.ts +1 -1
  33. package/fts.ts +4 -4
  34. package/geonames-postal.ts +2 -2
  35. package/index.ts +18 -14
  36. package/interpolation.ts +113 -19
  37. package/lookup.ts +110 -591
  38. package/name-score.ts +6 -4
  39. package/out/address-point-interpolation.d.ts.map +1 -1
  40. package/out/address-point-interpolation.js +13 -7
  41. package/out/address-point-interpolation.js.map +1 -1
  42. package/out/address-point-schema.d.ts +16 -6
  43. package/out/address-point-schema.d.ts.map +1 -1
  44. package/out/address-point-schema.js.map +1 -1
  45. package/out/address-point.d.ts.map +1 -1
  46. package/out/address-point.js +70 -14
  47. package/out/address-point.js.map +1 -1
  48. package/out/ancestry.d.ts +2 -2
  49. package/out/ancestry.d.ts.map +1 -1
  50. package/out/ancestry.js +5 -6
  51. package/out/ancestry.js.map +1 -1
  52. package/out/build-candidate.d.ts +75 -0
  53. package/out/build-candidate.d.ts.map +1 -1
  54. package/out/build-candidate.js +120 -122
  55. package/out/build-candidate.js.map +1 -1
  56. package/out/build-slim.d.ts +1 -1
  57. package/out/build-slim.js +3 -3
  58. package/out/build-slim.js.map +1 -1
  59. package/out/candidate/alias-bags.d.ts +17 -0
  60. package/out/candidate/alias-bags.d.ts.map +1 -0
  61. package/out/candidate/alias-bags.js +39 -0
  62. package/out/candidate/alias-bags.js.map +1 -0
  63. package/out/candidate/ancestors-sidecar.d.ts +33 -0
  64. package/out/candidate/ancestors-sidecar.d.ts.map +1 -0
  65. package/out/candidate/ancestors-sidecar.js +140 -0
  66. package/out/candidate/ancestors-sidecar.js.map +1 -0
  67. package/out/candidate/country-display-names.d.ts +35 -0
  68. package/out/candidate/country-display-names.d.ts.map +1 -0
  69. package/out/candidate/country-display-names.js +59 -0
  70. package/out/candidate/country-display-names.js.map +1 -0
  71. package/out/candidate/name-roles.d.ts +55 -0
  72. package/out/candidate/name-roles.d.ts.map +1 -0
  73. package/out/candidate/name-roles.js +165 -0
  74. package/out/candidate/name-roles.js.map +1 -0
  75. package/out/candidate/own-name.d.ts +50 -0
  76. package/out/candidate/own-name.d.ts.map +1 -0
  77. package/out/candidate/own-name.js +132 -0
  78. package/out/candidate/own-name.js.map +1 -0
  79. package/out/candidate/place-attrs.d.ts +43 -0
  80. package/out/candidate/place-attrs.d.ts.map +1 -0
  81. package/out/candidate/place-attrs.js +15 -0
  82. package/out/candidate/place-attrs.js.map +1 -0
  83. package/out/candidate/shard-fold.d.ts +31 -0
  84. package/out/candidate/shard-fold.d.ts.map +1 -0
  85. package/out/candidate/shard-fold.js +104 -0
  86. package/out/candidate/shard-fold.js.map +1 -0
  87. package/out/candidate-ancestors-schema.d.ts +150 -0
  88. package/out/candidate-ancestors-schema.d.ts.map +1 -0
  89. package/out/candidate-ancestors-schema.js +123 -0
  90. package/out/candidate-ancestors-schema.js.map +1 -0
  91. package/out/candidate-fts.d.ts +4 -2
  92. package/out/candidate-fts.d.ts.map +1 -1
  93. package/out/candidate-fts.js +4 -2
  94. package/out/candidate-fts.js.map +1 -1
  95. package/out/candidate-importance.d.ts.map +1 -1
  96. package/out/candidate-importance.js +1 -1
  97. package/out/candidate-importance.js.map +1 -1
  98. package/out/candidate-lookup.d.ts +22 -45
  99. package/out/candidate-lookup.d.ts.map +1 -1
  100. package/out/candidate-lookup.js +340 -135
  101. package/out/candidate-lookup.js.map +1 -1
  102. package/out/candidate-schema.d.ts +30 -6
  103. package/out/candidate-schema.d.ts.map +1 -1
  104. package/out/candidate-schema.js +3 -0
  105. package/out/candidate-schema.js.map +1 -1
  106. package/out/candidate-scoring.d.ts +34 -0
  107. package/out/candidate-scoring.d.ts.map +1 -0
  108. package/out/candidate-scoring.js +200 -0
  109. package/out/candidate-scoring.js.map +1 -0
  110. package/out/capital-schema.d.ts +51 -0
  111. package/out/capital-schema.d.ts.map +1 -0
  112. package/out/capital-schema.js +63 -0
  113. package/out/capital-schema.js.map +1 -0
  114. package/out/capitals.d.ts +69 -0
  115. package/out/capitals.d.ts.map +1 -0
  116. package/out/capitals.js +98 -0
  117. package/out/capitals.js.map +1 -0
  118. package/out/coincident-roles.d.ts +7 -0
  119. package/out/coincident-roles.d.ts.map +1 -1
  120. package/out/coincident-roles.js +42 -8
  121. package/out/coincident-roles.js.map +1 -1
  122. package/out/convention-schema.d.ts +51 -0
  123. package/out/convention-schema.d.ts.map +1 -0
  124. package/out/convention-schema.js +34 -0
  125. package/out/convention-schema.js.map +1 -0
  126. package/out/convention.d.ts +1 -1
  127. package/out/convention.js +2 -2
  128. package/out/coverage-manifest-schema.js +3 -7
  129. package/out/coverage-manifest-schema.js.map +1 -1
  130. package/out/currency-backfill.d.ts +46 -0
  131. package/out/currency-backfill.d.ts.map +1 -0
  132. package/out/currency-backfill.js +180 -0
  133. package/out/currency-backfill.js.map +1 -0
  134. package/out/exact-match.d.ts +25 -0
  135. package/out/exact-match.d.ts.map +1 -0
  136. package/out/exact-match.js +89 -0
  137. package/out/exact-match.js.map +1 -0
  138. package/out/fst-autocomplete.d.ts +11 -11
  139. package/out/fst-autocomplete.d.ts.map +1 -1
  140. package/out/fst-autocomplete.js +82 -99
  141. package/out/fst-autocomplete.js.map +1 -1
  142. package/out/fst-builder.d.ts.map +1 -1
  143. package/out/fst-builder.js +11 -12
  144. package/out/fst-builder.js.map +1 -1
  145. package/out/fst-freshness.d.ts +2 -2
  146. package/out/fst-freshness.js +2 -2
  147. package/out/fts-query.js +1 -1
  148. package/out/fts-query.js.map +1 -1
  149. package/out/fts.d.ts +4 -4
  150. package/out/fts.js +4 -4
  151. package/out/geonames-postal.d.ts +2 -2
  152. package/out/geonames-postal.js +2 -2
  153. package/out/index.d.ts +3 -2
  154. package/out/index.d.ts.map +1 -1
  155. package/out/index.js +2 -2
  156. package/out/index.js.map +1 -1
  157. package/out/interpolation.d.ts +8 -0
  158. package/out/interpolation.d.ts.map +1 -1
  159. package/out/interpolation.js +91 -19
  160. package/out/interpolation.js.map +1 -1
  161. package/out/lookup.d.ts +4 -5
  162. package/out/lookup.d.ts.map +1 -1
  163. package/out/lookup.js +94 -468
  164. package/out/lookup.js.map +1 -1
  165. package/out/name-score.d.ts +0 -10
  166. package/out/name-score.d.ts.map +1 -1
  167. package/out/name-score.js +6 -4
  168. package/out/name-score.js.map +1 -1
  169. package/out/place-importance-schema.d.ts +42 -5
  170. package/out/place-importance-schema.d.ts.map +1 -1
  171. package/out/place-importance-schema.js +54 -8
  172. package/out/place-importance-schema.js.map +1 -1
  173. package/out/poi-lookup.d.ts +1 -1
  174. package/out/poi-lookup.d.ts.map +1 -1
  175. package/out/poi-lookup.js +12 -13
  176. package/out/poi-lookup.js.map +1 -1
  177. package/out/poi-schema.d.ts +7 -3
  178. package/out/poi-schema.d.ts.map +1 -1
  179. package/out/poi-schema.js.map +1 -1
  180. package/out/polygon-schema.d.ts +37 -0
  181. package/out/polygon-schema.d.ts.map +1 -0
  182. package/out/polygon-schema.js +23 -0
  183. package/out/polygon-schema.js.map +1 -0
  184. package/out/postal-city-alias-lookup.d.ts +1 -1
  185. package/out/postal-city-alias-lookup.js +1 -1
  186. package/out/postal-city-candidate-schema.d.ts +2 -1
  187. package/out/postal-city-candidate-schema.d.ts.map +1 -1
  188. package/out/postal-city-candidate-schema.js.map +1 -1
  189. package/out/postcode-point-lookup.d.ts +1 -1
  190. package/out/postcode-point-lookup.js +1 -1
  191. package/out/primary-preference.d.ts +125 -0
  192. package/out/primary-preference.d.ts.map +1 -0
  193. package/out/primary-preference.js +138 -0
  194. package/out/primary-preference.js.map +1 -0
  195. package/out/proximity-rerank.d.ts +77 -0
  196. package/out/proximity-rerank.d.ts.map +1 -0
  197. package/out/proximity-rerank.js +86 -0
  198. package/out/proximity-rerank.js.map +1 -0
  199. package/out/region-keys.d.ts +47 -0
  200. package/out/region-keys.d.ts.map +1 -0
  201. package/out/region-keys.js +121 -0
  202. package/out/region-keys.js.map +1 -0
  203. package/out/reverse.d.ts.map +1 -1
  204. package/out/reverse.js +6 -9
  205. package/out/reverse.js.map +1 -1
  206. package/out/schema.d.ts +1 -1
  207. package/out/search-fetch.d.ts +57 -0
  208. package/out/search-fetch.d.ts.map +1 -0
  209. package/out/search-fetch.js +183 -0
  210. package/out/search-fetch.js.map +1 -0
  211. package/out/sharding.d.ts +3 -3
  212. package/out/sharding.js +1 -1
  213. package/out/sqlite-convention-source.d.ts +1 -1
  214. package/out/sqlite-convention-source.js +1 -1
  215. package/out/sqlite-utils.d.ts +19 -1
  216. package/out/sqlite-utils.d.ts.map +1 -1
  217. package/out/sqlite-utils.js +19 -1
  218. package/out/sqlite-utils.js.map +1 -1
  219. package/out/street-centroid-schema.d.ts +7 -2
  220. package/out/street-centroid-schema.d.ts.map +1 -1
  221. package/out/street-centroid-schema.js.map +1 -1
  222. package/out/street-centroid.d.ts.map +1 -1
  223. package/out/street-centroid.js +7 -7
  224. package/out/street-centroid.js.map +1 -1
  225. package/out/street-normalize.d.ts +82 -9
  226. package/out/street-normalize.d.ts.map +1 -1
  227. package/out/street-normalize.js +175 -9
  228. package/out/street-normalize.js.map +1 -1
  229. package/out/street-segment-schema.d.ts +6 -2
  230. package/out/street-segment-schema.d.ts.map +1 -1
  231. package/out/street-segment-schema.js.map +1 -1
  232. package/out/types.d.ts +35 -1
  233. package/out/types.d.ts.map +1 -1
  234. package/out/unified-schema.d.ts +1 -1
  235. package/out/unified-schema.js +1 -1
  236. package/out/uprn-lookup.d.ts +85 -0
  237. package/out/uprn-lookup.d.ts.map +1 -0
  238. package/out/uprn-lookup.js +152 -0
  239. package/out/uprn-lookup.js.map +1 -0
  240. package/out/uprn-schema.d.ts +93 -0
  241. package/out/uprn-schema.d.ts.map +1 -0
  242. package/out/uprn-schema.js +78 -0
  243. package/out/uprn-schema.js.map +1 -0
  244. package/out/weights-overlay-linker.d.ts +141 -0
  245. package/out/weights-overlay-linker.d.ts.map +1 -0
  246. package/out/weights-overlay-linker.js +259 -0
  247. package/out/weights-overlay-linker.js.map +1 -0
  248. package/package.json +288 -16
  249. package/place-importance-schema.ts +64 -15
  250. package/poi-lookup.ts +12 -13
  251. package/poi-schema.ts +8 -3
  252. package/polygon-schema.ts +47 -0
  253. package/postal-city-alias-lookup.ts +1 -1
  254. package/postal-city-candidate-schema.ts +3 -1
  255. package/postcode-point-lookup.ts +1 -1
  256. package/primary-preference.ts +207 -0
  257. package/proximity-rerank.ts +120 -0
  258. package/region-keys.ts +144 -0
  259. package/reverse.ts +17 -16
  260. package/schema.ts +1 -1
  261. package/search-fetch.ts +256 -0
  262. package/sharding.ts +3 -3
  263. package/sqlite-convention-source.ts +1 -1
  264. package/sqlite-utils.ts +43 -2
  265. package/street-centroid-schema.ts +8 -2
  266. package/street-centroid.ts +13 -8
  267. package/street-normalize.ts +252 -23
  268. package/street-segment-schema.ts +7 -2
  269. package/types.ts +35 -1
  270. package/unified-schema.ts +1 -1
  271. package/uprn-lookup.ts +210 -0
  272. package/uprn-schema.ts +124 -0
  273. package/weights-overlay-linker.ts +377 -0
  274. package/geo.ts +0 -121
  275. package/out/geo.d.ts +0 -74
  276. package/out/geo.d.ts.map +0 -1
  277. package/out/geo.js +0 -71
  278. package/out/geo.js.map +0 -1
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ * @file The per-place record every candidate-staging pass writes its rows from.
6
+ *
7
+ * Pass 1 reduces each current `spr` row to one {@link PlaceAttrs}; every later pass (alias bags,
8
+ * region abbreviations, country display names, the currency backfill, the shard folds) discovers
9
+ * ADDITIONAL name keys for a place already in that map and stages a row against the same record.
10
+ * That is what keeps each candidate row denormalized without re-reading the source, and it is why
11
+ * a pass needs exactly four things to stage: the key it found, the place, the id the row hangs on,
12
+ * and whether the key is the place's canonical name.
13
+ */
14
+
15
+ export interface PlaceAttrs {
16
+ cid: number
17
+ rid: number
18
+ ptid: number
19
+ name: string
20
+ lat: number
21
+ lon: number
22
+ mnLat: number
23
+ mnLon: number
24
+ mxLat: number
25
+ mxLon: number
26
+ pop: number
27
+ neg: number
28
+ pkey: string
29
+ /**
30
+ * The place's toponym-fame score, or null when the score source has no measurement for it (#28). A property of the
31
+ * PLACE, so it rides {@link StageRow} onto the alias and abbrev rows too — that is how a bare `Moscow` reaches
32
+ * Москва's score through the alias row that carries the key.
33
+ */
34
+ imp: number | null
35
+ }
36
+
37
+ /**
38
+ * Stage one candidate row: a normalized name key, the place it belongs to, the source id the row hangs on, and whether
39
+ * the key is that place's canonical name (`is_primary`).
40
+ *
41
+ * `sid` is passed separately rather than read off the place because a shard fold and the alias pass stage rows for ids
42
+ * the admin `attrs` map never held.
43
+ */
44
+ export type StageRow = (k: string, a: PlaceAttrs, sid: number, isPrimary: number) => void
@@ -0,0 +1,137 @@
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 shard into the staging table.
6
+ */
7
+
8
+ import { DatabaseSync } from "node:sqlite"
9
+
10
+ import { normalizeLocalityForKey } from "../street-normalize.ts"
11
+ import type { PlaceAttrs, StageRow } from "./place-attrs.ts"
12
+
13
+ /**
14
+ * Fold ONE shard (`spr` rows at `shardPlacetype` carrying real coordinates) in, then pass 4b: the alias names hanging
15
+ * off that same shard's `names` table.
16
+ *
17
+ * Self-contained by construction — it shares only the staging statement and the code dictionaries with the admin passes
18
+ * above it, and nothing downstream reads anything it produces except the two counters it returns.
19
+ */
20
+ export function foldShard(ctx: {
21
+ /**
22
+ * The staging connection. The shard itself is opened read-only here and closed before returning.
23
+ */
24
+ out: DatabaseSync
25
+ shardPath: string
26
+ shardPlacetype: "postalcode" | "locality"
27
+ ccID: (code: string | null) => number
28
+ ptID: (pt: string | null) => number
29
+ stageRow: StageRow
30
+ progress: (phase: string, message: string) => void
31
+ }): { primaries: number; aliases: number } {
32
+ const { out, shardPath, shardPlacetype, ccID, ptID, stageRow, progress } = ctx
33
+
34
+ progress(shardPlacetype === "postalcode" ? "postcodes" : "localities", `reading ${shardPath}`)
35
+
36
+ const pc = new DatabaseSync(shardPath, { readOnly: true })
37
+ const pcPtid = ptID(shardPlacetype)
38
+ // Per-shard, not the admin `attrs` map: pass 1 only ever sees the admin DB, so the alias pass
39
+ // below has nothing to join against unless this primary loop records what it staged.
40
+ const pcAttrs = new Map<number, PlaceAttrs>()
41
+ let primaries = 0
42
+ let aliases = 0
43
+
44
+ out.exec("BEGIN")
45
+
46
+ for (const r of pc
47
+ .prepare(
48
+ `SELECT id, name, country, latitude, longitude,
49
+ min_latitude AS mnlat, min_longitude AS mnlon, max_latitude AS mxlat, max_longitude AS mxlon
50
+ FROM spr WHERE placetype = ? AND latitude != 0 AND longitude != 0`
51
+ )
52
+ .iterate(shardPlacetype)) {
53
+ const name = String(r.name ?? "")
54
+ const key = normalizeLocalityForKey(name)
55
+
56
+ if (!key) continue
57
+
58
+ const lat = r.latitude as number
59
+ const lon = r.longitude as number
60
+
61
+ // region_id 0 (a postcode is unique by name+country — no same-name disambiguation); neg_rank 0
62
+ // (no population). bbox = the postcode's own min/max (falls back to the centroid point).
63
+ const a: PlaceAttrs = {
64
+ cid: ccID(r.country as string | null),
65
+ rid: 0,
66
+ ptid: pcPtid,
67
+ name,
68
+ lat,
69
+ lon,
70
+ mnLat: (r.mnlat as number) || lat,
71
+ mnLon: (r.mnlon as number) || lon,
72
+ mxLat: (r.mxlat as number) || lat,
73
+ mxLon: (r.mxlon as number) || lon,
74
+ pop: 0,
75
+ neg: 0,
76
+ pkey: key,
77
+ // A postcode has no toponym fame — nobody writes an encyclopedia article about SW1A 2AA — and
78
+ // the score source carries no `postalcode` rows to join against anyway. NULL is the truthful
79
+ // value: unmeasured, so the ranking key leaves postcode rows exactly where they were.
80
+ imp: null,
81
+ }
82
+
83
+ pcAttrs.set(Number(r.id), a)
84
+ stageRow(key, a, Number(r.id), 1)
85
+
86
+ primaries++
87
+ }
88
+
89
+ out.exec("COMMIT")
90
+
91
+ // --- pass 4b: postcode ALIAS names (#1495) ---
92
+ //
93
+ // The delivery-city names GeoNames supplies for a ZIP ("Brooklyn" for 11201) are written into
94
+ // the shard's `names` table by `postcode/centroid-fills.ts`'s `geonamesNameFill`. Everything
95
+ // downstream of `names` picked them up EXCEPT this build: `fts.ts` unions `spr.name` with every
96
+ // `names` row into `place_search.alt_names`, so the FTS backend resolved "Brooklyn" → 11201
97
+ // while the candidate backend — whose every row IS an exact-tier row — had no key for it at
98
+ // all. Pass 2 does the equivalent fold for admin places, but reads the ADMIN `place_search`,
99
+ // and `attrs` holds admin ids only, so a postcode shard could never reach it.
100
+ //
101
+ // Same discipline as pass 2: `is_primary = 0` (so `rankByPrimaryPreference` treats it as an
102
+ // alias, not a canonical postcode name), the row stays denormalized onto the POSTCODE's own
103
+ // spr_id/coords/bbox, and the display `name` stays the postcode — resolving "brooklyn" answers
104
+ // with place 11201, it does not rename the place to its delivery city.
105
+ const hasNames = pc.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='names'").get() !== undefined
106
+
107
+ if (hasNames) {
108
+ out.exec("BEGIN")
109
+
110
+ for (const r of pc.prepare("SELECT id, name FROM names").iterate()) {
111
+ const a = pcAttrs.get(Number(r.id))
112
+
113
+ if (!a) continue
114
+
115
+ const k = normalizeLocalityForKey(String(r.name ?? ""))
116
+
117
+ // The postcode's own key is already staged as the primary; `INSERT OR IGNORE` at
118
+ // materialization dedupes repeats, so this only skips the obvious self-alias.
119
+ if (!k || k === a.pkey) continue
120
+
121
+ stageRow(k, a, Number(r.id), 0)
122
+
123
+ aliases++
124
+ }
125
+
126
+ out.exec("COMMIT")
127
+ } else {
128
+ // Never a silent zero: real shards come from `createUnifiedSchema`, which always creates
129
+ // `names`. A shard without it has no alias surface to lose, but say so rather than reporting
130
+ // "0 aliases" from a table that was never read.
131
+ progress("postcode-aliases", `${shardPath} has no \`names\` table — no delivery-city aliases to fold`)
132
+ }
133
+
134
+ pc.close()
135
+
136
+ return { primaries, aliases }
137
+ }
@@ -0,0 +1,195 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Typed schema for the candidate gazetteer's ANCESTORS sidecar (`candidate_ancestor` +
7
+ * `candidate_interval`) — the containment lineage the candidate table itself cannot answer, built
8
+ * into the same `candidate.db` by `build-candidate.ts` and read by
9
+ * {@link WOFCandidateTableLookup.ancestors}.
10
+ *
11
+ * ENCODING: closure-lite rows, denormalized — one row per (place, ancestor) edge carrying the
12
+ * parent's placetype, display name and folded key — rather than a fixed-slot id chain on the
13
+ * candidate row. Decided by the two consumers:
14
+ *
15
+ * 1. The admin-coherence check needs the WINNER's chain as (placetype, name) pairs in one probe.
16
+ * A fixed-slot `[id;8]` chain answers with ids, and every id then needs a name lookup the
17
+ * artifact has no per-id table for — up to 8 indirections where the closure row has zero.
18
+ * 2. The account layer needs EVERY candidate under a `name_key` enumerable WITH its chain from
19
+ * one artifact probe ("present-but-outranked, discriminated by containment"). That is the
20
+ * candidate probe (contiguous) followed by one `spr_id`-clustered closure probe per candidate
21
+ * — each a handful of adjacent pages.
22
+ *
23
+ * Denormalizing the parent onto the edge is the same discipline as the candidate table itself:
24
+ * this artifact is read over HTTP byte ranges, where a join to a dimension table scatters page
25
+ * fetches, and a `WITHOUT ROWID` B-tree clustered on `(spr_id, depth)` keeps a whole chain in
26
+ * 1-2 pages. The repeated parent strings are the price of the zero-join read, paid at build time.
27
+ *
28
+ * `depth` is 1 for the NEAREST ancestor (deepest containment tier), increasing outward to the
29
+ * country — the same nearest-first order `ancestry.ts` serves for the FTS backend, so the two
30
+ * backends' `ancestors()` agree by construction. The order within a place is deterministic:
31
+ * containment depth descending (`placetypeDepth`), then ancestor id ascending, capped at
32
+ * {@link MAX_ANCESTOR_DEPTH}. `parent_name_key` is the SHARED {@link normalizeLocalityForKey}
33
+ * fold — the same fold `candidate.name_key` is built with, so a chain entry and a candidate key
34
+ * compare under one normalizer.
35
+ *
36
+ * `candidate_interval` carries pre/post-order labels over the CANONICAL-PARENT FOREST, assigned
37
+ * at build time: `a` contains `d` ⟺ `a.pre <= d.pre AND d.post <= a.post` — O(1) in either
38
+ * direction with no chain scan and no knowledge of either side's tier — and the descendants of
39
+ * `a` are the contiguous range `pre BETWEEN a.pre AND a.post`. Interval labels are classically
40
+ * avoided for their relabel-on-update cost; this database is a sealed read-only artifact rebuilt
41
+ * whole, which is exactly the regime where that cost is void.
42
+ *
43
+ * THE DAG CAVEAT, and the recorded choice: WOF places can carry more than one parent (multiple
44
+ * hierarchies, ambiguous boundaries). `candidate_ancestor` keeps EVERY parent — the closure rows
45
+ * are the complete containment record. A single interval pair can only encode a tree, so the
46
+ * interval forest links each place to ONE canonical parent: its depth-1 edge — the finest
47
+ * containment tier, lowest ancestor id — the same MIN-stability convention the candidate table's
48
+ * `region_id` stamp uses. A containment question about a NON-canonical hierarchy must consult the
49
+ * closure rows; the interval answer for it is `false`, which is why interval verdicts are
50
+ * "contained along the canonical hierarchy", never "not contained at all".
51
+ *
52
+ * ABSENCE SEMANTICS (meaning-of-zero): a place with no `candidate_interval` row has no recorded
53
+ * ancestry in the source (shard-fed postcodes and localities, isolated places, cycle-skipped
54
+ * rows). Absence is UNVERIFIABLE, never a containment verdict.
55
+ */
56
+
57
+ import { sql, type Kysely } from "kysely"
58
+
59
+ // Type-only and circular on purpose (candidate-schema extends CandidateAncestorsDatabase): Kysely's
60
+ // DB parameter is invariant, so the DDL functions must take the FULL database type their caller
61
+ // holds. Erased at runtime.
62
+ import type { CandidateDatabase } from "./candidate-schema.ts"
63
+ import type { NameKey } from "./street-normalize.ts"
64
+
65
+ /**
66
+ * The deepest chain the sidecar stores per place. WOF containment within the resolvable placetypes (country …
67
+ * microhood) never legitimately exceeds this; anything past it is source noise the build drops (and counts) rather than
68
+ * stores.
69
+ */
70
+ export const MAX_ANCESTOR_DEPTH = 8
71
+
72
+ /**
73
+ * The closure-row table's name — probed with `hasTable` by the reader, so an artifact predating the sidecar degrades to
74
+ * "no ancestors capability" rather than `no such table`.
75
+ */
76
+ export const CANDIDATE_ANCESTOR_TABLE = "candidate_ancestor"
77
+
78
+ /**
79
+ * The interval-label table's name — the closure table's seal-time sibling; existence-gated the same way.
80
+ */
81
+ export const CANDIDATE_INTERVAL_TABLE = "candidate_interval"
82
+
83
+ /**
84
+ * One (place, ancestor) edge, denormalized so a chain read is a single clustered probe (no join).
85
+ */
86
+ export interface CandidateAncestorTable {
87
+ /**
88
+ * WOF id of the place whose chain this row belongs to — the same `spr_id` the candidate table resolves to.
89
+ */
90
+ spr_id: number
91
+ /**
92
+ * 1 = nearest ancestor, increasing outward to the country. Deterministic within a place (containment depth
93
+ * descending, then ancestor id ascending), so `(spr_id, depth)` is a stable primary key across rebuilds.
94
+ */
95
+ depth: number
96
+ /**
97
+ * WOF id of the ancestor.
98
+ */
99
+ parent_spr_id: number
100
+ /**
101
+ * Small int from the shared `placetype_codes` dictionary (the same one the candidate table uses).
102
+ */
103
+ parent_placetype_id: number
104
+ /**
105
+ * The ancestor's canonical display name — what {@link Ancestor.name} serves, matching the FTS backend's register.
106
+ */
107
+ parent_name: string
108
+ /**
109
+ * The SHARED {@link normalizeLocalityForKey} fold of `parent_name` — comparable against `candidate.name_key` and
110
+ * against a query-side fold under one normalizer, by construction.
111
+ */
112
+ parent_name_key: NameKey
113
+ }
114
+
115
+ /**
116
+ * Pre/post-order labels over the canonical-parent forest — one row per place WITH recorded ancestry (see the module
117
+ * docstring for absence semantics). `pre < post` always; labels are unique across the artifact.
118
+ */
119
+ export interface CandidateIntervalTable {
120
+ spr_id: number
121
+ pre: number
122
+ post: number
123
+ }
124
+
125
+ /**
126
+ * The sidecar tables, for a `Kysely` view over the candidate DB. `CandidateDatabase` (candidate-schema.ts) extends
127
+ * this, so the builder's one typed client sees both families.
128
+ */
129
+ export interface CandidateAncestorsDatabase {
130
+ candidate_ancestor: CandidateAncestorTable
131
+ candidate_interval: CandidateIntervalTable
132
+ }
133
+
134
+ /**
135
+ * The `candidate_ancestor` columns in clustered-key order — the first two ARE the primary key, and the builder's
136
+ * positional `INSERT` binds by this order. Keep in sync with {@link CandidateAncestorTable}.
137
+ */
138
+ export const CANDIDATE_ANCESTOR_COLUMNS = [
139
+ "spr_id",
140
+ "depth",
141
+ "parent_spr_id",
142
+ "parent_placetype_id",
143
+ "parent_name",
144
+ "parent_name_key",
145
+ ] as const
146
+
147
+ /**
148
+ * Create the clustered closure table. The builder inserts in `(spr_id, depth)` order so the B-tree leaves are
149
+ * contiguous per place — the byte-range read discipline.
150
+ */
151
+ export async function createCandidateAncestorTable(db: Kysely<CandidateDatabase>): Promise<void> {
152
+ await db.schema
153
+ .createTable(CANDIDATE_ANCESTOR_TABLE)
154
+ .addColumn("spr_id", "integer", (c) => c.notNull())
155
+ .addColumn("depth", "integer", (c) => c.notNull())
156
+ .addColumn("parent_spr_id", "integer", (c) => c.notNull())
157
+ .addColumn("parent_placetype_id", "integer", (c) => c.notNull())
158
+ .addColumn("parent_name", "text", (c) => c.notNull())
159
+ .addColumn("parent_name_key", "text", (c) => c.notNull())
160
+ .addPrimaryKeyConstraint("candidate_ancestor_pk", ["spr_id", "depth"])
161
+ // `WITHOUT ROWID` has no first-class builder; the raw modifier is the idiomatic fallback.
162
+ .modifyEnd(sql`without rowid`)
163
+ .execute()
164
+ }
165
+
166
+ /**
167
+ * Create the interval-label table. Small rows probed by primary key — the `WITHOUT ROWID` win case.
168
+ */
169
+ export async function createCandidateIntervalTable(db: Kysely<CandidateDatabase>): Promise<void> {
170
+ await db.schema
171
+ .createTable(CANDIDATE_INTERVAL_TABLE)
172
+ .addColumn("spr_id", "integer", (c) => c.primaryKey())
173
+ .addColumn("pre", "integer", (c) => c.notNull())
174
+ .addColumn("post", "integer", (c) => c.notNull())
175
+ .modifyEnd(sql`without rowid`)
176
+ .execute()
177
+ }
178
+
179
+ /**
180
+ * One place's interval label — the shape both sides of a containment comparison read.
181
+ */
182
+ export interface IntervalLabel {
183
+ pre: number
184
+ post: number
185
+ }
186
+
187
+ /**
188
+ * Does `outer` contain `inner` along the canonical hierarchy? Reflexive: a place contains itself (containment
189
+ * degenerates to identity, the same reading the admin-coherence check gives a self-match). The shared FUNCTION for
190
+ * every consumer of the labels — a re-derived comparison risks disagreeing at exactly the strict/inclusive boundary
191
+ * this line settles.
192
+ */
193
+ export function intervalContains(outer: IntervalLabel, inner: IntervalLabel): boolean {
194
+ return outer.pre <= inner.pre && inner.post <= outer.post
195
+ }
package/candidate-fts.ts CHANGED
@@ -9,8 +9,10 @@
9
9
  * raw `name`), so a diacritic-stripped query (`munchen`) trigram-matches the stored `munchen`
10
10
  * rather than missing a raw `München`. The trigram tokenizer makes MATCH a substring/fuzzy
11
11
  * operation; the reader ({@link WOFCandidateTableLookup}) OR's the query's trigrams to fetch a
12
- * loose set, then re-ranks by the SAME `trigramJaccard` the admin/FTS backend uses, so a typo
13
- * resolves identically on either.
12
+ * loose set, then re-ranks it with a WORD-level similarity. The trigram index is the candidate
13
+ * GENERATOR, not the scorer — trigram Jaccard scores a true transposition correction BELOW a wrong
14
+ * answer, because shared generic suffixes count as evidence and transpositions count against it.
15
+ * The receipts are on `candidate-lookup.ts`'s `FUZZY_FETCH` and `WORD_FUZZY_MIN`.
14
16
  *
15
17
  * This is what unifies the two gazetteers: the candidate B-tree stays the common,
16
18
  * byte-range-optimal fast path (the browser's contiguous probe), and FTS5 is consulted ONLY on an
@@ -43,7 +43,8 @@
43
43
 
44
44
  import { DatabaseSync } from "node:sqlite"
45
45
 
46
- import { haversineKm } from "./geo.ts"
46
+ import { haversineKm } from "@mailwoman/spatial"
47
+
47
48
  import { normalizeLocalityForKey } from "./street-normalize.ts"
48
49
 
49
50
  /**