@mailwoman/resolver-wof-sqlite 12.0.0 → 12.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/lib/address/keys/build.ts +35 -0
- package/lib/address/keys/lookup.ts +318 -6
- package/lib/address/keys/open.ts +1 -0
- package/lib/address/keys/point-lookup.ts +61 -2
- package/lib/address/keys/schema.ts +37 -0
- package/lib/address/number-forms.ts +32 -13
- package/lib/address/point/interpolation.ts +23 -6
- package/lib/address/point/schema.ts +198 -2
- package/lib/address/point/scope-search.ts +179 -0
- package/lib/address/point.ts +365 -32
- package/lib/build/candidate.ts +36 -9
- package/lib/candidate/ancestors/sidecar.ts +73 -4
- package/lib/candidate/del1.ts +218 -0
- package/lib/candidate/lookup.ts +159 -100
- package/lib/candidate/schema.ts +14 -2
- package/lib/fst/binary-matcher.ts +9 -1
- package/lib/fst/builder.ts +7 -0
- package/lib/fst/del1-sidecar.ts +782 -0
- package/lib/fst/deserialize-web.ts +14 -2
- package/lib/fst/matcher.ts +7 -1
- package/lib/fst/types.ts +8 -0
- package/lib/fst.ts +1 -0
- package/lib/index.ts +22 -2
- package/lib/labels/street/canonical.ts +333 -0
- package/lib/labels/street/centroid/schema.ts +23 -1
- package/lib/labels/street/interpolation.ts +32 -6
- package/lib/labels/street/morphology/fst/builder.ts +13 -0
- package/lib/labels/street/morphology/fst/loader.ts +32 -2
- package/lib/labels/street/normalize.ts +153 -4
- package/lib/labels/street.ts +1 -0
- package/lib/landmark.ts +43 -2
- package/lib/lookup/types.ts +0 -6
- package/lib/poi/lookup.ts +148 -52
- package/lib/poi/schema.ts +9 -1
- package/lib/schema/overture-match.ts +2 -1
- package/lib/weights/overlay-linker.ts +6 -2
- package/out/address/keys/build.d.ts.map +1 -1
- package/out/address/keys/build.js +28 -1
- package/out/address/keys/build.js.map +1 -1
- package/out/address/keys/lookup.d.ts +42 -0
- package/out/address/keys/lookup.d.ts.map +1 -1
- package/out/address/keys/lookup.js +182 -6
- package/out/address/keys/lookup.js.map +1 -1
- package/out/address/keys/open.d.ts.map +1 -1
- package/out/address/keys/open.js +1 -0
- package/out/address/keys/open.js.map +1 -1
- package/out/address/keys/point-lookup.d.ts +6 -1
- package/out/address/keys/point-lookup.d.ts.map +1 -1
- package/out/address/keys/point-lookup.js +43 -1
- package/out/address/keys/point-lookup.js.map +1 -1
- package/out/address/keys/schema.d.ts +21 -0
- package/out/address/keys/schema.d.ts.map +1 -1
- package/out/address/keys/schema.js +18 -0
- package/out/address/keys/schema.js.map +1 -1
- package/out/address/number-forms.d.ts +10 -1
- package/out/address/number-forms.d.ts.map +1 -1
- package/out/address/number-forms.js +20 -11
- package/out/address/number-forms.js.map +1 -1
- package/out/address/point/interpolation.d.ts.map +1 -1
- package/out/address/point/interpolation.js +15 -5
- package/out/address/point/interpolation.js.map +1 -1
- package/out/address/point/schema.d.ts +45 -2
- package/out/address/point/schema.d.ts.map +1 -1
- package/out/address/point/schema.js +129 -0
- package/out/address/point/schema.js.map +1 -1
- package/out/address/point/scope-search.d.ts +77 -0
- package/out/address/point/scope-search.d.ts.map +1 -0
- package/out/address/point/scope-search.js +107 -0
- package/out/address/point/scope-search.js.map +1 -0
- package/out/address/point.d.ts +24 -1
- package/out/address/point.d.ts.map +1 -1
- package/out/address/point.js +231 -30
- package/out/address/point.js.map +1 -1
- package/out/build/candidate.d.ts +8 -0
- package/out/build/candidate.d.ts.map +1 -1
- package/out/build/candidate.js +17 -7
- package/out/build/candidate.js.map +1 -1
- package/out/candidate/ancestors/sidecar.d.ts +7 -3
- package/out/candidate/ancestors/sidecar.d.ts.map +1 -1
- package/out/candidate/ancestors/sidecar.js +56 -4
- package/out/candidate/ancestors/sidecar.js.map +1 -1
- package/out/candidate/del1.d.ts +61 -0
- package/out/candidate/del1.d.ts.map +1 -0
- package/out/candidate/del1.js +158 -0
- package/out/candidate/del1.js.map +1 -0
- package/out/candidate/lookup.d.ts +2 -14
- package/out/candidate/lookup.d.ts.map +1 -1
- package/out/candidate/lookup.js +122 -84
- package/out/candidate/lookup.js.map +1 -1
- package/out/candidate/schema.d.ts +8 -2
- package/out/candidate/schema.d.ts.map +1 -1
- package/out/candidate/schema.js +8 -2
- package/out/candidate/schema.js.map +1 -1
- package/out/fst/binary-matcher.d.ts +7 -1
- package/out/fst/binary-matcher.d.ts.map +1 -1
- package/out/fst/binary-matcher.js +7 -1
- package/out/fst/binary-matcher.js.map +1 -1
- package/out/fst/builder.d.ts +2 -0
- package/out/fst/builder.d.ts.map +1 -1
- package/out/fst/builder.js +5 -0
- package/out/fst/builder.js.map +1 -1
- package/out/fst/del1-sidecar.d.ts +158 -0
- package/out/fst/del1-sidecar.d.ts.map +1 -0
- package/out/fst/del1-sidecar.js +581 -0
- package/out/fst/del1-sidecar.js.map +1 -0
- package/out/fst/deserialize-web.d.ts +3 -1
- package/out/fst/deserialize-web.d.ts.map +1 -1
- package/out/fst/deserialize-web.js +9 -2
- package/out/fst/deserialize-web.js.map +1 -1
- package/out/fst/matcher.d.ts +6 -1
- package/out/fst/matcher.d.ts.map +1 -1
- package/out/fst/matcher.js +6 -1
- package/out/fst/matcher.js.map +1 -1
- package/out/fst/types.d.ts +7 -0
- package/out/fst/types.d.ts.map +1 -1
- package/out/fst.d.ts +1 -0
- package/out/fst.d.ts.map +1 -1
- package/out/fst.js +1 -0
- package/out/fst.js.map +1 -1
- package/out/index.d.ts +4 -3
- package/out/index.d.ts.map +1 -1
- package/out/index.js +3 -2
- package/out/index.js.map +1 -1
- package/out/labels/street/canonical.d.ts +68 -0
- package/out/labels/street/canonical.d.ts.map +1 -0
- package/out/labels/street/canonical.js +234 -0
- package/out/labels/street/canonical.js.map +1 -0
- package/out/labels/street/centroid/schema.d.ts +10 -1
- package/out/labels/street/centroid/schema.d.ts.map +1 -1
- package/out/labels/street/centroid/schema.js +12 -0
- package/out/labels/street/centroid/schema.js.map +1 -1
- package/out/labels/street/interpolation.d.ts +9 -1
- package/out/labels/street/interpolation.d.ts.map +1 -1
- package/out/labels/street/interpolation.js +8 -5
- package/out/labels/street/interpolation.js.map +1 -1
- package/out/labels/street/morphology/fst/builder.d.ts +8 -0
- package/out/labels/street/morphology/fst/builder.d.ts.map +1 -1
- package/out/labels/street/morphology/fst/builder.js +5 -0
- package/out/labels/street/morphology/fst/builder.js.map +1 -1
- package/out/labels/street/morphology/fst/loader.d.ts +12 -0
- package/out/labels/street/morphology/fst/loader.d.ts.map +1 -1
- package/out/labels/street/morphology/fst/loader.js +18 -2
- package/out/labels/street/morphology/fst/loader.js.map +1 -1
- package/out/labels/street/normalize.d.ts +8 -0
- package/out/labels/street/normalize.d.ts.map +1 -1
- package/out/labels/street/normalize.js +138 -4
- package/out/labels/street/normalize.js.map +1 -1
- package/out/labels/street.d.ts +1 -0
- package/out/labels/street.d.ts.map +1 -1
- package/out/labels/street.js +1 -0
- package/out/labels/street.js.map +1 -1
- package/out/landmark.d.ts +8 -0
- package/out/landmark.d.ts.map +1 -1
- package/out/landmark.js +25 -2
- package/out/landmark.js.map +1 -1
- package/out/lookup/types.d.ts +0 -5
- package/out/lookup/types.d.ts.map +1 -1
- package/out/poi/lookup.d.ts +9 -1
- package/out/poi/lookup.d.ts.map +1 -1
- package/out/poi/lookup.js +111 -53
- package/out/poi/lookup.js.map +1 -1
- package/out/poi/schema.d.ts +7 -2
- package/out/poi/schema.d.ts.map +1 -1
- package/out/poi/schema.js +3 -0
- package/out/poi/schema.js.map +1 -1
- package/out/schema/overture-match.d.ts +2 -1
- package/out/schema/overture-match.d.ts.map +1 -1
- package/out/schema/overture-match.js.map +1 -1
- package/out/weights/overlay-linker.d.ts +1 -1
- package/out/weights/overlay-linker.d.ts.map +1 -1
- package/out/weights/overlay-linker.js +5 -1
- package/out/weights/overlay-linker.js.map +1 -1
- package/package.json +12 -12
|
@@ -1,10 +1,20 @@
|
|
|
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 type { NumberMatchKind } from "@mailwoman/core/resolver"
|
|
4
5
|
import { isJoinedHouseNumber } from "@mailwoman/match/address-point"
|
|
5
6
|
|
|
6
7
|
import { streetKeyRulesOf, type StreetLocale } from "#labels/street/normalize"
|
|
7
8
|
|
|
9
|
+
/**
|
|
10
|
+
* One house-number form yielded by {@link houseNumberForms}, paired with how it
|
|
11
|
+
* relates to the query's normalized number.
|
|
12
|
+
*/
|
|
13
|
+
export interface HouseNumberForm {
|
|
14
|
+
number: string
|
|
15
|
+
match: NumberMatchKind
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
/**
|
|
9
19
|
* The forms a normalized house number is probed under, the written form first,
|
|
10
20
|
* each later form tried only after every earlier one missed.
|
|
@@ -26,15 +36,15 @@ import { streetKeyRulesOf, type StreetLocale } from "#labels/street/normalize"
|
|
|
26
36
|
*
|
|
27
37
|
* Both address readers call this, so they try one number in one order.
|
|
28
38
|
*/
|
|
29
|
-
export function* houseNumberForms(number: string, locale: StreetLocale): Generator<
|
|
30
|
-
yield number
|
|
39
|
+
export function* houseNumberForms(number: string, locale: StreetLocale): Generator<HouseNumberForm> {
|
|
40
|
+
yield { number, match: "written" }
|
|
31
41
|
|
|
32
42
|
const indexes = streetKeyRulesOf(locale).houseNumberRepetition
|
|
33
43
|
const repetition = indexes ? repetitionForms(number, indexes) : null
|
|
34
44
|
|
|
35
45
|
if (repetition) {
|
|
36
46
|
for (const form of repetition) {
|
|
37
|
-
if (form !== number) {
|
|
47
|
+
if (form.number !== number) {
|
|
38
48
|
yield form
|
|
39
49
|
}
|
|
40
50
|
}
|
|
@@ -43,36 +53,39 @@ export function* houseNumberForms(number: string, locale: StreetLocale): Generat
|
|
|
43
53
|
}
|
|
44
54
|
|
|
45
55
|
if (isJoinedHouseNumber(number)) {
|
|
46
|
-
yield number.replace("-", "")
|
|
56
|
+
yield { number: number.replace("-", ""), match: "respelled" }
|
|
47
57
|
}
|
|
48
58
|
|
|
49
59
|
const grid = /^([nesw])(?:\s+)?(\d+)$/.exec(number)
|
|
50
60
|
|
|
51
61
|
if (grid) {
|
|
52
|
-
yield
|
|
62
|
+
yield {
|
|
63
|
+
number: number.includes(" ") ? `${grid[1]}${grid[2]}` : `${grid[1]} ${grid[2]}`,
|
|
64
|
+
match: "respelled",
|
|
65
|
+
}
|
|
53
66
|
}
|
|
54
67
|
|
|
55
68
|
const low = /^(\d+[a-z]?)-\d+[a-z]?$/.exec(number)?.[1]
|
|
56
69
|
|
|
57
70
|
if (low) {
|
|
58
|
-
yield low
|
|
71
|
+
yield { number: low, match: "base" }
|
|
59
72
|
}
|
|
60
73
|
|
|
61
74
|
const joined = /^(\d+)\s+([a-z])$/.exec(number)
|
|
62
75
|
const spaced = /^(\d+)([a-z])$/.exec(number)
|
|
63
76
|
|
|
64
77
|
if (joined) {
|
|
65
|
-
yield `${joined[1]}${joined[2]}
|
|
66
|
-
yield joined[1]
|
|
78
|
+
yield { number: `${joined[1]}${joined[2]}`, match: "respelled" }
|
|
79
|
+
yield { number: joined[1]!, match: "base" }
|
|
67
80
|
} else if (spaced) {
|
|
68
|
-
yield `${spaced[1]} ${spaced[2]}
|
|
69
|
-
yield spaced[1]
|
|
81
|
+
yield { number: `${spaced[1]} ${spaced[2]}`, match: "respelled" }
|
|
82
|
+
yield { number: spaced[1]!, match: "base" }
|
|
70
83
|
}
|
|
71
84
|
|
|
72
85
|
const base = streetKeyRulesOf(locale).houseNumberBase?.exec(number)?.[1]
|
|
73
86
|
|
|
74
87
|
if (base) {
|
|
75
|
-
yield base
|
|
88
|
+
yield { number: base, match: "base" }
|
|
76
89
|
}
|
|
77
90
|
}
|
|
78
91
|
|
|
@@ -82,7 +95,7 @@ export function* houseNumberForms(number: string, locale: StreetLocale): Generat
|
|
|
82
95
|
*
|
|
83
96
|
* An index is written as its word or its first letter, spaced or joined.
|
|
84
97
|
*/
|
|
85
|
-
function repetitionForms(number: string, indexes: readonly string[]):
|
|
98
|
+
function repetitionForms(number: string, indexes: readonly string[]): HouseNumberForm[] | null {
|
|
86
99
|
const match = /^(\d+)\s*([a-z]+)$/.exec(number)
|
|
87
100
|
const digits = match?.[1]
|
|
88
101
|
const suffix = match?.[2]
|
|
@@ -93,5 +106,11 @@ function repetitionForms(number: string, indexes: readonly string[]): string[] |
|
|
|
93
106
|
|
|
94
107
|
if (!word) return null
|
|
95
108
|
|
|
96
|
-
return [
|
|
109
|
+
return [
|
|
110
|
+
{ number: `${digits} ${word}`, match: "respelled" },
|
|
111
|
+
{ number: `${digits}${word}`, match: "respelled" },
|
|
112
|
+
{ number: `${digits} ${word[0]}`, match: "respelled" },
|
|
113
|
+
{ number: `${digits}${word[0]}`, match: "respelled" },
|
|
114
|
+
{ number: digits, match: "base" },
|
|
115
|
+
]
|
|
97
116
|
}
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
* Standalone like the segment tier, wired through the ordered `spatialTiers` list.
|
|
35
35
|
*/
|
|
36
36
|
|
|
37
|
-
import type { InterpolationLookup } from "@mailwoman/core/resolver"
|
|
37
|
+
import type { InterpolationLookup, StreetMatchKind } from "@mailwoman/core/resolver"
|
|
38
38
|
import { haversine } from "@mailwoman/spatial/geometries/point/distance"
|
|
39
39
|
import { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
40
40
|
|
|
@@ -131,17 +131,25 @@ export class AddressPointInterpolator<
|
|
|
131
131
|
if (!this.#byPostcode || !query.postcode) return this.#fallback?.find(query) ?? null
|
|
132
132
|
|
|
133
133
|
// Key-variant ladder (see `streetKeyVariants`), the same probe order as the exact-point reader.
|
|
134
|
+
const variants = streetKeyVariants(query.street, EXTRACT_STREET_LOCALE)
|
|
134
135
|
let rows: PointRow[] = []
|
|
136
|
+
let matchedStreet: RouteKey | null = null
|
|
137
|
+
let streetMatch: StreetMatchKind = "written"
|
|
135
138
|
|
|
136
|
-
for (
|
|
137
|
-
const streetKey = canonicalizeRouteKey(
|
|
139
|
+
for (let i = 0; i < variants.length; i++) {
|
|
140
|
+
const streetKey = canonicalizeRouteKey(variants[i]!)
|
|
138
141
|
|
|
139
142
|
rows = this.#byPostcode(query.postcode.trim(), streetKey, n)
|
|
140
143
|
|
|
141
|
-
if (rows.length)
|
|
144
|
+
if (rows.length) {
|
|
145
|
+
matchedStreet = streetKey
|
|
146
|
+
streetMatch = i === 0 ? "written" : "respelled"
|
|
147
|
+
|
|
148
|
+
break
|
|
149
|
+
}
|
|
142
150
|
}
|
|
143
151
|
|
|
144
|
-
const hit = rows.length >= 2 ? interpolateFromNeighbors(rows, n) : null
|
|
152
|
+
const hit = rows.length >= 2 && matchedStreet ? interpolateFromNeighbors(rows, n, matchedStreet, streetMatch) : null
|
|
145
153
|
|
|
146
154
|
return hit ?? this.#fallback?.find(query) ?? null
|
|
147
155
|
}
|
|
@@ -178,7 +186,12 @@ function anchorsByNumber(rows: readonly PointRow[]): NumberAnchor[] {
|
|
|
178
186
|
.toSorted((a, b) => a.n - b.n)
|
|
179
187
|
}
|
|
180
188
|
|
|
181
|
-
function interpolateFromNeighbors(
|
|
189
|
+
function interpolateFromNeighbors(
|
|
190
|
+
rows: readonly PointRow[],
|
|
191
|
+
n: number,
|
|
192
|
+
matchedStreet: string,
|
|
193
|
+
streetMatch: StreetMatchKind
|
|
194
|
+
): InterpolatedHit | null {
|
|
182
195
|
const anchors = anchorsByNumber(rows)
|
|
183
196
|
|
|
184
197
|
// Nearest known number below and above the query.
|
|
@@ -210,6 +223,8 @@ function interpolateFromNeighbors(rows: readonly PointRow[], n: number): Interpo
|
|
|
210
223
|
uncertaintyM: Math.round(spanM / 2),
|
|
211
224
|
source: below.source,
|
|
212
225
|
release: below.release,
|
|
226
|
+
matchedStreet,
|
|
227
|
+
streetMatch,
|
|
213
228
|
}
|
|
214
229
|
}
|
|
215
230
|
|
|
@@ -239,5 +254,7 @@ function interpolateFromNeighbors(rows: readonly PointRow[], n: number): Interpo
|
|
|
239
254
|
uncertaintyM: Math.round(pairM + overshootM),
|
|
240
255
|
source: near.source,
|
|
241
256
|
release: near.release,
|
|
257
|
+
matchedStreet,
|
|
258
|
+
streetMatch,
|
|
242
259
|
}
|
|
243
260
|
}
|
|
@@ -1,9 +1,18 @@
|
|
|
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 type {
|
|
4
|
+
import type { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
5
|
+
import { type Kysely, sql } from "kysely"
|
|
5
6
|
|
|
6
|
-
import
|
|
7
|
+
import { addressPostcodeKey } from "#address/keys/postcode"
|
|
8
|
+
import { canonicalStreetKey } from "#labels/street/canonical"
|
|
9
|
+
import {
|
|
10
|
+
type NameKey,
|
|
11
|
+
type RouteKey,
|
|
12
|
+
type StreetKey,
|
|
13
|
+
streetKeyRulesOf,
|
|
14
|
+
type StreetLocale,
|
|
15
|
+
} from "#labels/street/normalize"
|
|
7
16
|
|
|
8
17
|
/**
|
|
9
18
|
* One rooftop address point; `(street_norm, number)` within a `postcode`
|
|
@@ -51,11 +60,25 @@ export interface AddressPointTable {
|
|
|
51
60
|
certified: number | null
|
|
52
61
|
}
|
|
53
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Compact `WITHOUT ROWID` scope-to-street index for `canonical-alias` and scoped `fuzzy-edit1` lookup.
|
|
65
|
+
*
|
|
66
|
+
* Clustered on `(scope_kind, scope, street_canon, street_norm)` so a canonical alias probe is
|
|
67
|
+
* one B-tree seek and a scoped 1-edit scan reads contiguous pages for one postcode or locality.
|
|
68
|
+
*/
|
|
69
|
+
export interface StreetScopeIndexTable {
|
|
70
|
+
scope_kind: "postcode" | "locality"
|
|
71
|
+
scope: string
|
|
72
|
+
street_canon: string
|
|
73
|
+
street_norm: StreetKey
|
|
74
|
+
}
|
|
75
|
+
|
|
54
76
|
/**
|
|
55
77
|
* The address-point database schema for `new DatabaseClient<AddressPointDatabase>(...)`.
|
|
56
78
|
*/
|
|
57
79
|
export interface AddressPointDatabase {
|
|
58
80
|
address_point: AddressPointTable
|
|
81
|
+
street_scope_index?: StreetScopeIndexTable
|
|
59
82
|
}
|
|
60
83
|
|
|
61
84
|
/**
|
|
@@ -64,6 +87,18 @@ export interface AddressPointDatabase {
|
|
|
64
87
|
*/
|
|
65
88
|
export type AddressPointSchemaHandle = Pick<Kysely<AddressPointDatabase>, "schema">
|
|
66
89
|
|
|
90
|
+
/**
|
|
91
|
+
* The subset of a `DatabaseClient` needed to create and populate `street_scope_index`.
|
|
92
|
+
*/
|
|
93
|
+
export type StreetScopeIndexHandle = Pick<DatabaseClient<AddressPointDatabase>, "schema" | "exec" | "prepare">
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Default number of rows written per transaction when streaming into `street_scope_index`.
|
|
97
|
+
*
|
|
98
|
+
* Keeps WAL growth bounded on 50M–100M+ row builds while amortizing commit overhead.
|
|
99
|
+
*/
|
|
100
|
+
const DEFAULT_SCOPE_INDEX_BATCH_SIZE = 100_000
|
|
101
|
+
|
|
67
102
|
/**
|
|
68
103
|
* The `address_point` columns in insert order, from which the builder derives its positional placeholders.
|
|
69
104
|
*/
|
|
@@ -127,3 +162,164 @@ export async function createAddressPointIndexes(db: AddressPointSchemaHandle): P
|
|
|
127
162
|
// so the reader scopes a `(street_norm, number)` probe by the resolved locality's bbox.
|
|
128
163
|
await db.schema.createIndex("idx_ap_street").on("address_point").columns(["street_norm", "number"]).execute()
|
|
129
164
|
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Creates the empty `street_scope_index` `WITHOUT ROWID` table.
|
|
168
|
+
*/
|
|
169
|
+
export async function createStreetScopeIndexTable(db: AddressPointSchemaHandle): Promise<void> {
|
|
170
|
+
await db.schema
|
|
171
|
+
.createTable("street_scope_index")
|
|
172
|
+
.addColumn("scope_kind", "text", (c) => c.notNull())
|
|
173
|
+
.addColumn("scope", "text", (c) => c.notNull())
|
|
174
|
+
.addColumn("street_canon", "text", (c) => c.notNull())
|
|
175
|
+
.addColumn("street_norm", "text", (c) => c.notNull())
|
|
176
|
+
.addPrimaryKeyConstraint("street_scope_index_pk", ["scope_kind", "scope", "street_canon", "street_norm"])
|
|
177
|
+
.modifyEnd(sql`without rowid`)
|
|
178
|
+
.execute()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Streams distinct scope rows from one query into `street_scope_index` in batched transactions.
|
|
183
|
+
*
|
|
184
|
+
* A single `db.exec()` call with JS SQLite user-defined functions retains V8 handles
|
|
185
|
+
* across millions of invocations and exhausts an 8 GiB heap on 50M–100M+ row builds.
|
|
186
|
+
* The `StatementSync.iterate()` cursor returns control to V8 on every row
|
|
187
|
+
* so temporary keys are collected between batches.
|
|
188
|
+
*/
|
|
189
|
+
function streamScopeRows(params: {
|
|
190
|
+
db: StreetScopeIndexHandle
|
|
191
|
+
selectSQL: string
|
|
192
|
+
scopeKind: "postcode" | "locality"
|
|
193
|
+
streetLocale: StreetLocale
|
|
194
|
+
batchSize: number
|
|
195
|
+
formatScope: (raw: unknown) => string
|
|
196
|
+
}): void {
|
|
197
|
+
const { db, selectSQL, scopeKind, streetLocale, batchSize, formatScope } = params
|
|
198
|
+
const select = db.prepare(selectSQL)
|
|
199
|
+
|
|
200
|
+
const insert = db.prepare(
|
|
201
|
+
"INSERT OR IGNORE INTO street_scope_index (scope_kind, scope, street_canon, street_norm) VALUES (?, ?, ?, ?)"
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
let inTransaction = false
|
|
205
|
+
let inBatch = 0
|
|
206
|
+
|
|
207
|
+
try {
|
|
208
|
+
db.exec("BEGIN")
|
|
209
|
+
inTransaction = true
|
|
210
|
+
|
|
211
|
+
for (const row of select.iterate()) {
|
|
212
|
+
const scope = formatScope(row["scope_raw"])
|
|
213
|
+
|
|
214
|
+
if (!scope) continue
|
|
215
|
+
|
|
216
|
+
const streetRaw = typeof row["street_raw"] === "string" ? row["street_raw"] : ""
|
|
217
|
+
|
|
218
|
+
if (!streetRaw) continue
|
|
219
|
+
|
|
220
|
+
const canon = canonicalStreetKey(streetRaw, streetLocale).key
|
|
221
|
+
|
|
222
|
+
if (!canon) continue
|
|
223
|
+
|
|
224
|
+
const streetNorm = typeof row["street_norm"] === "string" ? row["street_norm"] : ""
|
|
225
|
+
|
|
226
|
+
if (!streetNorm) continue
|
|
227
|
+
|
|
228
|
+
insert.run(scopeKind, scope, canon, streetNorm)
|
|
229
|
+
|
|
230
|
+
inBatch++
|
|
231
|
+
|
|
232
|
+
if (inBatch >= batchSize) {
|
|
233
|
+
db.exec("COMMIT")
|
|
234
|
+
inTransaction = false
|
|
235
|
+
db.exec("BEGIN")
|
|
236
|
+
inTransaction = true
|
|
237
|
+
inBatch = 0
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
db.exec("COMMIT")
|
|
242
|
+
inTransaction = false
|
|
243
|
+
} catch (error) {
|
|
244
|
+
if (inTransaction) {
|
|
245
|
+
db.exec("ROLLBACK")
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
throw error
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Creates and populates `street_scope_index` from a table holding `postcode`,
|
|
254
|
+
* `street_raw`, `street_norm`, and `localityColumn`.
|
|
255
|
+
*
|
|
256
|
+
* Postcode scopes are normalized with {@link addressPostcodeKey} so spaced
|
|
257
|
+
* and compact postcodes share one scope key in the index.
|
|
258
|
+
*/
|
|
259
|
+
export async function populateStreetScopeIndex(
|
|
260
|
+
db: StreetScopeIndexHandle,
|
|
261
|
+
streetLocale: StreetLocale,
|
|
262
|
+
options: {
|
|
263
|
+
sourceTable: "address_point" | "street_centroid"
|
|
264
|
+
localityColumn: "locality_norm" | "locality_base"
|
|
265
|
+
country?: string
|
|
266
|
+
batchSize?: number
|
|
267
|
+
}
|
|
268
|
+
): Promise<void> {
|
|
269
|
+
const batchSize = options.batchSize ?? DEFAULT_SCOPE_INDEX_BATCH_SIZE
|
|
270
|
+
|
|
271
|
+
if (!Number.isInteger(batchSize) || batchSize < 1) {
|
|
272
|
+
throw new RangeError(`populateStreetScopeIndex: batchSize must be a positive integer, got ${batchSize}`)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
await createStreetScopeIndexTable(db)
|
|
276
|
+
|
|
277
|
+
const rules = streetKeyRulesOf(streetLocale)
|
|
278
|
+
const { sourceTable, localityColumn, country } = options
|
|
279
|
+
|
|
280
|
+
streamScopeRows({
|
|
281
|
+
db,
|
|
282
|
+
selectSQL: `
|
|
283
|
+
SELECT DISTINCT postcode AS scope_raw, street_raw, street_norm
|
|
284
|
+
FROM ${sourceTable}
|
|
285
|
+
WHERE postcode IS NOT NULL AND postcode != ''
|
|
286
|
+
`,
|
|
287
|
+
scopeKind: "postcode",
|
|
288
|
+
streetLocale,
|
|
289
|
+
batchSize,
|
|
290
|
+
formatScope: (raw) => {
|
|
291
|
+
if (typeof raw !== "string") return ""
|
|
292
|
+
|
|
293
|
+
return (country ? addressPostcodeKey(country, raw) : rules.postcodeKey(raw)) ?? ""
|
|
294
|
+
},
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
streamScopeRows({
|
|
298
|
+
db,
|
|
299
|
+
selectSQL: `
|
|
300
|
+
SELECT DISTINCT ${localityColumn} AS scope_raw, street_raw, street_norm
|
|
301
|
+
FROM ${sourceTable}
|
|
302
|
+
WHERE ${localityColumn} IS NOT NULL AND ${localityColumn} != ''
|
|
303
|
+
`,
|
|
304
|
+
scopeKind: "locality",
|
|
305
|
+
streetLocale,
|
|
306
|
+
batchSize,
|
|
307
|
+
formatScope: (raw) => (typeof raw === "string" ? raw : ""),
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Creates and populates `street_scope_index` from distinct `(postcode, street_raw, street_norm)`
|
|
313
|
+
* and `(locality_norm, street_raw, street_norm)` tuples in `address_point`.
|
|
314
|
+
*/
|
|
315
|
+
export async function createStreetScopeIndex(
|
|
316
|
+
db: StreetScopeIndexHandle,
|
|
317
|
+
streetLocale: StreetLocale,
|
|
318
|
+
options?: { country?: string; batchSize?: number }
|
|
319
|
+
): Promise<void> {
|
|
320
|
+
await populateStreetScopeIndex(db, streetLocale, {
|
|
321
|
+
sourceTable: "address_point",
|
|
322
|
+
localityColumn: "locality_norm",
|
|
323
|
+
...options,
|
|
324
|
+
})
|
|
325
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Postcode scope normalization and `street_scope_index` search helpers for `AddressPointSqliteLookup`.
|
|
6
|
+
*
|
|
7
|
+
* Kept beside `point.ts` so postcode spacing normalization, `canonical-alias` lookup,
|
|
8
|
+
* and scoped `fuzzy-edit1` lookup stay isolated and testable.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
canonicalStreetKey,
|
|
15
|
+
findFuzzyEdit1Streets,
|
|
16
|
+
FUZZY_SCOPE_ROW_LIMIT,
|
|
17
|
+
MIN_FUZZY_CORE_LENGTH,
|
|
18
|
+
} from "#labels/street/canonical"
|
|
19
|
+
import { type StreetKey, streetKeyRulesOf, type StreetLocale } from "#labels/street/normalize"
|
|
20
|
+
import { hasTable, prepareAll, type PreparedAll } from "#sqlite-utils"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Computes the normalized postcode scope key for `postcode` under `locale`
|
|
24
|
+
* (uppercased with whitespace stripped, and ZIP+4 reduced to 5 digits for `us`).
|
|
25
|
+
*/
|
|
26
|
+
export function postcodeScopeKey(postcode: string | null | undefined, locale: StreetLocale): string | null {
|
|
27
|
+
return streetKeyRulesOf(locale).postcodeKey(postcode)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the postcode strings to probe in `address_point`: the trimmed
|
|
32
|
+
* written value first, followed by the compact normalized key when it differs
|
|
33
|
+
* (such as `"4413 ND"` → `"4413ND"` or `"SW1A 1AA"` → `"SW1A1AA"`).
|
|
34
|
+
*/
|
|
35
|
+
export function postcodeProbeForms(postcode: string, locale: StreetLocale): string[] {
|
|
36
|
+
const raw = postcode.trim()
|
|
37
|
+
|
|
38
|
+
if (!raw) return []
|
|
39
|
+
|
|
40
|
+
const compact = postcodeScopeKey(raw, locale)
|
|
41
|
+
|
|
42
|
+
return compact && compact !== raw ? [raw, compact] : [raw]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Reports whether a stored row postcode and a queried postcode normalize to
|
|
47
|
+
* the same postcode key under `locale`.
|
|
48
|
+
*/
|
|
49
|
+
export function postcodesAgree(stored: string, queried: string, locale: StreetLocale): boolean {
|
|
50
|
+
return postcodeScopeKey(stored, locale) === postcodeScopeKey(queried, locale)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Prepared statements over `street_scope_index`, opened once when the table exists.
|
|
55
|
+
*/
|
|
56
|
+
export interface StreetScopeIndexQueries {
|
|
57
|
+
byCanon: PreparedAll<
|
|
58
|
+
[scopeKind: "postcode" | "locality", scope: string, streetCanon: string],
|
|
59
|
+
{ street_norm: StreetKey }
|
|
60
|
+
>
|
|
61
|
+
byScope: PreparedAll<
|
|
62
|
+
[scopeKind: "postcode" | "locality", scope: string],
|
|
63
|
+
{ street_canon: string; street_norm: StreetKey }
|
|
64
|
+
>
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Prepares the `street_scope_index` statements if the open database contains that table,
|
|
69
|
+
* or returns `null` for legacy extracts built without it.
|
|
70
|
+
*/
|
|
71
|
+
export function prepareStreetScopeIndexQueries<DB>(db: DatabaseClient<DB>): StreetScopeIndexQueries | null {
|
|
72
|
+
if (!hasTable(db, "street_scope_index")) return null
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
byCanon: prepareAll(
|
|
76
|
+
db,
|
|
77
|
+
"SELECT street_norm FROM street_scope_index WHERE scope_kind = ? AND scope = ? AND street_canon = ?"
|
|
78
|
+
),
|
|
79
|
+
byScope: prepareAll(
|
|
80
|
+
db,
|
|
81
|
+
`SELECT street_canon, street_norm FROM street_scope_index WHERE scope_kind = ? AND scope = ? LIMIT ${FUZZY_SCOPE_ROW_LIMIT + 1}`
|
|
82
|
+
),
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Result of a Step 2 (`canonical-alias`) or Step 3 (`fuzzy-edit1`) match through `street_scope_index`.
|
|
88
|
+
*/
|
|
89
|
+
export interface ScopedStreetMatch<Hit> {
|
|
90
|
+
hit: Hit
|
|
91
|
+
matchedStreet: StreetKey
|
|
92
|
+
streetMatch: "canonical-alias" | "fuzzy-edit1"
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Runs Step 2 (`canonical-alias`) and Step 3 (`fuzzy-edit1`) over `street_scope_index`
|
|
97
|
+
* after Step 1 exact lookup missed.
|
|
98
|
+
*
|
|
99
|
+
* Step 2 issues one B-tree seek per available scope on `(scope_kind, scope, street_canon)`.
|
|
100
|
+
* Step 3 scans contiguous scope rows up to {@link FUZZY_SCOPE_ROW_LIMIT},
|
|
101
|
+
* filters candidates by thoroughfare class and 1-edit Damerau-Levenshtein distance
|
|
102
|
+
* on the core (length `>= 5`), confirms the exact house number via `probeInScope`,
|
|
103
|
+
* and abstains unless exactly one distinct `street_norm` confirms.
|
|
104
|
+
*/
|
|
105
|
+
export function searchStreetScopeIndex<Hit>(params: {
|
|
106
|
+
queries: StreetScopeIndexQueries
|
|
107
|
+
streetRaw: string
|
|
108
|
+
locale: StreetLocale
|
|
109
|
+
tried: ReadonlySet<string>
|
|
110
|
+
postcodeScope: string | null
|
|
111
|
+
localityScope: string | null
|
|
112
|
+
probeInScope: (scopeKind: "postcode" | "locality", streetNorm: StreetKey) => Hit | null
|
|
113
|
+
}): ScopedStreetMatch<Hit> | null {
|
|
114
|
+
const { queries, streetRaw, locale, tried, postcodeScope, localityScope, probeInScope } = params
|
|
115
|
+
const canon = canonicalStreetKey(streetRaw, locale)
|
|
116
|
+
|
|
117
|
+
if (!canon.key) return null
|
|
118
|
+
|
|
119
|
+
const scopes: Array<{ kind: "postcode" | "locality"; scope: string }> = []
|
|
120
|
+
|
|
121
|
+
if (postcodeScope) {
|
|
122
|
+
scopes.push({ kind: "postcode", scope: postcodeScope })
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (localityScope) {
|
|
126
|
+
scopes.push({ kind: "locality", scope: localityScope })
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (!scopes.length) return null
|
|
130
|
+
|
|
131
|
+
// Step 2: canonical-alias via one B-tree seek per scope.
|
|
132
|
+
for (const { kind, scope } of scopes) {
|
|
133
|
+
const aliasRows = queries.byCanon(kind, scope, canon.key)
|
|
134
|
+
|
|
135
|
+
for (const { street_norm: streetNorm } of aliasRows) {
|
|
136
|
+
if (tried.has(streetNorm)) continue
|
|
137
|
+
|
|
138
|
+
const hit = probeInScope(kind, streetNorm)
|
|
139
|
+
|
|
140
|
+
if (hit) {
|
|
141
|
+
return { hit, matchedStreet: streetNorm, streetMatch: "canonical-alias" }
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Step 3: scoped 1-edit fuzzy recovery, unique confirmed street only.
|
|
147
|
+
if (canon.core.length < MIN_FUZZY_CORE_LENGTH) return null
|
|
148
|
+
|
|
149
|
+
for (const { kind, scope } of scopes) {
|
|
150
|
+
const scopeRows = queries.byScope(kind, scope)
|
|
151
|
+
const candidates = findFuzzyEdit1Streets(canon, scopeRows)
|
|
152
|
+
|
|
153
|
+
if (!candidates.length) continue
|
|
154
|
+
|
|
155
|
+
let unique: { hit: Hit; matchedStreet: StreetKey } | null = null
|
|
156
|
+
let confirmedCount = 0
|
|
157
|
+
|
|
158
|
+
for (const candidate of candidates) {
|
|
159
|
+
const streetNorm = candidate as StreetKey
|
|
160
|
+
const hit = probeInScope(kind, streetNorm)
|
|
161
|
+
|
|
162
|
+
if (!hit) continue
|
|
163
|
+
|
|
164
|
+
confirmedCount++
|
|
165
|
+
|
|
166
|
+
if (confirmedCount > 1) {
|
|
167
|
+
return null
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
unique = { hit, matchedStreet: streetNorm }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (confirmedCount === 1 && unique) {
|
|
174
|
+
return { hit: unique.hit, matchedStreet: unique.matchedStreet, streetMatch: "fuzzy-edit1" }
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return null
|
|
179
|
+
}
|