// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * `house-venue` recipe — synthetic house_number+venue+street co-occurrence rows: * tuples → {@link synthesizeHouseVenueRow} → aligned LabeledRow. * * The v0.6.3 corrective companion to the no-street recipe: every row includes both * house_number and venue to represent their co-occurrence. * Ported from the root build script it replaced. */ import { layoutForCountry, layoutPrintsTag } from "@mailwoman/codex/address/layouts" import { makeLcg } from "@mailwoman/core/utils" import { withConventionStreet } from "#adapters/street/convention" import { alignAndWrite, readTuples, recipeSourceID, SYNTHETIC_TUPLE_LICENSE as LICENSE, type CorpusRecipe, } from "#recipes/scaffold" import { defaultRecipeSource } from "#recipes/sources" import { synthesizeHouseVenueRow, type HouseVenueBaseTuple } from "#synthesizers/house-venue" import { SurfaceOrigin } from "#types" /** * The venue name and house number are drawn and joined to a tuple's admin components. * * No register asserts that this venue stands at this number, so the row teaches the * co-occurrence shape rather than reporting a published record. */ const HOUSE_VENUE_PROVENANCE = { register: null, surface: SurfaceOrigin.Invented, // An invented row has no underlying record to name. baseSourceID: null, } /** * Recipe registered with the corpus builder. * * See the file header for the parse behavior it exercises. * See `description` below for the generated surface form. */ export const houseVenueRecipe: CorpusRecipe = { name: "house-venue", description: "House_number+venue co-occurrence rows: tuples → synthesizeHouseVenueRow → aligned LabeledRow", mode: "tuples", async run(opts, write) { if (!opts.input) throw new Error("house-venue recipe requires --input ") // The root build script this recipe replaced seeded the LCG via makeRandom(opts.seed) (s = seed). const random = makeLcg(opts.seed) const source = opts.sourceName ?? defaultRecipeSource("synth-house-venue") let read = 0 let emitted = 0 let skipped = 0 for await (const tuple of readTuples(opts.input)) { read++ // A region is required where the country's layout prints one. // The FR layout prints none, so an FR tuple without a region is whole. const layout = tuple.country ? layoutForCountry(tuple.country) : null const regionRequired = layout ? layoutPrintsTag(layout, "region") : true if (!tuple.locality || !tuple.postcode || !tuple.country || (!tuple.region && regionRequired)) { skipped++ continue } for (let v = 0; v < opts.variants; v++) { const synth = synthesizeHouseVenueRow(tuple as HouseVenueBaseTuple, { random }) if (!synth) { skipped++ continue } const ok = alignAndWrite( write, { raw: synth.raw, components: withConventionStreet(synth.components, tuple.country), country: tuple.country, locale: synth.locale, source, // The id covers the generated house number, street and venue as well as the admin tail. // Two rows generated in one postcode differ only in those, and an id without them // lists every row of a postcode, template and variant as one record (#2894). source_id: recipeSourceID(source, { locality: tuple.locality, region: tuple.region, postcode: tuple.postcode, country: tuple.country, template: synth.template, v: String(v), house_number: synth.components.house_number ?? undefined, street: synth.components.street ?? undefined, venue: synth.components.venue ?? undefined, }), license: LICENSE, }, synth.template, HOUSE_VENUE_PROVENANCE ) if (ok) { emitted++ } else { skipped++ } } } return { read, emitted, skipped } }, }