#!/usr/bin/env python3 """Agreement between keyword coding and manual judgement. Usage: python3 -I scripts/agreement.py coded_kw.jsonl manual/judgement.csv judgement.csv: idx (row order in coded file) or url, relevant (yes/adjacent/no), then optional manual codes; empty code = not coded by hand (relevance only).""" import csv, json, sys, collections coded = [json.loads(l) for l in open(sys.argv[1])] by_url = {c["url"]: c for c in coded} rows = list(csv.DictReader(open(sys.argv[2]))) FIELDS = ["target_host", "app_type", "framework", "model_provider", "topic", "error_type", "resolution_type", "ai_generated_app"] def get(r): if r.get("url"): return by_url.get(r["url"]) return coded[int(r["idx"])] rel = collections.Counter() for r in rows: c = get(r) if c is None: continue rel[(c["kw_relevant"], r["relevant"])] += 1 n = sum(rel.values()) print(f"relevance (kw, manual) on {n}: {dict(rel)}") yes_kw = sum(v for (k, m), v in rel.items() if k == "yes") print(f" precision of kw 'yes' vs manual yes: {sum(v for (k, m), v in rel.items() if k == 'yes' and m == 'yes')}/{yes_kw}") man_yes = sum(v for (k, m), v in rel.items() if m == "yes") print(f" recall of kw 'yes' over manual yes: {rel[('yes', 'yes')]}/{man_yes}") print(f" manual precision of the collected set: {man_yes}/{n} = {man_yes / n:.2f}") coded_rows = [r for r in rows if r["relevant"] == "yes" and r.get("app_type")] print(f"\nfield agreement on {len(coded_rows)} hand-coded relevant threads:") for f in FIELDS: ok = tot = 0 for r in coded_rows: c = get(r) kw = c["kw_" + f] if "kw_" + f in c else c.get(f) if f == "topic" and kw is None: # coded files from before 2026-10-30 kw = c.get("kw_problem_category") man = r.get(f) if f in r else r.get("problem_category", "") # judgement files keep the old column name if f == "model_provider": # compare as sets ok += set((kw or "").split(";")) == set(man.split(";")) else: ok += (kw or "") == man tot += 1 print(f" {f:18s} {ok}/{tot} = {ok / tot:.2f}")