File size: 2,962 Bytes
3cd21f3
 
ef2c648
3cd21f3
ef2c648
 
 
 
3cd21f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef2c648
 
3cd21f3
ef2c648
3cd21f3
 
ef2c648
3cd21f3
 
 
 
ef2c648
3cd21f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef2c648
 
 
 
 
 
3cd21f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef2c648
3cd21f3
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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())