CrossWOZ / CrossWOZ.py
zqwerty
update script
daf5af2
raw
history blame
16.1 kB
# 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("int32"),datasets.Value("string"),datasets.Value("string"),datasets.Value("string"),datasets.Value("bool"))),
# {
# "sub_goal_id": datasets.Value("int32"),
# "domain": datasets.Value("string"),
# "slot": datasets.Value("string"),
# "value": datasets.Value("string"),
# "has_mentioned": datasets.Value("bool"),
# }
# ),
"task description": datasets.Value("string"),
"type": datasets.Value("string"),
"messages": datasets.Sequence(
{
"content": datasets.Value("string"),
"role": datasets.Value("string"),
"dialog_act": datasets.Sequence(
{
"intent": datasets.Value("string"),
"domain": datasets.Value("string"),
"slot": datasets.Value("string"),
"value": datasets.Value("string"),
}
),
"user_state": datasets.Sequence(
{
"sub_goal_id": datasets.Value("int32"),
"domain": datasets.Value("string"),
"slot": datasets.Value("string"),
"value": datasets.Value("string"),
"has_mentioned": datasets.Value("bool"),
}
),
"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(
{
"sub_goal_id": datasets.Value("int32"),
"domain": datasets.Value("string"),
"slot": datasets.Value("string"),
"value": datasets.Value("string"),
"has_mentioned": datasets.Value("bool"),
}
)
}
)
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",
},
),
]
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 convert_goal(raw_goal):
goal = []
for subgoal in raw_goal:
goal.append({
"sub_goal_id": subgoal[0],
"domain": subgoal[1],
"slot": subgoal[2],
"value": str(subgoal[3]),
"has_mentioned": subgoal[4],
})
return goal
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():
messages = []
for turn in dialog["messages"]:
dialog_act = []
for da in turn["dialog_act"]:
dialog_act.append({
"intent": da[0],
"domain": da[1],
"slot": da[2],
"value": da[3],
})
turn["dialog_act"] = dialog_act
if "user_state" not in turn:
turn["user_state"] = []
else:
turn["user_state"] = convert_goal(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"{self.config.name}-{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": convert_goal(dialog["final_goal"])
}
key += 1