Download scripts/04_variants.py from arthrod/edgar_segmenter_artifacts: direct link, hf CLI and curl.
- Browser
- Download file 6.5 kB
-
https://huggingface.co/datasets/arthrod/edgar_segmenter_artifacts/resolve/main/scripts/04_variants.py
- Command line
-
hf download hf://datasets/arthrod/edgar_segmenter_artifacts/scripts/04_variants.py
-
curl -L -o 04_variants.py https://huggingface.co/datasets/arthrod/edgar_segmenter_artifacts/resolve/main/scripts/04_variants.py
6.5 kB
| """Gold variants of the annotated SOT (Arthur, 2026-09-23): enumerated items strict and not, | |
| signature blocks strict and not. | |
| sot_v_strict.jsonl copy of sot_annotated.jsonl as it is now | |
| sot_v_enum.jsonl enumeration relaxed: an item whose text starts with (i) ... (viii) or | |
| (w), (x), (y), whose parent is at level 2 or deeper and whose parent's | |
| text does not end with ":" is merged, with its descendants, into that | |
| parent ("For now, assume only (i)...(viii) and (w), (x), (y) that are | |
| children of a L2 or higher and that is NOT preceded by ':'") | |
| sot_v_nosig.jsonl signature blocks (full_marker "signature block") removed; used to train | |
| only: scoring drops them by region ("Drop from scoring") | |
| sot_v_enum_nosig.jsonl both | |
| Parent: the nearest earlier non-garbage row (by order) with a lower level. A merge needs the | |
| item to follow its parent directly, counting only non-garbage rows and rows already merged into | |
| that parent; otherwise the item is left as it is and reported. The merged row keeps the | |
| parent's row_id, order, level and marker; its range runs from the parent's start to the last | |
| merged row's end (garbage rows inside it stay as their own rows, as elsewhere in the SOT); | |
| segment_text joins the texts. Writes sot_variants.report.json with every merge and skip. | |
| Usage: uv run python claude_run/NEW/scripts/04_variants.py | |
| """ | |
| import os | |
| import json | |
| import re | |
| import shutil | |
| import sys | |
| from collections import defaultdict | |
| from pathlib import Path | |
| ROOT = Path(os.environ.get("SEG_ROOT") or Path(__file__).resolve().parents[1]).resolve() # SEG_ROOT: another document set (new100/) | |
| REPO = Path(__file__).resolve().parents[3] # the repo, whatever SEG_ROOT is | |
| sys.path.insert(0, str(REPO)) | |
| from engines.projection_core import snapshot # noqa: E402 | |
| ITEM = re.compile(r"\s*\((i|ii|iii|iv|v|vi|vii|viii|w|x|y)\)") | |
| LATER_ROMAN = re.compile(r"\s*\((ix|x[ivx]*|xv[i]*)\)") | |
| def lvl(r): | |
| v = r.get("level") | |
| return int(v) if v is not None and str(v).lstrip("-").isdigit() else None | |
| def relax_enum(rows, texts, report): | |
| by = defaultdict(list) | |
| for r in rows: by[r["document_id"]].append(r) | |
| out = [] | |
| for did, rr in by.items(): | |
| rr.sort(key=lambda r: int(r["order"])) | |
| content = [r for r in rr if not r.get("is_garbage")] | |
| into = {} # row_id -> parent row_id it was merged into | |
| for i, r in enumerate(content): | |
| t = r.get("segment_text") or "" | |
| if not ITEM.match(t) or lvl(r) is None or r["row_id"] in into: continue | |
| par = next((content[j] for j in range(i - 1, -1, -1) if lvl(content[j]) is not None and lvl(content[j]) < lvl(r)), None) | |
| if par is None or lvl(par) < 2: continue | |
| if (par.get("segment_text") or "").rstrip().endswith(":"): continue | |
| root = into.get(par["row_id"], par["row_id"]) | |
| between = content[content.index(par) + 1:i] | |
| if any(into.get(b["row_id"]) != root for b in between): | |
| report["enum_skipped_not_adjacent"].append({"document_id": did, "order": r["order"], "parent_order": par["order"], "text": " ".join(t.split())[:90]}); continue | |
| into[r["row_id"]] = root | |
| report["enum_merged"].append({"document_id": did, "order": r["order"], "into_order": next(x["order"] for x in content if x["row_id"] == root), | |
| "parent_level": lvl(par), "text": " ".join(t.split())[:90]}) | |
| for d in content[i + 1:]: # the item's descendants | |
| if lvl(d) is None or lvl(d) <= lvl(r): break | |
| into[d["row_id"]] = root | |
| for i, r in enumerate(content): | |
| t = r.get("segment_text") or "" | |
| if LATER_ROMAN.match(t) and r["row_id"] not in into: | |
| report["later_roman_not_in_rule"].append({"document_id": did, "order": r["order"], "text": " ".join(t.split())[:90]}) | |
| merged = defaultdict(list) | |
| for k, v in into.items(): merged[v].append(k) | |
| byid = {r["row_id"]: r for r in rr} | |
| for r in rr: | |
| if r["row_id"] in into: continue | |
| if r["row_id"] in merged: | |
| parts = [r] + sorted((byid[k] for k in merged[r["row_id"]]), key=lambda x: int(x["order"])) | |
| s = r["source"]["ranges"][0]["start"]; e = max(p["source"]["ranges"][-1]["end"] for p in parts) | |
| r = {**r, "source": {**r["source"], "ranges": [{"start": s, "end": e}]}, "literal": texts[did][s:e], | |
| "segment_text": " ".join((p.get("segment_text") or "").strip() for p in parts), "variant": "enum_relaxed", | |
| "merged_row_ids": [p["row_id"] for p in parts[1:]]} | |
| out.append(r) | |
| return out | |
| def main(): | |
| texts = {} | |
| for line in (ROOT / "sample/sot_input.jsonl").open(): | |
| d = json.loads(line); texts[d["id"]] = snapshot(d)["text"] | |
| rows = [json.loads(line) for line in (ROOT / "sot_annotated.jsonl").open() if line.strip()] | |
| shutil.copyfile(ROOT / "sot_annotated.jsonl", ROOT / "sot_v_strict.jsonl") | |
| report = {"enum_merged": [], "enum_skipped_not_adjacent": [], "later_roman_not_in_rule": []} | |
| enum = relax_enum(rows, texts, report) | |
| sig = lambda r: re.sub(r'\[(?:\d+|"[^"]*")\]', "", str(r.get("full_marker") or "").strip()).lower().replace("-", " ").endswith("signature block") # "signature block", "signature-block", "exhibit-a-signature-block" | |
| variants = {"sot_v_enum.jsonl": enum, "sot_v_nosig.jsonl": [r for r in rows if not sig(r)], "sot_v_enum_nosig.jsonl": [r for r in enum if not sig(r)]} | |
| for name, rr in variants.items(): | |
| with (ROOT / name).open("w") as fh: | |
| for r in rr: fh.write(json.dumps(r, ensure_ascii=False) + "\n") | |
| report["signature_rows_removed"] = sum(sig(r) for r in rows) | |
| (ROOT / "sot_variants.report.json").write_text(json.dumps(report, indent=1, ensure_ascii=False)) | |
| print(json.dumps({"strict": len(rows), **{k: len(v) for k, v in variants.items()}, "merged_items": len(report["enum_merged"]), | |
| "parents": len({(x["document_id"], x["into_order"]) for x in report["enum_merged"]}), | |
| "skipped_not_adjacent": len(report["enum_skipped_not_adjacent"]), "later_roman_not_in_rule": len(report["later_roman_not_in_rule"]), | |
| "signature_rows": report["signature_rows_removed"]})) | |
| if __name__ == "__main__": | |
| main() | |