File size: 6,489 Bytes
dd6edb0
f73fd1a
 
 
 
 
 
 
 
 
dd6edb0
 
 
 
 
 
 
 
 
 
 
f73fd1a
 
 
 
 
 
 
 
 
 
 
 
 
dd6edb0
f73fd1a
 
 
 
 
 
dd6edb0
f73fd1a
dd6edb0
f73fd1a
 
 
dd6edb0
f73fd1a
 
 
 
 
ffefa2b
 
 
 
 
 
 
f73fd1a
 
 
dd6edb0
 
 
 
 
f73fd1a
 
 
 
ffefa2b
 
 
 
f73fd1a
 
 
dd6edb0
 
 
 
 
f73fd1a
 
dd6edb0
f73fd1a
 
dd6edb0
 
f73fd1a
 
 
 
4ebd011
 
 
 
f73fd1a
 
 
dd6edb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f73fd1a
 
 
 
 
 
dd6edb0
 
f73fd1a
 
dd6edb0
 
 
 
f73fd1a
 
 
 
 
dd6edb0
f73fd1a
dd6edb0
f73fd1a
 
 
ffefa2b
 
f73fd1a
 
 
dd6edb0
 
 
 
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""OpenBookQA dataset."""


import json
import os
import textwrap

import datasets


_HOMEPAGE = "https://allenai.org/data/open-book-qa"

_DESCRIPTION = """\
OpenBookQA aims to promote research in advanced question-answering, probing a deeper understanding of both the topic
(with salient facts summarized as an open book, also provided with the dataset) and the language it is expressed in. In
particular, it contains questions that require multi-step reasoning, use of additional common and commonsense knowledge,
and rich text comprehension.
OpenBookQA is a new kind of question-answering dataset modeled after open book exams for assessing human understanding
of a subject.
"""

_CITATION = """\
@inproceedings{OpenBookQA2018,
 title={Can a Suit of Armor Conduct Electricity? A New Dataset for Open Book Question Answering},
 author={Todor Mihaylov and Peter Clark and Tushar Khot and Ashish Sabharwal},
 booktitle={EMNLP},
 year={2018}
}
"""

_URL = "https://s3-us-west-2.amazonaws.com/ai2-website/data/OpenBookQA-V1-Sep2018.zip"


class OpenbookqaConfig(datasets.BuilderConfig):
    def __init__(self, data_dir=None, filenames=None, version=datasets.Version("1.0.1", ""), **kwargs):
        """BuilderConfig for openBookQA dataset

        Args:
          data_dir: directory for the given dataset name
          **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(version=version, **kwargs)
        self.data_dir = data_dir
        self.filenames = filenames


class Openbookqa(datasets.GeneratorBasedBuilder):
    """OpenBookQA dataset."""

    BUILDER_CONFIGS = [
        OpenbookqaConfig(
            name="main",
            description=textwrap.dedent(
                """\
                It consists of 5,957 multiple-choice elementary-level science questions (4,957 train, 500 dev, 500 test),
                which probe the understanding of a small “book” of 1,326 core science facts and the application of these facts to novel
                situations. For training, the dataset includes a mapping from each question to the core science fact it was designed to
                probe. Answering OpenBookQA questions requires additional broad common knowledge, not contained in the book. The questions,
                by design, are answered incorrectly by both a retrieval-based algorithm and a word co-occurrence algorithm. Strong neural
                baselines achieve around 50% on OpenBookQA, leaving a large gap to the 92% accuracy of crowd-workers.
                """
            ),
            data_dir="Main",
            filenames={
                "train": "train.jsonl",
                "validation": "dev.jsonl",
                "test": "test.jsonl",
            },
        ),
        OpenbookqaConfig(
            name="additional",
            description=textwrap.dedent(
                """\
                Additionally, we provide 5,167 crowd-sourced common knowledge facts, and an expanded version of the train/dev/test questions where
                each question is associated with its originating core fact, a human accuracy score, a clarity score, and an anonymized crowd-worker
                ID (in the 'Additional' folder).
                """
            ),
            data_dir="Additional",
            filenames={
                "train": "train_complete.jsonl",
                "validation": "dev_complete.jsonl",
                "test": "test_complete.jsonl",
            },
        ),
    ]
    DEFAULT_CONFIG_NAME = "main"

    def _info(self):
        if self.config.name == "main":
            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "question_stem": datasets.Value("string"),
                    "choices": datasets.features.Sequence(
                        {
                            "text": datasets.Value("string"),
                            "label": datasets.Value("string"),
                        }
                    ),
                    "answerKey": datasets.Value("string"),
                }
            )
        else:
            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "question_stem": datasets.Value("string"),
                    "choices": datasets.features.Sequence(
                        {
                            "text": datasets.Value("string"),
                            "label": datasets.Value("string"),
                        }
                    ),
                    "answerKey": datasets.Value("string"),
                    "fact1": datasets.Value("string"),
                    "humanScore": datasets.Value("float"),
                    "clarity": datasets.Value("float"),
                    "turkIdAnonymized": datasets.Value("string"),
                }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        dl_dir = dl_manager.download_and_extract(_URL)
        data_dir = os.path.join(dl_dir, "OpenBookQA-V1-Sep2018", "Data", self.config.data_dir)
        splits = [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]
        return [
            datasets.SplitGenerator(
                name=split,
                gen_kwargs={"filepath": os.path.join(data_dir, self.config.filenames[split])},
            )
            for split in splits
        ]

    def _generate_examples(self, filepath):
        """Yields examples."""
        with open(filepath, encoding="utf-8") as f:
            for uid, row in enumerate(f):
                data = json.loads(row)
                example = {
                    "id": data["id"],
                    "question_stem": data["question"]["stem"],
                    "choices": {
                        "text": [choice["text"] for choice in data["question"]["choices"]],
                        "label": [choice["label"] for choice in data["question"]["choices"]],
                    },
                    "answerKey": data["answerKey"],
                }
                if self.config.name == "additional":
                    for key in ["fact1", "humanScore", "clarity", "turkIdAnonymized"]:
                        example[key] = data[key]
                yield uid, example