File size: 8,674 Bytes
e250e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09d032f
e250e6c
 
 
 
 
 
 
c3386a7
e250e6c
 
 
 
 
 
c3386a7
e250e6c
 
 
 
 
09d032f
e250e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17278ac
 
 
e250e6c
 
 
 
 
 
17278ac
 
 
e250e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33297f1
 
e250e6c
 
 
 
33297f1
 
 
 
e250e6c
 
 
 
 
 
 
 
 
 
c3386a7
e250e6c
c3386a7
e250e6c
 
c3386a7
 
 
 
e250e6c
 
 
 
 
 
c3386a7
e250e6c
 
c3386a7
 
e250e6c
c3386a7
 
 
 
 
33297f1
c3386a7
e250e6c
c3386a7
e250e6c
 
c3386a7
e250e6c
33297f1
 
 
 
c3386a7
e250e6c
 
 
 
 
 
c3386a7
e250e6c
 
 
 
 
c3386a7
 
 
 
e250e6c
c3386a7
e250e6c
 
 
c3386a7
 
 
 
 
 
e250e6c
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import json
import os

import datasets
from datasets import DatasetInfo, DownloadManager


class DuReaderConfig(datasets.BuilderConfig):
    """Config for DuReader dataset"""

    def __init__(self, name, data_url, **kwargs):
        super().__init__(name=name, version=datasets.Version("1.0.0", ""))
        self.data_url = data_url


class DuReader(datasets.GeneratorBasedBuilder):
    """ """

    BUILDER_CONFIGS = [
        DuReaderConfig(
            name="robust",
            data_url="https://dataset-bj.cdn.bcebos.com/qianyan/dureader_robust-data.tar.gz",
        ),
        DuReaderConfig(
            name="checklist",
            data_url="https://dataset-bj.cdn.bcebos.com/qianyan/dureader_checklist-data.tar.gz",
        ),
        # DuReaderConfig(
        #     name="yesno",
        #     data_url="https://dataset-bj.cdn.bcebos.com/qianyan/dureader_yesno-data.tar.gz",
        # ),
    ]

    def _info(self) -> DatasetInfo:
        if self.config.name == "robust":
            features = {
                "id": datasets.Value("string"),
                "context": datasets.Value("string"),
                "question": datasets.Value("string"),
                "answers": datasets.Sequence(
                    {
                        "text": datasets.Value("string"),
                        "answer_start": datasets.Value("int32"),
                    }
                ),
            }
            return datasets.DatasetInfo(
                description="",
                citation="",
                homepage="",
                features=datasets.Features(features),
            )
        if self.config.name == "checklist":
            features = {
                "id": datasets.Value("string"),
                "title": datasets.Value("string"),
                "context": datasets.Value("string"),
                "question": datasets.Value("string"),
                "is_impossible": datasets.Value("string"),
                "answers": datasets.Sequence(
                    {
                        "text": datasets.Value("string"),
                        "answer_start": datasets.Value("int32"),
                    }
                ),
                "type": datasets.Value("string"),
            }
            return datasets.DatasetInfo(
                description="",
                citation="",
                homepage="",
                features=datasets.Features(features),
            )

        return None

    def _split_generators(self, dl_manager: DownloadManager):
        """Split generators"""

        def _build(train_files, valid_files, test_files):
            train_split = datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_file": train_files,
                    "split": "train",
                },
            )
            valid_split = datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_file": valid_files,
                    "split": "dev",
                },
            )
            test_split = datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "data_file": test_files,
                    "split": "test",
                },
            )
            return [train_split, valid_split, test_split]

        if self.config.name == "robust":
            dl_dir = dl_manager.download_and_extract(self.config.data_url)
            splits = _build(
                train_files=os.path.join(dl_dir, "dureader_robust-data", "train.json"),
                valid_files=os.path.join(dl_dir, "dureader_robust-data", "dev.json"),
                test_files=os.path.join(dl_dir, "dureader_robust-data", "test.json"),
            )
            return splits

        if self.config.name == "checklist":
            dl_dir = dl_manager.download_and_extract(self.config.data_url)
            splits = _build(
                train_files=os.path.join(dl_dir, "dureader_checklist-data", "train.json"),
                valid_files=os.path.join(dl_dir, "dureader_checklist-data", "dev.json"),
                test_files=os.path.join(dl_dir, "dureader_checklist-data", "test.json"),
            )
            return splits

        return []

    def _generate_examples(self, data_file, split):
        if self.config.name == "robust":
            if split == "train" or split == "dev":
                return self._generate_robust_examples(data_file)
            return self._generate_robust_test_examples(data_file)

        if self.config.name == "checklist":
            if split == "train" or split == "dev":
                return self._generate_checklist_examples(data_file)
            return self._generate_checklist_test_examples(data_file)

    def _generate_robust_examples(self, data_file):
        with open(data_file, mode="rt", encoding="utf-8") as fin:
            data = json.load(fin)["data"]
        for d in data:
            for p in d["paragraphs"]:
                context = p["context"]
                for qa in p["qas"]:
                    starts = [x["answer_start"] for x in qa["answers"]]
                    answers = [x["text"] for x in qa["answers"]]
                    example = {
                        "id": qa["id"],
                        "context": context,
                        "question": qa["question"],
                        "answers": {
                            "text": answers,
                            "answer_start": starts,
                        },
                    }
                    yield example["id"], example

    def _generate_robust_test_examples(self, data_file):
        with open(data_file, mode="rt", encoding="utf-8") as fin:
            data = json.load(fin)["data"]
        for d in data:
            for p in d["paragraphs"]:
                context = p["context"]
                for qa in p["qas"]:
                    qid = qa["id"]
                    example = {
                        "id": qid,
                        "context": context,
                        "question": qa["question"],
                        "answers": {
                            "text": [],
                            "answer_start": [],
                        },
                    }
                    yield example["id"], example

    def _generate_checklist_examples(self, data_file):
        with open(data_file, mode="rt", encoding="utf-8") as fin:
            data = json.load(fin)["data"]
        exist_ids = set()
        for d in data:
            for p in d["paragraphs"]:
                title = p["title"].strip()
                context = p["context"].strip()
                for qa in p["qas"]:
                    qid = qa["id"]
                    # skip dumplicate keys
                    if qid in exist_ids:
                        continue
                    exist_ids.add(qid)
                    starts = [x["answer_start"] for x in qa["answers"]]
                    answers = [x["text"].strip() for x in qa["answers"]]
                    example = {
                        "id": qid,
                        "title": title,
                        "context": context,
                        "question": qa["question"].strip(),
                        "is_impossible": qa["is_impossible"],
                        "answers": {
                            "text": answers,
                            "answer_start": starts,
                        },
                        "type": qa["type"].strip(),
                    }
                    yield example["id"], example

    def _generate_checklist_test_examples(self, data_file):
        with open(data_file, mode="rt", encoding="utf-8") as fin:
            data = json.load(fin)["data"]
        exist_ids = set()
        for d in data:
            for p in d["paragraphs"]:
                title = p["title"]
                context = p["context"]
                for qa in p["qas"]:
                    qid = qa["id"]
                    if qid in exist_ids:
                        continue
                    exist_ids.add(qid)
                    example = {
                        "id": qid,
                        "title": title,
                        "context": context,
                        "question": qa["question"],
                        "is_impossible": None,
                        "answers": {
                            "text": [],
                            "answer_start": [],
                        },
                        "type": None,
                    }
                    yield example["id"], example