@mailwoman/registry 11.3.0 → 11.5.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/manifests/artifacts.json +5 -0
- package/data/manifests/coverage.json +571 -0
- package/data/manifests/sources.lock.json +48 -0
- package/data/normalized/groupings.json +10902 -0
- package/definitions/groupings.json +222 -0
- package/docs/data-policy.md +10 -0
- package/lib/artifacts/groupings/groupings.fixture.json +1222 -0
- package/lib/artifacts/groupings/groupings.ts +77 -0
- package/lib/groupings.ts +127 -0
- package/lib/index.ts +19 -2
- package/lib/types.ts +90 -0
- package/out/artifacts/groupings/groupings.d.ts +28 -0
- package/out/artifacts/groupings/groupings.d.ts.map +1 -0
- package/out/artifacts/groupings/groupings.fixture.json +1222 -0
- package/out/artifacts/groupings/groupings.js +59 -0
- package/out/artifacts/groupings/groupings.js.map +1 -0
- package/out/groupings.d.ts +59 -0
- package/out/groupings.d.ts.map +1 -0
- package/out/groupings.js +92 -0
- package/out/groupings.js.map +1 -0
- package/out/index.d.ts +6 -2
- package/out/index.d.ts.map +1 -1
- package/out/index.js +3 -1
- package/out/index.js.map +1 -1
- package/out/sdk/sources.d.ts +55 -1
- package/out/sdk/sources.d.ts.map +1 -1
- package/out/sdk/sources.js +83 -0
- package/out/sdk/sources.js.map +1 -1
- package/out/tools/generate.run.js +58 -0
- package/out/tools/generate.run.js.map +1 -1
- package/out/tools/groupings.d.ts +101 -0
- package/out/tools/groupings.d.ts.map +1 -0
- package/out/tools/groupings.js +295 -0
- package/out/tools/groupings.js.map +1 -0
- package/out/tools/normalize.d.ts +17 -3
- package/out/tools/normalize.d.ts.map +1 -1
- package/out/tools/normalize.js +6 -1
- package/out/tools/normalize.js.map +1 -1
- package/out/tools/normalize.run.js +50 -3
- package/out/tools/normalize.run.js.map +1 -1
- package/out/types.d.ts +81 -0
- package/out/types.d.ts.map +1 -1
- package/out/types.js +29 -0
- package/out/types.js.map +1 -1
- package/package.json +4 -2
- package/sdk/sources.ts +119 -0
- package/tools/generate.run.ts +65 -0
- package/tools/groupings.ts +428 -0
- package/tools/normalize.run.ts +64 -4
- package/tools/normalize.ts +16 -2
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Normalizes the country groupings from their pinned sources into one groupings table
|
|
6
|
+
* and one table of membership intervals.
|
|
7
|
+
*
|
|
8
|
+
* An organization's members are Wikidata statements through the property its
|
|
9
|
+
* definition names, P463 ("member of") or P527 ("has part(s)").
|
|
10
|
+
* The other property is read too, and the report lists the codes on which the two disagree.
|
|
11
|
+
*
|
|
12
|
+
* A classification's members come from its publisher's list: the World Bank's country API,
|
|
13
|
+
* and the IMF advanced-economy list in a pinned Wikipedia revision.
|
|
14
|
+
*
|
|
15
|
+
* A grouping whose statements can't be classified is kept with the reasons in `blockedBy`,
|
|
16
|
+
* and its members are left out of the membership table.
|
|
17
|
+
* It never reads as a grouping with no members.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { stringifyJSON } from "@mailwoman/core/values/json"
|
|
21
|
+
|
|
22
|
+
import type { GroupingDefinitions } from "#sdk/sources"
|
|
23
|
+
import type { SPARQLResults } from "#tools/normalize"
|
|
24
|
+
import type { CountryRecord, GroupingRecord, MemberCodeStatus, MembershipRole, MembershipRow, StatedDate } from "#types"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The version of the rules below.
|
|
28
|
+
* A change that moves a row's bytes moves this number.
|
|
29
|
+
*/
|
|
30
|
+
export const GROUPING_NORMALIZATION_VERSION = 1
|
|
31
|
+
|
|
32
|
+
export interface NormalizedGroupings {
|
|
33
|
+
profile: string
|
|
34
|
+
normalizationVersion: number
|
|
35
|
+
groupings: GroupingRecord[]
|
|
36
|
+
records: MembershipRow[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* What the normalizer found about one grouping that the tables don't hold.
|
|
41
|
+
*/
|
|
42
|
+
export interface GroupingReport {
|
|
43
|
+
/**
|
|
44
|
+
* Membership rows by role, before any were left out.
|
|
45
|
+
*/
|
|
46
|
+
roles: Partial<Record<MembershipRole, number>>
|
|
47
|
+
/**
|
|
48
|
+
* Rows whose statement states no role, read as `member`.
|
|
49
|
+
*/
|
|
50
|
+
withoutRole: number
|
|
51
|
+
withoutStart: number
|
|
52
|
+
/**
|
|
53
|
+
* Wikidata members whose item holds no ISO 3166-1 record, such as a kingdom item beside its country item.
|
|
54
|
+
*/
|
|
55
|
+
uncoded: string[]
|
|
56
|
+
/**
|
|
57
|
+
* For an organization: codes the selected property states and the other property doesn't, and the reverse.
|
|
58
|
+
*/
|
|
59
|
+
crossCheck: { property: string; otherStates: number; onlySelected: string[]; onlyOther: string[] } | null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface GroupingSources {
|
|
63
|
+
definitions: GroupingDefinitions
|
|
64
|
+
countries: readonly CountryRecord[]
|
|
65
|
+
p463: SPARQLResults
|
|
66
|
+
p527: SPARQLResults
|
|
67
|
+
worldbank: unknown
|
|
68
|
+
worldbankAsOf: string | null
|
|
69
|
+
wikitext: string
|
|
70
|
+
wikitextAsOf: string | null
|
|
71
|
+
profile: string
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const ENTITY = "http://www.wikidata.org/entity/"
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Wikidata's `wikibase:timePrecision` for a date stated to the day.
|
|
78
|
+
*/
|
|
79
|
+
const DAY_PRECISION = 11
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Wikidata's `wikibase:timePrecision` for a date stated to the month.
|
|
83
|
+
*/
|
|
84
|
+
const MONTH_PRECISION = 10
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Reads a Wikidata time value at its stated precision.
|
|
88
|
+
*
|
|
89
|
+
* Every precision coarser than a month, such as a year, decade or century,
|
|
90
|
+
* reads as the year Wikidata stores.
|
|
91
|
+
* A membership date stated that coarsely still places the change within that year.
|
|
92
|
+
*/
|
|
93
|
+
export function statedDate(value: string | undefined, precision: string | undefined): StatedDate | null {
|
|
94
|
+
if (!value) return null
|
|
95
|
+
|
|
96
|
+
const match = /^\+?(\d{4})-(\d{2})-(\d{2})/.exec(value)
|
|
97
|
+
|
|
98
|
+
if (!match) throw new Error(`unreadable Wikidata time ${value}`)
|
|
99
|
+
|
|
100
|
+
const [, year, month, day] = match
|
|
101
|
+
const level = Number(precision)
|
|
102
|
+
|
|
103
|
+
if (level >= DAY_PRECISION) return { date: `${year}-${month}-${day}`, precision: "day" }
|
|
104
|
+
|
|
105
|
+
if (level === MONTH_PRECISION) return { date: `${year}-${month}-01`, precision: "month" }
|
|
106
|
+
|
|
107
|
+
return { date: `${year}-01-01`, precision: "year" }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface Statement {
|
|
111
|
+
org: string
|
|
112
|
+
member: string
|
|
113
|
+
id: string
|
|
114
|
+
roles: string[]
|
|
115
|
+
start: StatedDate | null
|
|
116
|
+
end: StatedDate | null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Folds the query's rows, one per statement and role, into statements, leaving out deprecated ones.
|
|
121
|
+
*/
|
|
122
|
+
export function readStatements(results: SPARQLResults): Statement[] {
|
|
123
|
+
const byID = new Map<string, Statement>()
|
|
124
|
+
|
|
125
|
+
for (const row of results.results.bindings) {
|
|
126
|
+
if (row["rank"]?.value.endsWith("#DeprecatedRank")) continue
|
|
127
|
+
|
|
128
|
+
const id = row["statement"]!.value.replace(`${ENTITY}statement/`, "")
|
|
129
|
+
const role = row["role"]?.value.replace(ENTITY, "")
|
|
130
|
+
const existing = byID.get(id)
|
|
131
|
+
|
|
132
|
+
if (existing) {
|
|
133
|
+
if (role && !existing.roles.includes(role)) {
|
|
134
|
+
existing.roles.push(role)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
continue
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
byID.set(id, {
|
|
141
|
+
org: row["org"]!.value.replace(ENTITY, ""),
|
|
142
|
+
member: row["member"]!.value.replace(ENTITY, ""),
|
|
143
|
+
id,
|
|
144
|
+
roles: role ? [role] : [],
|
|
145
|
+
start: statedDate(row["start"]?.value, row["startPrecision"]?.value),
|
|
146
|
+
end: statedDate(row["end"]?.value, row["endPrecision"]?.value),
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return [...byID.values()]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The code status of an alpha-2 code: its ISO record's status, or `source-extension`
|
|
155
|
+
* for a code no record holds.
|
|
156
|
+
*/
|
|
157
|
+
function codeStatus(code: string, byAlpha2: ReadonlyMap<string, CountryRecord>): MemberCodeStatus {
|
|
158
|
+
return byAlpha2.get(code)?.status ?? "source-extension"
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function emptyReport(): GroupingReport {
|
|
162
|
+
return { roles: {}, withoutRole: 0, withoutStart: 0, uncoded: [], crossCheck: null }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Reads the IMF advanced economies from the "IMF advanced economies" section of the article.
|
|
167
|
+
*
|
|
168
|
+
* The section lists the IMF's economies and then, after a "Plus" line
|
|
169
|
+
* or with a `<sup>d</sup>` mark, the entries the CIA adds.
|
|
170
|
+
* Only the IMF's entries are read.
|
|
171
|
+
*
|
|
172
|
+
* The section's own count ("43 countries and territories") is compared with the entries read,
|
|
173
|
+
* and a difference is returned as a reason to block the grouping.
|
|
174
|
+
*/
|
|
175
|
+
export function readIMFAdvanced(wikitext: string): { names: string[]; stated: number | null } {
|
|
176
|
+
const start = wikitext.search(/==\s*IMF advanced economies\s*==+/)
|
|
177
|
+
|
|
178
|
+
if (start < 0) throw new Error("the pinned article has no IMF advanced economies section")
|
|
179
|
+
|
|
180
|
+
// The section ends at the next heading of any level, so a later subsection's list isn't read as the IMF's.
|
|
181
|
+
const rest = wikitext.slice(start + 2)
|
|
182
|
+
const end = rest.search(/\n=+[^=\n]/)
|
|
183
|
+
const section = end < 0 ? rest : rest.slice(0, end)
|
|
184
|
+
const names: string[] = []
|
|
185
|
+
let inAddition = false
|
|
186
|
+
|
|
187
|
+
// oxlint-disable-next-line mailwoman/prefer-spliterator -- One article section of about 60 lines, already resident.
|
|
188
|
+
for (const line of section.split("\n")) {
|
|
189
|
+
if (/^Plus\b/.test(line.trim())) {
|
|
190
|
+
inAddition = true
|
|
191
|
+
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (line.trim() === "}}") {
|
|
196
|
+
inAddition = false
|
|
197
|
+
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (inAddition || line.includes("<sup>d</sup>")) continue
|
|
202
|
+
|
|
203
|
+
const flag = /\{\{flag country\|([^}|]+)(?:\|name=([^}|]+))?\}\}/.exec(line)
|
|
204
|
+
|
|
205
|
+
if (line.trimStart().startsWith("*") && flag) {
|
|
206
|
+
names.push((flag[2] ?? flag[1]!).trim())
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const stated = /(\d+) countries and territories are officially listed/.exec(section)
|
|
211
|
+
|
|
212
|
+
return { names, stated: stated ? Number(stated[1]) : null }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Serializes the groupings with one grouping and one membership row per line, so a repin diffs by row.
|
|
217
|
+
*/
|
|
218
|
+
export function formatGroupings(normalized: NormalizedGroupings): string {
|
|
219
|
+
const lines = (rows: readonly unknown[]): string => rows.map((row) => `\t\t${stringifyJSON(row)}`).join(",\n")
|
|
220
|
+
|
|
221
|
+
return (
|
|
222
|
+
`{\n\t"profile": ${stringifyJSON(normalized.profile)},\n` +
|
|
223
|
+
`\t"normalizationVersion": ${normalized.normalizationVersion},\n` +
|
|
224
|
+
`\t"groupings": [\n${lines(normalized.groupings)}\n\t],\n` +
|
|
225
|
+
`\t"records": [\n${lines(normalized.records)}\n\t]\n}\n`
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
interface WorldBankCountry {
|
|
230
|
+
id: string
|
|
231
|
+
iso2Code: string
|
|
232
|
+
incomeLevel: { id: string }
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Normalizes every declared grouping.
|
|
237
|
+
*
|
|
238
|
+
* Returns the tables and a report per grouping for the coverage declaration.
|
|
239
|
+
*/
|
|
240
|
+
export function normalizeGroupings(sources: GroupingSources): {
|
|
241
|
+
normalized: NormalizedGroupings
|
|
242
|
+
reports: Record<string, GroupingReport>
|
|
243
|
+
} {
|
|
244
|
+
const byEntity = new Map(sources.countries.map((record) => [record.entityID.replace("wikidata:", ""), record]))
|
|
245
|
+
const byAlpha2 = new Map(sources.countries.map((record) => [record.alpha2, record]))
|
|
246
|
+
const byName = new Map(sources.countries.map((record) => [record.name, record]))
|
|
247
|
+
const statements = { P463: readStatements(sources.p463), P527: readStatements(sources.p527) }
|
|
248
|
+
const groupings: GroupingRecord[] = []
|
|
249
|
+
const records: MembershipRow[] = []
|
|
250
|
+
const reports: Record<string, GroupingReport> = {}
|
|
251
|
+
|
|
252
|
+
for (const definition of sources.definitions.groupings) {
|
|
253
|
+
const report = emptyReport()
|
|
254
|
+
const blockedBy: string[] = []
|
|
255
|
+
const rows: MembershipRow[] = []
|
|
256
|
+
const source = definition.source
|
|
257
|
+
let label: string
|
|
258
|
+
let asOf: string | null = null
|
|
259
|
+
|
|
260
|
+
if (source.type === "wikidata") {
|
|
261
|
+
label = `wikidata:${source.item} ${source.property}`
|
|
262
|
+
|
|
263
|
+
const properties = source.property === "both" ? (["P463", "P527"] as const) : [source.property]
|
|
264
|
+
|
|
265
|
+
const recordOf = (member: string): CountryRecord | undefined => {
|
|
266
|
+
const alias = sources.definitions.entities[member]
|
|
267
|
+
|
|
268
|
+
return alias ? byAlpha2.get(alias) : byEntity.get(member)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const codesOf = (property: "P463" | "P527"): Set<string> => {
|
|
272
|
+
const codes = new Set<string>()
|
|
273
|
+
|
|
274
|
+
for (const statement of statements[property]) {
|
|
275
|
+
if (statement.org !== source.item) continue
|
|
276
|
+
|
|
277
|
+
const record = recordOf(statement.member)
|
|
278
|
+
|
|
279
|
+
if (record) {
|
|
280
|
+
codes.add(record.alpha2)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return codes
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
for (const statement of properties.flatMap((property) => statements[property])) {
|
|
288
|
+
if (statement.org !== source.item) continue
|
|
289
|
+
|
|
290
|
+
const record = recordOf(statement.member)
|
|
291
|
+
|
|
292
|
+
if (!record) {
|
|
293
|
+
report.uncoded.push(statement.member)
|
|
294
|
+
|
|
295
|
+
continue
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const roles = statement.roles.map((role: string) => sources.definitions.roles[role] ?? null)
|
|
299
|
+
const unknown = statement.roles.filter((role: string) => !sources.definitions.roles[role])
|
|
300
|
+
|
|
301
|
+
if (unknown.length) {
|
|
302
|
+
blockedBy.push(`statement ${statement.id} has role ${unknown.join(", ")}, which the role table omits`)
|
|
303
|
+
|
|
304
|
+
continue
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const role: MembershipRole = roles.find((value: MembershipRole | null) => value !== "member") ?? "member"
|
|
308
|
+
|
|
309
|
+
report.roles[role] = (report.roles[role] ?? 0) + 1
|
|
310
|
+
|
|
311
|
+
if (!statement.roles.length) {
|
|
312
|
+
report.withoutRole++
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (!statement.start) {
|
|
316
|
+
report.withoutStart++
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
rows.push({
|
|
320
|
+
grouping: definition.id,
|
|
321
|
+
code: record.alpha2,
|
|
322
|
+
codeStatus: record.status,
|
|
323
|
+
role,
|
|
324
|
+
start: statement.start,
|
|
325
|
+
end: statement.end,
|
|
326
|
+
evidence: `wikidata:${statement.id}`,
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// A cross-check needs a second set of statements.
|
|
331
|
+
// An organization read through both properties has none, and one whose other property
|
|
332
|
+
// states no member is reported with that count rather than with every member listed.
|
|
333
|
+
if (source.property !== "both") {
|
|
334
|
+
const selected = codesOf(source.property)
|
|
335
|
+
const otherProperty = source.property === "P463" ? "P527" : "P463"
|
|
336
|
+
const other = codesOf(otherProperty)
|
|
337
|
+
|
|
338
|
+
report.crossCheck = {
|
|
339
|
+
property: otherProperty,
|
|
340
|
+
otherStates: other.size,
|
|
341
|
+
onlySelected: other.size ? [...selected].filter((code) => !other.has(code)).toSorted() : [],
|
|
342
|
+
onlyOther: [...other].filter((code) => !selected.has(code)).toSorted(),
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
} else if (source.type === "worldbank-income") {
|
|
346
|
+
label = `worldbank-countries ${source.level}`
|
|
347
|
+
asOf = sources.worldbankAsOf
|
|
348
|
+
|
|
349
|
+
const [, countries] = sources.worldbank as [unknown, WorldBankCountry[]]
|
|
350
|
+
|
|
351
|
+
for (const country of countries) {
|
|
352
|
+
if (country.incomeLevel.id !== source.level) continue
|
|
353
|
+
|
|
354
|
+
report.roles.member = (report.roles.member ?? 0) + 1
|
|
355
|
+
|
|
356
|
+
rows.push({
|
|
357
|
+
grouping: definition.id,
|
|
358
|
+
code: country.iso2Code,
|
|
359
|
+
codeStatus: codeStatus(country.iso2Code, byAlpha2),
|
|
360
|
+
role: "member",
|
|
361
|
+
start: null,
|
|
362
|
+
end: null,
|
|
363
|
+
evidence: `worldbank:${country.id}`,
|
|
364
|
+
})
|
|
365
|
+
}
|
|
366
|
+
} else {
|
|
367
|
+
label = "wikipedia-developed-country"
|
|
368
|
+
asOf = sources.wikitextAsOf
|
|
369
|
+
|
|
370
|
+
const { names, stated } = readIMFAdvanced(sources.wikitext)
|
|
371
|
+
|
|
372
|
+
if (stated !== null && stated !== names.length) {
|
|
373
|
+
blockedBy.push(`the section states ${stated} economies and lists ${names.length}`)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
for (const name of names) {
|
|
377
|
+
const code = source.aliases[name] ?? byName.get(name)?.alpha2
|
|
378
|
+
|
|
379
|
+
if (!code) {
|
|
380
|
+
blockedBy.push(`"${name}" matches no ISO record name and no alias`)
|
|
381
|
+
|
|
382
|
+
continue
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
report.roles.member = (report.roles.member ?? 0) + 1
|
|
386
|
+
|
|
387
|
+
rows.push({
|
|
388
|
+
grouping: definition.id,
|
|
389
|
+
code,
|
|
390
|
+
codeStatus: codeStatus(code, byAlpha2),
|
|
391
|
+
role: "member",
|
|
392
|
+
start: null,
|
|
393
|
+
end: null,
|
|
394
|
+
evidence: `wikipedia:${name}`,
|
|
395
|
+
})
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (!blockedBy.length && !rows.some((row) => row.role === "member")) {
|
|
400
|
+
blockedBy.push(`${label} states no member`)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
groupings.push({ id: definition.id, name: definition.name, kind: definition.kind, source: label, asOf, blockedBy })
|
|
404
|
+
reports[definition.id] = report
|
|
405
|
+
|
|
406
|
+
if (!blockedBy.length) {
|
|
407
|
+
records.push(...rows)
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
records.sort(
|
|
412
|
+
(a, b) =>
|
|
413
|
+
a.grouping.localeCompare(b.grouping) ||
|
|
414
|
+
a.code.localeCompare(b.code) ||
|
|
415
|
+
(a.start?.date ?? "").localeCompare(b.start?.date ?? "") ||
|
|
416
|
+
a.evidence.localeCompare(b.evidence)
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
normalized: {
|
|
421
|
+
profile: sources.profile,
|
|
422
|
+
normalizationVersion: GROUPING_NORMALIZATION_VERSION,
|
|
423
|
+
groupings,
|
|
424
|
+
records,
|
|
425
|
+
},
|
|
426
|
+
reports,
|
|
427
|
+
}
|
|
428
|
+
}
|
package/tools/normalize.run.ts
CHANGED
|
@@ -10,15 +10,17 @@
|
|
|
10
10
|
* The run refuses a source file whose digest differs from `data/manifests/sources.lock.json`.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import { readLocalTextFile } from "@mailwoman/core/fs/readers"
|
|
13
|
+
import { readLocalJSONFile, readLocalTextFile } from "@mailwoman/core/fs/readers"
|
|
14
14
|
import { writeLocalFile, writeLocalJSONFile } from "@mailwoman/core/fs/writers"
|
|
15
|
-
import { workspacePathBuilder } from "@mailwoman/core/paths"
|
|
15
|
+
import { workspacePath, workspacePathBuilder } from "@mailwoman/core/paths"
|
|
16
|
+
import { isoDate } from "@mailwoman/core/utils"
|
|
16
17
|
import { stringifyJSON } from "@mailwoman/core/values/json"
|
|
17
18
|
|
|
18
19
|
import { readSourceLock } from "#sdk/fetch"
|
|
19
|
-
import { pinnedSourceFile, SOURCES } from "#sdk/sources"
|
|
20
|
+
import { type GroupingDefinitions, pinnedSourceFile, type SourceID, SOURCES } from "#sdk/sources"
|
|
21
|
+
import { formatGroupings, normalizeGroupings } from "#tools/groupings"
|
|
20
22
|
import { normalizeSubtags, SUBTAG_TYPES } from "#tools/language-subtags"
|
|
21
|
-
import { formatNormalized, normalizeFromLock } from "#tools/normalize"
|
|
23
|
+
import { formatNormalized, normalizeFromLock, type SPARQLResults, sourceDefinition } from "#tools/normalize"
|
|
22
24
|
|
|
23
25
|
const lock = await readSourceLock()
|
|
24
26
|
|
|
@@ -44,6 +46,44 @@ for (const type of SUBTAG_TYPES) {
|
|
|
44
46
|
}
|
|
45
47
|
}
|
|
46
48
|
|
|
49
|
+
const groupingDefinitions = await readLocalJSONFile<GroupingDefinitions>(
|
|
50
|
+
workspacePath("registry", "definitions", "groupings.json")
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The date of a source's stated publication marker, or null when the server stated none.
|
|
55
|
+
*/
|
|
56
|
+
const publishedOn = (id: SourceID): string | null => {
|
|
57
|
+
const marker = lock.sources[id]?.publication
|
|
58
|
+
|
|
59
|
+
return marker && marker !== "unstated" ? isoDate(new Date(marker)) : null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { normalized: groupings, reports } = normalizeGroupings({
|
|
63
|
+
definitions: groupingDefinitions,
|
|
64
|
+
countries: countries.records,
|
|
65
|
+
p463: await readLocalJSONFile<SPARQLResults>(pinnedSourceFile(sourceDefinition("wikidata-memberships-p463"))),
|
|
66
|
+
p527: await readLocalJSONFile<SPARQLResults>(pinnedSourceFile(sourceDefinition("wikidata-memberships-p527"))),
|
|
67
|
+
worldbank: await readLocalJSONFile<unknown>(pinnedSourceFile(sourceDefinition("worldbank-countries"))),
|
|
68
|
+
worldbankAsOf: publishedOn("worldbank-countries"),
|
|
69
|
+
wikitext: await readLocalTextFile(pinnedSourceFile(sourceDefinition("wikipedia-developed-country"))),
|
|
70
|
+
wikitextAsOf: publishedOn("wikipedia-developed-country"),
|
|
71
|
+
profile: lock.profile,
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
coverage.namespaces["country-grouping"] = {
|
|
75
|
+
source: "wikidata-memberships-p463, wikidata-memberships-p527, worldbank-countries, wikipedia-developed-country",
|
|
76
|
+
count: groupings.groupings.length,
|
|
77
|
+
statuses: {
|
|
78
|
+
answering: groupings.groupings.filter((grouping) => !grouping.blockedBy.length).length,
|
|
79
|
+
blocked: groupings.groupings.filter((grouping) => grouping.blockedBy.length).length,
|
|
80
|
+
},
|
|
81
|
+
missMeans: "not-declared: definitions/groupings.json declares no grouping with this id",
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
coverage.groupings = reports
|
|
85
|
+
|
|
86
|
+
await writeLocalFile(formatGroupings(groupings), data("normalized", "groupings.json"))
|
|
47
87
|
await writeLocalFile(formatNormalized(countries), data("normalized", "iso3166-1.json"))
|
|
48
88
|
await writeLocalFile(formatNormalized(airports), data("normalized", "iata-airports.json"))
|
|
49
89
|
await writeLocalFile(formatNormalized(subtags), data("normalized", "language-subtags.json"))
|
|
@@ -54,4 +94,24 @@ console.log(
|
|
|
54
94
|
)
|
|
55
95
|
console.log(`airports: ${airports.records.length} (${stringifyJSON(coverage.namespaces["iata-location"]!.statuses)})`)
|
|
56
96
|
console.log(`subtags: ${subtags.records.length} (File-Date ${subtags.fileDate})`)
|
|
97
|
+
|
|
98
|
+
for (const grouping of groupings.groupings) {
|
|
99
|
+
const members = groupings.records.filter((row) => row.grouping === grouping.id && row.role === "member").length
|
|
100
|
+
const report = reports[grouping.id]!
|
|
101
|
+
|
|
102
|
+
console.log(
|
|
103
|
+
`grouping ${grouping.id}: ${grouping.blockedBy.length ? `blocked (${grouping.blockedBy.join("; ")})` : `${members} member rows`}` +
|
|
104
|
+
(report.crossCheck
|
|
105
|
+
? `, ${report.crossCheck.property} states ${report.crossCheck.otherStates}` +
|
|
106
|
+
(report.crossCheck.onlySelected.length
|
|
107
|
+
? `, only selected: ${report.crossCheck.onlySelected.join(" ")}`
|
|
108
|
+
: "") +
|
|
109
|
+
(report.crossCheck.onlyOther.length
|
|
110
|
+
? `, only ${report.crossCheck.property}: ${report.crossCheck.onlyOther.join(" ")}`
|
|
111
|
+
: "")
|
|
112
|
+
: "") +
|
|
113
|
+
(report.uncoded.length ? `, uncoded: ${report.uncoded.join(" ")}` : "")
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
57
117
|
console.log(`country lists:${coverage.namespaces["$countryListComparison"]!.kinds!.join("; ")}`)
|
package/tools/normalize.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { stringifyJSON } from "@mailwoman/core/values/json"
|
|
|
18
18
|
import { TextSpliterator } from "spliterator"
|
|
19
19
|
|
|
20
20
|
import { pinnedSourceFile, type SourceDefinition, type SourceLock, SOURCES } from "#sdk/sources"
|
|
21
|
+
import type { GroupingReport } from "#tools/groupings"
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* One ISO 3166-1 entry.
|
|
@@ -103,6 +104,11 @@ export interface CoverageDeclaration {
|
|
|
103
104
|
missMeans: string
|
|
104
105
|
}
|
|
105
106
|
>
|
|
107
|
+
/**
|
|
108
|
+
* What the groupings normalizer found per grouping: roles, statements without a role or a start,
|
|
109
|
+
* members with no ISO record, and where Wikidata's two membership properties disagree.
|
|
110
|
+
*/
|
|
111
|
+
groupings?: Record<string, GroupingReport>
|
|
106
112
|
}
|
|
107
113
|
|
|
108
114
|
/**
|
|
@@ -112,7 +118,10 @@ export interface CoverageDeclaration {
|
|
|
112
118
|
*/
|
|
113
119
|
export const NORMALIZATION_VERSION = 1
|
|
114
120
|
|
|
115
|
-
|
|
121
|
+
/**
|
|
122
|
+
* A Wikidata Query Service answer in the SPARQL 1.1 JSON results format.
|
|
123
|
+
*/
|
|
124
|
+
export interface SPARQLResults {
|
|
116
125
|
results: { bindings: Array<Record<string, { type: string; value: string }>> }
|
|
117
126
|
}
|
|
118
127
|
|
|
@@ -120,7 +129,12 @@ const ALPHA2 = /^[A-Z]{2}$/
|
|
|
120
129
|
const ALPHA3 = /^[A-Z]{3}$/
|
|
121
130
|
const NUMERIC = /^\d{3}$/
|
|
122
131
|
|
|
123
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Returns the source definition with `id`.
|
|
134
|
+
*
|
|
135
|
+
* @throws When the profile declares no such source.
|
|
136
|
+
*/
|
|
137
|
+
export function sourceDefinition(id: SourceDefinition["id"]): SourceDefinition {
|
|
124
138
|
const found = SOURCES.find((candidate) => candidate.id === id)
|
|
125
139
|
|
|
126
140
|
if (!found) throw new Error(`no source ${id}`)
|