@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.
- package/README.md +28 -9
- package/address-point-interpolation.ts +18 -8
- package/address-point-schema.ts +18 -6
- package/address-point.ts +111 -18
- package/ancestry.ts +9 -6
- package/build-candidate.ts +200 -163
- package/build-slim.ts +3 -3
- package/candidate/alias-bags.ts +54 -0
- package/candidate/ancestors-sidecar.ts +206 -0
- package/candidate/country-display-names.ts +79 -0
- package/candidate/name-roles.ts +237 -0
- package/candidate/own-name.ts +146 -0
- package/candidate/place-attrs.ts +44 -0
- package/candidate/shard-fold.ts +137 -0
- package/candidate-ancestors-schema.ts +195 -0
- package/candidate-fts.ts +4 -2
- package/candidate-importance.ts +2 -1
- package/candidate-lookup.ts +439 -186
- package/candidate-schema.ts +33 -5
- package/candidate-scoring.ts +268 -0
- package/capital-schema.ts +90 -0
- package/capitals.ts +148 -0
- package/coincident-roles.ts +69 -10
- package/convention-schema.ts +72 -0
- package/convention.ts +2 -2
- package/coverage-manifest-schema.ts +7 -7
- package/currency-backfill.ts +249 -0
- package/exact-match.ts +104 -0
- package/fst-autocomplete.ts +91 -119
- package/fst-builder.ts +14 -12
- package/fst-freshness.ts +2 -2
- package/fts-query.ts +1 -1
- package/fts.ts +4 -4
- package/geonames-postal.ts +2 -2
- package/index.ts +18 -14
- package/interpolation.ts +113 -19
- package/lookup.ts +110 -591
- package/name-score.ts +6 -4
- package/out/address-point-interpolation.d.ts.map +1 -1
- package/out/address-point-interpolation.js +13 -7
- package/out/address-point-interpolation.js.map +1 -1
- package/out/address-point-schema.d.ts +16 -6
- package/out/address-point-schema.d.ts.map +1 -1
- package/out/address-point-schema.js.map +1 -1
- package/out/address-point.d.ts.map +1 -1
- package/out/address-point.js +70 -14
- package/out/address-point.js.map +1 -1
- package/out/ancestry.d.ts +2 -2
- package/out/ancestry.d.ts.map +1 -1
- package/out/ancestry.js +5 -6
- package/out/ancestry.js.map +1 -1
- package/out/build-candidate.d.ts +75 -0
- package/out/build-candidate.d.ts.map +1 -1
- package/out/build-candidate.js +120 -122
- package/out/build-candidate.js.map +1 -1
- package/out/build-slim.d.ts +1 -1
- package/out/build-slim.js +3 -3
- package/out/build-slim.js.map +1 -1
- package/out/candidate/alias-bags.d.ts +17 -0
- package/out/candidate/alias-bags.d.ts.map +1 -0
- package/out/candidate/alias-bags.js +39 -0
- package/out/candidate/alias-bags.js.map +1 -0
- package/out/candidate/ancestors-sidecar.d.ts +33 -0
- package/out/candidate/ancestors-sidecar.d.ts.map +1 -0
- package/out/candidate/ancestors-sidecar.js +140 -0
- package/out/candidate/ancestors-sidecar.js.map +1 -0
- package/out/candidate/country-display-names.d.ts +35 -0
- package/out/candidate/country-display-names.d.ts.map +1 -0
- package/out/candidate/country-display-names.js +59 -0
- package/out/candidate/country-display-names.js.map +1 -0
- package/out/candidate/name-roles.d.ts +55 -0
- package/out/candidate/name-roles.d.ts.map +1 -0
- package/out/candidate/name-roles.js +165 -0
- package/out/candidate/name-roles.js.map +1 -0
- package/out/candidate/own-name.d.ts +50 -0
- package/out/candidate/own-name.d.ts.map +1 -0
- package/out/candidate/own-name.js +132 -0
- package/out/candidate/own-name.js.map +1 -0
- package/out/candidate/place-attrs.d.ts +43 -0
- package/out/candidate/place-attrs.d.ts.map +1 -0
- package/out/candidate/place-attrs.js +15 -0
- package/out/candidate/place-attrs.js.map +1 -0
- package/out/candidate/shard-fold.d.ts +31 -0
- package/out/candidate/shard-fold.d.ts.map +1 -0
- package/out/candidate/shard-fold.js +104 -0
- package/out/candidate/shard-fold.js.map +1 -0
- package/out/candidate-ancestors-schema.d.ts +150 -0
- package/out/candidate-ancestors-schema.d.ts.map +1 -0
- package/out/candidate-ancestors-schema.js +123 -0
- package/out/candidate-ancestors-schema.js.map +1 -0
- package/out/candidate-fts.d.ts +4 -2
- package/out/candidate-fts.d.ts.map +1 -1
- package/out/candidate-fts.js +4 -2
- package/out/candidate-fts.js.map +1 -1
- package/out/candidate-importance.d.ts.map +1 -1
- package/out/candidate-importance.js +1 -1
- package/out/candidate-importance.js.map +1 -1
- package/out/candidate-lookup.d.ts +22 -45
- package/out/candidate-lookup.d.ts.map +1 -1
- package/out/candidate-lookup.js +340 -135
- package/out/candidate-lookup.js.map +1 -1
- package/out/candidate-schema.d.ts +30 -6
- package/out/candidate-schema.d.ts.map +1 -1
- package/out/candidate-schema.js +3 -0
- package/out/candidate-schema.js.map +1 -1
- package/out/candidate-scoring.d.ts +34 -0
- package/out/candidate-scoring.d.ts.map +1 -0
- package/out/candidate-scoring.js +200 -0
- package/out/candidate-scoring.js.map +1 -0
- package/out/capital-schema.d.ts +51 -0
- package/out/capital-schema.d.ts.map +1 -0
- package/out/capital-schema.js +63 -0
- package/out/capital-schema.js.map +1 -0
- package/out/capitals.d.ts +69 -0
- package/out/capitals.d.ts.map +1 -0
- package/out/capitals.js +98 -0
- package/out/capitals.js.map +1 -0
- package/out/coincident-roles.d.ts +7 -0
- package/out/coincident-roles.d.ts.map +1 -1
- package/out/coincident-roles.js +42 -8
- package/out/coincident-roles.js.map +1 -1
- package/out/convention-schema.d.ts +51 -0
- package/out/convention-schema.d.ts.map +1 -0
- package/out/convention-schema.js +34 -0
- package/out/convention-schema.js.map +1 -0
- package/out/convention.d.ts +1 -1
- package/out/convention.js +2 -2
- package/out/coverage-manifest-schema.js +3 -7
- package/out/coverage-manifest-schema.js.map +1 -1
- package/out/currency-backfill.d.ts +46 -0
- package/out/currency-backfill.d.ts.map +1 -0
- package/out/currency-backfill.js +180 -0
- package/out/currency-backfill.js.map +1 -0
- package/out/exact-match.d.ts +25 -0
- package/out/exact-match.d.ts.map +1 -0
- package/out/exact-match.js +89 -0
- package/out/exact-match.js.map +1 -0
- package/out/fst-autocomplete.d.ts +11 -11
- package/out/fst-autocomplete.d.ts.map +1 -1
- package/out/fst-autocomplete.js +82 -99
- package/out/fst-autocomplete.js.map +1 -1
- package/out/fst-builder.d.ts.map +1 -1
- package/out/fst-builder.js +11 -12
- package/out/fst-builder.js.map +1 -1
- package/out/fst-freshness.d.ts +2 -2
- package/out/fst-freshness.js +2 -2
- package/out/fts-query.js +1 -1
- package/out/fts-query.js.map +1 -1
- package/out/fts.d.ts +4 -4
- package/out/fts.js +4 -4
- package/out/geonames-postal.d.ts +2 -2
- package/out/geonames-postal.js +2 -2
- package/out/index.d.ts +3 -2
- package/out/index.d.ts.map +1 -1
- package/out/index.js +2 -2
- package/out/index.js.map +1 -1
- package/out/interpolation.d.ts +8 -0
- package/out/interpolation.d.ts.map +1 -1
- package/out/interpolation.js +91 -19
- package/out/interpolation.js.map +1 -1
- package/out/lookup.d.ts +4 -5
- package/out/lookup.d.ts.map +1 -1
- package/out/lookup.js +94 -468
- package/out/lookup.js.map +1 -1
- package/out/name-score.d.ts +0 -10
- package/out/name-score.d.ts.map +1 -1
- package/out/name-score.js +6 -4
- package/out/name-score.js.map +1 -1
- package/out/place-importance-schema.d.ts +42 -5
- package/out/place-importance-schema.d.ts.map +1 -1
- package/out/place-importance-schema.js +54 -8
- package/out/place-importance-schema.js.map +1 -1
- package/out/poi-lookup.d.ts +1 -1
- package/out/poi-lookup.d.ts.map +1 -1
- package/out/poi-lookup.js +12 -13
- package/out/poi-lookup.js.map +1 -1
- package/out/poi-schema.d.ts +7 -3
- package/out/poi-schema.d.ts.map +1 -1
- package/out/poi-schema.js.map +1 -1
- package/out/polygon-schema.d.ts +37 -0
- package/out/polygon-schema.d.ts.map +1 -0
- package/out/polygon-schema.js +23 -0
- package/out/polygon-schema.js.map +1 -0
- package/out/postal-city-alias-lookup.d.ts +1 -1
- package/out/postal-city-alias-lookup.js +1 -1
- package/out/postal-city-candidate-schema.d.ts +2 -1
- package/out/postal-city-candidate-schema.d.ts.map +1 -1
- package/out/postal-city-candidate-schema.js.map +1 -1
- package/out/postcode-point-lookup.d.ts +1 -1
- package/out/postcode-point-lookup.js +1 -1
- package/out/primary-preference.d.ts +125 -0
- package/out/primary-preference.d.ts.map +1 -0
- package/out/primary-preference.js +138 -0
- package/out/primary-preference.js.map +1 -0
- package/out/proximity-rerank.d.ts +77 -0
- package/out/proximity-rerank.d.ts.map +1 -0
- package/out/proximity-rerank.js +86 -0
- package/out/proximity-rerank.js.map +1 -0
- package/out/region-keys.d.ts +47 -0
- package/out/region-keys.d.ts.map +1 -0
- package/out/region-keys.js +121 -0
- package/out/region-keys.js.map +1 -0
- package/out/reverse.d.ts.map +1 -1
- package/out/reverse.js +6 -9
- package/out/reverse.js.map +1 -1
- package/out/schema.d.ts +1 -1
- package/out/search-fetch.d.ts +57 -0
- package/out/search-fetch.d.ts.map +1 -0
- package/out/search-fetch.js +183 -0
- package/out/search-fetch.js.map +1 -0
- package/out/sharding.d.ts +3 -3
- package/out/sharding.js +1 -1
- package/out/sqlite-convention-source.d.ts +1 -1
- package/out/sqlite-convention-source.js +1 -1
- package/out/sqlite-utils.d.ts +19 -1
- package/out/sqlite-utils.d.ts.map +1 -1
- package/out/sqlite-utils.js +19 -1
- package/out/sqlite-utils.js.map +1 -1
- package/out/street-centroid-schema.d.ts +7 -2
- package/out/street-centroid-schema.d.ts.map +1 -1
- package/out/street-centroid-schema.js.map +1 -1
- package/out/street-centroid.d.ts.map +1 -1
- package/out/street-centroid.js +7 -7
- package/out/street-centroid.js.map +1 -1
- package/out/street-normalize.d.ts +82 -9
- package/out/street-normalize.d.ts.map +1 -1
- package/out/street-normalize.js +175 -9
- package/out/street-normalize.js.map +1 -1
- package/out/street-segment-schema.d.ts +6 -2
- package/out/street-segment-schema.d.ts.map +1 -1
- package/out/street-segment-schema.js.map +1 -1
- package/out/types.d.ts +35 -1
- package/out/types.d.ts.map +1 -1
- package/out/unified-schema.d.ts +1 -1
- package/out/unified-schema.js +1 -1
- package/out/uprn-lookup.d.ts +85 -0
- package/out/uprn-lookup.d.ts.map +1 -0
- package/out/uprn-lookup.js +152 -0
- package/out/uprn-lookup.js.map +1 -0
- package/out/uprn-schema.d.ts +93 -0
- package/out/uprn-schema.d.ts.map +1 -0
- package/out/uprn-schema.js +78 -0
- package/out/uprn-schema.js.map +1 -0
- package/out/weights-overlay-linker.d.ts +141 -0
- package/out/weights-overlay-linker.d.ts.map +1 -0
- package/out/weights-overlay-linker.js +259 -0
- package/out/weights-overlay-linker.js.map +1 -0
- package/package.json +288 -16
- package/place-importance-schema.ts +64 -15
- package/poi-lookup.ts +12 -13
- package/poi-schema.ts +8 -3
- package/polygon-schema.ts +47 -0
- package/postal-city-alias-lookup.ts +1 -1
- package/postal-city-candidate-schema.ts +3 -1
- package/postcode-point-lookup.ts +1 -1
- package/primary-preference.ts +207 -0
- package/proximity-rerank.ts +120 -0
- package/region-keys.ts +144 -0
- package/reverse.ts +17 -16
- package/schema.ts +1 -1
- package/search-fetch.ts +256 -0
- package/sharding.ts +3 -3
- package/sqlite-convention-source.ts +1 -1
- package/sqlite-utils.ts +43 -2
- package/street-centroid-schema.ts +8 -2
- package/street-centroid.ts +13 -8
- package/street-normalize.ts +252 -23
- package/street-segment-schema.ts +7 -2
- package/types.ts +35 -1
- package/unified-schema.ts +1 -1
- package/uprn-lookup.ts +210 -0
- package/uprn-schema.ts +124 -0
- package/weights-overlay-linker.ts +377 -0
- package/geo.ts +0 -121
- package/out/geo.d.ts +0 -74
- package/out/geo.d.ts.map +0 -1
- package/out/geo.js +0 -71
- 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
|
|
13
|
-
*
|
|
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
|
package/candidate-importance.ts
CHANGED