File size: 14,188 Bytes
39d73d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12c5f10
7272637
920db25
39d73d2
 
 
0d3fb2a
efe877d
 
39d73d2
 
 
 
 
 
 
 
 
 
 
 
 
cf9759a
39d73d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf9759a
39d73d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efe877d
39d73d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62f4783
39d73d2
5849ec1
62f4783
5849ec1
 
62f4783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f08aba0
62f4783
 
 
f08aba0
5849ec1
 
39d73d2
 
 
 
 
 
 
 
4f0ba2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39d73d2
 
 
 
f08aba0
5849ec1
39d73d2
 
 
 
 
efe877d
39d73d2
4f0ba2e
39d73d2
4f0ba2e
39d73d2
 
 
7272637
39d73d2
 
 
daf5af2
39d73d2
 
e529c4a
efe877d
39d73d2
e111ca7
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# coding=utf-8
# 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.
"""CrossWOZ: A Large-Scale Chinese Cross-Domain Task-Oriented Dialogue Dataset"""


import json
import os

import datasets


_CITATION = """\
@article{zhu2020crosswoz,
  author = {Qi Zhu and Kaili Huang and Zheng Zhang and Xiaoyan Zhu and Minlie Huang},
  title = {Cross{WOZ}: A Large-Scale Chinese Cross-Domain Task-Oriented Dialogue Dataset},
  journal = {Transactions of the Association for Computational Linguistics},
  year = {2020}
}
"""

_DESCRIPTION = """\
CrossWOZ is the first large-scale Chinese Cross-Domain Wizard-of-Oz task-oriented dataset. \
It contains 6K dialogue sessions and 102K utterances for 5 domains, including hotel, \
restaurant, attraction, metro, and taxi. Moreover, the corpus contains rich annotation of \
dialogue states and dialogue acts at both user and system sides.
"""

_HOMEPAGE = "https://github.com/thu-coai/CrossWOZ"

_LICENSE = "Apache License, Version 2.0"


class CrossWOZ(datasets.GeneratorBasedBuilder):
    """CrossWOZ: A Large-Scale Chinese Cross-Domain Task-Oriented Dialogue Dataset"""

    VERSION = datasets.Version("1.1.0")

    def _info(self):
        features = datasets.Features(
            {
                "gem_id": datasets.Value("string"),
                "dialog_id": datasets.Value("string"),
                "sys_id": datasets.Value("int32"),
                "usr_id": datasets.Value("int32"),
                "goal": datasets.Sequence((datasets.Value("string"),)),
                "task description": datasets.Sequence(datasets.Value("string")),
                "type": datasets.Value("string"),
                "messages": datasets.Sequence(
                    {
                        "content": datasets.Value("string"),
                        "role": datasets.Value("string"),
                        "dialog_act": datasets.Sequence((datasets.Value("string"),)),
                        "user_state": datasets.Sequence((datasets.Value("string"),)),
                        "sys_state": {
                            "景点": {
                                "名称": datasets.Value("string"),
                                "门票": datasets.Value("string"),
                                "游玩时间": datasets.Value("string"),
                                "评分": datasets.Value("string"),
                                "周边景点": datasets.Value("string"),
                                "周边餐馆": datasets.Value("string"),
                                "周边酒店": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "餐馆": {
                                "名称": datasets.Value("string"),
                                "推荐菜": datasets.Value("string"),
                                "人均消费": datasets.Value("string"),
                                "评分": datasets.Value("string"),
                                "周边景点": datasets.Value("string"),
                                "周边餐馆": datasets.Value("string"),
                                "周边酒店": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "酒店": {
                                "名称": datasets.Value("string"),
                                "酒店类型": datasets.Value("string"),
                                "酒店设施": datasets.Value("string"),
                                "价格": datasets.Value("string"),
                                "评分": datasets.Value("string"),
                                "周边景点": datasets.Value("string"),
                                "周边餐馆": datasets.Value("string"),
                                "周边酒店": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "地铁": {
                                "出发地": datasets.Value("string"),
                                "目的地": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "出租": {
                                "出发地": datasets.Value("string"),
                                "目的地": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            }
                        },
                        "sys_state_init": {
                            "景点": {
                                "名称": datasets.Value("string"),
                                "门票": datasets.Value("string"),
                                "游玩时间": datasets.Value("string"),
                                "评分": datasets.Value("string"),
                                "周边景点": datasets.Value("string"),
                                "周边餐馆": datasets.Value("string"),
                                "周边酒店": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "餐馆": {
                                "名称": datasets.Value("string"),
                                "推荐菜": datasets.Value("string"),
                                "人均消费": datasets.Value("string"),
                                "评分": datasets.Value("string"),
                                "周边景点": datasets.Value("string"),
                                "周边餐馆": datasets.Value("string"),
                                "周边酒店": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "酒店": {
                                "名称": datasets.Value("string"),
                                "酒店类型": datasets.Value("string"),
                                "酒店设施": datasets.Value("string"),
                                "价格": datasets.Value("string"),
                                "评分": datasets.Value("string"),
                                "周边景点": datasets.Value("string"),
                                "周边餐馆": datasets.Value("string"),
                                "周边酒店": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "地铁": {
                                "出发地": datasets.Value("string"),
                                "目的地": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            },
                            "出租": {
                                "出发地": datasets.Value("string"),
                                "目的地": datasets.Value("string"),
                                "selectedResults": datasets.Sequence(datasets.Value("string"))
                            }
                        },
                    }
                ),
                "final_goal": datasets.Sequence((datasets.Value("string"),)),
            }
        )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features,
            # specify them here. They'll be used if as_supervised=True in
            # builder.as_dataset.
            supervised_keys=None,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
        # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
        # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
        data_dir = dl_manager.download_and_extract("data.zip")
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, "train.json"),
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, "test.json"),
                    "split": "test"
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, "val.json"),
                    "split": "dev",
                },
            ),
            datasets.SplitGenerator(
                name="challenge_CMT",
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, "test.json"),
                    "split": "challenge_CMT",
                },
            ),
        ]

    def _generate_examples(
        self, filepath, split  # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    ):
        """ Yields examples as (key, example) tuples. """
        # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is here for legacy reason (tfds) and is not important in itself.
        def empty_sys_state():
            return {
                "景点": {
                    "名称": "",
                    "门票": "",
                    "游玩时间": "",
                    "评分": "",
                    "周边景点": "",
                    "周边餐馆": "",
                    "周边酒店": "",
                    "selectedResults": []
                },
                "餐馆": {
                    "名称": "",
                    "推荐菜": "",
                    "人均消费": "",
                    "评分": "",
                    "周边景点": "",
                    "周边餐馆": "",
                    "周边酒店": "",
                    "selectedResults": []
                },
                "酒店": {
                    "名称": "",
                    "酒店类型": "",
                    "酒店设施": "",
                    "价格": "",
                    "评分": "",
                    "周边景点": "",
                    "周边餐馆": "",
                    "周边酒店": "",
                    "selectedResults": []
                },
                "地铁": {
                    "出发地": "",
                    "目的地": "",
                    "selectedResults": []
                },
                "出租": {
                    "出发地": "",
                    "目的地": "",
                    "selectedResults": []
                }
            }

        key = 0
        with open(filepath, encoding="utf-8") as f:
            data = json.load(f)
            for dialog_id, dialog in data.items():
                if split == "challenge_CMT" and dialog["type"] != "不独立多领域+交通":
                    continue
                messages = []
                for turn in dialog["messages"]:
                    if "user_state" not in turn:
                        turn["user_state"] = []
                    else:
                        turn["user_state"] = list(map(tuple, turn["user_state"]))
                    if "sys_state" not in turn:
                        turn["sys_state"] = empty_sys_state()
                    if "sys_state_init" not in turn:
                        turn["sys_state_init"] = empty_sys_state()
                    messages.append(turn)

                yield key, {
                    "gem_id": f"GEM-CrossWOZ-{split}-{key}",
                    "dialog_id": dialog_id,
                    "sys_id": dialog["sys-usr"][0],
                    "usr_id": dialog["sys-usr"][1],
                    "goal": list(map(tuple, dialog["goal"])),
                    "task description": dialog["task description"],
                    "type": dialog["type"],
                    "messages": messages,
                    "final_goal": list(map(tuple, dialog["final_goal"]))
                }
                key += 1