#!/usr/bin/env python3 """ Shuffle MCQ candidates across all GomParam-v1 JSON modules. This script randomizes the position of the correct answer in every question to eliminate positional bias (where ~88% of correct answers were at index 0). Uses a fixed seed for reproducibility. """ import json import random from pathlib import Path SEED = 42 DATA_DIR = Path(__file__).resolve().parent.parent / "data" random.seed(SEED) stats_before = {0: 0, 1: 0, 2: 0, 3: 0} stats_after = {0: 0, 1: 0, 2: 0, 3: 0} total_items = 0 for json_file in sorted(DATA_DIR.glob("*.json")): with open(json_file, "r", encoding="utf-8") as f: items = json.load(f) modified = False for item in items: candidates = item.get("candidates", []) correct_idx = item.get("correct", -1) if not candidates or correct_idx < 0 or correct_idx >= len(candidates): continue total_items += 1 stats_before[correct_idx] = stats_before.get(correct_idx, 0) + 1 # Build index list and shuffle correct_answer = candidates[correct_idx] indices = list(range(len(candidates))) random.shuffle(indices) # Rearrange candidates and find new correct index new_candidates = [candidates[i] for i in indices] new_correct = indices.index(correct_idx) item["candidates"] = new_candidates item["correct"] = new_correct modified = True stats_after[new_correct] = stats_after.get(new_correct, 0) + 1 if modified: with open(json_file, "w", encoding="utf-8") as f: json.dump(items, f, ensure_ascii=False, indent=2) print(f" ✓ {json_file.name:40s} {len(items)} items shuffled") print(f"\nTotal items processed: {total_items}") print(f"\nBEFORE shuffle — correct answer position distribution:") for k, v in sorted(stats_before.items()): print(f" Index {k}: {v:5d} ({v/total_items*100:.1f}%)") print(f"\nAFTER shuffle — correct answer position distribution:") for k, v in sorted(stats_after.items()): print(f" Index {k}: {v:5d} ({v/total_items*100:.1f}%)")