Datasets:
File size: 9,351 Bytes
a5fb1a8 | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | """
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)
|