@mailwoman/corpus 7.2.0 → 7.2.1
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/package.json +59 -27
- package/src/adapter.ts +153 -0
- package/src/adapters/ban/adapter.ts +189 -0
- package/src/adapters/ban/street-decompose.ts +98 -0
- package/src/adapters/fcc-bdc/adapter.ts +188 -0
- package/src/adapters/geonames/adapter.ts +185 -0
- package/src/adapters/geonames-postal/adapter.ts +111 -0
- package/src/adapters/gnaf/adapter.ts +139 -0
- package/src/adapters/gnaf/assemble.ts +200 -0
- package/src/adapters/index.ts +135 -0
- package/src/adapters/openaddresses/adapter.ts +237 -0
- package/src/adapters/overture/adapter.ts +149 -0
- package/src/adapters/state-hi-schools/adapter.ts +171 -0
- package/src/adapters/state-ia-contractors/adapter.ts +148 -0
- package/src/adapters/state-ny-notaries/adapter.ts +169 -0
- package/src/adapters/state-tx-notaries/adapter.ts +160 -0
- package/src/adapters/synth-po-box/adapter.ts +197 -0
- package/src/adapters/tiger/adapter.ts +232 -0
- package/src/adapters/tiger/street-decompose.ts +120 -0
- package/src/adapters/usgov-hrsa-fqhc/adapter.ts +189 -0
- package/src/adapters/usgov-imls-pls/adapter.ts +152 -0
- package/src/adapters/usgov-irs-bmf/adapter.ts +155 -0
- package/src/adapters/usgov-nad/adapter.ts +338 -0
- package/src/adapters/usgov-nppes/adapter.ts +169 -0
- package/src/adapters/usgov-samhsa-treatment-locator/adapter.ts +211 -0
- package/src/adapters/wof-admin-jp/adapter.ts +176 -0
- package/src/adapters/wof-admin-json/adapter.ts +280 -0
- package/src/adapters/wof-postalcode-json/adapter.ts +209 -0
- package/src/align.ts +317 -0
- package/src/build.ts +348 -0
- package/src/codex/us-fips-state.ts +109 -0
- package/src/format.ts +14 -0
- package/src/golden.ts +147 -0
- package/src/index.ts +28 -0
- package/src/license.ts +48 -0
- package/src/parquet-wrapper/index.ts +9 -0
- package/src/parquet-wrapper/reader.ts +67 -0
- package/src/parquet-wrapper/schema.ts +80 -0
- package/src/parquet-wrapper/writer.ts +102 -0
- package/src/parquet.ts +360 -0
- package/src/runner.ts +283 -0
- package/src/shard-recipes/anchor-absorption.ts +86 -0
- package/src/shard-recipes/boundary-stress.ts +87 -0
- package/src/shard-recipes/country-balanced.ts +471 -0
- package/src/shard-recipes/cz-pcfirst-preposition.ts +83 -0
- package/src/shard-recipes/fr-admin-split.ts +212 -0
- package/src/shard-recipes/fr-bare-street.ts +85 -0
- package/src/shard-recipes/fr-fragment.ts +304 -0
- package/src/shard-recipes/fr-order.ts +283 -0
- package/src/shard-recipes/german.ts +206 -0
- package/src/shard-recipes/house-venue.ts +78 -0
- package/src/shard-recipes/index.ts +76 -0
- package/src/shard-recipes/intersection.ts +617 -0
- package/src/shard-recipes/locale.ts +415 -0
- package/src/shard-recipes/nl-postcode.ts +105 -0
- package/src/shard-recipes/no-fragment.ts +206 -0
- package/src/shard-recipes/no-street-led.ts +130 -0
- package/src/shard-recipes/no-street.ts +78 -0
- package/src/shard-recipes/po-box-cedex.ts +853 -0
- package/src/shard-recipes/po-box.ts +111 -0
- package/src/shard-recipes/scaffold.ts +187 -0
- package/src/shard-recipes/si-bare-village.ts +88 -0
- package/src/shard-recipes/street-affix.ts +549 -0
- package/src/shard-recipes/street-bare.ts +90 -0
- package/src/shard-recipes/street.ts +74 -0
- package/src/shard-recipes/unit.ts +315 -0
- package/src/split.ts +251 -0
- package/src/synthesize-anchor-absorption.ts +263 -0
- package/src/synthesize-boundary-stress.ts +504 -0
- package/src/synthesize-german.ts +227 -0
- package/src/synthesize-house-venue.ts +192 -0
- package/src/synthesize-intersection.ts +205 -0
- package/src/synthesize-no-street.ts +341 -0
- package/src/synthesize-po-box.ts +307 -0
- package/src/synthesize-street.ts +264 -0
- package/src/synthesize.ts +872 -0
- package/src/tokenize.ts +68 -0
- package/src/tools/align-shard.ts +67 -0
- package/src/tools/audit.ts +352 -0
- package/src/tools/corpus-stats.ts +183 -0
- package/src/tools/fetch/ban.ts +288 -0
- package/src/tools/fetch/download.ts +119 -0
- package/src/tools/fetch/hrsa.ts +65 -0
- package/src/tools/fetch/imls-pls.ts +162 -0
- package/src/tools/fetch/index.ts +121 -0
- package/src/tools/fetch/nad.ts +304 -0
- package/src/tools/fetch/nppes.ts +177 -0
- package/src/tools/fetch/openaddresses.ts +380 -0
- package/src/tools/fetch/state-hi-schools.ts +217 -0
- package/src/tools/fetch/state-sources.ts +142 -0
- package/src/tools/fetch/tiger-full.ts +318 -0
- package/src/tools/golden-expand.ts +524 -0
- package/src/tools/golden-promote.ts +304 -0
- package/src/tools/index.ts +24 -0
- package/src/tools/ingest-csv.ts +420 -0
- package/src/tools/jsonl-to-parquet.ts +200 -0
- package/src/tools/lint-shard-vocab.ts +357 -0
- package/src/tools/lint-shard.ts +502 -0
- package/src/tools/overlay-manifest.ts +141 -0
- package/src/tools/shard-kryptonite.ts +131 -0
- package/src/tools/shard-translit.ts +244 -0
- package/src/types.ts +213 -0
- package/src/wof-json.ts +236 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* `fcc-bdc`: FCC Broadband Data Collection (BDC) — Fabric-derived location consumer.
|
|
7
|
+
*
|
|
8
|
+
* The first member of Phase 1.6's "adversarial sources" class. BDC ships the public-domain US
|
|
9
|
+
* broadband-serviceable-location (BSL) fabric: ~120M addresses keyed by stable `location_id`,
|
|
10
|
+
* carrying `address_primary` + `city` + `state` + `zip` + `zip_suffix`. Compared to the clean
|
|
11
|
+
* gazetteer rows from WOF / TIGER / BAN, BDC carries the chaos of address data that has passed
|
|
12
|
+
* through several layers of human entry + automated geocoding + revision: abbreviation drift,
|
|
13
|
+
* inconsistent unit designators, "RR" / "HC" / "PSC" rural-route shapes, embedded apartment /
|
|
14
|
+
* suite numbers that did not survive the address parser cleanly. This is the highest-signal,
|
|
15
|
+
* hardest-to-normalize address corpus in the federal public-domain catalog.
|
|
16
|
+
*
|
|
17
|
+
* Following the `tiger` / `wof-admin` pattern, this adapter consumes a SQLite database the operator
|
|
18
|
+
* pre-builds via the isp-nexus BDC ETL (`/srv/isp-nexus/sync/fcc/bdc/`) or any equivalent
|
|
19
|
+
* host-side pipeline. The mailwoman side does not download or parse the raw CSV/ZIP distribution
|
|
20
|
+
* directly — that keeps the adapter narrow and the BDC ingest pluggable.
|
|
21
|
+
*
|
|
22
|
+
* The SQLite schema is documented in README.md and modeled after `NTIARecord`
|
|
23
|
+
* (`isp-nexus/fcc/bdc/data-collection.ts`): one row per `location_id`. The adapter splits
|
|
24
|
+
* `address_primary` into `house_number` (leading numeric prefix, if any) + `street` (everything
|
|
25
|
+
* after), and combines `zip` + `zip_suffix` into the canonical USPS `postcode` slot.
|
|
26
|
+
*
|
|
27
|
+
* One CanonicalRow per fabric record. Unlike `tiger` (multiple postcode variants per segment) or
|
|
28
|
+
* `wof-admin` (multiple hierarchy variants per place), BDC records already represent fully
|
|
29
|
+
* specified addresses; no fan-out is warranted. Adversarial composition (Phase 1.6 §2.1) is the
|
|
30
|
+
* mechanism for deriving multiple training rows per BDC record.
|
|
31
|
+
*
|
|
32
|
+
* License: stamped `"Public Domain"` per the BDC fabric's US federal-government distribution terms.
|
|
33
|
+
* The CostQuest Fabric source data has its own license; consumers who substitute that path should
|
|
34
|
+
* re-stamp accordingly.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import { DatabaseSync } from "node:sqlite"
|
|
38
|
+
|
|
39
|
+
import { lookupStateAbbreviation } from "../../codex/us-fips-state.ts"
|
|
40
|
+
import { formatAddress, reconcileComponents } from "../../format.ts"
|
|
41
|
+
import type { AdapterOptions, CanonicalRow, CorpusAdapter } from "../../types.ts"
|
|
42
|
+
|
|
43
|
+
export const FCC_BDC_ADAPTER_ID = "fcc-bdc"
|
|
44
|
+
export const FCC_BDC_DEFAULT_LICENSE = "Public Domain"
|
|
45
|
+
|
|
46
|
+
/** SQLite row shape — one row per BSL `location_id`. Columns mirror NTIARecord. */
|
|
47
|
+
interface BdcLocationRow {
|
|
48
|
+
location_id: number
|
|
49
|
+
address_primary: string
|
|
50
|
+
city: string
|
|
51
|
+
state: string
|
|
52
|
+
zip: string
|
|
53
|
+
zip_suffix: string | null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Split `address_primary` into a `(house_number, street)` pair.
|
|
58
|
+
*
|
|
59
|
+
* BDC's `address_primary` follows USPS Publication 28 conventions but with hand-entry drift. The canonical
|
|
60
|
+
* leading-digit prefix is the house number (`"123 Main St"`, `"6450 W Indian School Rd"`, even hyphenated forms `"40-12
|
|
61
|
+
* Bell Blvd"`). Anything that doesn't match the prefix shape (`"PO Box 1234"`, `"RR 2 Box 67"`, `"HC 1"`) is left as a
|
|
62
|
+
* single `street` value — the model sees the original surface form, and downstream classifiers/po-box handling can pick
|
|
63
|
+
* it up.
|
|
64
|
+
*
|
|
65
|
+
* The regex tolerates one trailing letter (`"123A Main St"`) and an optional hyphenated half (`"40-12"`) which is
|
|
66
|
+
* common in NYC + suburban garden-apartment numbering.
|
|
67
|
+
*/
|
|
68
|
+
const HOUSE_NUMBER_PREFIX = /^(\d+(?:-\d+)?[A-Za-z]?)\s+(.+)$/
|
|
69
|
+
|
|
70
|
+
interface SplitAddress {
|
|
71
|
+
house_number?: string
|
|
72
|
+
street: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function splitAddressPrimary(address: string): SplitAddress | null {
|
|
76
|
+
const trimmed = address.trim()
|
|
77
|
+
|
|
78
|
+
if (!trimmed) return null
|
|
79
|
+
const match = HOUSE_NUMBER_PREFIX.exec(trimmed)
|
|
80
|
+
|
|
81
|
+
if (match) {
|
|
82
|
+
return { house_number: match[1], street: match[2]!.trim() }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { street: trimmed }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Combine `zip` + optional `zip_suffix` into the canonical USPS postcode surface form.
|
|
90
|
+
*
|
|
91
|
+
* NTIARecord doc is ambiguous about whether `zip_suffix` is the 4-digit extension alone or the full ZIP+4 string. This
|
|
92
|
+
* handles both:
|
|
93
|
+
*
|
|
94
|
+
* - Bare 4-digit extension (`zip="94103"`, `zip_suffix="1234"`) → `"94103-1234"`
|
|
95
|
+
* - Already-joined form (`zip_suffix="94103-1234"`) → returned as-is
|
|
96
|
+
* - No suffix → bare `zip`
|
|
97
|
+
*
|
|
98
|
+
* Empty / whitespace-only suffix is treated as missing.
|
|
99
|
+
*/
|
|
100
|
+
export function buildPostcode(zip: string, suffix: string | null): string {
|
|
101
|
+
const z = zip.trim()
|
|
102
|
+
|
|
103
|
+
if (!z) return ""
|
|
104
|
+
const s = suffix?.trim() ?? ""
|
|
105
|
+
|
|
106
|
+
if (!s) return z
|
|
107
|
+
|
|
108
|
+
if (s.includes("-")) return s
|
|
109
|
+
|
|
110
|
+
return `${z}-${s}`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Build a BDC adapter. Pure factory so multiple instances can be created in tests. */
|
|
114
|
+
export function createFccBdcAdapter(): CorpusAdapter {
|
|
115
|
+
return {
|
|
116
|
+
id: FCC_BDC_ADAPTER_ID,
|
|
117
|
+
defaultLicense: FCC_BDC_DEFAULT_LICENSE,
|
|
118
|
+
description:
|
|
119
|
+
"FCC Broadband Data Collection — Fabric-derived BSL addresses (public-domain); SQLite DB the operator builds via the isp-nexus BDC ETL.",
|
|
120
|
+
|
|
121
|
+
async *rows(opts: AdapterOptions): AsyncIterable<CanonicalRow> {
|
|
122
|
+
if (opts.country && opts.country !== "US") {
|
|
123
|
+
throw new Error(`fcc-bdc adapter: only US supported, got country=${opts.country}`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const db = new DatabaseSync(opts.inputPath, { readOnly: true })
|
|
127
|
+
let emitted = 0
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
const stmt = db.prepare(
|
|
131
|
+
`SELECT location_id, address_primary, city, state, zip, zip_suffix
|
|
132
|
+
FROM bdc_locations
|
|
133
|
+
ORDER BY location_id`
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
for (const row of stmt.iterate() as IterableIterator<BdcLocationRow>) {
|
|
137
|
+
if (opts.signal?.aborted) return
|
|
138
|
+
|
|
139
|
+
if (opts.limit !== undefined && emitted >= opts.limit) return
|
|
140
|
+
|
|
141
|
+
const split = splitAddressPrimary(row.address_primary ?? "")
|
|
142
|
+
|
|
143
|
+
if (!split) continue
|
|
144
|
+
const state = lookupStateAbbreviation(row.state)
|
|
145
|
+
|
|
146
|
+
if (!state) continue
|
|
147
|
+
const locality = row.city?.trim()
|
|
148
|
+
|
|
149
|
+
if (!locality) continue
|
|
150
|
+
const postcode = buildPostcode(row.zip ?? "", row.zip_suffix ?? null)
|
|
151
|
+
|
|
152
|
+
if (!postcode) continue
|
|
153
|
+
|
|
154
|
+
const components: CanonicalRow["components"] = {
|
|
155
|
+
...(split.house_number ? { house_number: split.house_number } : {}),
|
|
156
|
+
street: split.street,
|
|
157
|
+
locality,
|
|
158
|
+
region: state.abbreviation,
|
|
159
|
+
postcode,
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const raw = formatAddress(components, "US", { separator: ", " })
|
|
163
|
+
|
|
164
|
+
if (!raw) continue
|
|
165
|
+
const aligned = reconcileComponents(components, raw)
|
|
166
|
+
|
|
167
|
+
if (Object.keys(aligned).length === 0) continue
|
|
168
|
+
|
|
169
|
+
yield {
|
|
170
|
+
raw,
|
|
171
|
+
components: aligned,
|
|
172
|
+
country: "US",
|
|
173
|
+
locale: "en-US",
|
|
174
|
+
source: FCC_BDC_ADAPTER_ID,
|
|
175
|
+
source_id: `${FCC_BDC_ADAPTER_ID}-${row.location_id}`,
|
|
176
|
+
corpus_version: "",
|
|
177
|
+
license: FCC_BDC_DEFAULT_LICENSE,
|
|
178
|
+
}
|
|
179
|
+
emitted++
|
|
180
|
+
}
|
|
181
|
+
} finally {
|
|
182
|
+
db.close()
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export const fccBdcAdapter = createFccBdcAdapter()
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* `geonames`: GeoNames populated-places consumer (https://www.geonames.org/, CC-BY-4.0).
|
|
7
|
+
*
|
|
8
|
+
* GeoNames is a global gazetteer of ~12M features. This adapter ingests the POPULATED PLACES
|
|
9
|
+
* (`feature_class = "P"`, excluding historical/abandoned/destroyed variants) from a per-country
|
|
10
|
+
* dump file — global locality coverage, including the small towns and villages a coarser admin
|
|
11
|
+
* gazetteer (WOF) lacks. It's the cheapest path to broadening the corpus's LOCALE coverage.
|
|
12
|
+
*
|
|
13
|
+
* Input: a per-country tab-separated dump (e.g. `US.txt` from
|
|
14
|
+
* `https://download.geonames.org/export/dump/`, 19 columns, no header). Two sibling files in the
|
|
15
|
+
* same directory supply human-readable names (downloaded once from the same place):
|
|
16
|
+
*
|
|
17
|
+
* - `admin1CodesASCII.txt` — `<CC>.<admin1_code>` → region name (e.g. `US.VT` → "Vermont").
|
|
18
|
+
* - `countryInfo.txt` — ISO alpha-2 → country name (e.g. `US` → "United States"); `#`-commented. If a
|
|
19
|
+
* sibling is missing, the corresponding component is simply omitted (graceful degradation).
|
|
20
|
+
*
|
|
21
|
+
* Output: per place, up to two hierarchy variants (mirroring `wof-admin`'s with/without-country
|
|
22
|
+
* balance so the model sees both domestic and international order) —
|
|
23
|
+
*
|
|
24
|
+
* 1. `{ locality, region }` → "City, Region"
|
|
25
|
+
* 2. `{ locality, region, country }` → "City, Region, Country" `reconcileComponents` drops any
|
|
26
|
+
* component that didn't survive into the rendered `raw`.
|
|
27
|
+
*
|
|
28
|
+
* License: stamped `"CC-BY-4.0"` per row (GeoNames' terms); provenance is the `geonames-<id>` key.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { createReadStream, existsSync, readFileSync } from "node:fs"
|
|
32
|
+
import { dirname, join } from "node:path"
|
|
33
|
+
|
|
34
|
+
import { parse as csvParse } from "csv-parse"
|
|
35
|
+
|
|
36
|
+
import { stableSourceID } from "../../adapter.ts"
|
|
37
|
+
import { reconcileComponents } from "../../format.ts"
|
|
38
|
+
import type { AdapterOptions, CanonicalRow, CorpusAdapter } from "../../types.ts"
|
|
39
|
+
|
|
40
|
+
export const GEONAMES_ADAPTER_ID = "geonames"
|
|
41
|
+
export const GEONAMES_DEFAULT_LICENSE = "CC-BY-4.0"
|
|
42
|
+
|
|
43
|
+
// GeoNames main-table column indices (0-based; see the export README).
|
|
44
|
+
const COL = {
|
|
45
|
+
geonameid: 0,
|
|
46
|
+
name: 1,
|
|
47
|
+
alternatenames: 3,
|
|
48
|
+
featureClass: 6,
|
|
49
|
+
featureCode: 7,
|
|
50
|
+
country: 8,
|
|
51
|
+
admin1: 10,
|
|
52
|
+
} as const
|
|
53
|
+
|
|
54
|
+
// Populated-place feature codes that are NOT current real places — skip them.
|
|
55
|
+
const NON_CURRENT_PPL = new Set(["PPLH", "PPLQ", "PPLW", "PPLCH"])
|
|
56
|
+
|
|
57
|
+
/** Load `admin1CodesASCII.txt` → Map("<CC>.<admin1>" → region name). Empty map if absent. */
|
|
58
|
+
function loadAdmin1(dir: string): Map<string, string> {
|
|
59
|
+
const map = new Map<string, string>()
|
|
60
|
+
const fp = join(dir, "admin1CodesASCII.txt")
|
|
61
|
+
|
|
62
|
+
if (!existsSync(fp)) return map
|
|
63
|
+
|
|
64
|
+
for (const line of readFileSync(fp, "utf8").split("\n")) {
|
|
65
|
+
if (!line) continue
|
|
66
|
+
const cols = line.split("\t")
|
|
67
|
+
|
|
68
|
+
if (cols[0] && cols[1]) {
|
|
69
|
+
map.set(cols[0], cols[1])
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return map
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Load `countryInfo.txt` → Map(ISO → country name). Empty map if absent. The file is `#`-commented. */
|
|
77
|
+
function loadCountries(dir: string): Map<string, string> {
|
|
78
|
+
const map = new Map<string, string>()
|
|
79
|
+
const fp = join(dir, "countryInfo.txt")
|
|
80
|
+
|
|
81
|
+
if (!existsSync(fp)) return map
|
|
82
|
+
|
|
83
|
+
for (const line of readFileSync(fp, "utf8").split("\n")) {
|
|
84
|
+
if (!line || line.startsWith("#")) continue
|
|
85
|
+
const cols = line.split("\t")
|
|
86
|
+
|
|
87
|
+
// ISO(0), ISO3(1), iso-numeric(2), fips(3), Country(4), ...
|
|
88
|
+
if (cols[0] && cols[4]) {
|
|
89
|
+
map.set(cols[0], cols[4])
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return map
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function createGeonamesAdapter(): CorpusAdapter {
|
|
97
|
+
return {
|
|
98
|
+
id: GEONAMES_ADAPTER_ID,
|
|
99
|
+
defaultLicense: GEONAMES_DEFAULT_LICENSE,
|
|
100
|
+
description:
|
|
101
|
+
"GeoNames populated places (CC-BY-4.0) — global locality coverage incl. small towns, with region/country names from the sibling admin1/countryInfo files.",
|
|
102
|
+
|
|
103
|
+
async *rows(opts: AdapterOptions): AsyncIterable<CanonicalRow> {
|
|
104
|
+
const dir = dirname(opts.inputPath)
|
|
105
|
+
const admin1 = loadAdmin1(dir)
|
|
106
|
+
const countries = loadCountries(dir)
|
|
107
|
+
|
|
108
|
+
const stream = createReadStream(opts.inputPath, { encoding: "utf8" })
|
|
109
|
+
const parser = stream.pipe(
|
|
110
|
+
csvParse({ delimiter: "\t", quote: false, relax_column_count: true, skip_empty_lines: true })
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
let emitted = 0
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
for await (const rec of parser as AsyncIterable<string[]>) {
|
|
117
|
+
if (opts.signal?.aborted) break
|
|
118
|
+
|
|
119
|
+
if (opts.limit !== undefined && emitted >= opts.limit) break
|
|
120
|
+
|
|
121
|
+
if (rec[COL.featureClass] !== "P") continue
|
|
122
|
+
|
|
123
|
+
if (NON_CURRENT_PPL.has(rec[COL.featureCode] ?? "")) continue
|
|
124
|
+
|
|
125
|
+
const cc = (rec[COL.country] ?? "").trim()
|
|
126
|
+
|
|
127
|
+
if (!cc) continue
|
|
128
|
+
|
|
129
|
+
if (opts.country && cc !== opts.country) continue
|
|
130
|
+
|
|
131
|
+
const locality = (rec[COL.name] ?? "").trim()
|
|
132
|
+
|
|
133
|
+
if (!locality) continue
|
|
134
|
+
const geonameid = (rec[COL.geonameid] ?? "").trim()
|
|
135
|
+
const region = admin1.get(`${cc}.${(rec[COL.admin1] ?? "").trim()}`)
|
|
136
|
+
const country = countries.get(cc)
|
|
137
|
+
|
|
138
|
+
// Two hierarchy variants (domestic + international order) — but only emit the
|
|
139
|
+
// distinct ones the available names support.
|
|
140
|
+
const variants: Array<{ slot: string; comp: CanonicalRow["components"]; raw: string }> = []
|
|
141
|
+
|
|
142
|
+
if (region) {
|
|
143
|
+
variants.push({ slot: "lr", comp: { locality, region }, raw: `${locality}, ${region}` })
|
|
144
|
+
|
|
145
|
+
if (country) {
|
|
146
|
+
variants.push({
|
|
147
|
+
slot: "lrc",
|
|
148
|
+
comp: { locality, region, country },
|
|
149
|
+
raw: `${locality}, ${region}, ${country}`,
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
} else if (country) {
|
|
153
|
+
variants.push({ slot: "lc", comp: { locality, country }, raw: `${locality}, ${country}` })
|
|
154
|
+
} else {
|
|
155
|
+
variants.push({ slot: "l", comp: { locality }, raw: locality })
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
for (const v of variants) {
|
|
159
|
+
if (opts.limit !== undefined && emitted >= opts.limit) break
|
|
160
|
+
const aligned = reconcileComponents(v.comp, v.raw)
|
|
161
|
+
|
|
162
|
+
if (Object.keys(aligned).length === 0) continue
|
|
163
|
+
const sourceID = geonameid
|
|
164
|
+
? `${GEONAMES_ADAPTER_ID}-${geonameid}-${v.slot}`
|
|
165
|
+
: stableSourceID(GEONAMES_ADAPTER_ID, aligned)
|
|
166
|
+
yield {
|
|
167
|
+
raw: v.raw,
|
|
168
|
+
components: aligned,
|
|
169
|
+
country: cc,
|
|
170
|
+
source: GEONAMES_ADAPTER_ID,
|
|
171
|
+
source_id: sourceID,
|
|
172
|
+
corpus_version: "",
|
|
173
|
+
license: GEONAMES_DEFAULT_LICENSE,
|
|
174
|
+
}
|
|
175
|
+
emitted++
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} finally {
|
|
179
|
+
stream.destroy()
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export const geonamesAdapter = createGeonamesAdapter()
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* `geonames-postal`: GeoNames postal-code dump consumer (https://www.geonames.org/, CC-BY-4.0).
|
|
7
|
+
*
|
|
8
|
+
* The GeoNames postal export (`https://download.geonames.org/export/zip/<CC>.zip`) is a clean,
|
|
9
|
+
* per-country `postcode → place → admin1` table with the place + region NAMES inline (no aux-file
|
|
10
|
+
* join needed). It broadens the corpus's postcode→locality→region coverage to ~80 countries, well
|
|
11
|
+
* beyond `wof-postalcode`/the coordinate-first table — forward coverage for the multi-locale
|
|
12
|
+
* goal.
|
|
13
|
+
*
|
|
14
|
+
* Input: a per-country postal dump (`<CC>.txt`, 12 tab-separated columns, no header): country,
|
|
15
|
+
* postcode, place, admin1_name, admin1_code, admin2__, admin3__, lat, lon, accuracy.
|
|
16
|
+
*
|
|
17
|
+
* Output: per row, postcode-FIRST (international) variants — the common order for the non-US
|
|
18
|
+
* locales this fills (US postcodes are already covered by TIGER/WOF, which use postcode-LAST):
|
|
19
|
+
*
|
|
20
|
+
* 1. `{ postcode, locality }` → "AD100 Canillo"
|
|
21
|
+
* 2. `{ postcode, locality, region }` → "AD100 Canillo, Canillo" Prefer configuring this adapter for
|
|
22
|
+
* non-US countries; for US, the postcode-last sources are the right order. License:
|
|
23
|
+
* `"CC-BY-4.0"` per row (attribute "GeoNames").
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { createReadStream } from "node:fs"
|
|
27
|
+
|
|
28
|
+
import { parse as csvParse } from "csv-parse"
|
|
29
|
+
|
|
30
|
+
import { stableSourceID } from "../../adapter.ts"
|
|
31
|
+
import { reconcileComponents } from "../../format.ts"
|
|
32
|
+
import type { AdapterOptions, CanonicalRow, CorpusAdapter } from "../../types.ts"
|
|
33
|
+
|
|
34
|
+
export const GEONAMES_POSTAL_ADAPTER_ID = "geonames-postal"
|
|
35
|
+
export const GEONAMES_POSTAL_DEFAULT_LICENSE = "CC-BY-4.0"
|
|
36
|
+
|
|
37
|
+
// GeoNames postal-dump columns (0-based).
|
|
38
|
+
const COL = { country: 0, postcode: 1, place: 2, admin1Name: 3 } as const
|
|
39
|
+
|
|
40
|
+
export function createGeonamesPostalAdapter(): CorpusAdapter {
|
|
41
|
+
return {
|
|
42
|
+
id: GEONAMES_POSTAL_ADAPTER_ID,
|
|
43
|
+
defaultLicense: GEONAMES_POSTAL_DEFAULT_LICENSE,
|
|
44
|
+
description:
|
|
45
|
+
"GeoNames postal codes (CC-BY-4.0) — multi-locale postcode→locality→region, names inline; international postcode-first order.",
|
|
46
|
+
|
|
47
|
+
async *rows(opts: AdapterOptions): AsyncIterable<CanonicalRow> {
|
|
48
|
+
const stream = createReadStream(opts.inputPath, { encoding: "utf8" })
|
|
49
|
+
const parser = stream.pipe(
|
|
50
|
+
csvParse({ delimiter: "\t", quote: false, relax_column_count: true, skip_empty_lines: true })
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
let emitted = 0
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
for await (const rec of parser as AsyncIterable<string[]>) {
|
|
57
|
+
if (opts.signal?.aborted) break
|
|
58
|
+
|
|
59
|
+
if (opts.limit !== undefined && emitted >= opts.limit) break
|
|
60
|
+
|
|
61
|
+
const cc = (rec[COL.country] ?? "").trim()
|
|
62
|
+
|
|
63
|
+
if (!cc) continue
|
|
64
|
+
|
|
65
|
+
if (opts.country && cc !== opts.country) continue
|
|
66
|
+
|
|
67
|
+
const postcode = (rec[COL.postcode] ?? "").trim()
|
|
68
|
+
const locality = (rec[COL.place] ?? "").trim()
|
|
69
|
+
|
|
70
|
+
if (!postcode || !locality) continue
|
|
71
|
+
const region = (rec[COL.admin1Name] ?? "").trim()
|
|
72
|
+
|
|
73
|
+
// Postcode-first (international) variants. Skip the region variant when admin1 just
|
|
74
|
+
// repeats the place (common for city-states / micro-admin) to avoid "X X" noise.
|
|
75
|
+
const variants: Array<{ slot: string; comp: CanonicalRow["components"]; raw: string }> = [
|
|
76
|
+
{ slot: "pl", comp: { postcode, locality }, raw: `${postcode} ${locality}` },
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
if (region && region.toLowerCase() !== locality.toLowerCase()) {
|
|
80
|
+
variants.push({
|
|
81
|
+
slot: "plr",
|
|
82
|
+
comp: { postcode, locality, region },
|
|
83
|
+
raw: `${postcode} ${locality}, ${region}`,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const v of variants) {
|
|
88
|
+
if (opts.limit !== undefined && emitted >= opts.limit) break
|
|
89
|
+
const aligned = reconcileComponents(v.comp, v.raw)
|
|
90
|
+
|
|
91
|
+
if (Object.keys(aligned).length < 2) continue
|
|
92
|
+
yield {
|
|
93
|
+
raw: v.raw,
|
|
94
|
+
components: aligned,
|
|
95
|
+
country: cc,
|
|
96
|
+
source: GEONAMES_POSTAL_ADAPTER_ID,
|
|
97
|
+
source_id: `${stableSourceID(GEONAMES_POSTAL_ADAPTER_ID, aligned)}-${v.slot}`,
|
|
98
|
+
corpus_version: "",
|
|
99
|
+
license: GEONAMES_POSTAL_DEFAULT_LICENSE,
|
|
100
|
+
}
|
|
101
|
+
emitted++
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} finally {
|
|
105
|
+
stream.destroy()
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const geonamesPostalAdapter = createGeonamesPostalAdapter()
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* G-NAF (Australia) corpus adapter — the parser-teaching half of #208.
|
|
7
|
+
*
|
|
8
|
+
* The model mis-parses Australian addresses in their native postcode-first / house-number-last
|
|
9
|
+
* order: it tags a leading 4-digit postcode as a house number (its US/EU prior) and swaps street
|
|
10
|
+
* ↔ locality with it. `scripts/eval/au-order-probe.ts` proved this is a word-ORDER coverage gap,
|
|
11
|
+
* not capability — the same addresses parse perfectly in canonical order (65% → 87% @25km if the
|
|
12
|
+
* parse were order-robust). EU survives the same eval because its postcodes are
|
|
13
|
+
* format-distinctive (a hyphenated `26-300` reads as a postcode anywhere); a bare AU `3053` only
|
|
14
|
+
* disambiguates by position.
|
|
15
|
+
*
|
|
16
|
+
* So this adapter renders each assembled G-NAF tuple (from {@link ./assemble}) in one of three real
|
|
17
|
+
* AU layouts — real-AU canonical (number-first, postcode-trailing), postcode-first,
|
|
18
|
+
* locality-first — ROTATED by row index (`i % 3`), so the locality + postcode each land in every
|
|
19
|
+
* position across the shard. This is the exact mechanism that fixed #148's v1.9.0 order-overfit
|
|
20
|
+
* for the 16 EU locales (`scripts/rerender-overture-multiorder.mjs`, v1.9.1 → shipped v4.13.0);
|
|
21
|
+
* AU was simply never in that train (`country_weights` had no AU, and `data_loader.py` excludes
|
|
22
|
+
* unlisted countries). Rotating one order per row (rather than emitting all three) keeps this a
|
|
23
|
+
* clean single-variable extension of the proven recipe + matches its source-mass structure. The
|
|
24
|
+
* corpus aligner BIO-labels each (every component surface form occurs verbatim in `raw`, so
|
|
25
|
+
* alignment lands).
|
|
26
|
+
*
|
|
27
|
+
* Input: the assembled component JSONL (one `{house_number,street,locality,region,postcode}` per
|
|
28
|
+
* line). Open G-NAF licence — attribute "Geoscape Australia".
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { TextSpliterator } from "spliterator"
|
|
32
|
+
|
|
33
|
+
import { stableSourceID } from "../../adapter.ts"
|
|
34
|
+
import { reconcileComponents } from "../../format.ts"
|
|
35
|
+
import type { AdapterOptions, CanonicalRow, CorpusAdapter } from "../../types.ts"
|
|
36
|
+
|
|
37
|
+
export const GNAF_ADAPTER_ID = "gnaf"
|
|
38
|
+
/** Open G-NAF is freely redistributable with attribution to Geoscape Australia (CC-BY-style). */
|
|
39
|
+
export const GNAF_DEFAULT_LICENSE = "CC-BY-4.0"
|
|
40
|
+
|
|
41
|
+
interface GNAFTuple {
|
|
42
|
+
house_number: string
|
|
43
|
+
street: string
|
|
44
|
+
locality: string
|
|
45
|
+
region?: string
|
|
46
|
+
postcode: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The address layouts an AU address actually arrives in. The model already handles postcode-TRAILING (canonical); the
|
|
51
|
+
* two postcode-LEADING forms are the ones it fails, so they carry the lever. We keep the canonical form too so the
|
|
52
|
+
* retrain doesn't forget it.
|
|
53
|
+
*/
|
|
54
|
+
function renderOrders(c: GNAFTuple): string[] {
|
|
55
|
+
const region = c.region ? ` ${c.region}` : ""
|
|
56
|
+
|
|
57
|
+
return [
|
|
58
|
+
// real-AU canonical: number-first, street, suburb [state] postcode — "50 Barry Street, Carlton NSW 2000"
|
|
59
|
+
`${c.house_number} ${c.street}, ${c.locality}${region} ${c.postcode}`,
|
|
60
|
+
// postcode-first (the dominant failure): "2000 Carlton, Barry Street 50"
|
|
61
|
+
`${c.postcode} ${c.locality}, ${c.street} ${c.house_number}`,
|
|
62
|
+
// locality-first: "Carlton, 2000, Barry Street 50"
|
|
63
|
+
`${c.locality}, ${c.postcode}, ${c.street} ${c.house_number}`,
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Build the G-NAF adapter. `inputPath` is the assembled component JSONL (see {@link ./assemble}); it is country-pinned
|
|
69
|
+
* to AU regardless of `opts.country` (G-NAF is Australia-only).
|
|
70
|
+
*/
|
|
71
|
+
export function createGNAFAdapter(): CorpusAdapter {
|
|
72
|
+
return {
|
|
73
|
+
id: GNAF_ADAPTER_ID,
|
|
74
|
+
defaultLicense: GNAF_DEFAULT_LICENSE,
|
|
75
|
+
description:
|
|
76
|
+
"G-NAF (Australia): assembled address tuples rendered in multiple word orders (canonical / postcode-first / locality-first) — teaches the model AU's postcode-first layout.",
|
|
77
|
+
|
|
78
|
+
async *rows(opts: AdapterOptions): AsyncIterable<CanonicalRow> {
|
|
79
|
+
let emitted = 0
|
|
80
|
+
let idx = 0
|
|
81
|
+
|
|
82
|
+
// Input is the assembled component JSONL (one tuple per line). TextSpliterator auto-disposes on
|
|
83
|
+
// loop completion and on an early `break` (abort / limit), so the old explicit handle teardown is
|
|
84
|
+
// gone; JSON.parse tolerates a trailing CR on CRLF sources and the `!line.trim()` guard skips blanks.
|
|
85
|
+
// The render order rotates (i % 3), matching v1.9.1's rerender.
|
|
86
|
+
for await (const line of TextSpliterator.fromAsync(opts.inputPath)) {
|
|
87
|
+
if (opts.signal?.aborted) break
|
|
88
|
+
|
|
89
|
+
if (opts.limit !== undefined && emitted >= opts.limit) break
|
|
90
|
+
|
|
91
|
+
if (!line.trim()) continue
|
|
92
|
+
|
|
93
|
+
let t: GNAFTuple
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
t = JSON.parse(line) as GNAFTuple
|
|
97
|
+
} catch {
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!t.house_number || !t.street || !t.locality || !t.postcode) continue
|
|
102
|
+
|
|
103
|
+
const orders = renderOrders(t)
|
|
104
|
+
const order = idx % orders.length
|
|
105
|
+
idx++
|
|
106
|
+
const raw = orders[order]!
|
|
107
|
+
const components: CanonicalRow["components"] = {
|
|
108
|
+
house_number: t.house_number,
|
|
109
|
+
street: t.street,
|
|
110
|
+
locality: t.locality,
|
|
111
|
+
postcode: t.postcode,
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// region (state) rides only the canonical render (order 0); the postcode-leading layouts
|
|
115
|
+
// omit it (matching the eval's serialization) so it never breaks verbatim alignment.
|
|
116
|
+
if (order === 0 && t.region) {
|
|
117
|
+
components.region = t.region
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const aligned = reconcileComponents(components, raw)
|
|
121
|
+
|
|
122
|
+
if (Object.keys(aligned).length === 0) continue
|
|
123
|
+
yield {
|
|
124
|
+
raw,
|
|
125
|
+
components: aligned,
|
|
126
|
+
country: "AU",
|
|
127
|
+
locale: "en-AU",
|
|
128
|
+
source: GNAF_ADAPTER_ID,
|
|
129
|
+
source_id: `${stableSourceID(GNAF_ADAPTER_ID, aligned)}-o${order}`,
|
|
130
|
+
corpus_version: "",
|
|
131
|
+
license: GNAF_DEFAULT_LICENSE,
|
|
132
|
+
}
|
|
133
|
+
emitted++
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export const gnafAdapter = createGNAFAdapter()
|