UnsolvedEarth / scripts /check_identifiers.py
przemekch's picture
Release UnsolvedEarth v1.0.0: 10,000 open/partially solved Earth & environmental science problems + 100 audit records
1c951bb verified
Raw History Blame Contribute Delete
1.83 kB
#!/usr/bin/env python3
"""
Identifier integrity checker for UnsolvedEarth v1.0.0.
Verifies DOI syntax, UEARTH-xxxxxx problem IDs, SRC-EARTH-xxxxxx source IDs,
and REL-EARTH-xxxxxx relation IDs across all JSON and JSONL files.
"""
import json
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
DOI_REGEX = re.compile(r"^10\.\d{4,9}/\S+$")
def main():
problems = json.loads((ROOT / "problems.json").read_text(encoding="utf-8"))
sources = json.loads((ROOT / "source_registry.json").read_text(encoding="utf-8"))
errors = []
doi_count = 0
for s in sources:
sid = s.get("source_id", "")
if not re.match(r"^SRC-EARTH-\d{6}$", sid):
errors.append(f"Invalid source_id: {sid}")
ids = s.get("identifiers") or {}
doi = ids.get("doi")
if doi:
if not DOI_REGEX.match(doi):
errors.append(f"{sid}: Malformed DOI '{doi}'")
else:
doi_count += 1
elif not (ids.get("isbn") or ids.get("report_number")):
errors.append(f"{sid}: Missing DOI, ISBN, and report_number")
for p in problems:
pid = p.get("id", "")
if not re.match(r"^UEARTH-\d{6}$", pid):
errors.append(f"Invalid problem id: {pid}")
sids = p.get("source_identifiers") or {}
doi = sids.get("doi")
if doi and not DOI_REGEX.match(doi):
errors.append(f"{pid}: Malformed source DOI '{doi}'")
print(f"Checked {len(problems)} problems and {len(sources)} sources ({doi_count} valid DOIs).")
if errors:
print(f"FAILED with {len(errors)} identifier errors:")
for e in errors[:20]:
print(" ", e)
sys.exit(1)
print("IDENTIFIER CHECK PASSED: 0 errors.")
if __name__ == "__main__":
main()