import os import sys import json from pathlib import Path from fetch_mathoverflow import batch_loader import random from utils import make_archive ARCHIVE_URL = "https://people.eecs.berkeley.edu/~hendrycks/MATH.tar" SAVE_PATH = "math-dataset" def main(): VAL_RATE=5e-2 Path(SAVE_PATH).mkdir(exist_ok=True) Path(os.path.join(SAVE_PATH, "train")).mkdir(exist_ok=True) Path(os.path.join(SAVE_PATH, "val")).mkdir(exist_ok=True) archive_path = os.path.join(SAVE_PATH, "archive.tar") os.system("wget -O " + archive_path + " " + ARCHIVE_URL) os.system("tar -xf " + archive_path + " -C " + SAVE_PATH) cat_dir = os.path.join(SAVE_PATH, "MATH/train") for cat_name in os.listdir(cat_dir): cat_path = os.path.join(cat_dir, cat_name) if os.path.isdir(cat_path): cat_texts = [] for f in os.listdir(cat_path): f_path = os.path.join(cat_path, f) with open(f_path) as fle: prob_json = json.load(fle) text = "{\\bf Problem.} " + prob_json["problem"] + "\n" +\ "{\\bf Level.} " + prob_json["level"] + "\n" +\ "{\\bf Type.} " + prob_json["type"] + "\n" +\ "{\\bf Solution.} " + prob_json["solution"] cat_texts.append(text) for i, text in enumerate(cat_texts): if random.random()>VAL_RATE: with open(os.path.join(SAVE_PATH, "train", f"{cat_name}_{i}.txt"), "w") as fle: fle.write(text.strip()) else: with open(os.path.join(SAVE_PATH, "val", f"{cat_name}_{i}.txt"), "w") as fle: fle.write(text.strip()) os.system("rm -r " + os.path.join(SAVE_PATH, "MATH")) os.remove(archive_path) if __name__=="__main__": main() make_archive("math-dataset/train") make_archive("math-dataset/val")