add files
Browse files- .gitattributes +1 -0
- data/math23k.csv +3 -0
- math23k.py +84 -4
.gitattributes
CHANGED
|
@@ -52,3 +52,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 52 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 53 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 54 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 52 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 53 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 54 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
*.csv filter=lfs diff=lfs merge=lfs -text
|
data/math23k.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:03df80a02b5404e7231abccb6e5511ef6cc05f101b260408dbbac201799997ec
|
| 3 |
+
size 11831918
|
math23k.py
CHANGED
|
@@ -1,8 +1,64 @@
|
|
|
|
|
| 1 |
import string
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import datasets
|
| 4 |
from datasets import DatasetInfo, load_dataset
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def get_expre(example):
|
| 8 |
seq = example["target_template"]
|
|
@@ -27,12 +83,14 @@ def get_expre(example):
|
|
| 27 |
def regular(example):
|
| 28 |
if example["id"] in ["17520"]:
|
| 29 |
return False
|
| 30 |
-
|
| 31 |
-
num_list = list(map(lambda x: "temp_" + x, alphabet[: len(example["num_list"])]))
|
| 32 |
eqs = example["expression"].format(**dict(zip(num_list, example["num_list"])))
|
| 33 |
return eval(eqs) == example["answer"]
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
class DatasetBuilder(datasets.DatasetBuilder):
|
| 37 |
def _info(self):
|
| 38 |
return DatasetInfo()
|
|
@@ -40,10 +98,32 @@ class DatasetBuilder(datasets.DatasetBuilder):
|
|
| 40 |
def __init__(self, **kwargs):
|
| 41 |
super().__init__(**kwargs)
|
| 42 |
|
| 43 |
-
def download_and_prepare(self, *args, **kwargs):
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def as_dataset(self, split, **kwargs):
|
|
|
|
| 47 |
ds = load_dataset("Gxg/Math23K", self.config.name, split=split)
|
| 48 |
ds = ds.map(get_expre).filter(regular)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return ds
|
|
|
|
| 1 |
+
import re
|
| 2 |
import string
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
|
| 7 |
import datasets
|
| 8 |
from datasets import DatasetInfo, load_dataset
|
| 9 |
|
| 10 |
+
ALPHABET = string.ascii_lowercase
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def temp_list(num_list):
|
| 14 |
+
return map(lambda x: "temp_" + x, ALPHABET[: len(num_list)])
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def extract_placeholders(text):
|
| 18 |
+
pattern = r"<<(.*?)>>"
|
| 19 |
+
matches = re.findall(pattern, text)
|
| 20 |
+
return matches
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def multiple_replace(text, replacement_dict):
|
| 24 |
+
for k, v in replacement_dict.items():
|
| 25 |
+
text = text.replace(k, v)
|
| 26 |
+
return text
|
| 27 |
+
# if replacement_dict:
|
| 28 |
+
# pattern = "|".join(map(re.escape, replacement_dict.keys()))
|
| 29 |
+
# return re.sub(pattern, lambda m: replacement_dict[m.group()], text)
|
| 30 |
+
# else:
|
| 31 |
+
# return text
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def solution_human(solution, num_list):
|
| 35 |
+
eqs = extract_placeholders(solution)
|
| 36 |
+
num_list = {key: str(value) for key, value in zip(temp_list(num_list), num_list)}
|
| 37 |
+
|
| 38 |
+
modified = []
|
| 39 |
+
cached = {}
|
| 40 |
+
for eq in eqs:
|
| 41 |
+
eq = multiple_replace(eq, num_list)
|
| 42 |
+
eq = multiple_replace(eq, cached)
|
| 43 |
+
try:
|
| 44 |
+
res = eval(eq)
|
| 45 |
+
be_eval = True
|
| 46 |
+
except Exception:
|
| 47 |
+
res = eq
|
| 48 |
+
be_eval = False
|
| 49 |
+
cached[eq] = str(res)
|
| 50 |
+
num_ops = sum([1 for char in eq if char in "+-*/"])
|
| 51 |
+
if num_ops and be_eval:
|
| 52 |
+
modified.append(f"{eq}={cached[eq]}")
|
| 53 |
+
else:
|
| 54 |
+
modified.append(f"{eq}")
|
| 55 |
+
|
| 56 |
+
text = solution
|
| 57 |
+
for t, rt in zip(eqs, modified):
|
| 58 |
+
text = text.replace(t, rt, 1)
|
| 59 |
+
|
| 60 |
+
return text
|
| 61 |
+
|
| 62 |
|
| 63 |
def get_expre(example):
|
| 64 |
seq = example["target_template"]
|
|
|
|
| 83 |
def regular(example):
|
| 84 |
if example["id"] in ["17520"]:
|
| 85 |
return False
|
| 86 |
+
num_list = list(temp_list(example["num_list"]))
|
|
|
|
| 87 |
eqs = example["expression"].format(**dict(zip(num_list, example["num_list"])))
|
| 88 |
return eval(eqs) == example["answer"]
|
| 89 |
|
| 90 |
|
| 91 |
+
_DATA_FILES = ["data/math23k.csv"]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
class DatasetBuilder(datasets.DatasetBuilder):
|
| 95 |
def _info(self):
|
| 96 |
return DatasetInfo()
|
|
|
|
| 98 |
def __init__(self, **kwargs):
|
| 99 |
super().__init__(**kwargs)
|
| 100 |
|
| 101 |
+
# def download_and_prepare(self, *args, **kwargs):
|
| 102 |
+
|
| 103 |
+
# return self
|
| 104 |
+
|
| 105 |
+
def _download_and_prepare(
|
| 106 |
+
self, dl_manager, verification_mode, **prepare_split_kwargs
|
| 107 |
+
):
|
| 108 |
+
from datasets import SplitDict
|
| 109 |
+
|
| 110 |
+
downloaded_files = dl_manager.download(_DATA_FILES)
|
| 111 |
+
print(downloaded_files)
|
| 112 |
+
self.info.solution_files = downloaded_files
|
| 113 |
+
df=pd.read_csv(downloaded_files[0])
|
| 114 |
+
print(df)
|
| 115 |
+
split_dict = SplitDict(dataset_name=self.name)
|
| 116 |
+
self.info.splits = split_dict
|
| 117 |
+
self.info.download_size = dl_manager.downloaded_size
|
| 118 |
|
| 119 |
def as_dataset(self, split, **kwargs):
|
| 120 |
+
df = pd.read_csv(self.info.solution_files[0])
|
| 121 |
ds = load_dataset("Gxg/Math23K", self.config.name, split=split)
|
| 122 |
ds = ds.map(get_expre).filter(regular)
|
| 123 |
+
ds = ds.add_column("solution", df["answers"])
|
| 124 |
+
ds = ds.map(
|
| 125 |
+
lambda exa: {
|
| 126 |
+
"solution_human": solution_human(exa["solution"], exa["num_list"])
|
| 127 |
+
}
|
| 128 |
+
)
|
| 129 |
return ds
|