// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
/**
* @file Writes the `wikidata-vital-places` parquet part and its entity holdout from a fetched inventory.
*
* Usage:
* node packages/corpus/lib/tools/vital-places.run.ts --out
[--source-root ] [--exempt ]
*
* `--source-root` is the `--out-root` `mailwoman corpus fetch wikidata-vital-places` wrote under,
* by default the current-source root `$MAILWOMAN_CACHE_ROOT/sources/`.
* `--exempt` is a JSONL file with a `qid` on each row. Those entities always train.
* The part goes to `/wikidata-vital-places-00000.parquet`, and the holdout and counts beside it.
*/
import { sha256Hex } from "@mailwoman/core/crypto/hash"
import { currentSourcesRoot } from "@mailwoman/core/data-root"
import { readLocalJSONFile } from "@mailwoman/core/fs/readers"
import { pathExists } from "@mailwoman/core/fs/readers/stat"
import { writeLocalJSONFile, writeLocalJSONLFile } from "@mailwoman/core/fs/writers"
import { parseArguments } from "@mailwoman/core/scripting/arguments"
import { stringifyJSON } from "@mailwoman/core/values/json"
import { PathBuilder, resolvePath } from "path-ts"
import { JSONSpliterator } from "spliterator"
import { rowToParquet, type ParquetRow } from "#parquet/schema"
import { writeParquetFile } from "#parquet/writers"
import { VITAL_PLACES_FILES, vitalPlacesPath } from "#tools/fetch/wikidata/vital-places"
import type { ClassRoots } from "#tools/fetch/wikidata/vital-places/classify"
import type { VitalPlace } from "#tools/fetch/wikidata/vital-places/inventory"
import { renderVitalPlaces, VITAL_PLACES_SOURCE } from "#tools/fetch/wikidata/vital-places/render"
import { SurfaceOrigin } from "#types"
import { alignRow } from "#utils/align"
const { values } = parseArguments({
options: {
out: { type: "string" },
"source-root": { type: "string" },
exempt: { type: "string" },
},
})
if (!values.out) throw new Error("--out names the directory the part is written to")
const out = PathBuilder.from(resolvePath(values.out as string))
const sourceDir = vitalPlacesPath(
values["source-root"] ? PathBuilder.from(resolvePath(values["source-root"])) : currentSourcesRoot()
)
const rootsPath = sourceDir(VITAL_PLACES_FILES.classRoots)
if (!(await pathExists(rootsPath))) {
throw new Error(`${rootsPath.toString()} is missing; run the fetch so every class is asked about the render's roots`)
}
const roots = await readLocalJSONFile(rootsPath)
const places = await JSONSpliterator.fromAsync(sourceDir(VITAL_PLACES_FILES.places)).toArray()
const exempt = new Set(
values.exempt
? (await JSONSpliterator.fromAsync<{ qid: string }>(resolvePath(values.exempt as string)).toArray()).map(
(row) => row.qid
)
: []
)
const render = renderVitalPlaces(places, roots, exempt)
const rows: ParquetRow[] = []
const quarantined: string[] = []
for (const row of render.rows) {
const aligned = alignRow({
raw: row.raw,
components: row.components,
country: row.country,
source: row.source,
source_id: row.source_id,
license: "CC0-1.0",
register: row.register,
surface: SurfaceOrigin.Rendered,
})
if (aligned.kind !== "labeled") {
quarantined.push(row.source_id)
continue
}
rows.push({ ...rowToParquet(aligned.row), locale: null, augmentation: null })
}
const coordinates = new Map(places.map((place) => [place.qid, place.coordinate]))
await writeParquetFile(rows, out(`${VITAL_PLACES_SOURCE}-00000.parquet`), {
extractHash: `${VITAL_PLACES_SOURCE}:${sha256Hex(stringifyJSON(places))}`,
codeHash: "packages/corpus/lib/tools/vital-places.run.ts",
config: { sourceDir: sourceDir.toString(), exempt: exempt.size },
})
// The holdout rows take the famous-places fixture's shape, so its scorer grades them.
await writeLocalJSONLFile(
render.holdout.map((row) => ({
qid: row.qid,
kind: "holdout",
country: row.country,
lat: coordinates.get(row.qid)?.lat,
lon: coordinates.get(row.qid)?.lon,
tolerance_m: 50_000,
form: "country" in row.components ? "with-country" : "bare",
input: row.raw,
components: row.components,
})),
out(`${VITAL_PLACES_SOURCE}-holdout.jsonl`)
)
const counts = {
...render.counts,
rows: rows.length,
holdoutRows: render.holdout.length,
quarantined: quarantined.length,
}
await writeLocalJSONFile(counts, out(`${VITAL_PLACES_SOURCE}-counts.json`))
console.log(stringifyJSON(counts))