File size: 11,715 Bytes
0cd9f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79a328c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f26ec99
 
 
 
 
79a328c
 
0cd9f79
 
 
 
 
 
 
 
 
 
 
 
f26ec99
0cd9f79
 
 
 
 
 
 
79a328c
0cd9f79
 
 
79a328c
0cd9f79
79a328c
0cd9f79
79a328c
 
 
 
 
 
0cd9f79
 
79a328c
0cd9f79
f26ec99
0cd9f79
 
79a328c
0cd9f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54e4115
0cd9f79
 
 
 
 
 
54e4115
0cd9f79
 
 
 
 
 
54e4115
cc7f32d
0cd9f79
 
 
 
54e4115
0cd9f79
 
 
 
 
 
79a328c
 
 
0cd9f79
79a328c
0cd9f79
 
 
 
 
 
 
79a328c
0cd9f79
 
 
 
 
 
 
79a328c
 
 
 
 
 
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# 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.
"""RiSAWOZ: A Large-Scale Multi-Domain Wizard-of-Oz Dataset with Rich Semantic Annotations for Task-Oriented Dialogue Modeling"""


import json
import os
from typing import Dict

import datasets


_CITATION = """\
@inproceedings{quan-etal-2020-risawoz,
    title = "{R}i{SAWOZ}: A Large-Scale Multi-Domain {W}izard-of-{O}z Dataset with Rich Semantic Annotations for Task-Oriented Dialogue Modeling",
    author = "Quan, Jun  and
      Zhang, Shian  and
      Cao, Qian  and
      Li, Zizhong  and
      Xiong, Deyi",
    booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)",
    month = nov,
    year = "2020",
    address = "Online",
    publisher = "Association for Computational Linguistics",
    url = "https://www.aclweb.org/anthology/2020.emnlp-main.67",
    pages = "930--940",
}
"""


_DESCRIPTION = """\
RiSAWOZ contains 11.2K human-to-human (H2H) multiturn semantically annotated dialogues, \
with more than 150K utterances spanning over 12 domains, \
which is larger than all previous annotated H2H conversational datasets.\
Both single- and multi-domain dialogues are constructed, accounting for 65% and 35%, respectively.
"""

_HOMEPAGE = "https://github.com/terryqj0107/RiSAWOZ"

_LICENSE = "Attribution 4.0 International (CC BY 4.0) license."

_EMPTY_BELIEF_STATE = [
    "旅游景点-名称",
    "旅游景点-区域",
    "旅游景点-景点类型",
    "旅游景点-最适合人群",
    "旅游景点-消费",
    "旅游景点-是否地铁直达",
    "旅游景点-门票价格",
    "旅游景点-电话号码",
    "旅游景点-地址",
    "旅游景点-评分",
    "旅游景点-开放时间",
    "旅游景点-特点",
    "餐厅-名称",
    "餐厅-区域",
    "餐厅-菜系",
    "餐厅-价位",
    "餐厅-是否地铁直达",
    "餐厅-人均消费",
    "餐厅-地址",
    "餐厅-电话号码",
    "餐厅-评分",
    "餐厅-营业时间",
    "餐厅-推荐菜",
    "酒店-名称",
    "酒店-区域",
    "酒店-星级",
    "酒店-价位",
    "酒店-酒店类型",
    "酒店-房型",
    "酒店-停车场",
    "酒店-房费",
    "酒店-地址",
    "酒店-电话号码",
    "酒店-评分",
    "电脑-品牌",
    "电脑-产品类别",
    "电脑-分类",
    "电脑-内存容量",
    "电脑-屏幕尺寸",
    "电脑-CPU",
    "电脑-价格区间",
    "电脑-系列",
    "电脑-商品名称",
    "电脑-系统",
    "电脑-游戏性能",
    "电脑-CPU型号",
    "电脑-裸机重量",
    "电脑-显卡类别",
    "电脑-显卡型号",
    "电脑-特性",
    "电脑-色系",
    "电脑-待机时长",
    "电脑-硬盘容量",
    "电脑-价格",
    "火车-出发地",
    "火车-目的地",
    "火车-日期",
    "火车-车型",
    "火车-坐席",
    "火车-车次信息",
    "火车-时长",
    "火车-出发时间",
    "火车-到达时间",
    "火车-票价",
    "飞机-出发地",
    "飞机-目的地",
    "飞机-日期",
    "飞机-舱位档次",
    "飞机-航班信息",
    "飞机-起飞时间",
    "飞机-到达时间",
    "飞机-票价",
    "飞机-准点率",
    "天气-城市",
    "天气-日期",
    "天气-天气",
    "天气-温度",
    "天气-风力风向",
    "天气-紫外线强度",
    "电影-制片国家/地区",
    "电影-类型",
    "电影-年代",
    "电影-主演",
    "电影-导演",
    "电影-片名",
    "电影-主演名单",
    "电影-具体上映时间",
    "电影-片长",
    "电影-豆瓣评分",
    "电视剧-制片国家/地区",
    "电视剧-类型",
    "电视剧-年代",
    "电视剧-主演",
    "电视剧-导演",
    "电视剧-片名",
    "电视剧-主演名单",
    "电视剧-首播时间",
    "电视剧-集数",
    "电视剧-单集片长",
    "电视剧-豆瓣评分",
    "辅导班-班号",
    "辅导班-难度",
    "辅导班-科目",
    "辅导班-年级",
    "辅导班-区域",
    "辅导班-校区",
    "辅导班-上课方式",
    "辅导班-开始日期",
    "辅导班-结束日期",
    "辅导班-每周",
    "辅导班-上课时间",
    "辅导班-下课时间",
    "辅导班-时段",
    "辅导班-课次",
    "辅导班-课时",
    "辅导班-教室地点",
    "辅导班-教师",
    "辅导班-价格",
    "辅导班-课程网址",
    "辅导班-教师网址",
    "汽车-名称",
    "汽车-车型",
    "汽车-级别",
    "汽车-座位数",
    "汽车-车身尺寸(mm)",
    "汽车-厂商",
    "汽车-能源类型",
    "汽车-发动机排量(L)",
    "汽车-发动机马力(Ps)",
    "汽车-驱动方式",
    "汽车-综合油耗(L/100km)",
    "汽车-环保标准",
    "汽车-驾驶辅助影像",
    "汽车-巡航系统",
    "汽车-价格(万元)",
    "汽车-车系",
    "汽车-动力水平",
    "汽车-油耗水平",
    "汽车-倒车影像",
    "汽车-定速巡航",
    "汽车-座椅加热",
    "汽车-座椅通风",
    "汽车-所属价格区间",
    "医院-名称",
    "医院-等级",
    "医院-类别",
    "医院-性质",
    "医院-区域",
    "医院-地址",
    "医院-电话",
    "医院-挂号时间",
    "医院-门诊时间",
    "医院-公交线路",
    "医院-地铁可达",
    "医院-地铁线路",
    "医院-重点科室",
    "医院-CT",
    "医院-3.0T MRI",
    "医院-DSA",
    "通用-产品类别",
    "火车-舱位档次",
    "通用-系列",
    "通用-价格区间",
    "通用-品牌"
]


class RiSAWOZ(datasets.GeneratorBasedBuilder):
    """RiSAWOZ: A Large-Scale Multi-Domain Wizard-of-Oz Dataset with Rich Semantic Annotations for Task-Oriented Dialogue Modeling"""

    VERSION = datasets.Version("1.1.0")

    def _info(self):
        features = datasets.Features(
            {
                "dialogue_id": datasets.Value("string"),
                "goal": datasets.Value("string"),
                "domains": datasets.Sequence(datasets.Value("string")),
                "dialogue": datasets.Sequence(
                    {
                        "turn_id": datasets.Value("int32"),
                        "turn_domain": datasets.Sequence(datasets.Value("string")),
                        "user_utterance": datasets.Value("string"),
                        "system_utterance": datasets.Value("string"),
                        "belief_state": {
                            "inform slot-values": {
                                d: datasets.Value("string") for d in _EMPTY_BELIEF_STATE
                            },
                            # "inform slot-values": datasets.Value("string"),
                            "turn_inform": {
                                d: datasets.Value("string") for d in _EMPTY_BELIEF_STATE
                            },
                            "turn request": datasets.Sequence(datasets.Value("string")),
                        },
                        "user_actions": datasets.Sequence(
                            datasets.Sequence(datasets.Value("string"))
                        ),
                        "system_actions": datasets.Sequence(
                            datasets.Sequence(datasets.Value("string"))
                        ),
                        "db_results": datasets.Sequence(datasets.Value("string")),
                        "segmented_user_utterance": datasets.Value("string"),
                        "segmented_system_utterance": 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
        _URL = {"train": "train.json", "test": "test.json", "dev": "dev.json"}

        data_dir = dl_manager.download_and_extract(_URL)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": data_dir["train"],
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={"filepath": data_dir["test"], "split": "test"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": data_dir["dev"],
                    "split": "dev",
                },
            ),
        ]

    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.

        with open(filepath, encoding="utf-8") as f:
            all_data = json.load(f)
            id_ = 0
            for data in all_data:
                for slot in _EMPTY_BELIEF_STATE:
                    for dia in data["dialogue"]:
                        if slot not in dia["belief_state"]["inform slot-values"]:
                            dia["belief_state"]["inform slot-values"][slot] = ""
                        if slot not in dia["belief_state"]["turn_inform"]:
                            dia["belief_state"]["turn_inform"][slot] = ""

                yield id_, {
                    "dialogue_id": data["dialogue_id"],
                    "goal": data["goal"],
                    "domains": data["domains"],
                    "dialogue": data["dialogue"],
                }
                id_ += 1