// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * `ban`: Base Adresse Nationale CSV adapter, street-level over eleven jurisdictions. * * Input is a semicolon-separated CSV dump from `adresse.data.gouv.fr`. * The adapter reads `numero` as `house_number`, `rep` as a repetition index appended to it, * `nom_voie` as `street`, `code_postal` as `postcode`, and `nom_commune` as `locality`. * * BAN holds no region column, so region is left to the wof-postalcode * and wof-admin cross-reference at corpus build time. * * IGN publishes one file per INSEE department beside the whole-country extract, * and the overseas departments hold the same 23-column header as the metropolitan rows. * A row's country is therefore derived from `code_insee` rather than asserted by the caller. * * That derivation reads the same whether the input is one department's file or an extract spanning several. * * The official BAN is dual-licensed under Licence Ouverte 2.0 and ODbL. * This adapter elects Licence Ouverte 2.0. * * Its terms permit training with attribution. * It records that license on every row. * The model card must include BAN attribution. * * The adapter streams with `CSVSpliterator.fromAsync`, so a 25M-row file never sits in memory. * It honors `opts.limit` and `opts.signal`. * * `opts.country` is optional: when set it both rejects a jurisdiction BAN doesn't publish * and keeps only the rows of the one requested. */ import { formatAddressRow } from "@mailwoman/codex/address/format" import { hashFNV1a } from "@mailwoman/core/coarse-placer/fnv-hash" import { isPresent } from "@mailwoman/core/values/objects" import { CSVSpliterator } from "spliterator" import { UnsupportedCountryError } from "#adapters/errors" import { stableSourceID } from "#adapters/source-id" import { composeHouseNumber } from "#adapters/street/line" import { createUnitSplitter } from "#adapters/street/unit" import { countryOfInseeCode, INSEE_COUNTRIES } from "#fr/insee-country" import { decomposeFrStreet } from "#fr/street-decompose" import { SourceRegister } from "#registers" import { streetIndexOpener } from "#street-index/reader" import { AddressRole, type AdapterOptions, type CanonicalRow, type CorpusAdapter, countDropped, SurfaceOrigin, } from "#types" /** * Registry id for this adapter. * * Stamped into every row it emits, so a corpus record can be traced back to the dataset it came from. */ export const BAN_ADAPTER_ID = "ban" /** * Subset of BAN CSV columns the adapter consults. * * Everything else is ignored. * The explicit shape catches column-name drift early if BAN evolves its schema. */ interface BanRow { id: string numero: string rep: string nom_voie: string code_postal: string code_insee: string nom_commune: string } /** * Number of hash buckets per 100 rows rendered in French postcode-first or separator formats * (5% total: 1% per variant, preserving canonical street-first rendering for 95% of BAN rows). */ const POSTCODE_FIRST_BUCKET_MODULUS = 100 const POSTCODE_FIRST_VARIANT_COUNT = 5 /** * Renders a deterministic 5% share of postcode-carrying, unit-free BAN rows in observed French * postcode-first and comma/dash separator shapes, returning `null` for the 95% canonical majority. */ function maybeFormatPostcodeFirstRow( components: CanonicalRow["components"], sourceID: string ): { raw: string; components: CanonicalRow["components"] } | null { if (!components.postcode || !components.locality || components.unit) return null const bucket = hashFNV1a(`ban-postcode-first:${sourceID}`) % POSTCODE_FIRST_BUCKET_MODULUS if (bucket >= POSTCODE_FIRST_VARIANT_COUNT) return null const { postcode, locality } = components const streetLine = [components.house_number, components.street_prefix, components.street].filter(isPresent).join(" ") if (!streetLine) return null switch (bucket) { case 0: // `47110, 14 Rue Mozart, Sainte-Livrade-sur-Lot` return { raw: `${postcode}, ${streetLine}, ${locality}`, components } case 1: // `37000, Tours` return { raw: `${postcode}, ${locality}`, components: { postcode, locality } } case 2: // `35650 - Le Rheu` return { raw: `${postcode} - ${locality}`, components: { postcode, locality } } case 3: // `47110, Sainte-Livrade-sur-Lot, 14 Rue Mozart` return { raw: `${postcode}, ${locality}, ${streetLine}`, components } case 4: // `47110 - 14 Rue Mozart, Sainte-Livrade-sur-Lot` return { raw: `${postcode} - ${streetLine}, ${locality}`, components } default: return null } } export function createBanAdapter(): CorpusAdapter { return { id: BAN_ADAPTER_ID, defaultLicense: "Licence Ouverte 2.0", addressRole: AddressRole.Premise, register: SourceRegister.BaseAdresseNationale, surface: SurfaceOrigin.Rendered, description: "Base Adresse Nationale: house-number-level street addresses for France and ten overseas jurisdictions.", async *rows(opts: AdapterOptions): AsyncIterable { if (opts.country && !INSEE_COUNTRIES.includes(opts.country)) { throw new UnsupportedCountryError(BAN_ADAPTER_ID, INSEE_COUNTRIES, opts.country) } const rows = CSVSpliterator.fromAsync(opts.inputPath, { normalizeKeys: false, columnDelimiter: ";", }) let emitted = 0 const units = await createUnitSplitter({ streetIndex: streetIndexOpener(opts.streetIndex) }) for await (const record of rows as AsyncIterable) { if (opts.signal?.aborted) break if (typeof opts.limit === "number" && emitted >= opts.limit) break const country = countryOfInseeCode(record.code_insee ?? "") // A caller that requested one jurisdiction gets that jurisdiction's rows, // so one extract spanning several departments can be read once per country. if (opts.country && country !== opts.country) continue // BAN stores a repetition index ("bis", "ter", "quater") in `rep`, // which follows the number as a separate word. const house = composeHouseNumber(record.numero ?? "", record.rep ?? "", " ") const street = record.nom_voie ?? "" const postcode = record.code_postal ?? "" const locality = record.nom_commune ?? "" // A row with a number and a commune but no street name is a premise BAN numbers // within the commune, as all 958 of French Polynesia's Pirae rows are. // It renders no street line, so it is refused and counted. if (!street) { countDropped(opts, "row:street-absent") continue } if (!locality) { countDropped(opts, "row:locality-absent") continue } if (!house && !postcode) { countDropped(opts, "row:house-number-and-postcode-absent") continue } // A few streets end in the building they name (`Rue du General Charles de Gaulle Bat B`): // the building becomes the unit and the street keeps the rest. const withUnit = units.split(street, { country, locality, postcode }) const decomposed = decomposeFrStreet(withUnit?.street ?? street) const components: CanonicalRow["components"] = {} if (house) { components.house_number = house } if (withUnit) { components.unit = withUnit.unit } if (decomposed.prefix) { components.street_prefix = decomposed.prefix } if (decomposed.street) { components.street = decomposed.street } if (postcode) { components.postcode = postcode } if (locality) { components.locality = locality } // BAN repeats an `id` across the postcodes of a merged commune: `27213_0028_00001` is // `1 Grande Rue` in each of Vexin-sur-Epte's 27420, 27510 and 27630 villages (#2894). // The postcode in the id keeps those addresses apart. const recordID = record.id ? `${BAN_ADAPTER_ID}-${record.id}${postcode ? `-${postcode}` : ""}` : null const variant = maybeFormatPostcodeFirstRow(components, recordID ?? stableSourceID(BAN_ADAPTER_ID, components)) const rendered = variant ?? formatAddressRow(components, country, { singleLine: true }) if (!rendered) { countDropped(opts, "row:render-failed") continue } const { raw, components: aligned } = rendered const sourceID = recordID ?? stableSourceID(BAN_ADAPTER_ID, aligned) yield { raw, components: aligned, country, // Every jurisdiction BAN publishes writes its addresses in French, // so the language is constant and the region holds the jurisdiction. locale: `fr-${country}`, source: BAN_ADAPTER_ID, source_id: sourceID, license: "Licence Ouverte 2.0", } emitted++ } }, } } /** * The configured adapter instance registered with the corpus builder. */ export const banAdapter = createBanAdapter()