File size: 1,646 Bytes
9b14204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)