@mailwoman/corpus 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/data/address-source-register.json +545 -11
- package/data/holdout/evaluation-groups.v0.9.3.json +224169 -0
- package/data/license-decisions.json +440 -9
- package/data/licenses/PROVENANCE.md +27 -18
- package/data/licenses/at-bev-adressregister-cc-by-4.0.md +25 -0
- package/data/licenses/br-ibge-cnefe-open-data.md +21 -0
- package/data/licenses/ca-qc-mrnf-cc-by-4.0.md +29 -0
- package/data/licenses/ca-statcan-open-licence.md +41 -0
- package/data/licenses/ca-yukon-ogl-2.0.md +19 -0
- package/data/licenses/dk-dmikg-cc-by-4.0.md +43 -0
- package/data/licenses/jp-mlit-isj-pdl-1.0.md +35 -0
- package/data/source-additions.json +90 -0
- package/data/source-resolutions.json +3 -2
- package/data/training-configs/v9.1.0-bytestem-global-v091-s42.yaml +710 -0
- package/data/training-configs/v9.2.0-bytestem-global-v092-s42.yaml +714 -0
- package/data/training-configs/v9.3.0-bytestem-global-v093-s42.yaml +746 -0
- package/data/training-manifests/v0.9.1-compact.json +9660 -0
- package/data/training-manifests/v0.9.2-compact.effective.json +2670 -0
- package/data/training-manifests/v0.9.2-compact.json +9660 -0
- package/data/training-manifests/v0.9.3-compact.effective.json +2660 -0
- package/data/training-manifests/v0.9.3-compact.json +9648 -0
- package/lib/adapters/geonames/features/adapter.ts +163 -20
- package/lib/adapters/overture/adapter.ts +26 -6
- package/lib/adapters/street/line.ts +6 -5
- package/lib/adapters/street/unit.ts +7 -0
- package/lib/adapters/wof/admin/json/dual-role.ts +46 -21
- package/lib/au/adapters/gnaf/source.ts +12 -0
- package/lib/build/schema-invariants.ts +447 -0
- package/lib/fr/adapters/ban/adapter.ts +57 -8
- package/lib/fr/recipes/admin-split.ts +14 -2
- package/lib/fr/recipes/lieudit.ts +1 -1
- package/lib/fr/recipes/order.ts +14 -5
- package/lib/international/recipes/locale.ts +1 -1
- package/lib/jp/corpus/assemble.ts +20 -1
- package/lib/jp/corpus/rows.ts +87 -0
- package/lib/recipes/po/box/cedex/recipe.ts +1 -1
- package/lib/recipes/po/box/cedex/vocabulary.ts +4 -4
- package/lib/recipes/prominent/places.ts +42 -9
- package/lib/recipes/trailing-region.ts +180 -20
- package/lib/synthesizers/po-box.ts +51 -12
- package/lib/test-kit/corpus-recipe.ts +3 -0
- package/lib/tools/compact-v9.ts +22 -4
- package/lib/tools/fetch/wikidata/vital-places/classify.ts +23 -7
- package/lib/tools/fetch/wikidata/vital-places/render.ts +57 -7
- package/lib/tools/source-register/build.ts +1 -0
- package/lib/training/config/schema.ts +3 -0
- package/lib/us/adapters/usgov/usgs-structures/adapter.ts +103 -6
- package/lib/us/street-unit.ts +14 -1
- package/out/adapters/geonames/features/adapter.d.ts +2 -2
- package/out/adapters/geonames/features/adapter.d.ts.map +1 -1
- package/out/adapters/geonames/features/adapter.js +114 -20
- package/out/adapters/geonames/features/adapter.js.map +1 -1
- package/out/adapters/overture/adapter.d.ts.map +1 -1
- package/out/adapters/overture/adapter.js +21 -5
- package/out/adapters/overture/adapter.js.map +1 -1
- package/out/adapters/street/line.d.ts +4 -4
- package/out/adapters/street/line.d.ts.map +1 -1
- package/out/adapters/street/line.js +5 -5
- package/out/adapters/street/line.js.map +1 -1
- package/out/adapters/street/unit.d.ts +4 -0
- package/out/adapters/street/unit.d.ts.map +1 -1
- package/out/adapters/street/unit.js +3 -0
- package/out/adapters/street/unit.js.map +1 -1
- package/out/adapters/wof/admin/json/dual-role.d.ts +8 -3
- package/out/adapters/wof/admin/json/dual-role.d.ts.map +1 -1
- package/out/adapters/wof/admin/json/dual-role.js +40 -19
- package/out/adapters/wof/admin/json/dual-role.js.map +1 -1
- package/out/au/adapters/gnaf/source.d.ts +9 -0
- package/out/au/adapters/gnaf/source.d.ts.map +1 -1
- package/out/au/adapters/gnaf/source.js +9 -0
- package/out/au/adapters/gnaf/source.js.map +1 -1
- package/out/build/schema-invariants.d.ts +101 -0
- package/out/build/schema-invariants.d.ts.map +1 -0
- package/out/build/schema-invariants.js +302 -0
- package/out/build/schema-invariants.js.map +1 -0
- package/out/fr/adapters/ban/adapter.d.ts.map +1 -1
- package/out/fr/adapters/ban/adapter.js +49 -7
- package/out/fr/adapters/ban/adapter.js.map +1 -1
- package/out/fr/recipes/admin-split.d.ts.map +1 -1
- package/out/fr/recipes/admin-split.js +15 -2
- package/out/fr/recipes/admin-split.js.map +1 -1
- package/out/fr/recipes/lieudit.js +1 -1
- package/out/fr/recipes/lieudit.js.map +1 -1
- package/out/fr/recipes/order.d.ts.map +1 -1
- package/out/fr/recipes/order.js +16 -5
- package/out/fr/recipes/order.js.map +1 -1
- package/out/international/recipes/locale.js +1 -1
- package/out/international/recipes/locale.js.map +1 -1
- package/out/jp/corpus/assemble.d.ts.map +1 -1
- package/out/jp/corpus/assemble.js +14 -2
- package/out/jp/corpus/assemble.js.map +1 -1
- package/out/jp/corpus/rows.d.ts +8 -0
- package/out/jp/corpus/rows.d.ts.map +1 -1
- package/out/jp/corpus/rows.js +72 -0
- package/out/jp/corpus/rows.js.map +1 -1
- package/out/recipes/po/box/cedex/recipe.js +1 -1
- package/out/recipes/po/box/cedex/recipe.js.map +1 -1
- package/out/recipes/po/box/cedex/vocabulary.d.ts +1 -1
- package/out/recipes/po/box/cedex/vocabulary.d.ts.map +1 -1
- package/out/recipes/po/box/cedex/vocabulary.js +4 -4
- package/out/recipes/po/box/cedex/vocabulary.js.map +1 -1
- package/out/recipes/prominent/places.d.ts.map +1 -1
- package/out/recipes/prominent/places.js +30 -5
- package/out/recipes/prominent/places.js.map +1 -1
- package/out/recipes/trailing-region.d.ts.map +1 -1
- package/out/recipes/trailing-region.js +148 -20
- package/out/recipes/trailing-region.js.map +1 -1
- package/out/synthesizers/po-box.d.ts +7 -0
- package/out/synthesizers/po-box.d.ts.map +1 -1
- package/out/synthesizers/po-box.js +36 -12
- package/out/synthesizers/po-box.js.map +1 -1
- package/out/test-kit/corpus-recipe.d.ts +3 -0
- package/out/test-kit/corpus-recipe.d.ts.map +1 -1
- package/out/test-kit/corpus-recipe.js.map +1 -1
- package/out/tools/compact-v9.d.ts +8 -1
- package/out/tools/compact-v9.d.ts.map +1 -1
- package/out/tools/compact-v9.js +17 -4
- package/out/tools/compact-v9.js.map +1 -1
- package/out/tools/fetch/wikidata/vital-places/classify.d.ts +12 -2
- package/out/tools/fetch/wikidata/vital-places/classify.d.ts.map +1 -1
- package/out/tools/fetch/wikidata/vital-places/classify.js +19 -7
- package/out/tools/fetch/wikidata/vital-places/classify.js.map +1 -1
- package/out/tools/fetch/wikidata/vital-places/render.d.ts +9 -5
- package/out/tools/fetch/wikidata/vital-places/render.d.ts.map +1 -1
- package/out/tools/fetch/wikidata/vital-places/render.js +44 -7
- package/out/tools/fetch/wikidata/vital-places/render.js.map +1 -1
- package/out/tools/source-register/build.d.ts.map +1 -1
- package/out/tools/source-register/build.js +1 -0
- package/out/tools/source-register/build.js.map +1 -1
- package/out/training/config/schema.d.ts +21 -0
- package/out/training/config/schema.d.ts.map +1 -1
- package/out/training/config/schema.js +3 -0
- package/out/training/config/schema.js.map +1 -1
- package/out/us/adapters/usgov/usgs-structures/adapter.d.ts.map +1 -1
- package/out/us/adapters/usgov/usgs-structures/adapter.js +78 -4
- package/out/us/adapters/usgov/usgs-structures/adapter.js.map +1 -1
- package/out/us/street-unit.d.ts.map +1 -1
- package/out/us/street-unit.js +11 -1
- package/out/us/street-unit.js.map +1 -1
- package/package.json +19 -19
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @file Pre-training corpus schema-invariant audit over a built or assembled parquet manifest.
|
|
6
|
+
*
|
|
7
|
+
* Catches stale adapter outputs before a training launch bills GPU hours.
|
|
8
|
+
* Each invariant scans the parquet files a manifest lists through DuckDB, reports exact per-source
|
|
9
|
+
* denominators and violation counts, and confirms concrete failing sample rows with UTF-16 span slicing.
|
|
10
|
+
*
|
|
11
|
+
* A missing manifest, an empty file list, a missing parquet file, or an unreadable parquet file throws
|
|
12
|
+
* rather than reporting zero violations.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { thoroughfareClassOf } from "@mailwoman/codex/labels/street/thoroughfare-classes"
|
|
16
|
+
import { readLocalJSONFile } from "@mailwoman/core/fs/readers"
|
|
17
|
+
import { isFile } from "@mailwoman/core/fs/readers/stat"
|
|
18
|
+
import { foldNFKCWhitespace } from "@mailwoman/normalize/fold"
|
|
19
|
+
import { dirname, type PathBuilderLike, resolvePath } from "path-ts"
|
|
20
|
+
|
|
21
|
+
import { PARQUET_MANIFEST_FILE } from "#build/holdout-check"
|
|
22
|
+
import { type DuckDBLimits, escapeSQLString, openDuckDB } from "#parquet/duckdb"
|
|
23
|
+
import { componentAtIndexSQL } from "#parquet/span-sql"
|
|
24
|
+
import { baseManifestFiles, localManifestFilePath } from "#tools/overlay/manifest"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Route and thoroughfare words disallowed in a US `street_prefix` span.
|
|
28
|
+
* Supplements {@link thoroughfareClassOf} (locale `"us"`).
|
|
29
|
+
*
|
|
30
|
+
* Under USPS Publication 28 and `SCHEMA.mdx`, a US `street_prefix` holds only a pre-directional
|
|
31
|
+
* or positional pre-modifier (`N`, `SW`, `North`, `Upper`, `Old`).
|
|
32
|
+
* Numbered-route words and thoroughfare types belong inside `street` or `street_suffix`.
|
|
33
|
+
*/
|
|
34
|
+
export const US_STREET_PREFIX_DISALLOWED_WORDS: ReadonlySet<string> = new Set([
|
|
35
|
+
"alternate",
|
|
36
|
+
"alt",
|
|
37
|
+
"bus",
|
|
38
|
+
"business",
|
|
39
|
+
"byp",
|
|
40
|
+
"bypass",
|
|
41
|
+
"calle",
|
|
42
|
+
"corte",
|
|
43
|
+
"county",
|
|
44
|
+
"dr",
|
|
45
|
+
"drive",
|
|
46
|
+
"farm",
|
|
47
|
+
"fm",
|
|
48
|
+
"interstate",
|
|
49
|
+
"loop",
|
|
50
|
+
"market",
|
|
51
|
+
"paseo",
|
|
52
|
+
"piazza",
|
|
53
|
+
"placita",
|
|
54
|
+
"plaza",
|
|
55
|
+
"ranch",
|
|
56
|
+
"rm",
|
|
57
|
+
"spur",
|
|
58
|
+
"sr",
|
|
59
|
+
"state",
|
|
60
|
+
"tpke",
|
|
61
|
+
"trace",
|
|
62
|
+
"trail",
|
|
63
|
+
"trl",
|
|
64
|
+
"turnpike",
|
|
65
|
+
"us",
|
|
66
|
+
"via",
|
|
67
|
+
"way",
|
|
68
|
+
])
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* One concrete row violating a corpus schema invariant, with exact UTF-16 span substrings.
|
|
72
|
+
*/
|
|
73
|
+
export interface SchemaInvariantSample {
|
|
74
|
+
source_id: string
|
|
75
|
+
source: string
|
|
76
|
+
raw: string
|
|
77
|
+
street_prefix: string
|
|
78
|
+
street: string | null
|
|
79
|
+
offending_token: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Frequency of one offending `street_prefix` string inside a failing source.
|
|
84
|
+
*/
|
|
85
|
+
export interface SchemaInvariantPrefixCount {
|
|
86
|
+
prefix: string
|
|
87
|
+
count: number
|
|
88
|
+
offending_token: string
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Measured denominator, violation count, top offending values, and sample rows for one source.
|
|
93
|
+
*/
|
|
94
|
+
export interface SchemaInvariantSourceReport {
|
|
95
|
+
us_rows: number
|
|
96
|
+
checked_rows: number
|
|
97
|
+
violation_rows: number
|
|
98
|
+
violation_rate: number
|
|
99
|
+
top_prefixes: SchemaInvariantPrefixCount[]
|
|
100
|
+
samples: SchemaInvariantSample[]
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Outcome of one schema invariant across all scanned sources.
|
|
105
|
+
*/
|
|
106
|
+
export interface SchemaInvariantResult {
|
|
107
|
+
id: "us_street_prefix_directional_only"
|
|
108
|
+
status: "pass" | "fail"
|
|
109
|
+
total_us_rows: number
|
|
110
|
+
total_checked_rows: number
|
|
111
|
+
violation_rows: number
|
|
112
|
+
violation_rate: number
|
|
113
|
+
by_source: Record<string, SchemaInvariantSourceReport>
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Complete corpus schema-invariant audit report for one manifest.
|
|
118
|
+
*/
|
|
119
|
+
export interface SchemaInvariantAuditReport {
|
|
120
|
+
corpus_dir: string
|
|
121
|
+
manifest_path: string
|
|
122
|
+
files_scanned: number
|
|
123
|
+
sources_filter: string[] | null
|
|
124
|
+
status: "pass" | "fail"
|
|
125
|
+
invariants: SchemaInvariantResult[]
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Options for {@linkcode auditCorpusSchemaInvariants}.
|
|
130
|
+
*/
|
|
131
|
+
export interface SchemaInvariantAuditOptions {
|
|
132
|
+
corpusDir: PathBuilderLike
|
|
133
|
+
manifestPath?: PathBuilderLike
|
|
134
|
+
/**
|
|
135
|
+
* Optional source filter.
|
|
136
|
+
*
|
|
137
|
+
* An empty collection throws rather than producing a vacuous pass.
|
|
138
|
+
*/
|
|
139
|
+
sources?: readonly string[] | ReadonlySet<string> | null
|
|
140
|
+
sampleLimit?: number
|
|
141
|
+
topPrefixLimit?: number
|
|
142
|
+
limits?: DuckDBLimits
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Returns the first thoroughfare or route token in a US `street_prefix` span, or `null`.
|
|
147
|
+
*
|
|
148
|
+
* Removes periods before tokenizing so `U.S.` folds to `us` and `N.` folds to `n`.
|
|
149
|
+
*
|
|
150
|
+
* @public
|
|
151
|
+
*/
|
|
152
|
+
export function findDisallowedUSStreetPrefixToken(prefix: string): string | null {
|
|
153
|
+
const folded = foldNFKCWhitespace(prefix).replaceAll(".", "")
|
|
154
|
+
|
|
155
|
+
if (!folded) return null
|
|
156
|
+
|
|
157
|
+
const tokens = folded.split(/[^\p{L}\p{N}]+/u).filter((token) => token.length > 0)
|
|
158
|
+
|
|
159
|
+
for (const token of tokens) {
|
|
160
|
+
if (thoroughfareClassOf(token, "us") != null || US_STREET_PREFIX_DISALLOWED_WORDS.has(token)) {
|
|
161
|
+
return token
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return null
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function sqlQuoted(value: string): string {
|
|
169
|
+
return `'${escapeSQLString(value)}'`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function toStringArray(value: unknown): string[] {
|
|
173
|
+
if (Array.isArray(value)) return value.map((entry) => String(entry))
|
|
174
|
+
|
|
175
|
+
if (value && typeof value === "object" && "items" in value) {
|
|
176
|
+
const items = (value as { items?: unknown }).items
|
|
177
|
+
|
|
178
|
+
if (Array.isArray(items)) return items.map((entry) => String(entry))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return []
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function toNumberArray(value: unknown): number[] {
|
|
185
|
+
if (Array.isArray(value)) return value.map((entry) => Number(entry))
|
|
186
|
+
|
|
187
|
+
if (value && typeof value === "object" && "items" in value) {
|
|
188
|
+
const items = (value as { items?: unknown }).items
|
|
189
|
+
|
|
190
|
+
if (Array.isArray(items)) return items.map((entry) => Number(entry))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return []
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Maximum number of missing parquet paths printed inline in an audit refusal error.
|
|
198
|
+
*/
|
|
199
|
+
const MAX_REPORTED_MISSING_PATHS = 5
|
|
200
|
+
|
|
201
|
+
function sliceSpanUTF16(raw: string, tags: string[], starts: number[], ends: number[], target: string): string | null {
|
|
202
|
+
const index = tags.indexOf(target)
|
|
203
|
+
|
|
204
|
+
if (index === -1) return null
|
|
205
|
+
|
|
206
|
+
const start = starts[index]
|
|
207
|
+
const end = ends[index]
|
|
208
|
+
|
|
209
|
+
if (typeof start !== "number" || typeof end !== "number") return null
|
|
210
|
+
|
|
211
|
+
return raw.slice(start, end)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Audits a corpus manifest's parquet files for adapter schema invariants before training.
|
|
216
|
+
*
|
|
217
|
+
* Currently enforces `us_street_prefix_directional_only`: every `US` row's `street_prefix` span
|
|
218
|
+
* must hold only directional or positional pre-modifiers, never thoroughfare types or route words.
|
|
219
|
+
* Non-US rows are excluded because Romance conventions place thoroughfare types
|
|
220
|
+
* (`Rue`, `Via`, `Calle`) in `street_prefix`.
|
|
221
|
+
*
|
|
222
|
+
* @throws When the manifest is missing or lists no files, when any listed parquet file
|
|
223
|
+
* is missing or unreadable, or when `sources` is explicitly empty.
|
|
224
|
+
*/
|
|
225
|
+
export async function auditCorpusSchemaInvariants(
|
|
226
|
+
options: SchemaInvariantAuditOptions
|
|
227
|
+
): Promise<SchemaInvariantAuditReport> {
|
|
228
|
+
const corpusDir = options.corpusDir.toString()
|
|
229
|
+
const manifestPath = (options.manifestPath ?? resolvePath(corpusDir, PARQUET_MANIFEST_FILE)).toString()
|
|
230
|
+
const sampleLimit = options.sampleLimit ?? 5
|
|
231
|
+
const topPrefixLimit = options.topPrefixLimit ?? 15
|
|
232
|
+
|
|
233
|
+
if (!(await isFile(manifestPath))) {
|
|
234
|
+
throw new Error(`Corpus schema-invariant audit requires manifest at ${manifestPath}, but the file is missing.`)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let sourcesFilter: string[] | null = null
|
|
238
|
+
|
|
239
|
+
if (options.sources != null) {
|
|
240
|
+
sourcesFilter = [...new Set(options.sources)].toSorted()
|
|
241
|
+
|
|
242
|
+
if (!sourcesFilter.length) {
|
|
243
|
+
throw new Error("Corpus schema-invariant audit received an empty sources filter.")
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const manifest = await readLocalJSONFile<object>(manifestPath)
|
|
248
|
+
const entries = baseManifestFiles(manifest)
|
|
249
|
+
|
|
250
|
+
if (!entries.length) {
|
|
251
|
+
throw new Error(`Corpus manifest ${manifestPath} lists zero parquet files.`)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const manifestDir = dirname(manifestPath)
|
|
255
|
+
const parquetPaths: string[] = []
|
|
256
|
+
const missingPaths: string[] = []
|
|
257
|
+
|
|
258
|
+
for (const entry of entries) {
|
|
259
|
+
const resolved = resolvePath(manifestDir, localManifestFilePath(entry.path)).toString()
|
|
260
|
+
|
|
261
|
+
if (await isFile(resolved)) {
|
|
262
|
+
parquetPaths.push(resolved)
|
|
263
|
+
} else {
|
|
264
|
+
missingPaths.push(resolved)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (missingPaths.length) {
|
|
269
|
+
const preview = missingPaths.slice(0, MAX_REPORTED_MISSING_PATHS).join(", ")
|
|
270
|
+
|
|
271
|
+
const extra =
|
|
272
|
+
missingPaths.length > MAX_REPORTED_MISSING_PATHS
|
|
273
|
+
? ` (+${missingPaths.length - MAX_REPORTED_MISSING_PATHS} more)`
|
|
274
|
+
: ""
|
|
275
|
+
|
|
276
|
+
throw new Error(
|
|
277
|
+
`Corpus schema-invariant audit cannot read ${missingPaths.length} missing parquet file(s) from ${manifestPath}: ${preview}${extra}`
|
|
278
|
+
)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const fileListSQL = parquetPaths.map(sqlQuoted).join(", ")
|
|
282
|
+
const sourcePredicate = sourcesFilter ? ` AND source IN (${sourcesFilter.map(sqlQuoted).join(", ")})` : ""
|
|
283
|
+
|
|
284
|
+
using db = await openDuckDB(options.limits)
|
|
285
|
+
|
|
286
|
+
const denominatorResult = await db.runAndReadAll(
|
|
287
|
+
`SELECT source, ` +
|
|
288
|
+
`count(*)::BIGINT AS us_rows, ` +
|
|
289
|
+
`count(*) FILTER (WHERE list_contains(span_tags, 'street_prefix'))::BIGINT AS checked_rows ` +
|
|
290
|
+
`FROM read_parquet([${fileListSQL}], union_by_name=true) ` +
|
|
291
|
+
`WHERE country = 'US'${sourcePredicate} ` +
|
|
292
|
+
`GROUP BY source ORDER BY source`
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
const bySource = new Map<string, SchemaInvariantSourceReport>()
|
|
296
|
+
let totalUSRows = 0
|
|
297
|
+
let totalCheckedRows = 0
|
|
298
|
+
|
|
299
|
+
for (const row of denominatorResult.getRowObjects()) {
|
|
300
|
+
const source = String(row.source)
|
|
301
|
+
const usRows = Number(row.us_rows)
|
|
302
|
+
const checkedRows = Number(row.checked_rows)
|
|
303
|
+
|
|
304
|
+
totalUSRows += usRows
|
|
305
|
+
totalCheckedRows += checkedRows
|
|
306
|
+
|
|
307
|
+
bySource.set(source, {
|
|
308
|
+
us_rows: usRows,
|
|
309
|
+
checked_rows: checkedRows,
|
|
310
|
+
violation_rows: 0,
|
|
311
|
+
violation_rate: 0,
|
|
312
|
+
top_prefixes: [],
|
|
313
|
+
samples: [],
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const prefixExpr = componentAtIndexSQL("idx", "raw")
|
|
318
|
+
|
|
319
|
+
const prefixResult = await db.runAndReadAll(
|
|
320
|
+
`WITH us_prefixed AS (` +
|
|
321
|
+
`SELECT source, raw, span_starts, span_ends, list_position(span_tags, 'street_prefix') AS idx ` +
|
|
322
|
+
`FROM read_parquet([${fileListSQL}], union_by_name=true) ` +
|
|
323
|
+
`WHERE country = 'US' AND list_contains(span_tags, 'street_prefix')${sourcePredicate}` +
|
|
324
|
+
`) ` +
|
|
325
|
+
`SELECT source, trim(${prefixExpr}) AS prefix, count(*)::BIGINT AS n ` +
|
|
326
|
+
`FROM us_prefixed GROUP BY source, prefix`
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
const offendingBySource = new Map<string, Map<string, { count: number; token: string }>>()
|
|
330
|
+
const rawOffendingPrefixesBySource = new Map<string, Set<string>>()
|
|
331
|
+
|
|
332
|
+
for (const row of prefixResult.getRowObjects()) {
|
|
333
|
+
const source = String(row.source)
|
|
334
|
+
const rawPrefix = String(row.prefix ?? "")
|
|
335
|
+
const count = Number(row.n)
|
|
336
|
+
const offendingToken = findDisallowedUSStreetPrefixToken(rawPrefix)
|
|
337
|
+
|
|
338
|
+
if (!offendingToken) continue
|
|
339
|
+
|
|
340
|
+
const foldedKey = foldNFKCWhitespace(rawPrefix)
|
|
341
|
+
let sourceMap = offendingBySource.get(source)
|
|
342
|
+
|
|
343
|
+
if (!sourceMap) {
|
|
344
|
+
sourceMap = new Map()
|
|
345
|
+
offendingBySource.set(source, sourceMap)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const prev = sourceMap.get(foldedKey)
|
|
349
|
+
|
|
350
|
+
sourceMap.set(foldedKey, { count: (prev?.count ?? 0) + count, token: offendingToken })
|
|
351
|
+
|
|
352
|
+
let rawSet = rawOffendingPrefixesBySource.get(source)
|
|
353
|
+
|
|
354
|
+
if (!rawSet) {
|
|
355
|
+
rawSet = new Set()
|
|
356
|
+
rawOffendingPrefixesBySource.set(source, rawSet)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
rawSet.add(rawPrefix)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
let totalViolations = 0
|
|
363
|
+
|
|
364
|
+
for (const [source, sourceMap] of offendingBySource) {
|
|
365
|
+
const entry = bySource.get(source)
|
|
366
|
+
|
|
367
|
+
if (!entry) continue
|
|
368
|
+
|
|
369
|
+
const sorted = [...sourceMap.entries()]
|
|
370
|
+
.map(([prefix, info]) => ({ prefix, count: info.count, offending_token: info.token }))
|
|
371
|
+
.toSorted((a, b) => b.count - a.count || a.prefix.localeCompare(b.prefix))
|
|
372
|
+
|
|
373
|
+
const violations = sorted.reduce((sum, item) => sum + item.count, 0)
|
|
374
|
+
|
|
375
|
+
entry.violation_rows = violations
|
|
376
|
+
entry.violation_rate = entry.checked_rows > 0 ? violations / entry.checked_rows : 0
|
|
377
|
+
entry.top_prefixes = sorted.slice(0, topPrefixLimit)
|
|
378
|
+
totalViolations += violations
|
|
379
|
+
|
|
380
|
+
if (sampleLimit > 0) {
|
|
381
|
+
const candidatePrefixes = [...(rawOffendingPrefixesBySource.get(source) ?? [])].slice(0, 64)
|
|
382
|
+
|
|
383
|
+
if (candidatePrefixes.length) {
|
|
384
|
+
const inList = candidatePrefixes.map(sqlQuoted).join(", ")
|
|
385
|
+
const fetchLimit = Math.max(sampleLimit * 3, 15)
|
|
386
|
+
|
|
387
|
+
const sampleResult = await db.runAndReadAll(
|
|
388
|
+
`WITH us_prefixed AS (` +
|
|
389
|
+
`SELECT source, source_id, raw, span_tags, span_starts, span_ends, ` +
|
|
390
|
+
`list_position(span_tags, 'street_prefix') AS idx ` +
|
|
391
|
+
`FROM read_parquet([${fileListSQL}], union_by_name=true) ` +
|
|
392
|
+
`WHERE country = 'US' AND source = ${sqlQuoted(source)} ` +
|
|
393
|
+
`AND list_contains(span_tags, 'street_prefix')` +
|
|
394
|
+
`) ` +
|
|
395
|
+
`SELECT source, source_id, raw, span_tags, span_starts, span_ends ` +
|
|
396
|
+
`FROM us_prefixed ` +
|
|
397
|
+
`WHERE trim(${prefixExpr}) IN (${inList}) ` +
|
|
398
|
+
`LIMIT ${fetchLimit}`
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
for (const row of sampleResult.getRowObjects()) {
|
|
402
|
+
if (entry.samples.length >= sampleLimit) break
|
|
403
|
+
|
|
404
|
+
const raw = String(row.raw ?? "")
|
|
405
|
+
const tags = toStringArray(row.span_tags)
|
|
406
|
+
const starts = toNumberArray(row.span_starts)
|
|
407
|
+
const ends = toNumberArray(row.span_ends)
|
|
408
|
+
const exactPrefix = sliceSpanUTF16(raw, tags, starts, ends, "street_prefix")
|
|
409
|
+
|
|
410
|
+
if (exactPrefix == null) continue
|
|
411
|
+
|
|
412
|
+
const confirmedToken = findDisallowedUSStreetPrefixToken(exactPrefix)
|
|
413
|
+
|
|
414
|
+
if (!confirmedToken) continue
|
|
415
|
+
|
|
416
|
+
entry.samples.push({
|
|
417
|
+
source_id: String(row.source_id ?? ""),
|
|
418
|
+
source: String(row.source ?? ""),
|
|
419
|
+
raw,
|
|
420
|
+
street_prefix: exactPrefix,
|
|
421
|
+
street: sliceSpanUTF16(raw, tags, starts, ends, "street"),
|
|
422
|
+
offending_token: confirmedToken,
|
|
423
|
+
})
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const invariant: SchemaInvariantResult = {
|
|
430
|
+
id: "us_street_prefix_directional_only",
|
|
431
|
+
status: totalViolations === 0 ? "pass" : "fail",
|
|
432
|
+
total_us_rows: totalUSRows,
|
|
433
|
+
total_checked_rows: totalCheckedRows,
|
|
434
|
+
violation_rows: totalViolations,
|
|
435
|
+
violation_rate: totalCheckedRows > 0 ? totalViolations / totalCheckedRows : 0,
|
|
436
|
+
by_source: Object.fromEntries(bySource),
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return {
|
|
440
|
+
corpus_dir: corpusDir,
|
|
441
|
+
manifest_path: manifestPath,
|
|
442
|
+
files_scanned: parquetPaths.length,
|
|
443
|
+
sources_filter: sourcesFilter,
|
|
444
|
+
status: invariant.status,
|
|
445
|
+
invariants: [invariant],
|
|
446
|
+
}
|
|
447
|
+
}
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
*/
|
|
33
33
|
|
|
34
34
|
import { formatAddressRow } from "@mailwoman/codex/address/format"
|
|
35
|
+
import { hashFNV1a } from "@mailwoman/core/coarse-placer/fnv-hash"
|
|
36
|
+
import { isPresent } from "@mailwoman/core/values/objects"
|
|
35
37
|
import { CSVSpliterator } from "spliterator"
|
|
36
38
|
|
|
37
39
|
import { UnsupportedCountryError } from "#adapters/errors"
|
|
@@ -74,6 +76,53 @@ interface BanRow {
|
|
|
74
76
|
nom_commune: string
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Number of hash buckets per 100 rows rendered in French postcode-first or separator formats
|
|
81
|
+
* (5% total: 1% per variant, preserving canonical street-first rendering for 95% of BAN rows).
|
|
82
|
+
*/
|
|
83
|
+
const POSTCODE_FIRST_BUCKET_MODULUS = 100
|
|
84
|
+
const POSTCODE_FIRST_VARIANT_COUNT = 5
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Renders a deterministic 5% share of postcode-carrying, unit-free BAN rows in observed French
|
|
88
|
+
* postcode-first and comma/dash separator shapes, returning `null` for the 95% canonical majority.
|
|
89
|
+
*/
|
|
90
|
+
function maybeFormatPostcodeFirstRow(
|
|
91
|
+
components: CanonicalRow["components"],
|
|
92
|
+
sourceID: string
|
|
93
|
+
): { raw: string; components: CanonicalRow["components"] } | null {
|
|
94
|
+
if (!components.postcode || !components.locality || components.unit) return null
|
|
95
|
+
|
|
96
|
+
const bucket = hashFNV1a(`ban-postcode-first:${sourceID}`) % POSTCODE_FIRST_BUCKET_MODULUS
|
|
97
|
+
|
|
98
|
+
if (bucket >= POSTCODE_FIRST_VARIANT_COUNT) return null
|
|
99
|
+
|
|
100
|
+
const { postcode, locality } = components
|
|
101
|
+
const streetLine = [components.house_number, components.street_prefix, components.street].filter(isPresent).join(" ")
|
|
102
|
+
|
|
103
|
+
if (!streetLine) return null
|
|
104
|
+
|
|
105
|
+
switch (bucket) {
|
|
106
|
+
case 0:
|
|
107
|
+
// `47110, 14 Rue Mozart, Sainte-Livrade-sur-Lot`
|
|
108
|
+
return { raw: `${postcode}, ${streetLine}, ${locality}`, components }
|
|
109
|
+
case 1:
|
|
110
|
+
// `37000, Tours`
|
|
111
|
+
return { raw: `${postcode}, ${locality}`, components: { postcode, locality } }
|
|
112
|
+
case 2:
|
|
113
|
+
// `35650 - Le Rheu`
|
|
114
|
+
return { raw: `${postcode} - ${locality}`, components: { postcode, locality } }
|
|
115
|
+
case 3:
|
|
116
|
+
// `47110, Sainte-Livrade-sur-Lot, 14 Rue Mozart`
|
|
117
|
+
return { raw: `${postcode}, ${locality}, ${streetLine}`, components }
|
|
118
|
+
case 4:
|
|
119
|
+
// `47110 - 14 Rue Mozart, Sainte-Livrade-sur-Lot`
|
|
120
|
+
return { raw: `${postcode} - ${streetLine}, ${locality}`, components }
|
|
121
|
+
default:
|
|
122
|
+
return null
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
77
126
|
export function createBanAdapter(): CorpusAdapter {
|
|
78
127
|
return {
|
|
79
128
|
id: BAN_ADAPTER_ID,
|
|
@@ -167,7 +216,13 @@ export function createBanAdapter(): CorpusAdapter {
|
|
|
167
216
|
components.locality = locality
|
|
168
217
|
}
|
|
169
218
|
|
|
170
|
-
|
|
219
|
+
// BAN repeats an `id` across the postcodes of a merged commune: `27213_0028_00001` is
|
|
220
|
+
// `1 Grande Rue` in each of Vexin-sur-Epte's 27420, 27510 and 27630 villages (#2894).
|
|
221
|
+
// The postcode in the id keeps those addresses apart.
|
|
222
|
+
const recordID = record.id ? `${BAN_ADAPTER_ID}-${record.id}${postcode ? `-${postcode}` : ""}` : null
|
|
223
|
+
const variant = maybeFormatPostcodeFirstRow(components, recordID ?? stableSourceID(BAN_ADAPTER_ID, components))
|
|
224
|
+
|
|
225
|
+
const rendered = variant ?? formatAddressRow(components, country, { singleLine: true })
|
|
171
226
|
|
|
172
227
|
if (!rendered) {
|
|
173
228
|
countDropped(opts, "row:render-failed")
|
|
@@ -176,13 +231,7 @@ export function createBanAdapter(): CorpusAdapter {
|
|
|
176
231
|
}
|
|
177
232
|
|
|
178
233
|
const { raw, components: aligned } = rendered
|
|
179
|
-
|
|
180
|
-
// BAN repeats an `id` across the postcodes of a merged commune: `27213_0028_00001` is
|
|
181
|
-
// `1 Grande Rue` in each of Vexin-sur-Epte's 27420, 27510 and 27630 villages (#2894).
|
|
182
|
-
// The postcode in the id keeps those addresses apart.
|
|
183
|
-
const sourceID = record.id
|
|
184
|
-
? `${BAN_ADAPTER_ID}-${record.id}${postcode ? `-${postcode}` : ""}`
|
|
185
|
-
: stableSourceID(BAN_ADAPTER_ID, aligned)
|
|
234
|
+
const sourceID = recordID ?? stableSourceID(BAN_ADAPTER_ID, aligned)
|
|
186
235
|
|
|
187
236
|
yield {
|
|
188
237
|
raw,
|
|
@@ -77,6 +77,9 @@ const BARE_COMMA_CUTOFF = 0.25
|
|
|
77
77
|
const BARE_COMMA_PC_CUTOFF = 0.5
|
|
78
78
|
const SPACE_PC_CUTOFF = 0.7
|
|
79
79
|
const CANONICAL_PC_FIRST_CUTOFF = 0.85
|
|
80
|
+
// Separator split inside postcode-first commune rows: 50% space, 25% comma, 25% dash.
|
|
81
|
+
const PC_FIRST_SPACE_CUTOFF = 0.5
|
|
82
|
+
const PC_FIRST_COMMA_CUTOFF = 0.75
|
|
80
83
|
// Share of rows with an explicit country component.
|
|
81
84
|
const APPEND_COUNTRY_SHARE = 0.2
|
|
82
85
|
|
|
@@ -104,8 +107,17 @@ function render(random: () => number, c: CommuneRow): AdminSplitVariant {
|
|
|
104
107
|
// Space-delimited region with postcode.
|
|
105
108
|
out = { raw: `${loc} ${dep} ${pc}`, components: { locality: loc, region: dep, postcode: pc }, order: "space-pc" }
|
|
106
109
|
} else if (r < CANONICAL_PC_FIRST_CUTOFF) {
|
|
107
|
-
//
|
|
108
|
-
|
|
110
|
+
// Postcode-first commune forms without département, covering space (`37000 Tours`),
|
|
111
|
+
// comma (`37000, Tours`), and dash (`35650 - Le Rheu`) separators.
|
|
112
|
+
const sepRoll = random()
|
|
113
|
+
|
|
114
|
+
if (sepRoll < PC_FIRST_SPACE_CUTOFF) {
|
|
115
|
+
out = { raw: `${pc} ${loc}`, components: { postcode: pc, locality: loc }, order: "canonical-pc-first" }
|
|
116
|
+
} else if (sepRoll < PC_FIRST_COMMA_CUTOFF) {
|
|
117
|
+
out = { raw: `${pc}, ${loc}`, components: { postcode: pc, locality: loc }, order: "comma-pc-first" }
|
|
118
|
+
} else {
|
|
119
|
+
out = { raw: `${pc} - ${loc}`, components: { postcode: pc, locality: loc }, order: "dash-pc-first" }
|
|
120
|
+
}
|
|
109
121
|
} else {
|
|
110
122
|
// Commune and postcode without département.
|
|
111
123
|
out = { raw: `${loc} ${pc}`, components: { locality: loc, postcode: pc }, order: "commune-pc" }
|
|
@@ -249,7 +249,7 @@ export const frLieuditRecipe: CorpusRecipe = {
|
|
|
249
249
|
// This keeps the byte-stream a function of `countryFraction` rather than of
|
|
250
250
|
// the mixture of jurisdictions in the input files.
|
|
251
251
|
if (countryFraction > 0 && random() < countryFraction) {
|
|
252
|
-
const forms = countryRenderForms(country)
|
|
252
|
+
const forms = countryRenderForms(country, { afterPostcode: Boolean(components.postcode) })
|
|
253
253
|
|
|
254
254
|
if (forms.length) {
|
|
255
255
|
const form = sample(forms, random)
|
package/lib/fr/recipes/order.ts
CHANGED
|
@@ -84,11 +84,14 @@ function renderCanonical(
|
|
|
84
84
|
return { raw, components: { house_number: hn, street, postcode, locality } }
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
// Cumulative cutoffs for
|
|
88
|
-
// Each moves the admin tail ahead of the street or splits it around the street
|
|
87
|
+
// Cumulative cutoffs for five reversed layouts.
|
|
88
|
+
// Each moves the admin tail ahead of the street or splits it around the street, including comma-
|
|
89
|
+
// and dash-separated postcode-first shapes observed in French postcode misses.
|
|
89
90
|
// The training-time `postcode_move` augmentation (#2750) moves the postcode across the locality.
|
|
90
|
-
const REVERSED_VARIANT_A_CUTOFF =
|
|
91
|
-
const REVERSED_VARIANT_B_CUTOFF =
|
|
91
|
+
const REVERSED_VARIANT_A_CUTOFF = 0.2
|
|
92
|
+
const REVERSED_VARIANT_B_CUTOFF = 0.4
|
|
93
|
+
const REVERSED_VARIANT_C_CUTOFF = 0.6
|
|
94
|
+
const REVERSED_VARIANT_D_CUTOFF = 0.8
|
|
92
95
|
|
|
93
96
|
/**
|
|
94
97
|
* Recipe registered with the corpus builder.
|
|
@@ -145,9 +148,15 @@ export const frOrderRecipe: CorpusRecipe = {
|
|
|
145
148
|
} else if (variantRoll < REVERSED_VARIANT_B_CUTOFF) {
|
|
146
149
|
// Locality, postcode, number and street.
|
|
147
150
|
raw = `${locality}, ${postcode}, ${house_number} ${street}`
|
|
148
|
-
} else {
|
|
151
|
+
} else if (variantRoll < REVERSED_VARIANT_C_CUTOFF) {
|
|
149
152
|
// Postcode, number, street, then locality.
|
|
150
153
|
raw = `${postcode}, ${house_number} ${street}, ${locality}`
|
|
154
|
+
} else if (variantRoll < REVERSED_VARIANT_D_CUTOFF) {
|
|
155
|
+
// Postcode, locality, then number and street.
|
|
156
|
+
raw = `${postcode}, ${locality}, ${house_number} ${street}`
|
|
157
|
+
} else {
|
|
158
|
+
// Dash-separated postcode, number and street, then locality.
|
|
159
|
+
raw = `${postcode} - ${house_number} ${street}, ${locality}`
|
|
151
160
|
}
|
|
152
161
|
|
|
153
162
|
rendered = { raw, components: { house_number, street, postcode, locality } }
|
|
@@ -549,7 +549,7 @@ export function applyCountryAppend(
|
|
|
549
549
|
random: () => number
|
|
550
550
|
): void {
|
|
551
551
|
if (countryFraction > 0 && random() < countryFraction) {
|
|
552
|
-
const forms = countryRenderForms(country)
|
|
552
|
+
const forms = countryRenderForms(country, { afterPostcode: Boolean(synth.components.postcode) })
|
|
553
553
|
|
|
554
554
|
if (!forms.length) {
|
|
555
555
|
throw new Error(
|
|
@@ -40,6 +40,7 @@ import { normalizeCJKText } from "#cjk/text"
|
|
|
40
40
|
import {
|
|
41
41
|
availableJPRegisters,
|
|
42
42
|
chooseJPRegister,
|
|
43
|
+
collectBareJPDistrictRows,
|
|
43
44
|
JP_CORPUS_SOURCE,
|
|
44
45
|
JP_LABEL_SET_NAME,
|
|
45
46
|
JP_REGISTER_WEIGHTS,
|
|
@@ -388,15 +389,33 @@ export async function buildJPCorpus(options: JPCorpusOptions): Promise<JPCorpusR
|
|
|
388
389
|
|
|
389
390
|
const splits = new Map<string, CJKSplitReport>()
|
|
390
391
|
|
|
392
|
+
const bareTrainDistricts = collectBareJPDistrictRows(selection.train)
|
|
393
|
+
|
|
394
|
+
for (const record of bareTrainDistricts) {
|
|
395
|
+
verifyJPRecord(record, tagSet)
|
|
396
|
+
}
|
|
397
|
+
|
|
391
398
|
for (const [split, sourceRows] of [
|
|
392
399
|
["train", selection.train],
|
|
393
400
|
["val", selection.val],
|
|
394
401
|
] as const) {
|
|
402
|
+
let emittedTrainRows = 0
|
|
403
|
+
|
|
395
404
|
const report = await writeCJKSplit({
|
|
396
405
|
directory: resolvePath(outDir, split),
|
|
397
406
|
rows: sourceRows,
|
|
398
407
|
rowsPerPart: options.rowsPerPart,
|
|
399
|
-
encodePart: (rows) =>
|
|
408
|
+
encodePart: (rows) => {
|
|
409
|
+
const encoded = encoder.encode(rows)
|
|
410
|
+
|
|
411
|
+
emittedTrainRows += rows.length
|
|
412
|
+
|
|
413
|
+
if (split === "train" && bareTrainDistricts.length && emittedTrainRows === sourceRows.length) {
|
|
414
|
+
return [...encoded, ...bareTrainDistricts]
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return encoded
|
|
418
|
+
},
|
|
400
419
|
partPrefix: "part",
|
|
401
420
|
context: "JP corpus assembly",
|
|
402
421
|
statsSample: { kind: CJKStatsSample.FirstOfEachPart, rows: options.statsSamplePerPart },
|