asdiv.py 4.01 KB
Newer Older
Jonathan Tow's avatar
Jonathan Tow committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Jon Tow's avatar
Jon Tow committed
14
"""ASDIV dataset."""
Jonathan Tow's avatar
Jonathan Tow committed
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


import os
import xml.etree.ElementTree as ET

import datasets


_CITATION = """\
@misc{miao2021diverse,
    title={A Diverse Corpus for Evaluating and Developing English Math Word Problem Solvers},
    author={Shen-Yun Miao and Chao-Chun Liang and Keh-Yih Su},
    year={2021},
    eprint={2106.15772},
    archivePrefix={arXiv},
    primaryClass={cs.AI}
}
"""

_DESCRIPTION = """\
ASDiv (Academia Sinica Diverse MWP Dataset) is a diverse (in terms of both language
patterns and problem types) English math word problem (MWP) corpus for evaluating
the capability of various MWP solvers. Existing MWP corpora for studying AI progress
remain limited either in language usage patterns or in problem types. We thus present
a new English MWP corpus with 2,305 MWPs that cover more text patterns and most problem
types taught in elementary school. Each MWP is annotated with its problem type and grade
level (for indicating the level of difficulty).
"""

_HOMEPAGE = "https://github.com/chaochun/nlu-asdiv-dataset"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""

_URLS = "https://github.com/chaochun/nlu-asdiv-dataset/archive/55790e5270bb91ccfa5053194b25732534696b50.zip"


class ASDiv(datasets.GeneratorBasedBuilder):
bzantium's avatar
bzantium committed
53
    """ASDiv: A Diverse Corpus for Evaluating and Developing English Math Word Problem Solvers"""
Jonathan Tow's avatar
Jonathan Tow committed
54
55
56
57

    VERSION = datasets.Version("0.0.1")

    BUILDER_CONFIGS = [
bzantium's avatar
bzantium committed
58
59
60
61
62
        datasets.BuilderConfig(
            name="asdiv",
            version=VERSION,
            description="A diverse corpus for evaluating and developing english math word problem solvers",
        )
Jonathan Tow's avatar
Jonathan Tow committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    ]

    def _info(self):
        features = datasets.Features(
            {
                "body": datasets.Value("string"),
                "question": datasets.Value("string"),
                "solution_type": datasets.Value("string"),
                "answer": datasets.Value("string"),
                "formula": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        urls = _URLS
        data_dir = dl_manager.download_and_extract(urls)
        base_filepath = "nlu-asdiv-dataset-55790e5270bb91ccfa5053194b25732534696b50"
        return [
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
bzantium's avatar
bzantium committed
92
93
94
                    "filepath": os.path.join(
                        data_dir, base_filepath, "dataset", "ASDiv.xml"
                    ),
Jonathan Tow's avatar
Jonathan Tow committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                    "split": datasets.Split.VALIDATION,
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath, split):
        tree = ET.parse(filepath)
        root = tree.getroot()
        for key, problem in enumerate(root.iter("Problem")):
            yield key, {
                "body": problem.find("Body").text,
                "question": problem.find("Question").text,
                "solution_type": problem.find("Solution-Type").text,
                "answer": problem.find("Answer").text,
                "formula": problem.find("Formula").text,
            }