""" validate_dataset.py =================== Pre-release validation script for GomParam-v1. Run before every release to ensure dataset integrity. Exit codes: 0 = PASS (all checks passed) 1 = FAIL (one or more checks failed) """ import json import re import sys import unicodedata from collections import Counter from pathlib import Path DATA_DIR = Path("/mnt/data/projects/GomParam-v1/data") CHECKS_PASSED = 0 CHECKS_FAILED = 0 WARNINGS = 0 def check(name, condition, detail=""): global CHECKS_PASSED, CHECKS_FAILED if condition: CHECKS_PASSED += 1 print(f" ✓ {name}") else: CHECKS_FAILED += 1 print(f" ✗ FAIL: {name}") if detail: print(f" → {detail}") def warn(name, detail=""): global WARNINGS WARNINGS += 1 print(f" ⚠ WARN: {name}") if detail: print(f" → {detail}") # ────────────────────────────────────────────── # Load all items # ────────────────────────────────────────────── print("="*60) print("GomParam-v1 Pre-Release Validation") print("="*60) json_files = sorted(DATA_DIR.glob("*.json")) check("JSON files exist", len(json_files) > 0, f"Found {len(json_files)} files") all_items = [] all_ids = [] module_counts = Counter() for json_file in json_files: module_name = json_file.stem try: with open(json_file, "r", encoding="utf-8") as f: data = json.load(f) check(f"Valid JSON: {module_name}", True) except json.JSONDecodeError as e: check(f"Valid JSON: {module_name}", False, str(e)) continue for item in data: item["_module"] = module_name all_items.append(item) all_ids.append(item.get("id", "MISSING")) module_counts[module_name] = len(data) N = len(all_items) print(f"\nTotal items: {N}") # ────────────────────────────────────────────── # Check 1: Duplicate IDs # ────────────────────────────────────────────── print("\n--- Duplicate IDs ---") id_counts = Counter(all_ids) dupes = {k: v for k, v in id_counts.items() if v > 1} check("No duplicate IDs", len(dupes) == 0, f"{len(dupes)} duplicated IDs: {list(dupes.keys())[:5]}") # ────────────────────────────────────────────── # Check 2: Exactly 4 candidates per item # ────────────────────────────────────────────── print("\n--- Option Count ---") bad_options = [] for item in all_items: candidates = item.get("candidates", []) if len(candidates) != 4: bad_options.append((item.get("id", "?"), len(candidates))) check("All items have exactly 4 candidates", len(bad_options) == 0, f"{len(bad_options)} items have wrong candidate count: {bad_options[:5]}") # ────────────────────────────────────────────── # Check 3: Valid answer labels (0-3) # ────────────────────────────────────────────── print("\n--- Answer Labels ---") bad_answers = [] for item in all_items: correct = item.get("correct") if correct not in [0, 1, 2, 3]: bad_answers.append((item.get("id", "?"), correct)) check("All answer indices are 0-3", len(bad_answers) == 0, f"{len(bad_answers)} items have invalid answer index: {bad_answers[:5]}") # ────────────────────────────────────────────── # Check 4: No empty fields # ────────────────────────────────────────────── print("\n--- Missing Fields ---") missing = [] for item in all_items: ctx = item.get("context", "") q = item.get("question", "") candidates = item.get("candidates", []) # At least one of context or question must be non-empty if not ctx and not q: missing.append((item.get("id", "?"), "no context or question")) if not candidates: missing.append((item.get("id", "?"), "no candidates")) for i, c in enumerate(candidates): if not str(c).strip(): missing.append((item.get("id", "?"), f"empty candidate {i}")) check("No critical missing fields", len(missing) == 0, f"{len(missing)} issues: {missing[:5]}") # ────────────────────────────────────────────── # Check 5: Answer exists among candidates # ────────────────────────────────────────────── print("\n--- Answer in Candidates ---") out_of_range = [] for item in all_items: correct = item.get("correct", -1) candidates = item.get("candidates", []) if correct is not None and (correct < 0 or correct >= len(candidates)): out_of_range.append((item.get("id", "?"), correct, len(candidates))) check("Answer index within candidate range", len(out_of_range) == 0, f"{len(out_of_range)} items have out-of-range answer: {out_of_range[:5]}") # ────────────────────────────────────────────── # Check 6: UTF-8 encoding and NFC normalization # ────────────────────────────────────────────── print("\n--- Unicode Normalization ---") unnormalized = 0 for item in all_items: for field in ["context", "question"]: text = item.get(field, "") or "" if unicodedata.normalize("NFC", text) != text: unnormalized += 1 for c in item.get("candidates", []): if unicodedata.normalize("NFC", str(c)) != str(c): unnormalized += 1 check("All strings are NFC-normalized", unnormalized == 0, f"{unnormalized} unnormalized strings found") # ────────────────────────────────────────────── # Check 7: No mixed scripts within words # ────────────────────────────────────────────── print("\n--- Script Consistency ---") mixed = 0 for item in all_items: for field in ["context", "question"]: text = item.get(field, "") or "" for word in text.split(): has_dev = bool(re.search(r'[\u0900-\u097F]', word)) has_lat = bool(re.search(r'[a-zA-Z]', word)) if has_dev and has_lat: mixed += 1 check("No mixed-script words", mixed == 0, f"{mixed} words mix Devanagari and Latin characters") # ────────────────────────────────────────────── # Check 8: Duplicate questions # ────────────────────────────────────────────── print("\n--- Duplicate Questions ---") seen = set() exact_dupes = 0 for item in all_items: ctx = item.get("context", "") or "" q = item.get("question", "") or "" key = f"{ctx}|||{q}" if key in seen: exact_dupes += 1 seen.add(key) if exact_dupes > 0: warn(f"{exact_dupes} exact duplicate context+question pairs found") else: check("No exact duplicate questions", True) # ────────────────────────────────────────────── # Check 9: Category consistency # ────────────────────────────────────────────── print("\n--- Category Consistency ---") items_without_category = sum(1 for item in all_items if not item.get("category")) items_without_difficulty = sum(1 for item in all_items if not item.get("difficulty")) if items_without_category > 0: warn(f"{items_without_category} items missing 'category' field") else: check("All items have category field", True) if items_without_difficulty > 0: warn(f"{items_without_difficulty} items missing 'difficulty' field") else: check("All items have difficulty field", True) # ────────────────────────────────────────────── # Final Verdict # ────────────────────────────────────────────── print("\n" + "="*60) print(f"RESULTS: {CHECKS_PASSED} passed, {CHECKS_FAILED} failed, {WARNINGS} warnings") if CHECKS_FAILED == 0: print("VERDICT: ✓ PASS") sys.exit(0) else: print("VERDICT: ✗ FAIL") sys.exit(1)