Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 2,017 Bytes
18b1f9d
 
 
c91a5cf
18b1f9d
 
f293502
 
 
18b1f9d
 
 
 
c91a5cf
 
18b1f9d
f293502
18b1f9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f293502
18b1f9d
 
 
c91a5cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18b1f9d
 
 
 
 
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
57
58
59
60
61
62
63
import os
import sys
import json
import ndjson
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"

random.seed(20)

def main():  
    VAL_RATE=5e-2
    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["solution"]

                cat_texts.append(text)

        random.shuffle(cat_texts) 
        instances = [{"text": x.strip(), "meta": {"set_name": "MATH"}} for x in cat_texts]
        split = int(VAL_RATE*len(instances))
        train = instances[split:]
        val = instances[:split]

        with open(os.path.join(SAVE_PATH, "train.jsonl"), "a+") as f: 
            f.write(ndjson.dumps(train))
            f.write("\n")
        with open(os.path.join(SAVE_PATH, "val.jsonl"), "a+") as f: 
            f.write(ndjson.dumps(val))
            f.write("\n")

    os.system("gzip " + os.path.join(SAVE_PATH, "train.jsonl"))
    os.system("gzip " + os.path.join(SAVE_PATH, "val.jsonl"))
    os.system("rm -r " + os.path.join(SAVE_PATH, "MATH"))
    os.remove(archive_path)

if __name__=="__main__": 
    main()