"""Garbage (page chrome) assessment against the SOT. Unit level: every A0 unit is SOT-garbage when >= 80% of its non-whitespace characters lie in SOT garbage segments or in text no SOT content segment covers; otherwise SOT-content. Detectors: A0's own artifact_candidates (possible_page_number, repeated_short_text, either) and a chrome regex (page numbers / "Page n", signature-page and exhibit footers, confidentiality legends, file names). Also: what the SOT's garbage segments are, by kind. Usage: uv run python claude_run/NEW/scripts/42_garbage.py [run_dir] """ import json import re import sys from collections import Counter, defaultdict from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUN = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / "ws_sot/runs/a0" CHROME = re.compile(r"^\W*(page\s*)?[\divxlc]{1,4}(\s*of\s*\d+)?\W*$|^\W*[A-Z]{0,3}-?\s?\d{1,3}\W*$|signature page|\[?\s*remainder of (this )?page|" r"confidential|omitted and filed separately|\.htm\b|^\W*exhibit\s+[\w.()-]+\W*$|^\W*\*+\W*$", re.I) KINDS = [("page number", re.compile(r"^\W*(page\s*)?[\divxlc]{1,4}(\s*of\s*\d+)?\W*$|^\W*[A-Z]{0,3}-\s?\d{1,3}\W*$", re.I)), ("signature-page footer", re.compile(r"signature page|remainder of (this )?page", re.I)), ("confidentiality legend", re.compile(r"confidential|omitted and filed", re.I)), ("exhibit/filing header", re.compile(r"\.htm|^\W*ex(hibit)?[\s-]*[\d.()a-z-]+\W*$", re.I)), ("empty / symbols", re.compile(r"^\W*$"))] def prf(tp, fp, fn): p = tp / (tp + fp) if tp + fp else 0.0; r = tp / (tp + fn) if tp + fn else 0.0 return p, r, (2 * p * r / (p + r) if p + r else 0.0) content = defaultdict(list); gkind = Counter(); ex = defaultdict(list) for line in (ROOT / "sot_provisioned.jsonl").open(): r = json.loads(line); x = r["source"]["ranges"][0] if not r["is_garbage"]: content[r["document_id"]].append((x["start"], x["end"])); continue t = " ".join(r["literal"].split()); k = next((n for n, rx in KINDS if rx.search(t)), "other") gkind[k] += 1 if len(ex[k]) < 4: ex[k].append(t[:70]) tot = sum(gkind.values()) print(f"SOT garbage segments placed: {tot}") for k, v in gkind.most_common(): print(f" {v:5} ({100 * v / tot:4.1f}%) {k}: {ex[k]}") cnt = defaultdict(Counter) for line in (RUN / "projection.jsonl").open(): r = json.loads(line); r = r.get("current", r) if r["record_type"] != "segment": continue did = r["source"]["document_id"]; lit = r["literal"]; a = r["source"]["ranges"][0]["start"] ns = [a + i for i, c in enumerate(lit) if not c.isspace()] if not ns: continue inside = sum(1 for p in ns if any(s <= p < e for s, e in content[did])) truth = inside <= 0.2 * len(ns) kinds = {c["kind"] for c in r["projection"]["artifact_candidates"]} det = {"A0 possible_page_number": "possible_page_number" in kinds, "A0 repeated_short_text": "repeated_short_text" in kinds, "A0 either flag": bool(kinds), "chrome regex": bool(CHROME.search(" ".join(lit.split()))), "chrome regex OR page-number flag": bool(CHROME.search(" ".join(lit.split()))) or "possible_page_number" in kinds} for name, d in det.items(): cnt[name]["tp" if d and truth else "fp" if d else "fn" if truth else "tn"] += 1 print(f"\nA0 units judged SOT-garbage: {cnt['A0 either flag']['tp'] + cnt['A0 either flag']['fn']}") print(f"{'detector':36} {'P':>6} {'R':>6} {'F1':>6} tp/fp/fn") for name, c in cnt.items(): p, r_, f = prf(c["tp"], c["fp"], c["fn"]); print(f"{name:36} {p:6.3f} {r_:6.3f} {f:6.3f} {c['tp']}/{c['fp']}/{c['fn']}")