@mailwoman/resolver-wof-sqlite 11.5.0 → 12.1.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 +204 -6
- package/lib/address/keys/point-lookup.ts +4 -0
- 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 +120 -2
- package/lib/address/point/scope-search.ts +179 -0
- package/lib/address/point.ts +225 -28
- package/lib/build/candidate.ts +222 -9
- package/lib/build/population-corrections.ts +11 -2
- package/lib/candidate/ancestors/sidecar.ts +73 -4
- package/lib/candidate/del1.ts +218 -0
- package/lib/candidate/lookup.ts +316 -113
- package/lib/candidate/name-roles.ts +112 -6
- package/lib/candidate/outcomes.ts +191 -0
- package/lib/candidate/schema.ts +79 -3
- 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/geonames/aliases.ts +39 -1
- 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 +34 -4
- package/lib/labels/street.ts +1 -0
- package/lib/landmark.ts +51 -4
- package/lib/lookup/types.ts +25 -6
- package/lib/outcome/memberships.ts +131 -0
- package/lib/outcome/schema.ts +147 -0
- package/lib/poi/lookup.ts +4 -0
- package/lib/poi/schema.ts +2 -1
- package/lib/ranking/primary-preference.ts +14 -6
- package/lib/schema/overture-match.ts +2 -1
- 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 +17 -0
- package/out/address/keys/lookup.d.ts.map +1 -1
- package/out/address/keys/lookup.js +119 -6
- package/out/address/keys/lookup.js.map +1 -1
- package/out/address/keys/point-lookup.d.ts.map +1 -1
- package/out/address/keys/point-lookup.js +4 -0
- 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 +43 -2
- package/out/address/point/schema.d.ts.map +1 -1
- package/out/address/point/schema.js +73 -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 +12 -0
- package/out/address/point.d.ts.map +1 -1
- package/out/address/point.js +143 -26
- package/out/address/point.js.map +1 -1
- package/out/build/candidate.d.ts +49 -0
- package/out/build/candidate.d.ts.map +1 -1
- package/out/build/candidate.js +101 -8
- package/out/build/candidate.js.map +1 -1
- package/out/build/population-corrections.d.ts +2 -2
- package/out/build/population-corrections.d.ts.map +1 -1
- package/out/build/population-corrections.js +10 -2
- package/out/build/population-corrections.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 +16 -17
- package/out/candidate/lookup.d.ts.map +1 -1
- package/out/candidate/lookup.js +222 -92
- package/out/candidate/lookup.js.map +1 -1
- package/out/candidate/name-roles.d.ts +3 -2
- package/out/candidate/name-roles.d.ts.map +1 -1
- package/out/candidate/name-roles.js +75 -5
- package/out/candidate/name-roles.js.map +1 -1
- package/out/candidate/outcomes.d.ts +57 -0
- package/out/candidate/outcomes.d.ts.map +1 -0
- package/out/candidate/outcomes.js +109 -0
- package/out/candidate/outcomes.js.map +1 -0
- package/out/candidate/schema.d.ts +51 -3
- package/out/candidate/schema.d.ts.map +1 -1
- package/out/candidate/schema.js +40 -0
- 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/geonames/aliases.d.ts +10 -0
- package/out/geonames/aliases.d.ts.map +1 -1
- package/out/geonames/aliases.js +33 -1
- package/out/geonames/aliases.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 +24 -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 +15 -3
- package/out/landmark.d.ts.map +1 -1
- package/out/landmark.js +38 -3
- package/out/landmark.js.map +1 -1
- package/out/lookup/types.d.ts +21 -5
- package/out/lookup/types.d.ts.map +1 -1
- package/out/outcome/memberships.d.ts +58 -0
- package/out/outcome/memberships.d.ts.map +1 -0
- package/out/outcome/memberships.js +83 -0
- package/out/outcome/memberships.js.map +1 -0
- package/out/outcome/schema.d.ts +74 -0
- package/out/outcome/schema.d.ts.map +1 -0
- package/out/outcome/schema.js +63 -0
- package/out/outcome/schema.js.map +1 -0
- package/out/poi/lookup.d.ts +4 -0
- package/out/poi/lookup.d.ts.map +1 -1
- package/out/poi/lookup.js.map +1 -1
- package/out/poi/schema.d.ts +2 -1
- package/out/poi/schema.d.ts.map +1 -1
- package/out/poi/schema.js.map +1 -1
- package/out/ranking/primary-preference.d.ts +9 -5
- package/out/ranking/primary-preference.d.ts.map +1 -1
- package/out/ranking/primary-preference.js +11 -3
- package/out/ranking/primary-preference.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/package.json +15 -15
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { isOfficialLanguage } from "@mailwoman/codex/country"
|
|
9
|
+
import { COUNTRY_WRITTEN_SCRIPTS } from "@mailwoman/codex/country/writing-systems"
|
|
9
10
|
import type { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
10
11
|
import { tableExists } from "@mailwoman/sqlite/introspection"
|
|
11
12
|
|
|
@@ -37,9 +38,56 @@ export const GLOSS_EXCLUDED_PLACETYPES: ReadonlySet<string> = new Set([
|
|
|
37
38
|
"county",
|
|
38
39
|
])
|
|
39
40
|
|
|
41
|
+
const NON_ASCII = /\P{ASCII}/u
|
|
42
|
+
|
|
43
|
+
const foreignScriptRegexCache = new Map<string, RegExp | null>()
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Compiles a Unicode set-subtraction regex matching any letter whose script is neither
|
|
47
|
+
* Common/Inherited, Latin (`Latn`), nor one of `country`'s written scripts.
|
|
48
|
+
*
|
|
49
|
+
* Returns `null` when `COUNTRY_WRITTEN_SCRIPTS` has no entry for `country`.
|
|
50
|
+
*/
|
|
51
|
+
function foreignScriptPatternFor(country: string): RegExp | null {
|
|
52
|
+
const cached = foreignScriptRegexCache.get(country)
|
|
53
|
+
|
|
54
|
+
if (cached !== undefined) return cached
|
|
55
|
+
|
|
56
|
+
const written = COUNTRY_WRITTEN_SCRIPTS[country]
|
|
57
|
+
|
|
58
|
+
if (!written?.length) {
|
|
59
|
+
foreignScriptRegexCache.set(country, null)
|
|
60
|
+
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const allowed = new Set<string>(["Latn", ...written])
|
|
65
|
+
const allowedClass = [...allowed].map((script) => `\\p{sc=${script}}`).join("")
|
|
66
|
+
const pattern = new RegExp(`[\\p{L}--[\\p{sc=Common}\\p{sc=Inherited}${allowedClass}]]`, "v")
|
|
67
|
+
|
|
68
|
+
foreignScriptRegexCache.set(country, pattern)
|
|
69
|
+
|
|
70
|
+
return pattern
|
|
71
|
+
}
|
|
72
|
+
|
|
40
73
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
74
|
+
* Reports whether `nameKey` contains a non-Latin letter in a script outside `country`'s
|
|
75
|
+
* national and regional written scripts (`COUNTRY_WRITTEN_SCRIPTS`).
|
|
76
|
+
*
|
|
77
|
+
* Latin (`Latn`) is always exempt as the global romanization script, so `moskva` on `RU`,
|
|
78
|
+
* `peking` on `CN`, and `tokio` on `JP` return `false`.
|
|
79
|
+
*/
|
|
80
|
+
function isForeignScriptNameKey(nameKey: string, country: string): boolean {
|
|
81
|
+
if (!NON_ASCII.test(nameKey)) return false
|
|
82
|
+
|
|
83
|
+
const pattern = foreignScriptPatternFor(country)
|
|
84
|
+
|
|
85
|
+
return pattern !== null && pattern.test(nameKey)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Stamp `name_role` on non-primary rows: `abbr` by provenance, `foreign-script` by non-Latin script
|
|
90
|
+
* outside `COUNTRY_WRITTEN_SCRIPTS`, `variant` by own-name verdict, then `gloss` by key-volume anomaly.
|
|
43
91
|
*
|
|
44
92
|
* @returns The stamp counts plus the census the prototype exists to report:
|
|
45
93
|
* how much of the ≥-threshold key tail has any role.
|
|
@@ -53,7 +101,14 @@ export function stampNameRoles(ctx: {
|
|
|
53
101
|
ptcodes: Map<string, number>
|
|
54
102
|
ccodes: Map<string, number>
|
|
55
103
|
progress: (phase: string, message: string) => void
|
|
56
|
-
}): {
|
|
104
|
+
}): {
|
|
105
|
+
roleGloss: number
|
|
106
|
+
roleAbbr: number
|
|
107
|
+
roleVariant: number
|
|
108
|
+
roleForeignScript: number
|
|
109
|
+
keyTailPlaces: number
|
|
110
|
+
keyTailWithRole: number
|
|
111
|
+
} {
|
|
57
112
|
const { src, out, attrs, keyCounts, glossThreshold, progress } = ctx
|
|
58
113
|
progress("roles", "stamping name roles (gloss anomaly + abbr provenance)")
|
|
59
114
|
|
|
@@ -116,7 +171,8 @@ export function stampNameRoles(ctx: {
|
|
|
116
171
|
progress("roles", "source carries no `names` table — abbr detector skipped (gloss still runs)")
|
|
117
172
|
}
|
|
118
173
|
|
|
119
|
-
// Stamp order is precedence: abbr first, then
|
|
174
|
+
// Stamp order is precedence: abbr first, then foreign-script, then the own-name
|
|
175
|
+
// variant verdict, then gloss fills what none of them claimed.
|
|
120
176
|
const roleAbbr = Number(
|
|
121
177
|
out
|
|
122
178
|
.prepare(
|
|
@@ -127,6 +183,55 @@ export function stampNameRoles(ctx: {
|
|
|
127
183
|
.run().changes
|
|
128
184
|
)
|
|
129
185
|
|
|
186
|
+
// Runs before variant and gloss so Cyrillic exonyms on Latin-script namesakes
|
|
187
|
+
// (such as `одесса` on Odessa, US or `лондон` on London, CA) receive `foreign-script` rather
|
|
188
|
+
// than `variant` (whose Cyrillic romanizer would otherwise match their primary name), and so
|
|
189
|
+
// zero-population hamlets with scraped non-Latin exonyms receive `foreign-script` rather than `gloss`.
|
|
190
|
+
// Latin romanizations (`brest` on `Брэст`, `moskva` on `Москва`) and home-script spelling
|
|
191
|
+
// variants (`харків` on `Харьков` in `UA`) stay unstamped here and proceed to `variant`.
|
|
192
|
+
out.exec(
|
|
193
|
+
"CREATE TEMP TABLE foreign_script_key (spr_id INTEGER NOT NULL, name_key TEXT NOT NULL, PRIMARY KEY (spr_id, name_key)) WITHOUT ROWID"
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
const insForeignScriptKey = out.prepare("INSERT OR IGNORE INTO foreign_script_key VALUES (?, ?)")
|
|
197
|
+
let foreignScriptScanned = 0
|
|
198
|
+
|
|
199
|
+
out.exec("BEGIN")
|
|
200
|
+
|
|
201
|
+
for (const r of out
|
|
202
|
+
.prepare("SELECT DISTINCT spr_id, name_key FROM cand_stage WHERE is_primary = 0 AND name_role IS NULL")
|
|
203
|
+
.iterate()) {
|
|
204
|
+
foreignScriptScanned++
|
|
205
|
+
const a = attrs.get(Number(r.spr_id))
|
|
206
|
+
|
|
207
|
+
if (!a) continue
|
|
208
|
+
const iso2 = iso2ByCID.get(a.cid) ?? "??"
|
|
209
|
+
const nameKey = String(r.name_key)
|
|
210
|
+
|
|
211
|
+
if (isForeignScriptNameKey(nameKey, iso2)) {
|
|
212
|
+
insForeignScriptKey.run(Number(r.spr_id), nameKey)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
out.exec("COMMIT")
|
|
217
|
+
|
|
218
|
+
const roleForeignScript = Number(
|
|
219
|
+
out
|
|
220
|
+
.prepare(
|
|
221
|
+
`UPDATE cand_stage SET name_role = 'foreign-script'
|
|
222
|
+
WHERE is_primary = 0 AND name_role IS NULL AND EXISTS (
|
|
223
|
+
SELECT 1 FROM foreign_script_key fsk WHERE fsk.spr_id = cand_stage.spr_id AND fsk.name_key = cand_stage.name_key)`
|
|
224
|
+
)
|
|
225
|
+
.run().changes
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
out.exec("DROP TABLE foreign_script_key")
|
|
229
|
+
|
|
230
|
+
progress(
|
|
231
|
+
"roles",
|
|
232
|
+
`foreign-script: ${roleForeignScript.toLocaleString()} of ${foreignScriptScanned.toLocaleString()} unstamped alias keys use a non-Latin foreign script`
|
|
233
|
+
)
|
|
234
|
+
|
|
130
235
|
// Runs before gloss so the place's own name remains its source name.
|
|
131
236
|
// An uncovered script answers no-verdict.
|
|
132
237
|
out.exec(
|
|
@@ -212,9 +317,10 @@ export function stampNameRoles(ctx: {
|
|
|
212
317
|
|
|
213
318
|
progress(
|
|
214
319
|
"roles",
|
|
215
|
-
`${roleAbbr.toLocaleString()} abbr + ${
|
|
320
|
+
`${roleAbbr.toLocaleString()} abbr + ${roleForeignScript.toLocaleString()} foreign-script + ` +
|
|
321
|
+
`${roleVariant.toLocaleString()} variant + ${roleGloss.toLocaleString()} gloss rows stamped; ` +
|
|
216
322
|
`key tail (>= ${glossThreshold} keys): ${keyTailWithRole.toLocaleString()} of ${keyTailPlaces.toLocaleString()} places carry a role`
|
|
217
323
|
)
|
|
218
324
|
|
|
219
|
-
return { roleGloss, roleAbbr, roleVariant, keyTailPlaces, keyTailWithRole }
|
|
325
|
+
return { roleGloss, roleAbbr, roleVariant, roleForeignScript, keyTailPlaces, keyTailWithRole }
|
|
220
326
|
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Compiles the candidate database's outcome tables from one pinned revision of the definitions.
|
|
6
|
+
*
|
|
7
|
+
* `outcome_default` holds every placetype default of the spec.
|
|
8
|
+
* `record_outcome` holds the assertions GeoNames feature codes make beyond a record's defaults.
|
|
9
|
+
*
|
|
10
|
+
* A record's feature code is read from the GeoNames per-country dumps, through the GeoNames
|
|
11
|
+
* range id of a place the GeoNames step added, or a record's `gn:id` concordance.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { readUnquotedTSV } from "@mailwoman/core/fs/delimited"
|
|
15
|
+
import { GEONAMES_ID_BASE, GEONAMES_POSTAL_ID_BASE } from "@mailwoman/core/resolver/synthetic-id-ranges"
|
|
16
|
+
import { createOutcomeEvidenceTable } from "@mailwoman/gazetteer/whosonfirst/outcomes/evidence"
|
|
17
|
+
import type { CompiledOutcomeSpec, OutcomeSpecPin } from "@mailwoman/gazetteer/whosonfirst/outcomes/spec"
|
|
18
|
+
import type { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
19
|
+
import { resolvePath, type PathBuilderLike } from "path-ts"
|
|
20
|
+
import { Globerator } from "spliterator/node/fs"
|
|
21
|
+
|
|
22
|
+
import type { PlaceAttrs } from "#candidate/place-attrs"
|
|
23
|
+
import { type OutcomeDatabase, writeOutcomeDefaults } from "#outcome/schema"
|
|
24
|
+
import type { WOFDatabase } from "#schema"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The outcome inputs of a candidate build.
|
|
28
|
+
*/
|
|
29
|
+
export interface CandidateOutcomeInputs {
|
|
30
|
+
spec: CompiledOutcomeSpec
|
|
31
|
+
pin: OutcomeSpecPin
|
|
32
|
+
/**
|
|
33
|
+
* The GeoNames per-country dump directory, `<CC>.txt` files.
|
|
34
|
+
*/
|
|
35
|
+
geonamesDir: PathBuilderLike
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Counts from {@link stageCandidateOutcomes}.
|
|
40
|
+
*/
|
|
41
|
+
export interface CandidateOutcomeResult {
|
|
42
|
+
defaultRows: number
|
|
43
|
+
/**
|
|
44
|
+
* `record_outcome` rows, one per explicit assertion.
|
|
45
|
+
*/
|
|
46
|
+
recordRows: number
|
|
47
|
+
/**
|
|
48
|
+
* Records with one `record_outcome` row or more.
|
|
49
|
+
*/
|
|
50
|
+
recordsAsserted: number
|
|
51
|
+
contestedRows: number
|
|
52
|
+
/**
|
|
53
|
+
* Records whose GeoNames feature code the dumps gave.
|
|
54
|
+
*/
|
|
55
|
+
recordsWithFeatureCode: number
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isGeonamesPlaceID(id: number): boolean {
|
|
59
|
+
return id >= GEONAMES_ID_BASE && id < GEONAMES_POSTAL_ID_BASE
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Write the outcome tables into a candidate build in progress.
|
|
64
|
+
*
|
|
65
|
+
* @throws When a place the GeoNames step added has no row in the dumps, because its
|
|
66
|
+
* type field is then unmapped and the build would write an empty set for it.
|
|
67
|
+
*/
|
|
68
|
+
export async function stageCandidateOutcomes(options: {
|
|
69
|
+
src: DatabaseClient<WOFDatabase>
|
|
70
|
+
out: DatabaseClient<OutcomeDatabase>
|
|
71
|
+
/**
|
|
72
|
+
* Each candidate place's attributes, by record id.
|
|
73
|
+
*/
|
|
74
|
+
attrs: ReadonlyMap<number, Pick<PlaceAttrs, "ptid">>
|
|
75
|
+
/**
|
|
76
|
+
* The placetype dictionary, from name to id.
|
|
77
|
+
*/
|
|
78
|
+
ptcodes: ReadonlyMap<string, number>
|
|
79
|
+
inputs: CandidateOutcomeInputs
|
|
80
|
+
progress: (phase: string, message: string) => void
|
|
81
|
+
}): Promise<CandidateOutcomeResult> {
|
|
82
|
+
const { src, out, inputs, progress } = options
|
|
83
|
+
const placetypeNames = new Map([...options.ptcodes].map(([placetype, id]) => [id, placetype]))
|
|
84
|
+
const placetypeOf = new Map([...options.attrs].map(([sid, a]) => [sid, placetypeNames.get(a.ptid) ?? ""]))
|
|
85
|
+
const table = createOutcomeEvidenceTable(inputs.spec)
|
|
86
|
+
|
|
87
|
+
const defaultRows = await writeOutcomeDefaults(out, inputs.spec, inputs.pin)
|
|
88
|
+
|
|
89
|
+
// Each GeoNames number, mapped to the candidate places whose id or concordance holds it.
|
|
90
|
+
const sprOf = new Map<number, number[]>()
|
|
91
|
+
const geonamesPlaces = new Set<number>()
|
|
92
|
+
|
|
93
|
+
const link = (geonameid: number, sprID: number) => {
|
|
94
|
+
const ids = sprOf.get(geonameid)
|
|
95
|
+
|
|
96
|
+
if (ids) {
|
|
97
|
+
if (!ids.includes(sprID)) {
|
|
98
|
+
ids.push(sprID)
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
sprOf.set(geonameid, [sprID])
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const sprID of placetypeOf.keys()) {
|
|
106
|
+
if (isGeonamesPlaceID(sprID)) {
|
|
107
|
+
geonamesPlaces.add(sprID)
|
|
108
|
+
link(sprID - GEONAMES_ID_BASE, sprID)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const row of src.prepare("SELECT id, other_id FROM concordances WHERE other_source = 'gn:id'").iterate()) {
|
|
113
|
+
const sprID = Number(row.id)
|
|
114
|
+
const geonameid = Number(row.other_id)
|
|
115
|
+
|
|
116
|
+
if (placetypeOf.has(sprID) && Number.isSafeInteger(geonameid) && geonameid > 0) {
|
|
117
|
+
link(geonameid, sprID)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
progress("outcomes", `${sprOf.size.toLocaleString()} GeoNames numbers on candidate places; reading feature codes`)
|
|
122
|
+
|
|
123
|
+
const assertionsOf = new Map<number, ReturnType<typeof table.assertionsFor>>()
|
|
124
|
+
const coded = new Set<number>()
|
|
125
|
+
|
|
126
|
+
for await (const path of Globerator.from("[A-Z][A-Z].txt", {
|
|
127
|
+
cwd: resolvePath(inputs.geonamesDir),
|
|
128
|
+
absolute: true,
|
|
129
|
+
})) {
|
|
130
|
+
for await (const f of readUnquotedTSV(path)) {
|
|
131
|
+
const ids = sprOf.get(Number(f[0]))
|
|
132
|
+
|
|
133
|
+
if (!ids) continue
|
|
134
|
+
|
|
135
|
+
const assertions = table.assertionsFor("geonames", `${f[6]}.${f[7]}`)
|
|
136
|
+
|
|
137
|
+
for (const sprID of ids) {
|
|
138
|
+
coded.add(sprID)
|
|
139
|
+
|
|
140
|
+
if (assertions.length) {
|
|
141
|
+
assertionsOf.set(sprID, [...(assertionsOf.get(sprID) ?? []), ...assertions])
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const uncoded = [...geonamesPlaces].filter((id) => !coded.has(id))
|
|
148
|
+
|
|
149
|
+
if (uncoded.length) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`${uncoded.length.toLocaleString()} places the GeoNames step added have no row in ${inputs.geonamesDir.toString()}, ` +
|
|
152
|
+
`so their feature code is unknown; first ${uncoded.slice(0, 5).join(", ")}.`
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const insert = out.prepare("INSERT INTO record_outcome (spr_id, outcome_id, contested) VALUES (?, ?, ?)")
|
|
157
|
+
let recordRows = 0
|
|
158
|
+
let contestedRows = 0
|
|
159
|
+
let recordsAsserted = 0
|
|
160
|
+
|
|
161
|
+
out.exec("BEGIN")
|
|
162
|
+
|
|
163
|
+
for (const [sprID, assertions] of assertionsOf) {
|
|
164
|
+
const rows = table.recordRows(placetypeOf.get(sprID) ?? null, assertions)
|
|
165
|
+
|
|
166
|
+
for (const row of rows) {
|
|
167
|
+
insert.run(sprID, row.outcomeID, row.contested ? 1 : 0)
|
|
168
|
+
|
|
169
|
+
if (row.contested) {
|
|
170
|
+
contestedRows++
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
recordRows += rows.length
|
|
175
|
+
|
|
176
|
+
if (rows.length) {
|
|
177
|
+
recordsAsserted++
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
out.exec("COMMIT")
|
|
182
|
+
|
|
183
|
+
progress(
|
|
184
|
+
"outcomes",
|
|
185
|
+
`${defaultRows} placetype defaults; ${recordRows.toLocaleString()} assertions on ` +
|
|
186
|
+
`${recordsAsserted.toLocaleString()} records (${contestedRows.toLocaleString()} contested); ` +
|
|
187
|
+
`${coded.size.toLocaleString()} records carry a feature code`
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
return { defaultRows, recordRows, recordsAsserted, contestedRows, recordsWithFeatureCode: coded.size }
|
|
191
|
+
}
|
package/lib/candidate/schema.ts
CHANGED
|
@@ -5,7 +5,9 @@ import { sql, type Kysely } from "kysely"
|
|
|
5
5
|
|
|
6
6
|
import type { CandidateAncestorsDatabase } from "#candidate/ancestors/schema"
|
|
7
7
|
import type { CapitalTable } from "#candidate/capital-schema"
|
|
8
|
+
import type { CandidateDel1Table } from "#candidate/del1"
|
|
8
9
|
import type { NameKey } from "#labels/street/normalize"
|
|
10
|
+
import type { OutcomeDatabase } from "#outcome/schema"
|
|
9
11
|
import type { OvertureWOFMatchTable } from "#schema/overture-match"
|
|
10
12
|
|
|
11
13
|
/**
|
|
@@ -60,9 +62,8 @@ export interface CandidateTable {
|
|
|
60
62
|
*/
|
|
61
63
|
importance: number | null
|
|
62
64
|
/**
|
|
63
|
-
* The name's detected role on this row (`'abbr'`, `'
|
|
65
|
+
* The name's detected role on this row (`'abbr'`, `'foreign-script'`, `'variant'` or `'gloss'`),
|
|
64
66
|
* or NULL when no role was detected.
|
|
65
|
-
* The column is write-only in this build generation.
|
|
66
67
|
*/
|
|
67
68
|
name_role: string | null
|
|
68
69
|
}
|
|
@@ -83,11 +84,39 @@ export interface PlacetypeCodeTable {
|
|
|
83
84
|
placetype: string
|
|
84
85
|
}
|
|
85
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Wikidata concordance row copied from the source admin database (`other_source = 'wd:id'`).
|
|
89
|
+
*/
|
|
90
|
+
export interface CandidateConcordanceTable {
|
|
91
|
+
id: number
|
|
92
|
+
other_source: string
|
|
93
|
+
other_id: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Per-candidate Wikipedia sitelink count joined from Wikidata / landmarks via `wd:id`,
|
|
98
|
+
* providing cross-layer prominence `log10(sitelinks + 1)`.
|
|
99
|
+
*/
|
|
100
|
+
export interface CandidateSitelinksTable {
|
|
101
|
+
spr_id: number
|
|
102
|
+
sitelinks: number
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Table name for {@link CandidateConcordanceTable}.
|
|
107
|
+
*/
|
|
108
|
+
export const CANDIDATE_CONCORDANCES_TABLE = "concordances"
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Table name for {@link CandidateSitelinksTable}.
|
|
112
|
+
*/
|
|
113
|
+
export const CANDIDATE_SITELINKS_TABLE = "candidate_sitelinks"
|
|
114
|
+
|
|
86
115
|
/**
|
|
87
116
|
* The candidate database schema, extending the ancestors sidecar so one typed
|
|
88
117
|
* client covers every table in the artifact.
|
|
89
118
|
*/
|
|
90
|
-
export interface CandidateDatabase extends CandidateAncestorsDatabase {
|
|
119
|
+
export interface CandidateDatabase extends CandidateAncestorsDatabase, OutcomeDatabase {
|
|
91
120
|
/**
|
|
92
121
|
* The clustered `without rowid` lookup table the reader probes.
|
|
93
122
|
*/
|
|
@@ -108,6 +137,18 @@ export interface CandidateDatabase extends CandidateAncestorsDatabase {
|
|
|
108
137
|
* See overture-match-schema.ts.
|
|
109
138
|
*/
|
|
110
139
|
overture_wof_match: OvertureWOFMatchTable
|
|
140
|
+
/**
|
|
141
|
+
* Wikidata (`wd:id`) concordances for candidate `spr_id`s.
|
|
142
|
+
*/
|
|
143
|
+
concordances: CandidateConcordanceTable
|
|
144
|
+
/**
|
|
145
|
+
* Wikipedia sitelink counts per `spr_id` for cross-layer prominence.
|
|
146
|
+
*/
|
|
147
|
+
candidate_sitelinks: CandidateSitelinksTable
|
|
148
|
+
/**
|
|
149
|
+
* SymSpell 1-delete fuzzy index over prominent primary `name_key` values.
|
|
150
|
+
*/
|
|
151
|
+
candidate_del1: CandidateDel1Table
|
|
111
152
|
}
|
|
112
153
|
|
|
113
154
|
/**
|
|
@@ -247,3 +288,38 @@ export async function createCandidatePostcodePlaceIndex(
|
|
|
247
288
|
.where(sql<boolean>`placetype_id = ${sql.lit(placetypeID)} AND is_primary = 1`)
|
|
248
289
|
.execute()
|
|
249
290
|
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Create the clustered `without rowid` `concordances` table storing Wikidata (`wd:id`)
|
|
294
|
+
* links for candidate `spr_id`s, with a covering reverse index so a QID from a
|
|
295
|
+
* clicked map label resolves to its `spr_id` in one index seek.
|
|
296
|
+
*/
|
|
297
|
+
export async function createCandidateConcordancesTable(db: Kysely<CandidateDatabase>): Promise<void> {
|
|
298
|
+
await db.schema
|
|
299
|
+
.createTable(CANDIDATE_CONCORDANCES_TABLE)
|
|
300
|
+
.addColumn("id", "integer", (c) => c.notNull())
|
|
301
|
+
.addColumn("other_source", "text", (c) => c.notNull())
|
|
302
|
+
.addColumn("other_id", "text", (c) => c.notNull())
|
|
303
|
+
.addPrimaryKeyConstraint("concordances_pk", ["id", "other_source", "other_id"])
|
|
304
|
+
.modifyEnd(sql`without rowid`)
|
|
305
|
+
.execute()
|
|
306
|
+
|
|
307
|
+
await db.schema
|
|
308
|
+
.createIndex("concordances_by_other")
|
|
309
|
+
.on(CANDIDATE_CONCORDANCES_TABLE)
|
|
310
|
+
.columns(["other_source", "other_id", "id"])
|
|
311
|
+
.execute()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Create the clustered `without rowid` `candidate_sitelinks` table storing
|
|
316
|
+
* Wikipedia sitelink counts per candidate `spr_id`.
|
|
317
|
+
*/
|
|
318
|
+
export async function createCandidateSitelinksTable(db: Kysely<CandidateDatabase>): Promise<void> {
|
|
319
|
+
await db.schema
|
|
320
|
+
.createTable(CANDIDATE_SITELINKS_TABLE)
|
|
321
|
+
.addColumn("spr_id", "integer", (c) => c.primaryKey())
|
|
322
|
+
.addColumn("sitelinks", "integer", (c) => c.notNull())
|
|
323
|
+
.modifyEnd(sql`without rowid`)
|
|
324
|
+
.execute()
|
|
325
|
+
}
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
import type { AncestrieMatch } from "@mailwoman/ancestrie"
|
|
16
16
|
import { hashFNV1a } from "@mailwoman/core/coarse-placer/fnv-hash"
|
|
17
17
|
|
|
18
|
+
import type { BinaryFSTDel1Lookup } from "#fst/del1-sidecar"
|
|
18
19
|
import {
|
|
19
20
|
decodePlaceEntry,
|
|
20
21
|
idTableOffset,
|
|
@@ -65,6 +66,11 @@ function edgeHash(stateID: number, stringIndex: number): number {
|
|
|
65
66
|
*/
|
|
66
67
|
export class BinaryFSTMatcher {
|
|
67
68
|
readonly stateCount: number
|
|
69
|
+
/**
|
|
70
|
+
* Optional 1-delete fuzzy token sidecar (`FD1\0`) paired with this FST binary,
|
|
71
|
+
* or `null` when loaded without a sidecar.
|
|
72
|
+
*/
|
|
73
|
+
readonly del1Sidecar: BinaryFSTDel1Lookup | null
|
|
68
74
|
readonly #view: DataView
|
|
69
75
|
readonly #bytes: Uint8Array
|
|
70
76
|
readonly #decoder = new TextDecoder("utf-8")
|
|
@@ -88,10 +94,12 @@ export class BinaryFSTMatcher {
|
|
|
88
94
|
readonly #edgeSlotTarget: Uint32Array
|
|
89
95
|
#placeCount: number | null = null
|
|
90
96
|
|
|
91
|
-
constructor(input: ArrayBuffer | Uint8Array) {
|
|
97
|
+
constructor(input: ArrayBuffer | Uint8Array, del1Sidecar: BinaryFSTDel1Lookup | null = null) {
|
|
92
98
|
const bytes = input instanceof ArrayBuffer ? new Uint8Array(input) : input
|
|
93
99
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
94
100
|
|
|
101
|
+
this.del1Sidecar = del1Sidecar
|
|
102
|
+
|
|
95
103
|
if (bytes.byteLength < HEADER_SIZE) throw new Error("FST buffer too small for header")
|
|
96
104
|
|
|
97
105
|
if (FST_MAGIC_BYTES.some((byte, index) => bytes[index] !== byte)) {
|
package/lib/fst/builder.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { allRows, getRow } from "@mailwoman/core/utils"
|
|
|
16
16
|
import { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
17
17
|
import { resolvePath } from "path-ts"
|
|
18
18
|
|
|
19
|
+
import { buildFSTDel1Sidecar, type BuiltFSTDel1Sidecar } from "#fst/del1-sidecar"
|
|
19
20
|
import { readWOFSourceIdentity } from "#fst/freshness"
|
|
20
21
|
import type { FSTNode } from "#fst/matcher"
|
|
21
22
|
import { FSTMatcher, normalizeTokens } from "#fst/matcher"
|
|
@@ -65,6 +66,7 @@ export async function buildFSTFromWOF(opts: BuildFSTOpts): Promise<{
|
|
|
65
66
|
matcher: FSTMatcher
|
|
66
67
|
provenance: FSTProvenance
|
|
67
68
|
result: BuildFSTResult
|
|
69
|
+
del1Sidecar?: BuiltFSTDel1Sidecar
|
|
68
70
|
}> {
|
|
69
71
|
const countries = opts.countries ?? DEFAULT_COUNTRIES
|
|
70
72
|
const placetypes = opts.placetypes ?? DEFAULT_PLACETYPES
|
|
@@ -382,6 +384,10 @@ export async function buildFSTFromWOF(opts: BuildFSTOpts): Promise<{
|
|
|
382
384
|
: { exclusionPolicy: null, excludedInsertions: null }),
|
|
383
385
|
}
|
|
384
386
|
|
|
387
|
+
const del1Sidecar = opts.buildDel1Sidecar
|
|
388
|
+
? buildFSTDel1Sidecar(matcher, opts.buildDel1Sidecar === true ? {} : opts.buildDel1Sidecar)
|
|
389
|
+
: undefined
|
|
390
|
+
|
|
385
391
|
return {
|
|
386
392
|
matcher,
|
|
387
393
|
provenance,
|
|
@@ -391,5 +397,6 @@ export async function buildFSTFromWOF(opts: BuildFSTOpts): Promise<{
|
|
|
391
397
|
edgeCount,
|
|
392
398
|
tokenCount: insertCount,
|
|
393
399
|
},
|
|
400
|
+
...(del1Sidecar ? { del1Sidecar } : {}),
|
|
394
401
|
}
|
|
395
402
|
}
|