Datasets:
Download scripts/build_dataset.py from PiotrSty/sejm-committee-transcripts: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/datasets/PiotrSty/sejm-committee-transcripts/resolve/main/scripts/build_dataset.py
- Command line
-
hf download hf://datasets/PiotrSty/sejm-committee-transcripts/scripts/build_dataset.py
-
curl -L -o build_dataset.py https://huggingface.co/datasets/PiotrSty/sejm-committee-transcripts/resolve/main/scripts/build_dataset.py
2.84 kB
| """Build final dataset from extracted turns: dedup, artifacts, parquet.""" | |
| import json, pathlib, hashlib, re, sys | |
| from collections import Counter | |
| CACHE = pathlib.Path("sejm_cache") | |
| OUT = pathlib.Path("build_out"); OUT.mkdir(exist_ok=True) | |
| rows = [] | |
| for term in (10, 9): | |
| f = CACHE / "parsed" / f"turns_{term}.jsonl" | |
| if f.exists(): | |
| rows += [json.loads(l) for l in open(f, encoding="utf-8")] | |
| n_raw = len(rows) | |
| # sitting-level dedup: joint sittings produce identical content under two codes | |
| from collections import defaultdict | |
| sittings = defaultdict(list) | |
| for r in rows: | |
| sittings[(r["term"], r["committee_code"], r["sitting_num"])].append(r) | |
| by_sit = sorted(sittings.values(), key=lambda g: (g[0]["term"], g[0]["committee_code"], g[0]["sitting_num"])) | |
| keep, seen_keys, dedup_dropped = [], set(), 0 | |
| for group in by_sit: | |
| key = (group[0]["term"], group[0]["content_sha1"]) | |
| if key in seen_keys: | |
| dedup_dropped += len(group) | |
| continue | |
| seen_keys.add(key) | |
| keep.extend(sorted(group, key=lambda r: r["turn_idx"])) | |
| rows = keep | |
| # exact duplicate turns (same sitting, same speaker+text adjacent) | |
| final = [] | |
| exact_dup = 0 | |
| prev = None | |
| for r in rows: | |
| if prev and prev["term"] == r["term"] and prev["committee_code"] == r["committee_code"] \ | |
| and prev["sitting_num"] == r["sitting_num"] and prev["speaker"] == r["speaker"] and prev["text"] == r["text"]: | |
| exact_dup += 1 | |
| continue | |
| final.append(r) | |
| prev = r | |
| rows = final | |
| chars = sum(len(r["text"]) for r in rows) | |
| sittings = len({(r["term"], r["committee_code"], r["sitting_num"]) for r in rows}) | |
| committees = len({(r["term"], r["committee_code"]) for r in rows}) | |
| speakers = len({r["speaker"] for r in rows}) | |
| by_term = Counter(r["term"] for r in rows) | |
| by_role = Counter(r["speaker"].split()[0] for r in rows) | |
| dates = [r["date"] for r in rows if r["date"]] | |
| audit = { | |
| "rows_raw_extracted": n_raw, | |
| "rows_joint_sitting_dedup_dropped": dedup_dropped, | |
| "rows_exact_duplicate_dropped": exact_dup, | |
| "rows_final": len(rows), | |
| "chars_final": chars, | |
| "sittings": sittings, | |
| "committees": committees, | |
| "unique_speaker_labels": speakers, | |
| "date_min": min(dates) if dates else None, | |
| "date_max": max(dates) if dates else None, | |
| "by_term": dict(by_term), | |
| "top_speaker_roles": dict(by_role.most_common(15)), | |
| } | |
| (OUT / "audit.json").write_text(json.dumps(audit, ensure_ascii=False, indent=2), encoding="utf-8") | |
| with open(OUT / "committee_transcripts.jsonl", "w", encoding="utf-8") as fh: | |
| for r in rows: | |
| fh.write(json.dumps(r, ensure_ascii=False) + "\n") | |
| print(json.dumps(audit, ensure_ascii=False, indent=1)) | |
| # parquet | |
| import pandas as pd | |
| (OUT / "data").mkdir(exist_ok=True) | |
| df = pd.DataFrame(rows) | |
| df.to_parquet(OUT / "data" / "train.parquet", index=False) | |
| print("parquet rows:", len(df)) | |