#!/usr/bin/env python3 """Merge keyword coding + manual judgements into the publishable CSV and the private verification file. Usage: python3 -I scripts/build_dataset.py CODED.jsonl OUT_PUBLIC.csv OUT_PRIVATE.csv [--all] [JUDGEMENT.csv ...] JUDGEMENT.csv rows are keyed by `url`, or by `idx` = row number in raw/coded_kw.jsonl (pilot file). Files are merged field by field in the order given: a non-empty value in a later file wins. Pass manual/judgement_v4.csv last: it holds the relevance (yes/no) and thread_type under rules v4. The judgement files call the topic column `problem_category` (its name before 2026-10-30); it is read as `topic`. A judged topic `other` predates the topics added 2026-10-30 (agent_behavior, observability, performance_scaling): when the keywords give one of those, the keyword value is kept (same for an agent_* error_type over a judged other_unknown). All judgements so far are by the AI assistant (Claude), not by a person (column judged_by). --all : write every thread with a `relevance` column (pilot.csv); default: only relevant == yes. Published file: NO text, NO author info (no title, no excerpt, no author hash). Private file : url, title, 30-word excerpt, codes — local verification only, never published. """ import csv, json, os, sys from datetime import date ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) args = [a for a in sys.argv[1:] if a != "--all"] ALL = "--all" in sys.argv coded_path, out_pub, out_priv, judg_paths = args[0], args[1], args[2], args[3:] coded = [json.loads(l) for l in open(coded_path)] pilot = [json.loads(l) for l in open(os.path.join(ROOT, "raw", "coded_kw.jsonl"))] manual = {} for jp in judg_paths: for r in csv.DictReader(open(jp)): url = r.get("url") or pilot[int(r["idx"])]["url"] m = manual.setdefault(url, {}) for k, v in r.items(): if k == "problem_category": # old name of `topic` k = "topic" if v not in ("", None): m[k] = v coded_urls = {c["url"] for c in coded} for url, m in manual.items(): if url in coded_urls and m.get("relevant") not in ("yes", "no"): sys.exit(f"{url}: relevance must be yes/no under rules v4 (got {m.get('relevant')!r}); re-judge it in manual/judgement_v4.csv") CODES = ["target_host", "app_type", "framework", "model_provider", "topic", "error_type", "resolution_type", "ai_generated_app", "ai_tool"] HAND = ["target_host", "app_type", "framework", "model_provider", "topic", "error_type", "resolution_type"] PUB = ["url", "source", "repo", "repo_group", "created_at", "closed_at", "state", "state_reason", "n_comments", "n_reactions", "maintainer_replied", "time_to_first_response_h", "time_to_close_h", "resolved", "thread_type"] + \ CODES[:3] + ["stack"] + CODES[3:] + ["coding_method", "coding_confidence", "relevance_method", "collected_on"] if ALL: PUB.insert(1, "relevance") PRIV = ["url", "title", "excerpt_private", "relevance", "relevance_method", "kw_relevant", "kw_unsure", "manual_note", "thread_type", "kw_thread_type"] + CODES + ["coding_method"] NEW_TOPICS = {"agent_behavior", "observability", "performance_scaling"} # added 2026-10-30 today = date.today().isoformat() pub_rows, priv_rows = [], [] for c in coded: m = manual.get(c["url"]) rel = m["relevant"] if m else c["kw_relevant"] row = {k: c.get(k) for k in ["url", "source", "repo", "created_at", "closed_at", "state", "state_reason", "n_comments", "n_reactions", "maintainer_replied", "time_to_first_response_h", "time_to_close_h", "resolved", "coding_confidence"]} row["repo_group"] = c["group"] row["stack"] = c.get("kw_stack") for f in CODES: row[f] = c.get("kw_" + f) hand = bool(m and any(m.get(f) for f in HAND)) if m: # manual values override when given (partial overrides allowed, e.g. ai_generated_app only) for f in CODES: if m.get(f): if f == "topic" and m[f] == "other" and row[f] in NEW_TOPICS: continue if f == "error_type" and m[f] == "other_unknown" and (row[f] or "").startswith("agent_"): continue row[f] = m[f] row["thread_type"] = (m or {}).get("thread_type") or c.get("kw_thread_type") row["coding_method"] = "keyword+agent_reviewed" if hand else "keyword" row["relevance"] = rel row["relevance_method"] = "agent_reviewed" if m else "keyword" row["collected_on"] = c.get("collected_on", today) if ALL or rel == "yes": pub_rows.append({k: row.get(k) for k in PUB}) priv_rows.append({**{k: row.get(k) for k in PRIV}, "title": c["title"], "excerpt_private": c.get("excerpt_private"), "kw_relevant": c["kw_relevant"], "kw_unsure": c.get("kw_unsure"), "kw_thread_type": c.get("kw_thread_type"), "manual_note": (m or {}).get("note")}) for path, rows, cols in ((out_pub, pub_rows, PUB), (out_priv, priv_rows, PRIV)): os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True) with open(path, "w", newline="") as f: w = csv.DictWriter(f, fieldnames=cols) w.writeheader() w.writerows(rows) print(f"{out_pub}: {len(pub_rows)} rows; {out_priv}: {len(priv_rows)} rows")