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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
from nusacrowd.utils import schemas
|
7 |
+
from nusacrowd.utils.configs import NusantaraConfig
|
8 |
+
from nusacrowd.utils.constants import Tasks
|
9 |
+
|
10 |
+
_CITATION = """\
|
11 |
+
@inproceedings{van-der-goot-etal-2020-cross,
|
12 |
+
title={From Masked-Language Modeling to Translation: Non-{E}nglish Auxiliary Tasks Improve Zero-shot Spoken Language Understanding},
|
13 |
+
author={van der Goot, Rob and Sharaf, Ibrahim and Imankulova, Aizhan and {\"U}st{\"u}n, Ahmet and Stepanovic, Marija and Ramponi, Alan and Khairunnisa, Siti Oryza and Komachi, Mamoru and Plank, Barbara},
|
14 |
+
booktitle = "Proceedings of the 2021 Conference of the North {A}merican Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long and Short Papers)",
|
15 |
+
year = "2021",
|
16 |
+
address = "Mexico City, Mexico",
|
17 |
+
publisher = "Association for Computational Linguistics"
|
18 |
+
}
|
19 |
+
"""
|
20 |
+
_DATASETNAME = "xsid"
|
21 |
+
_DESCRIPTION = """\
|
22 |
+
XSID is a new benchmark for cross-lingual (X) Slot and Intent Detection in 13 languages from 6 language families, including a very low-resource dialect.
|
23 |
+
"""
|
24 |
+
_HOMEPAGE = "https://bitbucket.org/robvanderg/xsid/src/master/"
|
25 |
+
_LANGUAGES = ["ind"]
|
26 |
+
_LICENSE = "CC-BY-SA 4.0"
|
27 |
+
_LOCAL = False
|
28 |
+
_URLS = {
|
29 |
+
_DATASETNAME: "https://bitbucket.org/robvanderg/xsid/get/04ce1e6c8c28.zip",
|
30 |
+
}
|
31 |
+
_SUPPORTED_TASKS = [Tasks.INTENT_CLASSIFICATION, Tasks.POS_TAGGING]
|
32 |
+
_SOURCE_VERSION = "0.3.0"
|
33 |
+
_NUSANTARA_VERSION = "1.0.0"
|
34 |
+
|
35 |
+
INTENT_LIST = [
|
36 |
+
"AddToPlaylist",
|
37 |
+
"BookRestaurant",
|
38 |
+
"PlayMusic",
|
39 |
+
"RateBook",
|
40 |
+
"SearchCreativeWork",
|
41 |
+
"SearchScreeningEvent",
|
42 |
+
"alarm/cancel_alarm",
|
43 |
+
"alarm/modify_alarm",
|
44 |
+
"alarm/set_alarm",
|
45 |
+
"alarm/show_alarms",
|
46 |
+
"alarm/snooze_alarm",
|
47 |
+
"alarm/time_left_on_alarm",
|
48 |
+
"reminder/cancel_reminder",
|
49 |
+
"reminder/set_reminder",
|
50 |
+
"reminder/show_reminders",
|
51 |
+
"weather/checkSunrise",
|
52 |
+
"weather/checkSunset",
|
53 |
+
"weather/find"
|
54 |
+
]
|
55 |
+
|
56 |
+
TAG_LIST = [
|
57 |
+
"B-album",
|
58 |
+
"B-artist",
|
59 |
+
"B-best_rating",
|
60 |
+
"B-condition_description",
|
61 |
+
"B-condition_temperature",
|
62 |
+
"B-cuisine",
|
63 |
+
"B-datetime",
|
64 |
+
"B-ecurring_datetime",
|
65 |
+
"B-entity_name",
|
66 |
+
"B-facility",
|
67 |
+
"B-genre",
|
68 |
+
"B-location",
|
69 |
+
"B-movie_name",
|
70 |
+
"B-movie_type",
|
71 |
+
"B-music_item",
|
72 |
+
"B-object_location_type",
|
73 |
+
"B-object_name",
|
74 |
+
"B-object_part_of_series_type",
|
75 |
+
"B-object_select",
|
76 |
+
"B-object_type",
|
77 |
+
"B-party_size_description",
|
78 |
+
"B-party_size_number",
|
79 |
+
"B-playlist",
|
80 |
+
"B-rating_unit",
|
81 |
+
"B-rating_value",
|
82 |
+
"B-recurring_datetime",
|
83 |
+
"B-reference",
|
84 |
+
"B-reminder/todo",
|
85 |
+
"B-restaurant_name",
|
86 |
+
"B-restaurant_type",
|
87 |
+
"B-served_dish",
|
88 |
+
"B-service",
|
89 |
+
"B-sort",
|
90 |
+
"B-track",
|
91 |
+
"B-weather/attribute",
|
92 |
+
"I-album",
|
93 |
+
"I-artist",
|
94 |
+
"I-best_rating",
|
95 |
+
"I-condition_description",
|
96 |
+
"I-condition_temperature",
|
97 |
+
"I-cuisine",
|
98 |
+
"I-datetime",
|
99 |
+
"I-ecurring_datetime",
|
100 |
+
"I-entity_name",
|
101 |
+
"I-facility",
|
102 |
+
"I-genre",
|
103 |
+
"I-location",
|
104 |
+
"I-movie_name",
|
105 |
+
"I-movie_type",
|
106 |
+
"I-music_item",
|
107 |
+
"I-object_location_type",
|
108 |
+
"I-object_name",
|
109 |
+
"I-object_part_of_series_type",
|
110 |
+
"I-object_select",
|
111 |
+
"I-object_type",
|
112 |
+
"I-party_size_description",
|
113 |
+
"I-party_size_number",
|
114 |
+
"I-playlist",
|
115 |
+
"I-rating_unit",
|
116 |
+
"I-rating_value",
|
117 |
+
"I-recurring_datetime",
|
118 |
+
"I-reference",
|
119 |
+
"I-reminder/todo",
|
120 |
+
"I-restaurant_name",
|
121 |
+
"I-restaurant_type",
|
122 |
+
"I-served_dish",
|
123 |
+
"I-service",
|
124 |
+
"I-sort",
|
125 |
+
"I-track",
|
126 |
+
"I-weather/attribute",
|
127 |
+
"O",
|
128 |
+
"Orecurring_datetime"
|
129 |
+
]
|
130 |
+
|
131 |
+
class XSID(datasets.GeneratorBasedBuilder):
|
132 |
+
"""xSID datasets contains datasets to detect the intent from the text"""
|
133 |
+
|
134 |
+
BUILDER_CONFIGS = [
|
135 |
+
NusantaraConfig(
|
136 |
+
name="xsid_source",
|
137 |
+
version=datasets.Version(_SOURCE_VERSION),
|
138 |
+
description="xSID source schema",
|
139 |
+
schema="source",
|
140 |
+
subset_id="xsid",
|
141 |
+
),
|
142 |
+
NusantaraConfig(
|
143 |
+
name="xsid_nusantara_text",
|
144 |
+
version=datasets.Version(_NUSANTARA_VERSION),
|
145 |
+
description="xSID Nusantara intent classification schema",
|
146 |
+
schema="nusantara_text",
|
147 |
+
subset_id="xsid",
|
148 |
+
),
|
149 |
+
NusantaraConfig(
|
150 |
+
name="xsid_nusantara_seq_label",
|
151 |
+
version=datasets.Version(_NUSANTARA_VERSION),
|
152 |
+
description="xSID Nusantara pos tagging schema",
|
153 |
+
schema="nusantara_seq_label",
|
154 |
+
subset_id="xsid",
|
155 |
+
),
|
156 |
+
]
|
157 |
+
|
158 |
+
DEFAULT_CONFIG_NAME = "xsid_source"
|
159 |
+
|
160 |
+
def _info(self) -> datasets.DatasetInfo:
|
161 |
+
if self.config.schema == "source":
|
162 |
+
features = datasets.Features(
|
163 |
+
{
|
164 |
+
"id": datasets.Value("string"),
|
165 |
+
"text": datasets.Value("string"),
|
166 |
+
"text-en": datasets.Value("string"),
|
167 |
+
"intent": datasets.Value("string"),
|
168 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
169 |
+
}
|
170 |
+
)
|
171 |
+
elif self.config.schema == "nusantara_text":
|
172 |
+
features = schemas.text_features(label_names=INTENT_LIST)
|
173 |
+
elif self.config.schema == "nusantara_seq_label":
|
174 |
+
features = schemas.seq_label_features(label_names=TAG_LIST)
|
175 |
+
else:
|
176 |
+
raise ValueError(f"Invalid config schema: {self.config.schema}")
|
177 |
+
|
178 |
+
return datasets.DatasetInfo(
|
179 |
+
description=_DESCRIPTION,
|
180 |
+
features=features,
|
181 |
+
homepage=_HOMEPAGE,
|
182 |
+
license=_LICENSE,
|
183 |
+
citation=_CITATION,
|
184 |
+
)
|
185 |
+
|
186 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
187 |
+
urls = _URLS[_DATASETNAME]
|
188 |
+
base_path = Path(dl_manager.download_and_extract(urls)) / "robvanderg-xsid-04ce1e6c8c28" / "data" / "xSID-0.3"
|
189 |
+
data_files = {
|
190 |
+
"train": base_path / "id.projectedTrain.conll",
|
191 |
+
"test": base_path / "id.test.conll",
|
192 |
+
"validation": base_path / "id.valid.conll"
|
193 |
+
}
|
194 |
+
|
195 |
+
return [
|
196 |
+
datasets.SplitGenerator(
|
197 |
+
name=datasets.Split.TRAIN,
|
198 |
+
gen_kwargs={"filepath": data_files["train"]},
|
199 |
+
),
|
200 |
+
datasets.SplitGenerator(
|
201 |
+
name=datasets.Split.TEST,
|
202 |
+
gen_kwargs={"filepath": data_files["test"]},
|
203 |
+
),
|
204 |
+
datasets.SplitGenerator(
|
205 |
+
name=datasets.Split.VALIDATION,
|
206 |
+
gen_kwargs={"filepath": data_files["validation"]},
|
207 |
+
),
|
208 |
+
]
|
209 |
+
|
210 |
+
def _generate_examples(self, filepath: Path):
|
211 |
+
print('filepath', filepath)
|
212 |
+
if self.config.name == "xsid_source":
|
213 |
+
with open(filepath, "r") as file:
|
214 |
+
data = file.read().strip("\n").split("\n\n")
|
215 |
+
|
216 |
+
i = 0
|
217 |
+
for sample in data:
|
218 |
+
id = ""
|
219 |
+
tokens = []
|
220 |
+
for row_sample in sample.split("\n"):
|
221 |
+
s = row_sample.split(": ")
|
222 |
+
if s[0] == "# id":
|
223 |
+
id = s[1]
|
224 |
+
elif s[0] == "# text-en":
|
225 |
+
text_en = s[1]
|
226 |
+
elif s[0] == "# text":
|
227 |
+
text = s[1]
|
228 |
+
elif s[0] == "# intent":
|
229 |
+
intent = s[1]
|
230 |
+
else:
|
231 |
+
tokens.append(s[0])
|
232 |
+
|
233 |
+
if id == "":
|
234 |
+
id = i
|
235 |
+
i = i + 1
|
236 |
+
|
237 |
+
ex = {
|
238 |
+
"id": id,
|
239 |
+
"text": text,
|
240 |
+
"text-en": text_en,
|
241 |
+
"intent": intent,
|
242 |
+
"tokens": tokens
|
243 |
+
}
|
244 |
+
yield id, ex
|
245 |
+
|
246 |
+
elif self.config.name == "xsid_nusantara_text":
|
247 |
+
with open(filepath, "r") as file:
|
248 |
+
data = file.read().strip("\n").split("\n\n")
|
249 |
+
|
250 |
+
i = 0
|
251 |
+
for sample in data:
|
252 |
+
id = ""
|
253 |
+
for row_sample in sample.split("\n"):
|
254 |
+
s = row_sample.split(": ")
|
255 |
+
if s[0] == "# id":
|
256 |
+
id = s[1]
|
257 |
+
elif s[0] == "# text":
|
258 |
+
text = s[1]
|
259 |
+
elif s[0] == "# intent":
|
260 |
+
intent = s[1]
|
261 |
+
|
262 |
+
if id == "":
|
263 |
+
id = i
|
264 |
+
i = i + 1
|
265 |
+
|
266 |
+
ex = {
|
267 |
+
"id": id,
|
268 |
+
"text": text,
|
269 |
+
"label": intent
|
270 |
+
}
|
271 |
+
yield id, ex
|
272 |
+
|
273 |
+
elif self.config.name == "xsid_nusantara_seq_label":
|
274 |
+
with open(filepath, "r") as file:
|
275 |
+
data = file.read().strip("\n").split("\n\n")
|
276 |
+
|
277 |
+
i = 0
|
278 |
+
for sample in data:
|
279 |
+
id = ""
|
280 |
+
tokens = []
|
281 |
+
labels = []
|
282 |
+
for row_sample in sample.split("\n"):
|
283 |
+
s = row_sample.split(": ")
|
284 |
+
if s[0] == "# id":
|
285 |
+
id = s[1]
|
286 |
+
elif len(s) == 1:
|
287 |
+
tokens.append(s[0].split("\t")[1])
|
288 |
+
labels.append(s[0].split("\t")[3])
|
289 |
+
|
290 |
+
if id == "":
|
291 |
+
id = i
|
292 |
+
i = i + 1
|
293 |
+
|
294 |
+
ex = {
|
295 |
+
"id": id,
|
296 |
+
"tokens": tokens,
|
297 |
+
"labels": labels
|
298 |
+
}
|
299 |
+
yield id, ex
|
300 |
+
|
301 |
+
else:
|
302 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|