dataloader
Browse files- dstc10_track2_task2.py +182 -0
dstc10_track2_task2.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
from datasets import BuilderConfig
|
7 |
+
from datasets.features import Features
|
8 |
+
|
9 |
+
MAX_DIRECTORY_NAME_LENGTH = 255
|
10 |
+
|
11 |
+
|
12 |
+
_CITATION = """\
|
13 |
+
@article{kim2020domain,
|
14 |
+
title={Beyond Domain APIs: Task-oriented Conversational Modeling with Unstructured Knowledge Access},
|
15 |
+
author={Seokhwan Kim and Mihail Eric and Karthik Gopalakrishnan and Behnam Hedayatnia and Yang Liu and Dilek Hakkani-Tur},
|
16 |
+
journal={arXiv preprint arXiv:2006.03533}
|
17 |
+
year={2020}
|
18 |
+
}
|
19 |
+
"""
|
20 |
+
|
21 |
+
_HOMEPAGE = "https://github.com/alexa/alexa-with-dstc10-track2-dataset"
|
22 |
+
|
23 |
+
|
24 |
+
_DESCRIPTION = """\
|
25 |
+
|
26 |
+
"""
|
27 |
+
|
28 |
+
_BASE_URL_DSTC10 = "https://raw.githubusercontent.com/alexa/alexa-with-dstc10-track2-dataset/main/task2"
|
29 |
+
_BASE_URL_DSTC9 = "https://raw.githubusercontent.com/alexa/alexa-with-dstc9-track1-dataset/master"
|
30 |
+
_URLs = {
|
31 |
+
"train": {
|
32 |
+
"logs": f"{_BASE_URL_DSTC9}/data/train/logs.json",
|
33 |
+
"labels": f"{_BASE_URL_DSTC9}/data/train/labels.json",
|
34 |
+
"knowledge": f"{_BASE_URL_DSTC9}/data/knowledge.json",
|
35 |
+
},
|
36 |
+
"validation": {
|
37 |
+
"logs": f"{_BASE_URL_DSTC10}/data/val/logs.json",
|
38 |
+
"labels": f"{_BASE_URL_DSTC10}/data/val/labels.json",
|
39 |
+
"knowledge": f"{_BASE_URL_DSTC10}/data/knowledge.json",
|
40 |
+
},
|
41 |
+
"test": {
|
42 |
+
"logs": f"{_BASE_URL_DSTC10}/data/test/logs.json",
|
43 |
+
"labels": f"{_BASE_URL_DSTC10}/data/test/labels.json",
|
44 |
+
"knowledge": f"{_BASE_URL_DSTC10}/data/knowledge.json",
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
class DSTC10Track2Task2(datasets.GeneratorBasedBuilder):
|
49 |
+
|
50 |
+
VERSION = datasets.Version("1.0.0")
|
51 |
+
|
52 |
+
BUILDER_CONFIGS = [
|
53 |
+
BuilderConfig(
|
54 |
+
name="detection",
|
55 |
+
version=VERSION,
|
56 |
+
description="",
|
57 |
+
),
|
58 |
+
BuilderConfig(
|
59 |
+
name="selection",
|
60 |
+
version=VERSION,
|
61 |
+
description="",
|
62 |
+
),
|
63 |
+
BuilderConfig(
|
64 |
+
name="generation",
|
65 |
+
version=VERSION,
|
66 |
+
description="",
|
67 |
+
),
|
68 |
+
]
|
69 |
+
|
70 |
+
DEFAULT_CONFIG_NAME = "generation"
|
71 |
+
|
72 |
+
def _info(self):
|
73 |
+
|
74 |
+
if self.config.name == "generation":
|
75 |
+
features = datasets.Features(
|
76 |
+
{
|
77 |
+
"id": datasets.Value("string"),
|
78 |
+
"gem_id": datasets.Value("string"),
|
79 |
+
"turns": [
|
80 |
+
{
|
81 |
+
"speaker": datasets.Value("string"),
|
82 |
+
"text": datasets.Value("string"),
|
83 |
+
"nbest": [
|
84 |
+
{
|
85 |
+
"hyp": datasets.Value("string"),
|
86 |
+
"score": datasets.Value("float")
|
87 |
+
}
|
88 |
+
]
|
89 |
+
}
|
90 |
+
],
|
91 |
+
"knowledge":
|
92 |
+
{
|
93 |
+
"domain": datasets.Value("string"),
|
94 |
+
"entity_name": datasets.Value("string"),
|
95 |
+
"title": datasets.Value("string"),
|
96 |
+
"body": datasets.Value("string"),
|
97 |
+
},
|
98 |
+
"response": datasets.Value("string"),
|
99 |
+
"source": datasets.Value("string"),
|
100 |
+
}
|
101 |
+
)
|
102 |
+
else:
|
103 |
+
raise Exception(f"Unexpected config name: {self.config.name}")
|
104 |
+
|
105 |
+
return datasets.DatasetInfo(
|
106 |
+
description=_DESCRIPTION,
|
107 |
+
features=features,
|
108 |
+
supervised_keys=None,
|
109 |
+
homepage=_HOMEPAGE,
|
110 |
+
citation=_CITATION,
|
111 |
+
)
|
112 |
+
|
113 |
+
def _generate_examples(self, logs, knowledge, labels, split=None):
|
114 |
+
with open(logs) as fp:
|
115 |
+
logs_data = json.load(fp)
|
116 |
+
with open(labels) as fp:
|
117 |
+
labels_data = json.load(fp)
|
118 |
+
with open(knowledge) as fp:
|
119 |
+
knowledge_data = json.load(fp)
|
120 |
+
|
121 |
+
i = 0
|
122 |
+
|
123 |
+
for log, label in zip(logs_data, labels_data):
|
124 |
+
if not label["target"]:
|
125 |
+
continue
|
126 |
+
|
127 |
+
# Ensure that nbest is in all turns
|
128 |
+
for turn in log:
|
129 |
+
if "nbest" not in turn:
|
130 |
+
turn["nbest"] = []
|
131 |
+
|
132 |
+
if "source" not in label:
|
133 |
+
source = "multiwoz"
|
134 |
+
else:
|
135 |
+
source = label["source"]
|
136 |
+
|
137 |
+
domain, entity_id, doc_id = (label["knowledge"][0].get(key) for key in ["domain", "entity_id", "doc_id"])
|
138 |
+
entity_name = knowledge_data[domain][str(entity_id)]["name"]
|
139 |
+
snippet = knowledge_data[domain][str(entity_id)]["docs"][str(doc_id)]
|
140 |
+
|
141 |
+
x = {
|
142 |
+
"id": str(i),
|
143 |
+
"gem_id": f"GEM-dstc10_track2_task2-{split}-{i}",
|
144 |
+
"turns": log,
|
145 |
+
"source": source,
|
146 |
+
"knowledge": {
|
147 |
+
"domain": domain,
|
148 |
+
"entity_name": entity_name,
|
149 |
+
"title": snippet["title"],
|
150 |
+
"body": snippet["body"]
|
151 |
+
},
|
152 |
+
"response": label["response"]
|
153 |
+
}
|
154 |
+
|
155 |
+
i += 1
|
156 |
+
|
157 |
+
yield x["id"], x
|
158 |
+
|
159 |
+
def _download_files(self, urls, data_files, dl_manager):
|
160 |
+
if data_files is not None:
|
161 |
+
for split, update_dict in data_files.items():
|
162 |
+
if isinstance(update_dict, dict):
|
163 |
+
for key, value in update_dict.items():
|
164 |
+
urls[split][key] = value
|
165 |
+
|
166 |
+
return dl_manager.download_and_extract(urls)
|
167 |
+
|
168 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
169 |
+
urls_to_download = _URLs
|
170 |
+
downloaded_files = self._download_files(urls_to_download, self.config.data_files, dl_manager)
|
171 |
+
for split in ["train", "validation", "test"]:
|
172 |
+
downloaded_files[split]["split"] = split
|
173 |
+
|
174 |
+
return [
|
175 |
+
datasets.SplitGenerator(name=ds_split, gen_kwargs=downloaded_files[split])
|
176 |
+
for ds_split, split in (
|
177 |
+
(datasets.Split.TRAIN, "train"),
|
178 |
+
(datasets.Split.VALIDATION, "validation"),
|
179 |
+
(datasets.Split.TEST, "test"),
|
180 |
+
)
|
181 |
+
]
|
182 |
+
|