mirror of
https://github.com/bitbaum/solon.git
synced 2026-12-05 07:28:04 +01:00
The 710 decided federal popular votes of the Swissvotes dataset become law events beside the ones written up by hand: result, chamber votes, who started it, who recommended what. Votes since 1995 chart the statistics of their policy area, with a caveat that says how they were chosen. Each proposal on /places/votes links to its event and back to the communes. The events list now filters and pages in the database, and a statistic's page lists ten events and links the rest. Fixes the deciding question of 8 March 2026, which /places/votes showed on the counter-proposal: the FSO's kind for it is 6, not 5. Claude-Session: https://claude.ai/code/session_019MDtmvTCDbJAYNKQpNyKzz Co-authored-by: Claude <noreply@anthropic.com>
156 lines
5.6 KiB
JavaScript
156 lines
5.6 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* Writes every decided federal popular vote since 1848 from the Swissvotes
|
||
* dataset (Année Politique Suisse, University of Bern, CC BY 4.0):
|
||
* src/lib/config/statistics/swissvotes.json
|
||
* One row per proposal: what was voted on, who stood where, the result, and
|
||
* the FSO's number for the same proposal where Solon holds its commune results
|
||
* (src/lib/config/places/federal-votes.json), matched by vote day and yes share.
|
||
* config/statistics/events-swissvotes.ts turns the rows into law events.
|
||
*
|
||
* node scripts/fetch-swissvotes.mjs [path to an already downloaded CSV]
|
||
*/
|
||
import { readFileSync, writeFileSync } from "node:fs";
|
||
|
||
const SOURCE = "https://swissvotes.ch/page/dataset/swissvotes_dataset.csv";
|
||
const OUT = "src/lib/config/statistics/swissvotes.json";
|
||
const FSO = "src/lib/config/places/federal-votes.json";
|
||
|
||
const text = process.argv[2]
|
||
? readFileSync(process.argv[2], "utf8")
|
||
: await (await fetch(SOURCE, { redirect: "follow" })).text();
|
||
|
||
/** Semicolon-separated, quoted where a cell holds a semicolon, a quote or a line break. */
|
||
function parseCsv(input) {
|
||
const rows = [];
|
||
let row = [];
|
||
let cell = "";
|
||
let quoted = false;
|
||
for (let i = 0; i < input.length; i++) {
|
||
const c = input[i];
|
||
if (quoted) {
|
||
if (c === '"' && input[i + 1] === '"') {
|
||
cell += '"';
|
||
i++;
|
||
} else if (c === '"') quoted = false;
|
||
else cell += c;
|
||
} else if (c === '"') quoted = true;
|
||
else if (c === ";") {
|
||
row.push(cell);
|
||
cell = "";
|
||
} else if (c === "\n") {
|
||
row.push(cell.replace(/\r$/, ""));
|
||
rows.push(row);
|
||
row = [];
|
||
cell = "";
|
||
} else cell += c;
|
||
}
|
||
if (cell || row.length) {
|
||
row.push(cell);
|
||
rows.push(row);
|
||
}
|
||
return rows;
|
||
}
|
||
|
||
const [header, ...lines] = parseCsv(text.replace(/^/, ""));
|
||
const records = lines
|
||
.filter((l) => l.length === header.length)
|
||
.map((l) => Object.fromEntries(header.map((h, i) => [h, l[i].trim()])));
|
||
|
||
const missing = (v) => v === "" || v === "." || v === "9999";
|
||
const num = (v) => (missing(v) ? null : Number(v.replace(/[’']/g, "")));
|
||
const day = (v) => {
|
||
const m = /^(\d{2})\.(\d{2})\.(\d{4})$/.exec(v);
|
||
return m ? `${m[3]}-${m[2]}-${m[1]}` : null;
|
||
};
|
||
const textOf = (v) => (missing(v) ? null : v);
|
||
|
||
/** The dataset writes the third level without its dot: 3.21 is 3.2.1, 10.21 is 10.2.1. */
|
||
function domain(r, n) {
|
||
const [e1, e2, e3] = [r[`d${n}e1`], r[`d${n}e2`], r[`d${n}e3`]];
|
||
if (!missing(e3)) {
|
||
const [top, rest] = e3.split(".");
|
||
return `${top}.${rest[0]}.${rest.slice(1)}`;
|
||
}
|
||
if (!missing(e2)) return e2;
|
||
return missing(e1) ? null : e1;
|
||
}
|
||
|
||
// Parties, associations and conferences of cantonal directors, in the dataset's order.
|
||
const ACTORS = header.filter((h) => /^p-[a-z]+$/.test(h)).map((h) => h.slice(2));
|
||
|
||
const CANTONS =
|
||
"zh be lu ur sz ow nw gl zg fr so bs bl sh ar ai sg gr ag tg ti vd vs ne ge ju".split(" ");
|
||
const HALF = new Set(["ow", "nw", "bs", "bl", "ar", "ai"]);
|
||
|
||
/**
|
||
* Cantonal votes, a half-canton counting a half, counted from each canton's own result. The
|
||
* dataset's totals are rounded to whole numbers in many rows (5.5 to 17.5 reads 6 to 18) and
|
||
* swapped in one, so they are not read. On a deciding question a canton's yes is the initiative.
|
||
*/
|
||
function cantonalVotes(r) {
|
||
let yes = 0;
|
||
let no = 0;
|
||
for (const c of CANTONS) {
|
||
const code = r[`${c}-annahme`];
|
||
const weight = HALF.has(c) ? 0.5 : 1;
|
||
if (code === "1" || code === "9") yes += weight;
|
||
else if (code === "0" || code === "8") no += weight;
|
||
}
|
||
return yes + no > 0 ? [yes, no] : null;
|
||
}
|
||
|
||
const fso = JSON.parse(readFileSync(FSO, "utf8")).filter((v) => v.counted);
|
||
|
||
const out = [];
|
||
for (const r of records) {
|
||
const date = day(r.datum);
|
||
const form = num(r.rechtsform);
|
||
const yes = num(r["volkja-proz"]);
|
||
// A vote still to come has no result; it becomes an event once it has one.
|
||
if (!date || !form || yes === null) continue;
|
||
const stances = { yes: [], no: [], free: [] };
|
||
for (const a of ACTORS) {
|
||
const code = r[`p-${a}`];
|
||
if (code === "1") stances.yes.push(a);
|
||
else if (code === "2") stances.no.push(a);
|
||
else if (code === "5") stances.free.push(a);
|
||
}
|
||
// The same proposal in the FSO's files: same day, same yes share to a tenth of a point.
|
||
const same = fso.filter((v) => v.date === date && Math.abs(v.yesPercent - yes) < 0.1);
|
||
const pair = (a, b) => (num(r[a]) === null || num(r[b]) === null ? null : [num(r[a]), num(r[b])]);
|
||
const title = { de: r.titel_kurz_d, fr: r.titel_kurz_f };
|
||
if (!missing(r.titel_kurz_e)) title.en = r.titel_kurz_e;
|
||
out.push({
|
||
anr: r.anr,
|
||
date,
|
||
form,
|
||
title,
|
||
domains: [1, 2, 3].map((n) => domain(r, n)).filter(Boolean),
|
||
council: num(r["br-pos"]),
|
||
parliament: num(r["bv-pos"]),
|
||
nationalCouncil: pair("nrja", "nrnein"),
|
||
councilOfStates: pair("srja", "srnein"),
|
||
signatures: num(r.unter_g),
|
||
collectionDays: num(r["i-dauer_samm"]) ?? num(r["fr-dauer_samm"]),
|
||
author: textOf(r.urheber),
|
||
// 1 accepted, 0 rejected; 8 and 9 on a deciding question: the counter-proposal or the
|
||
// initiative came into force; null where the deciding question did not matter.
|
||
result: num(r.annahme),
|
||
people: num(r.volk),
|
||
cantonsCount: r.stand !== "3",
|
||
yes,
|
||
turnout: num(r.bet),
|
||
cantons: cantonalVotes(r),
|
||
inForce: day(r["dat-force"]),
|
||
stances,
|
||
fso: same.length === 1 ? same[0].id : null,
|
||
});
|
||
}
|
||
out.sort((a, b) => a.date.localeCompare(b.date) || Number(a.anr) - Number(b.anr));
|
||
writeFileSync(OUT, JSON.stringify(out, null, 2) + "\n");
|
||
const matched = out.filter((v) => v.fso).length;
|
||
console.log(
|
||
`${out.length} decided proposals, ${out[0].date} to ${out.at(-1).date}; ${matched} of ${fso.length} FSO proposals matched`,
|
||
);
|