import os import sys import json from pathlib import Path from fetch_mathoverflow import batch_loader ARCHIVE_URL = "https://people.eecs.berkeley.edu/~hendrycks/MATH.tar" SAVE_PATH = "math-dataset" def main(): Path(SAVE_PATH).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["type"] cat_texts.append(text) batches = batch_loader(cat_texts, 100) for i, batch in enumerate(batches): with open(os.path.join(SAVE_PATH, f"{cat_name}_{i}.txt"), "w") as fle: fle.write("<|endoftext|>\n".join(batch)) os.system("rm -r " + os.path.join(SAVE_PATH, "MATH")) os.remove(archive_path) if __name__=="__main__": main()