File size: 10,560 Bytes
13b916e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os

import datasets

_OPEN_SLU_CITATION = """\
xxx"""

_OPEN_SLU_DESCRIPTION = """\
xxx"""

_ATIS_CITATION = """\
@inproceedings{hemphill1990atis,
	title = "The {ATIS} Spoken Language Systems Pilot Corpus",
	author = "Hemphill, Charles T.  and
	Godfrey, John J.  and
	Doddington, George R.",
	booktitle = "Speech and Natural Language: Proceedings of a Workshop Held at Hidden Valley, {P}ennsylvania, June 24-27,1990",
	year = "1990",
	url = "https://aclanthology.org/H90-1021",
}
"""

_ATIS_DESCRIPTION = """\
 A widely used SLU corpus for single-intent SLU.
"""

_SNIPS_CITATION = """\
@article{coucke2018snips,
	title={Snips voice platform: an embedded spoken language understanding system for private-by-design voice interfaces},
	author={Coucke, Alice and Saade, Alaa and Ball, Adrien and Bluche, Th{\'e}odore and Caulier, Alexandre and Leroy, David and Doumouro, Cl{\'e}ment and Giss\textsf{el}brecht, Thibault and Caltagirone, Francesco and Lavril, Thibaut and others},
	journal={arXiv preprint arXiv:1805.10190},
	year={2018}
}
"""

_SNIPS_DESCRIPTION = """\
 A widely used SLU corpus for single-intent SLU.
"""

_MIX_ATIS_CITATION = """\
@inproceedings{qin2020agif,
	title = "{AGIF}: An Adaptive Graph-Interactive Framework for Joint Multiple Intent Detection and Slot Filling",
	author = "Qin, Libo  and
	Xu, Xiao  and
	Che, Wanxiang  and
	Liu, Ting",
	booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2020",
	month = nov,
	year = "2020",
	address = "Online",
	publisher = "Association for Computational Linguistics",
	url = "https://aclanthology.org/2020.findings-emnlp.163",
	doi = "10.18653/v1/2020.findings-emnlp.163",
	pages = "1807--1816",
	abstract = "In real-world scenarios, users usually have multiple intents in the same utterance. Unfortunately, most spoken language understanding (SLU) models either mainly focused on the single intent scenario, or simply incorporated an overall intent context vector for all tokens, ignoring the fine-grained multiple intents information integration for token-level slot prediction. In this paper, we propose an Adaptive Graph-Interactive Framework (AGIF) for joint multiple intent detection and slot filling, where we introduce an intent-slot graph interaction layer to model the strong correlation between the slot and intents. Such an interaction layer is applied to each token adaptively, which has the advantage to automatically extract the relevant intents information, making a fine-grained intent information integration for the token-level slot prediction. Experimental results on three multi-intent datasets show that our framework obtains substantial improvement and achieves the state-of-the-art performance. In addition, our framework achieves new state-of-the-art performance on two single-intent datasets.",
}
"""

_MIX_ATIS_DESCRIPTION = """\
 A widely used SLU corpus for multi-intent SLU.
"""

_MIX_SNIPS_CITATION = """\
@inproceedings{qin2020agif,
	title = "{AGIF}: An Adaptive Graph-Interactive Framework for Joint Multiple Intent Detection and Slot Filling",
	author = "Qin, Libo  and
	Xu, Xiao  and
	Che, Wanxiang  and
	Liu, Ting",
	booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2020",
	month = nov,
	year = "2020",
	address = "Online",
	publisher = "Association for Computational Linguistics",
	url = "https://aclanthology.org/2020.findings-emnlp.163",
	doi = "10.18653/v1/2020.findings-emnlp.163",
	pages = "1807--1816",
	abstract = "In real-world scenarios, users usually have multiple intents in the same utterance. Unfortunately, most spoken language understanding (SLU) models either mainly focused on the single intent scenario, or simply incorporated an overall intent context vector for all tokens, ignoring the fine-grained multiple intents information integration for token-level slot prediction. In this paper, we propose an Adaptive Graph-Interactive Framework (AGIF) for joint multiple intent detection and slot filling, where we introduce an intent-slot graph interaction layer to model the strong correlation between the slot and intents. Such an interaction layer is applied to each token adaptively, which has the advantage to automatically extract the relevant intents information, making a fine-grained intent information integration for the token-level slot prediction. Experimental results on three multi-intent datasets show that our framework obtains substantial improvement and achieves the state-of-the-art performance. In addition, our framework achieves new state-of-the-art performance on two single-intent datasets.",
}
"""

_MIX_SNIPS_DESCRIPTION = """\
 A widely used SLU corpus for multi-intent SLU.
"""


class OpenSLUConfig(datasets.BuilderConfig):
    """BuilderConfig for OpenSLU."""

    def __init__(self, features, data_url, citation, url, intent_label_classes=None, slot_label_classes=None, **kwargs):
        """BuilderConfig for OpenSLU.
        Args:
          features: `list[string]`, list of the features that will appear in the
            feature dict. Should not include "label".
          data_url: `string`, url to download the zip file from.
          citation: `string`, citation for the data set.
          url: `string`, url for information about the data set.
          intent_label_classes: `list[string]`, the list of classes for the intent label
          slot_label_classes: `list[string]`, the list of classes for the slot label
          **kwargs: keyword arguments forwarded to super.
        """
        # Version history:
        # 0.0.1: Initial version.
        super(OpenSLUConfig, self).__init__(version=datasets.Version("0.0.1"), **kwargs)
        self.features = features
        self.intent_label_classes = intent_label_classes
        self.slot_label_classes = slot_label_classes
        self.data_url = data_url
        self.citation = citation
        self.url = url


class OpenSLU(datasets.GeneratorBasedBuilder):
    """The SuperGLUE benchmark."""

    BUILDER_CONFIGS = [
        OpenSLUConfig(
            name="atis",
            description=_ATIS_DESCRIPTION,
            features=["text"],
            data_url="https://huggingface.co/datasets/LightChen2333/OpenSLU/resolve/main/atis.tar.gz",
            citation=_ATIS_CITATION,
            url="https://aclanthology.org/H90-1021",
        ),
        OpenSLUConfig(
            name="snips",
            description=_SNIPS_DESCRIPTION,
            features=["text"],
            data_url="https://huggingface.co/datasets/LightChen2333/OpenSLU/resolve/main/snips.tar.gz",
            citation=_SNIPS_CITATION,
            url="https://arxiv.org/abs/1805.10190",
        ),
        OpenSLUConfig(
            name="mix-atis",
            description=_MIX_ATIS_DESCRIPTION,
            features=["text"],
            data_url="https://huggingface.co/datasets/LightChen2333/OpenSLU/resolve/main/mix-atis.tar.gz",
            citation=_MIX_ATIS_CITATION,
            url="https://aclanthology.org/2020.findings-emnlp.163",
        ),
        OpenSLUConfig(
            name="mix-snips",
            description=_MIX_SNIPS_DESCRIPTION,
            features=["text"],
            data_url="https://huggingface.co/datasets/LightChen2333/OpenSLU/resolve/main/mix-snips.tar.gz",
            citation=_MIX_SNIPS_CITATION,
            url="https://aclanthology.org/2020.findings-emnlp.163",
        ),
    ]

    def _info(self):
        features = {feature: datasets.Sequence(datasets.Value("string")) for feature in self.config.features}
        features["slot"] = datasets.Sequence(datasets.Value("string"))
        features["intent"] = datasets.Value("string")

        return datasets.DatasetInfo(
            description=_OPEN_SLU_DESCRIPTION + self.config.description,
            features=datasets.Features(features),
            homepage=self.config.url,
            citation=self.config.citation + "\n" + _OPEN_SLU_CITATION,
        )

    def _split_generators(self, dl_manager):
        print(self.config.data_url)
        dl_dir = dl_manager.download_and_extract(self.config.data_url) or ""

        task_name = _get_task_name_from_data_url(self.config.data_url)
        print(dl_dir)
        print(task_name)
        dl_dir = os.path.join(dl_dir, task_name)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "train.jsonl"),
                    "split": datasets.Split.TRAIN,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "dev.jsonl"),
                    "split": datasets.Split.VALIDATION,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "test.jsonl"),
                    "split": datasets.Split.TEST,
                },
            ),
        ]

    def _generate_examples(self, data_file, split):
        with open(data_file, encoding="utf-8") as f:
            for index, line in enumerate(f):
                row = json.loads(line)
                yield index, row


def _cast_label(label):
    """Converts the label into the appropriate string version."""
    if isinstance(label, str):
        return label
    elif isinstance(label, bool):
        return "True" if label else "False"
    elif isinstance(label, int):
        assert label in (0, 1)
        return str(label)
    else:
        raise ValueError("Invalid label format.")


def _get_record_entities(passage):
    """Returns the unique set of entities."""
    text = passage["text"]
    entity_spans = list()
    for entity in passage["entities"]:
        entity_text = text[entity["start"]: entity["end"] + 1]
        entity_spans.append({"text": entity_text, "start": entity["start"], "end": entity["end"] + 1})
    entity_spans = sorted(entity_spans, key=lambda e: e["start"])  # sort by start index
    entity_texts = set(e["text"] for e in entity_spans)  # for backward compatability
    return entity_texts, entity_spans


def _get_record_answers(qa):
    """Returns the unique set of answers."""
    if "answers" not in qa:
        return []
    answers = set()
    for answer in qa["answers"]:
        answers.add(answer["text"])
    return sorted(answers)


def _get_task_name_from_data_url(data_url):
    return data_url.split("/")[-1].split(".")[0]