UnsolvedMedical / scripts /deduplicate.py
przemekch's picture
Release UnsolvedMed v2.0.0 & v1.5.0 (cleaned <400MB footprint, zero raw excerpts, strict deduplication & sub-domain verification)
ef2c648 verified
Raw History Blame Contribute Delete
2.96 kB
#!/usr/bin/env python3
"""
Deduplication and semantic signature verification script for UnsolvedMed (Section 10 & Gate 15).
Verifies:
1. Zero duplicate normalized titles across all published records.
2. Zero duplicate normalized canonical statements across all published records.
3. Zero duplicate origin_source_id across all published records (strict 1-to-1 primary literature paper mapping).
4. All 15 archived DUPLICATE calibration records have valid `duplicate_of`
targets pointing to published canonical records and `status = 'merged'`.
"""
import json
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
def norm(s: str) -> str:
return re.sub(r"[^a-z0-9]+", " ", s.lower()).strip()
def main() -> int:
published = json.loads((ROOT / "problems.json").read_text(encoding="utf-8"))
archived = [
json.loads(line)
for line in (ROOT / "data" / "archived" / "problems.jsonl").open("r", encoding="utf-8")
if line.strip()
]
archived_dups = [r for r in archived if r["publication_class"] == "DUPLICATE"]
published_ids = {r["id"] for r in published}
seen_titles = {}
seen_stmts = {}
seen_origins = {}
errors = []
for r in published:
t = norm(r["title"])
s = norm(r["statement"])
if t in seen_titles:
errors.append(f"Duplicate normalized title between {r['id']} and {seen_titles[t]}")
else:
seen_titles[t] = r["id"]
if s in seen_stmts:
errors.append(f"Duplicate normalized statement between {r['id']} and {seen_stmts[s]}")
else:
seen_stmts[s] = r["id"]
origin_sid = next((ss["source_id"] for ss in r["source_support"] if ss["role"] == "problem_origin"), None)
if origin_sid in seen_origins:
errors.append(f"Duplicate problem_origin source {origin_sid} between {r['id']} and {seen_origins[origin_sid]}")
else:
seen_origins[origin_sid] = r["id"]
if len(archived_dups) != 15:
errors.append(f"Expected 15 archived DUPLICATE records, found {len(archived_dups)}")
for d in archived_dups:
if d["status"] != "merged" or d["rejection_code"] != "R_DUPLICATE":
errors.append(f"Archived duplicate {d['id']} has invalid status/rejection_code")
if d.get("duplicate_of") not in published_ids:
errors.append(f"Archived duplicate {d['id']} points to missing canonical target {d.get('duplicate_of')}")
if errors:
print(f"DEDUPLICATION CHECK FAILED ({len(errors)} errors):")
for e in errors[:20]:
print(" -", e)
return 1
print(
f"DEDUPLICATION CHECK PASSED: {len(published):,} unique published titles, statements & origin papers, "
f"0 unmerged duplicates, and {len(archived_dups)} verified merged duplicate audit records."
)
return 0
if __name__ == "__main__":
sys.exit(main())