import json import os from typing import Dict, Any, List OPTION_KEYS = ["A", "B", "C", "D", "E", "F"] def normalize_option(option: Dict[str, Any]) -> Dict[str, str]: new_option = {} option = option or {} for k in OPTION_KEYS: new_option[k] = str(option.get(k, "")).strip() return new_option def normalize_item(item: Dict[str, Any], idx: int) -> Dict[str, Any]: return { "id": item.get("id", idx), "exam_type": item.get("exam_type", ""), "exam_class": item.get("exam_class", ""), "exam_subject": item.get("exam_subject", ""), "question": item.get("question", ""), "answer": item.get("answer", ""), "explanation": item.get("explanation", ""), "question_type": item.get("question_type", ""), "option": normalize_option(item.get("option")), } def clean_json(input_path: str) -> None: with open(input_path, "r", encoding="utf-8") as f: data: List[Dict[str, Any]] = json.load(f) cleaned = [] for idx, item in enumerate(data, start=1): cleaned.append(normalize_item(item, idx)) base, ext = os.path.splitext(input_path) output_path = f"{base}-clean{ext}" with open(output_path, "w", encoding="utf-8") as f: json.dump(cleaned, f, ensure_ascii=False, indent=2) print(f"[OK] {output_path} ({len(cleaned)} samples)") if __name__ == "__main__": input_files = [ "CMB-Exam/CMB-train/CMB-train-merge.json", "CMB-Exam/CMB-val/CMB-val-merge.json", "CMB-Exam/CMB-test/CMB-test-choice-question-merge.json", ] for path in input_files: clean_json(path)