File size: 2,839 Bytes
a6a2c69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""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))