File size: 9,528 Bytes
796f0a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
from typing import List

import datasets

from nusacrowd.utils import schemas
from nusacrowd.utils.configs import NusantaraConfig
from nusacrowd.utils.constants import Tasks

_CITATION = """\
@inproceedings{van-der-goot-etal-2020-cross,
      title={From Masked-Language Modeling to Translation: Non-{E}nglish Auxiliary Tasks Improve Zero-shot Spoken Language Understanding},
      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},
    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)",
    year = "2021",
    address = "Mexico City, Mexico",
    publisher = "Association for Computational Linguistics"
}
"""
_DATASETNAME = "xsid"
_DESCRIPTION = """\
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.
"""
_HOMEPAGE = "https://bitbucket.org/robvanderg/xsid/src/master/"
_LANGUAGES = ["ind"]
_LICENSE = "CC-BY-SA 4.0"
_LOCAL = False
_URLS = {
    _DATASETNAME: "https://bitbucket.org/robvanderg/xsid/get/04ce1e6c8c28.zip",
}
_SUPPORTED_TASKS = [Tasks.INTENT_CLASSIFICATION, Tasks.POS_TAGGING]
_SOURCE_VERSION = "0.3.0"
_NUSANTARA_VERSION = "1.0.0"

INTENT_LIST = [
    "AddToPlaylist",
    "BookRestaurant",
    "PlayMusic",
    "RateBook",
    "SearchCreativeWork",
    "SearchScreeningEvent",
    "alarm/cancel_alarm",
    "alarm/modify_alarm",
    "alarm/set_alarm",
    "alarm/show_alarms",
    "alarm/snooze_alarm",
    "alarm/time_left_on_alarm",
    "reminder/cancel_reminder",
    "reminder/set_reminder",
    "reminder/show_reminders",
    "weather/checkSunrise",
    "weather/checkSunset",
    "weather/find"
]

TAG_LIST = [
    "B-album",
    "B-artist",
    "B-best_rating",
    "B-condition_description",
    "B-condition_temperature",
    "B-cuisine",
    "B-datetime",
    "B-ecurring_datetime",
    "B-entity_name",
    "B-facility",
    "B-genre",
    "B-location",
    "B-movie_name",
    "B-movie_type",
    "B-music_item",
    "B-object_location_type",
    "B-object_name",
    "B-object_part_of_series_type",
    "B-object_select",
    "B-object_type",
    "B-party_size_description",
    "B-party_size_number",
    "B-playlist",
    "B-rating_unit",
    "B-rating_value",
    "B-recurring_datetime",
    "B-reference",
    "B-reminder/todo",
    "B-restaurant_name",
    "B-restaurant_type",
    "B-served_dish",
    "B-service",
    "B-sort",
    "B-track",
    "B-weather/attribute",
    "I-album",
    "I-artist",
    "I-best_rating",
    "I-condition_description",
    "I-condition_temperature",
    "I-cuisine",
    "I-datetime",
    "I-ecurring_datetime",
    "I-entity_name",
    "I-facility",
    "I-genre",
    "I-location",
    "I-movie_name",
    "I-movie_type",
    "I-music_item",
    "I-object_location_type",
    "I-object_name",
    "I-object_part_of_series_type",
    "I-object_select",
    "I-object_type",
    "I-party_size_description",
    "I-party_size_number",
    "I-playlist",
    "I-rating_unit",
    "I-rating_value",
    "I-recurring_datetime",
    "I-reference",
    "I-reminder/todo",
    "I-restaurant_name",
    "I-restaurant_type",
    "I-served_dish",
    "I-service",
    "I-sort",
    "I-track",
    "I-weather/attribute",
    "O",
    "Orecurring_datetime"
]

class XSID(datasets.GeneratorBasedBuilder):
    """xSID datasets contains datasets to detect the intent from the text"""

    BUILDER_CONFIGS = [
        NusantaraConfig(
            name="xsid_source",
            version=datasets.Version(_SOURCE_VERSION),
            description="xSID source schema",
            schema="source",
            subset_id="xsid",
        ),
        NusantaraConfig(
            name="xsid_nusantara_text",
            version=datasets.Version(_NUSANTARA_VERSION),
            description="xSID Nusantara intent classification schema",
            schema="nusantara_text",
            subset_id="xsid",
        ),
        NusantaraConfig(
            name="xsid_nusantara_seq_label",
            version=datasets.Version(_NUSANTARA_VERSION),
            description="xSID Nusantara pos tagging schema",
            schema="nusantara_seq_label",
            subset_id="xsid",
        ),
    ]

    DEFAULT_CONFIG_NAME = "xsid_source"

    def _info(self) -> datasets.DatasetInfo:
        if self.config.schema == "source":
            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "text": datasets.Value("string"),
                    "text-en": datasets.Value("string"),
                    "intent": datasets.Value("string"),
                    "tokens": datasets.Sequence(datasets.Value("string")),
                }
            )
        elif self.config.schema == "nusantara_text":
            features = schemas.text_features(label_names=INTENT_LIST)
        elif self.config.schema == "nusantara_seq_label":
            features = schemas.seq_label_features(label_names=TAG_LIST)
        else:
            raise ValueError(f"Invalid config schema: {self.config.schema}")

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        urls = _URLS[_DATASETNAME]
        base_path = Path(dl_manager.download_and_extract(urls)) / "robvanderg-xsid-04ce1e6c8c28" / "data" / "xSID-0.3"
        data_files = {
            "train": base_path / "id.projectedTrain.conll",
            "test": base_path / "id.test.conll",
            "validation": base_path / "id.valid.conll"
        }

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": data_files["train"]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": data_files["test"]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": data_files["validation"]},
            ),
        ]

    def _generate_examples(self, filepath: Path):
        print('filepath', filepath)
        if self.config.name == "xsid_source":
            with open(filepath, "r") as file:
                data = file.read().strip("\n").split("\n\n")

            i = 0
            for sample in data:
                id = ""
                tokens = []
                for row_sample in sample.split("\n"):
                    s = row_sample.split(": ")
                    if s[0] == "# id":
                        id = s[1]
                    elif s[0] == "# text-en":
                        text_en = s[1]
                    elif s[0] == "# text":
                        text = s[1]
                    elif s[0] == "# intent":
                        intent = s[1]
                    else:
                        tokens.append(s[0])
                
                if id == "":
                    id = i
                    i = i + 1

                ex = {
                    "id": id,
                    "text": text,
                    "text-en": text_en,
                    "intent": intent,
                    "tokens": tokens
                }
                yield id, ex

        elif self.config.name == "xsid_nusantara_text":
            with open(filepath, "r") as file:
                data = file.read().strip("\n").split("\n\n")

            i = 0
            for sample in data:
                id = ""
                for row_sample in sample.split("\n"):
                    s = row_sample.split(": ")
                    if s[0] == "# id":
                        id = s[1]
                    elif s[0] == "# text":
                        text = s[1]
                    elif s[0] == "# intent":
                        intent = s[1]
                
                if id == "":
                    id = i
                    i = i + 1

                ex = {
                    "id": id,
                    "text": text,
                    "label": intent
                }
                yield id, ex

        elif self.config.name == "xsid_nusantara_seq_label":
            with open(filepath, "r") as file:
                data = file.read().strip("\n").split("\n\n")

            i = 0
            for sample in data:
                id = ""
                tokens = []
                labels = []
                for row_sample in sample.split("\n"):
                    s = row_sample.split(": ")
                    if s[0] == "# id":
                        id = s[1]
                    elif len(s) == 1:
                        tokens.append(s[0].split("\t")[1])
                        labels.append(s[0].split("\t")[3])
                
                if id == "":
                    id = i
                    i = i + 1

                ex = {
                    "id": id,
                    "tokens": tokens,
                    "labels": labels
                }
                yield id, ex

        else:
            raise ValueError(f"Invalid config: {self.config.name}")