Download validate.py from sumankrsh/corvane-claim-notes: direct link, hf CLI and curl.
- Browser
- Download file 4.16 kB
-
https://huggingface.co/datasets/sumankrsh/corvane-claim-notes/resolve/main/validate.py
- Command line
-
hf download hf://datasets/sumankrsh/corvane-claim-notes/validate.py
-
curl -L -o validate.py https://huggingface.co/datasets/sumankrsh/corvane-claim-notes/resolve/main/validate.py
4.16 kB
| """Validate corvane-claim-notes and print the figures used in its card. | |
| python validate.py [FOLDER] # exit code 0 = pass | |
| Checks: row counts, schema, no empty fields, span offsets match the text, labels known, every identifier | |
| follows a clearly fake format, every long digit run sits inside a labelled span, no note repeated across | |
| splits, and held-out names do not appear in train or validation. Author: Suman Mukherjee. | |
| """ | |
| import collections | |
| import json | |
| import re | |
| import sys | |
| from pathlib import Path | |
| root = Path(sys.argv[1] if len(sys.argv) > 1 else Path(__file__).parent) | |
| EXPECT = {"train": 2100, "validation": 450, "heldout": 450} | |
| TYPES = {"death", "accidental_death", "critical_illness", "hospital_cash", "disability"} | |
| URG = {"high", "normal", "low"} | |
| FAKE = { | |
| "PHONE": re.compile(r"^(\(\+91\) |\+91[ -]?)?00[\d -]{8,11}$"), | |
| "EMAIL": re.compile(r"^[\w.]+@example\.(com|org|net)$", re.I), | |
| "POLICY_NO": re.compile(r"^CVL[-/]TL[-/]\d{6}$", re.I), | |
| "CLAIM_NO": re.compile(r"^CLM[-/]2026[-/]\d{5}$"), | |
| "PAN": re.compile(r"^ZZ[A-Z]X[A-Z] ?\d{4} ?[A-Z]$"), | |
| "AADHAAR": re.compile(r"^0\d{3}[ -]\d{4}[ -]\d{4}$"), | |
| "PERSON": re.compile(r"^[A-Z][a-z]+( [A-Z][a-z]+)?$"), | |
| } | |
| fails, rows = [], {} | |
| for split, n in EXPECT.items(): | |
| rows[split] = [json.loads(line) for line in open(root / "data" / f"{split}.jsonl", encoding="utf-8")] | |
| if len(rows[split]) != n: | |
| fails.append(f"{split}: {len(rows[split])} rows, expected {n}") | |
| ids, texts, names = set(), {}, collections.defaultdict(set) | |
| for split, rs in rows.items(): | |
| for r in rs: | |
| for k in ("note_id", "split", "claim_type", "urgency", "text", "pii_spans"): | |
| if k not in r or r[k] in ("", None): | |
| fails.append(f"{r.get('note_id')}: empty {k}") | |
| if r["note_id"] in ids: fails.append(f"duplicate id {r['note_id']}") | |
| ids.add(r["note_id"]) | |
| if r["split"] != split: fails.append(f"{r['note_id']} in wrong split") | |
| if r["claim_type"] not in TYPES: fails.append(f"{r['note_id']} bad claim_type") | |
| if r["urgency"] not in URG: fails.append(f"{r['note_id']} bad urgency") | |
| if r["text"] in texts and texts[r["text"]] != split: fails.append(f"{r['note_id']} text repeated across splits") | |
| texts.setdefault(r["text"], split) | |
| covered = [False] * len(r["text"]) | |
| for s in r["pii_spans"]: | |
| if r["text"][s["start"]:s["end"]] != s["text"]: fails.append(f"{r['note_id']} span offset mismatch {s}") | |
| if s["label"] not in FAKE: fails.append(f"{r['note_id']} unknown label {s['label']}") | |
| elif not FAKE[s["label"]].match(s["text"]): fails.append(f"{r['note_id']} {s['label']} not in the fake format: {s['text']}") | |
| if s["label"] == "PERSON": names[split].add(s["text"].split()[-1]) | |
| for i in range(s["start"], s["end"]): covered[i] = True | |
| for m in re.finditer(r"\d[\d -]{5,}\d", r["text"]): | |
| digits = re.sub(r"\D", "", m.group()) | |
| if len(digits) >= 7 and not all(covered[m.start():m.end()]): | |
| fails.append(f"{r['note_id']} unlabelled digit run {m.group()}") | |
| if re.search(r"metlife|stryker|circle k|deloitte|genpact|exponentia", r["text"], re.I): fails.append(f"{r['note_id']} employer name") | |
| leak = names["heldout"] & (names["train"] | names["validation"]) | |
| if leak: fails.append(f"held-out names also in train/validation: {sorted(leak)[:5]}") | |
| print(f"notes: {sum(len(v) for v in rows.values())} ({', '.join(f'{k} {len(v)}' for k, v in rows.items())})") | |
| for split, rs in rows.items(): | |
| ct = collections.Counter(r["claim_type"] for r in rs); ur = collections.Counter(r["urgency"] for r in rs) | |
| sp = collections.Counter(s["label"] for r in rs for s in r["pii_spans"]) | |
| words = [len(r["text"].split()) for r in rs] | |
| print(f"{split}: types {dict(sorted(ct.items()))} | urgency {dict(sorted(ur.items()))}") | |
| print(f" spans {sum(sp.values())} {dict(sorted(sp.items()))} | words per note {min(words)}-{max(words)}, mean {sum(words)/len(words):.0f}") | |
| print("FAIL" if fails else "PASS", "dataset validation", *fails[:20], sep="\n " if fails else " ") | |
| sys.exit(1 if fails else 0) | |