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
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""OCWCourses"""
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
import zipfile
|
| 8 |
+
|
| 9 |
+
import datasets
|
| 10 |
+
|
| 11 |
+
logger = datasets.logging.get_logger(__name__)
|
| 12 |
+
|
| 13 |
+
_CITATION = """\
|
| 14 |
+
@misc{lewkowycz2022solving,
|
| 15 |
+
title={Solving Quantitative Reasoning Problems with Language Models},
|
| 16 |
+
author={Aitor Lewkowycz and Anders Andreassen and David Dohan and Ethan Dyer and Henryk Michalewski and Vinay Ramasesh and Ambrose Slone and Cem Anil and Imanol Schlag and Theo Gutman-Solo and Yuhuai Wu and Behnam Neyshabur and Guy Gur-Ari and Vedant Misra},
|
| 17 |
+
year={2022},
|
| 18 |
+
eprint={2206.14858},
|
| 19 |
+
archivePrefix={arXiv},
|
| 20 |
+
primaryClass={cs.CL}
|
| 21 |
+
}
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
_DESCRIPTION = """\
|
| 25 |
+
OCWCourses
|
| 26 |
+
"""
|
| 27 |
+
_URL = "https://openreview.net/forum?id=IFXTZERXdM7"
|
| 28 |
+
_URLS = {
|
| 29 |
+
"test": "https://openreview.net/attachment?id=IFXTZERXdM7&name=supplementary_material",
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
def _read_from_zip(zip_path, text_filename='minerva_supplement/ocw_courses_problems.text'):
|
| 33 |
+
with zipfile.ZipFile(zip_path, 'r') as myzip:
|
| 34 |
+
with myzip.open(text_filename) as myfile:
|
| 35 |
+
return myfile.read().decode('utf-8')
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _parse_latex(text):
|
| 39 |
+
problems = re.findall(r'\\textbf{Problem:}(.*?)\\textbf{Solution:}', text, re.DOTALL)
|
| 40 |
+
solutions = re.findall(r'\\textbf{Solution:}(.*?)(\\textbf{Problem:}|$)', text, re.DOTALL)
|
| 41 |
+
|
| 42 |
+
parsed_list = []
|
| 43 |
+
for problem, solution in zip(problems, solutions):
|
| 44 |
+
parsed_list.append({
|
| 45 |
+
'problem': problem.strip(),
|
| 46 |
+
'solution': solution[0].strip()
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
return parsed_list
|
| 50 |
+
|
| 51 |
+
def _last_boxed_only_string(string):
|
| 52 |
+
|
| 53 |
+
idx = string.rfind("\\boxed")
|
| 54 |
+
if "\\boxed " in string:
|
| 55 |
+
return "\\boxed " + string.split("\\boxed ")[-1].split("$")[0]
|
| 56 |
+
if idx < 0:
|
| 57 |
+
idx = string.rfind("\\fbox")
|
| 58 |
+
if idx < 0:
|
| 59 |
+
return None
|
| 60 |
+
|
| 61 |
+
i = idx
|
| 62 |
+
right_brace_idx = None
|
| 63 |
+
num_left_braces_open = 0
|
| 64 |
+
while i < len(string):
|
| 65 |
+
if string[i] == "{":
|
| 66 |
+
num_left_braces_open += 1
|
| 67 |
+
if string[i] == "}":
|
| 68 |
+
num_left_braces_open -= 1
|
| 69 |
+
if num_left_braces_open == 0:
|
| 70 |
+
right_brace_idx = i
|
| 71 |
+
break
|
| 72 |
+
i += 1
|
| 73 |
+
|
| 74 |
+
if right_brace_idx is None:
|
| 75 |
+
retval = None
|
| 76 |
+
else:
|
| 77 |
+
retval = string[idx : right_brace_idx + 1]
|
| 78 |
+
|
| 79 |
+
return retval
|
| 80 |
+
|
| 81 |
+
def _remove_boxed(s):
|
| 82 |
+
if "\\boxed " in s:
|
| 83 |
+
left = "\\boxed "
|
| 84 |
+
assert s[: len(left)] == left
|
| 85 |
+
return s[len(left) :]
|
| 86 |
+
|
| 87 |
+
left = "\\boxed{"
|
| 88 |
+
|
| 89 |
+
assert s[: len(left)] == left
|
| 90 |
+
assert s[-1] == "}"
|
| 91 |
+
|
| 92 |
+
return s[len(left) : -1]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class OCWConfig(datasets.BuilderConfig):
|
| 96 |
+
"""BuilderConfig for OCW."""
|
| 97 |
+
|
| 98 |
+
def __init__(self, **kwargs):
|
| 99 |
+
"""BuilderConfig for OCW.
|
| 100 |
+
|
| 101 |
+
Args:
|
| 102 |
+
**kwargs: keyword arguments forwarded to super.
|
| 103 |
+
"""
|
| 104 |
+
super(OCWConfig, self).__init__(**kwargs)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class OCWCourses(datasets.GeneratorBasedBuilder):
|
| 108 |
+
"""OCWCourses"""
|
| 109 |
+
|
| 110 |
+
BUILDER_CONFIGS = [
|
| 111 |
+
OCWConfig(
|
| 112 |
+
name="ocwcourses",
|
| 113 |
+
version=datasets.Version("1.0.0", ""),
|
| 114 |
+
description="OCWCourses",
|
| 115 |
+
),
|
| 116 |
+
]
|
| 117 |
+
|
| 118 |
+
def _info(self):
|
| 119 |
+
return datasets.DatasetInfo(
|
| 120 |
+
description=_DESCRIPTION,
|
| 121 |
+
features=datasets.Features(
|
| 122 |
+
{
|
| 123 |
+
"problem": datasets.Value("string"),
|
| 124 |
+
"solution": datasets.Value("string"),
|
| 125 |
+
"answer": datasets.Value("string"),
|
| 126 |
+
}
|
| 127 |
+
),
|
| 128 |
+
# No default supervised_keys (as we have to pass both question
|
| 129 |
+
# and context as input).
|
| 130 |
+
supervised_keys=None,
|
| 131 |
+
homepage="https://openreview.net/forum?id=IFXTZERXdM7",
|
| 132 |
+
citation=_CITATION,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
def _split_generators(self, dl_manager):
|
| 136 |
+
downloaded_files = dl_manager.download_and_extract(_URLS)
|
| 137 |
+
|
| 138 |
+
return [
|
| 139 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
|
| 140 |
+
]
|
| 141 |
+
|
| 142 |
+
def _generate_examples(self, filepath):
|
| 143 |
+
"""This function returns the examples in the raw (text) form."""
|
| 144 |
+
logger.info("generating examples from = %s", filepath)
|
| 145 |
+
|
| 146 |
+
with open(os.path.join(filepath, "minerva_supplement/ocw_courses_problems.tex")) as f:
|
| 147 |
+
text = f.read()
|
| 148 |
+
|
| 149 |
+
parsed_problems_solutions = _parse_latex(text)
|
| 150 |
+
|
| 151 |
+
rows = [{**x, "answer": _remove_boxed(_last_boxed_only_string(x["solution"]))} for x in parsed_problems_solutions]
|
| 152 |
+
|
| 153 |
+
for key, row in enumerate(rows):
|
| 154 |
+
yield key, row
|
test_dataloader.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
|
| 3 |
+
data = load_dataset("ocw.py")
|
| 4 |
+
|
| 5 |
+
[print(row) for row in data["test"]]
|
| 6 |
+
print(data)
|