@mailwoman/corpus 12.1.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.
Files changed (55) hide show
  1. package/data/holdout/evaluation-groups.v0.9.3.json +224169 -0
  2. package/data/training-configs/v9.3.0-bytestem-global-v093-s42.yaml +746 -0
  3. package/data/training-manifests/v0.9.3-compact.effective.json +2660 -0
  4. package/data/training-manifests/v0.9.3-compact.json +9648 -0
  5. package/lib/adapters/geonames/features/adapter.ts +135 -10
  6. package/lib/adapters/wof/admin/json/dual-role.ts +46 -21
  7. package/lib/build/schema-invariants.ts +447 -0
  8. package/lib/jp/corpus/assemble.ts +20 -1
  9. package/lib/jp/corpus/rows.ts +87 -0
  10. package/lib/recipes/prominent/places.ts +42 -9
  11. package/lib/recipes/trailing-region.ts +37 -7
  12. package/lib/tools/fetch/wikidata/vital-places/classify.ts +23 -7
  13. package/lib/tools/fetch/wikidata/vital-places/render.ts +57 -7
  14. package/lib/training/config/schema.ts +3 -0
  15. package/lib/us/street-unit.ts +14 -1
  16. package/out/adapters/geonames/features/adapter.d.ts.map +1 -1
  17. package/out/adapters/geonames/features/adapter.js +90 -10
  18. package/out/adapters/geonames/features/adapter.js.map +1 -1
  19. package/out/adapters/wof/admin/json/dual-role.d.ts +8 -3
  20. package/out/adapters/wof/admin/json/dual-role.d.ts.map +1 -1
  21. package/out/adapters/wof/admin/json/dual-role.js +40 -19
  22. package/out/adapters/wof/admin/json/dual-role.js.map +1 -1
  23. package/out/build/schema-invariants.d.ts +101 -0
  24. package/out/build/schema-invariants.d.ts.map +1 -0
  25. package/out/build/schema-invariants.js +302 -0
  26. package/out/build/schema-invariants.js.map +1 -0
  27. package/out/jp/corpus/assemble.d.ts.map +1 -1
  28. package/out/jp/corpus/assemble.js +14 -2
  29. package/out/jp/corpus/assemble.js.map +1 -1
  30. package/out/jp/corpus/rows.d.ts +8 -0
  31. package/out/jp/corpus/rows.d.ts.map +1 -1
  32. package/out/jp/corpus/rows.js +72 -0
  33. package/out/jp/corpus/rows.js.map +1 -1
  34. package/out/recipes/prominent/places.d.ts.map +1 -1
  35. package/out/recipes/prominent/places.js +30 -5
  36. package/out/recipes/prominent/places.js.map +1 -1
  37. package/out/recipes/trailing-region.d.ts.map +1 -1
  38. package/out/recipes/trailing-region.js +30 -7
  39. package/out/recipes/trailing-region.js.map +1 -1
  40. package/out/tools/fetch/wikidata/vital-places/classify.d.ts +12 -2
  41. package/out/tools/fetch/wikidata/vital-places/classify.d.ts.map +1 -1
  42. package/out/tools/fetch/wikidata/vital-places/classify.js +19 -7
  43. package/out/tools/fetch/wikidata/vital-places/classify.js.map +1 -1
  44. package/out/tools/fetch/wikidata/vital-places/render.d.ts +9 -5
  45. package/out/tools/fetch/wikidata/vital-places/render.d.ts.map +1 -1
  46. package/out/tools/fetch/wikidata/vital-places/render.js +44 -7
  47. package/out/tools/fetch/wikidata/vital-places/render.js.map +1 -1
  48. package/out/training/config/schema.d.ts +21 -0
  49. package/out/training/config/schema.d.ts.map +1 -1
  50. package/out/training/config/schema.js +3 -0
  51. package/out/training/config/schema.js.map +1 -1
  52. package/out/us/street-unit.d.ts.map +1 -1
  53. package/out/us/street-unit.js +11 -1
  54. package/out/us/street-unit.js.map +1 -1
  55. package/package.json +19 -19
@@ -29,6 +29,14 @@ import {
29
29
  loadAdmin1,
30
30
  loadCountries,
31
31
  } from "#adapters/geonames/adapter"
32
+ import {
33
+ type CapitalSeats,
34
+ DualRoleBasis,
35
+ dualRoleKey,
36
+ type DualRolePair,
37
+ dualRoleRowRule,
38
+ readCapitalSeats,
39
+ } from "#adapters/wof/admin/json/dual-role"
32
40
  import { SourceRegister } from "#registers"
33
41
  import { AddressRole, type AdapterOptions, type CanonicalRow, type CorpusAdapter, SurfaceOrigin } from "#types"
34
42
 
@@ -83,6 +91,17 @@ export const GEONAMES_FEATURES_PER_CELL = 25
83
91
  */
84
92
  export const GEONAMES_FEATURES_WITH_COUNTRY_DIVISOR = 4
85
93
 
94
+ /**
95
+ * Historical, abandoned, or destroyed populated-place feature codes skipped
96
+ * when detecting dual-role regions.
97
+ */
98
+ const NON_CURRENT_PPL = new Set(["PPLH", "PPLQ", "PPLW", "PPLCH"])
99
+
100
+ /**
101
+ * GeoNames feature codes for national capitals and admin-1 seats.
102
+ */
103
+ const ADMIN1_SEAT_CODES = new Set(["PPLC", "PPLA"])
104
+
86
105
  /**
87
106
  * One feature the cap kept.
88
107
  */
@@ -92,22 +111,84 @@ interface KeptFeature {
92
111
  }
93
112
 
94
113
  /**
95
- * The geonameids each cell keeps: its {@linkcode GEONAMES_FEATURES_PER_CELL} lowest hashes.
114
+ * Result of pass 1 over one GeoNames dump: the capped feature IDs and the dual-role
115
+ * `(country, admin1)` pairs whose homonymous city-region name stays out of `region` (#3504).
116
+ */
117
+ interface ScannedDump {
118
+ kept: Set<string>
119
+ dualRolePairs: DualRolePair[]
120
+ }
121
+
122
+ /**
123
+ * Scans one GeoNames dump in a single pass to collect:
124
+ * 1. The {@linkcode GEONAMES_FEATURES_PER_CELL} lowest-hashed feature geonameids
125
+ * per `(country, featureCode)` cell.
126
+ * 2. The dual-role admin-1 pairs where a current populated place in `(cc, admin1)`
127
+ * shares the folded name of that admin-1 unit and is either a capital/admin-1 seat
128
+ * or the unit's most populous locality.
129
+ * When a country has no populated-place rows in the dump (as in features-only fixtures),
130
+ * the committed capitals reference decides seats.
96
131
  */
97
- async function keptFeatureIDs(input: PathBuilder, country: string | undefined): Promise<Set<string>> {
132
+ async function scanDump(
133
+ input: PathBuilder,
134
+ country: string | undefined,
135
+ admin1: ReadonlyMap<string, string>,
136
+ seats: CapitalSeats
137
+ ): Promise<ScannedDump> {
98
138
  const cells = new Map<string, KeptFeature[]>()
139
+ const countriesWithPPL = new Set<string>()
140
+ const largestByAdmin1 = new Map<string, { id: number; population: number }>()
141
+
142
+ const candidates: Array<{
143
+ admin1Key: string
144
+ cc: string
145
+ regionName: string
146
+ localityID: number
147
+ isSeat: boolean
148
+ }> = []
99
149
 
100
150
  for await (const rec of readUnquotedTSV(input)) {
101
151
  const cls = rec[COL.featureClass] ?? ""
102
152
  const code = (rec[COL.featureCode] ?? "").trim()
103
-
104
- if (!isSelectedGeonamesFeature(cls, code)) continue
105
153
  const cc = (rec[COL.country] ?? "").trim()
106
154
 
107
155
  if (!cc || (country && cc !== country)) continue
108
156
  const geonameid = (rec[COL.geonameid] ?? "").trim()
157
+ const name = (rec[COL.name] ?? "").trim()
158
+
159
+ if (!geonameid || !name) continue
160
+
161
+ if (cls === "P" && !NON_CURRENT_PPL.has(code)) {
162
+ countriesWithPPL.add(cc)
163
+ const admin1Code = (rec[COL.admin1] ?? "").trim()
164
+ const admin1Key = `${cc}.${admin1Code}`
165
+ const regionName = admin1.get(admin1Key)
166
+
167
+ if (regionName) {
168
+ const id = Number.parseInt(geonameid, 10) || 0
169
+ const population = Number.parseInt(rec[COL.population] ?? "0", 10) || 0
170
+ const prev = largestByAdmin1.get(admin1Key)
171
+
172
+ if (
173
+ population > 0 &&
174
+ (!prev || population > prev.population || (population === prev.population && id < prev.id))
175
+ ) {
176
+ largestByAdmin1.set(admin1Key, { id, population })
177
+ }
178
+
179
+ const regionFolded = dualRoleKey(regionName)
180
+ const asciiName = (rec[COL.asciiname] ?? "").trim()
181
+
182
+ if (dualRoleKey(name) === regionFolded || (asciiName && dualRoleKey(asciiName) === regionFolded)) {
183
+ const isSeat = ADMIN1_SEAT_CODES.has(code) || seats.has(`${cc}|${regionFolded}`)
184
+
185
+ candidates.push({ admin1Key, cc, regionName, localityID: id, isSeat })
186
+ }
187
+ }
188
+ }
189
+
190
+ if (!isSelectedGeonamesFeature(cls, code)) continue
109
191
 
110
- if (!geonameid || !(rec[COL.name] ?? "").trim()) continue
111
192
  const key = `${cc}\t${code}`
112
193
  const kept = cells.get(key) ?? []
113
194
  const hash = hashFNV1a(geonameid)
@@ -129,7 +210,47 @@ async function keptFeatureIDs(input: PathBuilder, country: string | undefined):
129
210
  }
130
211
  }
131
212
 
132
- return new Set([...cells.values()].flatMap((kept) => kept.map((feature) => feature.geonameid)))
213
+ const pairsByRegion = new Map<string, DualRolePair>()
214
+
215
+ for (const candidate of candidates) {
216
+ const basis = candidate.isSeat
217
+ ? DualRoleBasis.seat
218
+ : largestByAdmin1.get(candidate.admin1Key)?.id === candidate.localityID
219
+ ? DualRoleBasis.largest
220
+ : null
221
+
222
+ if (basis && !pairsByRegion.has(candidate.admin1Key)) {
223
+ pairsByRegion.set(candidate.admin1Key, {
224
+ regionID: 0,
225
+ localityID: candidate.localityID,
226
+ country: candidate.cc,
227
+ name: candidate.regionName,
228
+ basis,
229
+ })
230
+ }
231
+ }
232
+
233
+ // Fallback for features-only dumps that omit populated-place rows: check admin1 names against the capitals reference.
234
+ for (const [admin1Key, regionName] of admin1) {
235
+ const cc = admin1Key.split(".")[0] ?? ""
236
+
237
+ if (!cc || (country && cc !== country) || countriesWithPPL.has(cc) || pairsByRegion.has(admin1Key)) continue
238
+
239
+ if (seats.has(`${cc}|${dualRoleKey(regionName)}`)) {
240
+ pairsByRegion.set(admin1Key, {
241
+ regionID: 0,
242
+ localityID: 0,
243
+ country: cc,
244
+ name: regionName,
245
+ basis: DualRoleBasis.seat,
246
+ })
247
+ }
248
+ }
249
+
250
+ return {
251
+ kept: new Set([...cells.values()].flatMap((kept) => kept.map((feature) => feature.geonameid))),
252
+ dualRolePairs: [...pairsByRegion.values()],
253
+ }
133
254
  }
134
255
 
135
256
  /**
@@ -162,8 +283,7 @@ export function createGeonamesFeaturesAdapter(): CorpusAdapter {
162
283
  .map((name: string) => input(name))
163
284
  : [input]
164
285
 
165
- const admin1 = await loadAdmin1(dir)
166
- const countries = await loadCountries(dir)
286
+ const [admin1, countries, seats] = await Promise.all([loadAdmin1(dir), loadCountries(dir), readCapitalSeats()])
167
287
  let emitted = 0
168
288
 
169
289
  for (const dump of dumps) {
@@ -171,7 +291,8 @@ export function createGeonamesFeaturesAdapter(): CorpusAdapter {
171
291
  }
172
292
 
173
293
  async function* rowsOf(dump: PathBuilder): AsyncIterable<CanonicalRow> {
174
- const kept = await keptFeatureIDs(dump, opts.country)
294
+ const { kept, dualRolePairs } = await scanDump(dump, opts.country, admin1, seats)
295
+ const dualRole = dualRoleRowRule(GEONAMES_FEATURES_ADAPTER_ID, dualRolePairs)
175
296
 
176
297
  for await (const rec of readUnquotedTSV(dump)) {
177
298
  if (opts.signal?.aborted) break
@@ -203,7 +324,7 @@ export function createGeonamesFeaturesAdapter(): CorpusAdapter {
203
324
 
204
325
  if (!aligned.venue) continue
205
326
 
206
- yield {
327
+ const row: CanonicalRow = {
207
328
  raw: variant.raw,
208
329
  components: aligned,
209
330
  country: cc,
@@ -212,6 +333,10 @@ export function createGeonamesFeaturesAdapter(): CorpusAdapter {
212
333
  license: GEONAMES_DEFAULT_LICENSE,
213
334
  }
214
335
 
336
+ if (dualRole.drops(row)) continue
337
+
338
+ yield row
339
+
215
340
  emitted++
216
341
  }
217
342
  }
@@ -2,17 +2,21 @@
2
2
  // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
3
 
4
4
  /**
5
- * @file The `wof-admin` rule for a region that shares its name with the city inside it (Berlin, Zürich, Madrid).
5
+ * @file The `wof-admin` rule for a region that shares its name with the city inside it (Berlin, Zürich, Madrid, Kharkiv, Amman, Riyadh).
6
6
  *
7
- * In the two-part form `<name>, <country>`, the shared name reads as the city: `Berlin, Germany` is the city. The
8
- * adapter therefore leaves out the region's `<region>, <country>` rows and keeps the city's `<locality>, <country>`
9
- * row whatever the sample would keep (#2909).
7
+ * In both the bare `<name>` form and the two-part `<name>, <country>` form, the shared name reads as the city:
8
+ * `Berlin`, `Berlin, Germany`, `Kharkiv`, `Amman`, and `Riyadh` refer to the city rather than the surrounding admin-1
9
+ * unit. The adapter therefore leaves out the region's bare `<region>` and `<region>, <country>` rows and keeps the
10
+ * city's `<locality>, <country>` row whatever the sample would keep (#2909, #3647).
10
11
  *
11
- * A region qualifies when it holds a same-name locality near its centroid, and that locality is a seat in the
12
- * capitals reference for the country or the most populous locality inside the region. The pair test is the one the
13
- * gazetteer's coincident-roles relation used, with the names compared without case or diacritics: the canton
14
- * `Zurich` holds the city `Zürich`. The locality descends from the region, and the centroids lie within 15% of the
15
- * region's bounding-box diagonal, or within 12 km.
12
+ * A region qualifies when it holds a same-name locality that descends from it, with names compared without case or
13
+ * diacritics (the canton `Zurich` holds the city `Zürich`), and either:
14
+ *
15
+ * - the locality is a seat in the capitals reference for the country and lies inside the region's bounding box (or
16
+ * within the centroid tolerance when no bounding box is present), which handles large oblasts, governorates, and
17
+ * provinces whose capitals sit off-center.
18
+ * - the locality is the most populous locality inside the region and its centroid lies within 15% of the region's
19
+ * bounding-box diagonal, or within 12 km.
16
20
  */
17
21
 
18
22
  import { foldToken } from "@mailwoman/codex/names/normalize"
@@ -43,7 +47,7 @@ const BBOX_2D_LENGTH = 4
43
47
  /**
44
48
  * The key two spellings of one place name share: diacritics and case removed, ends trimmed.
45
49
  */
46
- function dualRoleKey(name: string): string {
50
+ export function dualRoleKey(name: string): string {
47
51
  return foldToken(name).trim()
48
52
  }
49
53
 
@@ -207,8 +211,12 @@ export function findDualRolePairs(
207
211
  largest.set(regionID, locality)
208
212
  }
209
213
 
210
- if (dualRoleKey(locality.name) === dualRoleKey(region.name) && isCoincident(region, locality)) {
211
- candidates.push({ region, locality })
214
+ if (dualRoleKey(locality.name) === dualRoleKey(region.name)) {
215
+ const isSeat = seats.has(`${locality.country}|${dualRoleKey(locality.name)}`)
216
+
217
+ if (isCoincident(region, locality) || (isSeat && isInsideBBox(region, locality))) {
218
+ candidates.push({ region, locality })
219
+ }
212
220
  }
213
221
  }
214
222
  }
@@ -249,9 +257,27 @@ function isCoincident(region: DualRolePlace, locality: DualRolePlace): boolean {
249
257
  }
250
258
 
251
259
  /**
252
- * The row rule for `pairs`: a `<region>, <country>` row whose region folds to a paired
253
- * region's name in the row's country is left out, whichever record or name slot wrote it,
254
- * and each paired locality's canonical `<locality>, <country>` row is required.
260
+ * Whether a locality's point lies inside the region's declared bounding box.
261
+ */
262
+ function isInsideBBox(region: DualRolePlace, locality: DualRolePlace): boolean {
263
+ if (!region.bbox) return false
264
+
265
+ const [west, south, east, north] = region.bbox
266
+ const latIn = locality.latitude >= south && locality.latitude <= north
267
+
268
+ const lonIn =
269
+ west <= east
270
+ ? locality.longitude >= west && locality.longitude <= east
271
+ : locality.longitude >= west || locality.longitude <= east
272
+
273
+ return latIn && lonIn
274
+ }
275
+
276
+ /**
277
+ * The row rule for `pairs`: drops bare `<region>`, `<region>, <country>`,
278
+ * and `<venue>, <region>[, <country>]` rows whose region folds to a paired region's
279
+ * name in that country without a `locality` beside it, and requires each paired
280
+ * locality's canonical `<locality>, <country>` row.
255
281
  */
256
282
  export function dualRoleRowRule(adapterID: string, pairs: readonly DualRolePair[]): WOFRowRule {
257
283
  const regionNames = new Set(pairs.map((pair) => `${pair.country}|${dualRoleKey(pair.name)}`))
@@ -262,12 +288,11 @@ export function dualRoleRowRule(adapterID: string, pairs: readonly DualRolePair[
262
288
  const tags = Object.keys(row.components)
263
289
  const region = row.components.region
264
290
 
265
- return (
266
- tags.length === 2 &&
267
- typeof region === "string" &&
268
- typeof row.components.country === "string" &&
269
- regionNames.has(`${row.country}|${dualRoleKey(region)}`)
270
- )
291
+ if (typeof region !== "string" || !regionNames.has(`${row.country}|${dualRoleKey(region)}`)) {
292
+ return false
293
+ }
294
+
295
+ return tags.every((tag) => tag === "region" || tag === "country" || tag === "venue")
271
296
  },
272
297
  requires: (row: CanonicalRow) => required.has(row.source_id),
273
298
  }