mathqa.py 1.28 KB
Newer Older
1
import re
2
3
4
from lm_eval.base import MultipleChoiceTask
from . common import HFTask

5
6

class MathQA(HFTask, MultipleChoiceTask):
7
8
9
10
11
12
13
14
15
16
17
18
    DATASET_PATH = "math_qa"
    DATASET_NAME = None

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

19
    def _convert_standard(self, doc):
20

21
        answer_idx = ['a', 'b', 'c', 'd', 'e'].index(doc['correct'])
Anthony DiPofi's avatar
Anthony DiPofi committed
22
        choices = [c[4:].rstrip(" ,") for c in re.findall(r"[abcd] \) .*?, |e \) .*?$", doc['options'])]
23

24
        out_doc = {
25
26
27
            "query": "Question: " + doc['Problem'] +"\nAnswer:",
            "choices": choices,
            "gold": answer_idx,
28
29
        }
        return out_doc
30

31
32
33
    def _load_docs(self, docs):
        for record in docs:
            yield self._convert_standard(record)
34

35
36
37
    def training_docs(self):
        docs = super().training_docs()
        return self._load_docs(docs)
38

39
40
41
    def validation_docs(self):
        docs = super().validation_docs()
        return self._load_docs(docs)
42

43
44
45
    def test_docs(self):
        docs = super().test_docs()
        return self._load_docs(docs)
46

47
48
49
    def fewshot_description(self):
        # TODO: figure out description
        return ""
50

51
52
    def doc_to_text(self, doc):
        return doc["query"]