Spaces:
Runtime error
Runtime error
Update merge_questions.py
Browse files- merge_questions.py +9 -9
merge_questions.py
CHANGED
|
@@ -30,12 +30,12 @@ def safe_load(path):
|
|
| 30 |
return None
|
| 31 |
|
| 32 |
|
| 33 |
-
def merge_all():
|
| 34 |
-
os.makedirs(
|
| 35 |
merged = []
|
| 36 |
next_id = 1000
|
| 37 |
|
| 38 |
-
p = Path(
|
| 39 |
files = sorted(p.glob("spm_*_*.json"))
|
| 40 |
|
| 41 |
for file in files:
|
|
@@ -78,18 +78,18 @@ def merge_all():
|
|
| 78 |
"year": int(year)
|
| 79 |
}
|
| 80 |
|
| 81 |
-
# attach correct_answer if scheme provides it (scheme may be {"1": "A"} or {"1": {"correct_answer": "x"}})
|
| 82 |
correct_answer = None
|
| 83 |
val = scheme.get(str(idx)) if isinstance(scheme, dict) else None
|
| 84 |
if isinstance(val, str):
|
| 85 |
-
# if it's a letter like "A", map to choice text
|
| 86 |
v = val.strip()
|
| 87 |
if len(v) == 1 and merged_q["choices"]:
|
| 88 |
pos = ord(v.upper()) - ord("A")
|
| 89 |
if 0 <= pos < len(merged_q["choices"]):
|
| 90 |
correct_answer = merged_q["choices"][pos]
|
|
|
|
|
|
|
| 91 |
else:
|
| 92 |
-
correct_answer = v
|
| 93 |
elif isinstance(val, dict):
|
| 94 |
correct_answer = val.get("correct_answer")
|
| 95 |
else:
|
|
@@ -99,11 +99,10 @@ def merge_all():
|
|
| 99 |
merged.append(merged_q)
|
| 100 |
next_id += 1
|
| 101 |
|
| 102 |
-
|
| 103 |
-
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
|
| 104 |
json.dump(merged, f, indent=2, ensure_ascii=False)
|
| 105 |
|
| 106 |
-
print(f"✅ Merged {len(merged)} questions into {
|
| 107 |
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
|
@@ -115,3 +114,4 @@ if __name__ == "__main__":
|
|
| 115 |
|
| 116 |
|
| 117 |
|
|
|
|
|
|
| 30 |
return None
|
| 31 |
|
| 32 |
|
| 33 |
+
def merge_all(data_dir=DATA_DIR, output_file=OUTPUT_FILE):
|
| 34 |
+
os.makedirs(data_dir, exist_ok=True)
|
| 35 |
merged = []
|
| 36 |
next_id = 1000
|
| 37 |
|
| 38 |
+
p = Path(data_dir)
|
| 39 |
files = sorted(p.glob("spm_*_*.json"))
|
| 40 |
|
| 41 |
for file in files:
|
|
|
|
| 78 |
"year": int(year)
|
| 79 |
}
|
| 80 |
|
|
|
|
| 81 |
correct_answer = None
|
| 82 |
val = scheme.get(str(idx)) if isinstance(scheme, dict) else None
|
| 83 |
if isinstance(val, str):
|
|
|
|
| 84 |
v = val.strip()
|
| 85 |
if len(v) == 1 and merged_q["choices"]:
|
| 86 |
pos = ord(v.upper()) - ord("A")
|
| 87 |
if 0 <= pos < len(merged_q["choices"]):
|
| 88 |
correct_answer = merged_q["choices"][pos]
|
| 89 |
+
else:
|
| 90 |
+
correct_answer = None
|
| 91 |
else:
|
| 92 |
+
correct_answer = v if v else None
|
| 93 |
elif isinstance(val, dict):
|
| 94 |
correct_answer = val.get("correct_answer")
|
| 95 |
else:
|
|
|
|
| 99 |
merged.append(merged_q)
|
| 100 |
next_id += 1
|
| 101 |
|
| 102 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
|
|
|
| 103 |
json.dump(merged, f, indent=2, ensure_ascii=False)
|
| 104 |
|
| 105 |
+
print(f"✅ Merged {len(merged)} questions into {output_file}")
|
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
+
|