File size: 4,774 Bytes
e077548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807227e
e077548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
957a67e
 
 
 
 
 
 
 
e077548
 
957a67e
 
 
e077548
 
 
 
 
957a67e
 
 
e077548
 
 
 
 
957a67e
e077548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import json
import os
import datasets

_CITATION = """\
@inproceedings{cs_restaurants,
	address = {Tokyo, Japan},
	title = {Neural {Generation} for {Czech}: {Data} and {Baselines}},
	shorttitle = {Neural {Generation} for {Czech}},
	url = {https://www.aclweb.org/anthology/W19-8670/},
	urldate = {2019-10-18},
	booktitle = {Proceedings of the 12th {International} {Conference} on {Natural} {Language} {Generation} ({INLG} 2019)},
	author = {Dušek, Ondřej and Jurčíček, Filip},
	month = oct,
	year = {2019},
	pages = {563--574},
}
"""

_DESCRIPTION = """\
The task is generating responses in the context of a (hypothetical) dialogue
system that provides information about restaurants. The input is a basic
intent/dialogue act type and a list of slots (attributes) and their values.
The output is a natural language sentence.
"""

_URLs = {
    "train": "https://raw.githubusercontent.com/UFAL-DSG/cs_restaurant_dataset/master/train.json",
    "validation": "https://raw.githubusercontent.com/UFAL-DSG/cs_restaurant_dataset/master/devel.json",
    "test": "https://raw.githubusercontent.com/UFAL-DSG/cs_restaurant_dataset/master/test.json",
    "challenge_set": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/cs_restaurants.zip",
}


class CSRestaurants(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    DEFAULT_CONFIG_NAME = "cs_restaurants"

    def _info(self):
        features = datasets.Features(
            {
                "gem_id": datasets.Value("string"),
                "gem_parent_id": datasets.Value("string"),
                "dialog_act": datasets.Value("string"),
                "dialog_act_delexicalized": datasets.Value("string"),
                "target_delexicalized": datasets.Value("string"),
                "target": datasets.Value("string"),
                "references": [datasets.Value("string")],
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=datasets.info.SupervisedKeysData(
                input="dialog_act", output="target"
            ),
            homepage="https://github.com/UFAL-DSG/cs_restaurant_dataset",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        dl_dir = dl_manager.download_and_extract(_URLs)
        challenge_sets = [
            ("challenge_train_sample", "train_cs_restaurants_RandomSample500.json"),
            (
                "challenge_validation_sample",
                "validation_cs_restaurants_RandomSample500.json",
            ),
            (
                "challenge_test_scramble",
                "test_cs_restaurants_ScrambleInputStructure500.json",
            ),
        ]
        return [
            datasets.SplitGenerator(
                name=spl, gen_kwargs={"filepath": dl_dir[spl], "split": spl}
            )
            for spl in ["train", "validation", "test"]
        ] + [
            datasets.SplitGenerator(
                name=challenge_split,
                gen_kwargs={
                    "filepath": os.path.join(
                        dl_dir["challenge_set"], "cs_restaurants", filename
                    ),
                    "split": challenge_split,
                },
            )
            for challenge_split, filename in challenge_sets
        ]

    def _generate_examples(self, filepath, split, filepaths=None, lang=None):
        """Yields examples."""
        if split.startswith("challenge"):
            exples = json.load(open(filepath, encoding="utf-8"))
            if isinstance(exples, dict):
                assert len(exples) == 1, "multiple entries found"
                exples = list(exples.values())[0]
            for id_, exple in enumerate(exples):
                if len(exple) == 0:
                    continue
                exple["gem_parent_id"] = exple["gem_id"]
                exple["gem_id"] = f"cs_restaurants-{split}-{id_}"
                yield id_, exple
        else:
            with open(filepath, encoding="utf8") as f:
                data = json.load(f)
                for id_, instance in enumerate(data):
                    yield id_, {
                        "gem_id": f"cs_restaurants-{split}-{id_}",
                        "gem_parent_id": f"cs_restaurants-{split}-{id_}",
                        "dialog_act": instance["da"],
                        "dialog_act_delexicalized": instance["delex_da"],
                        "target": instance["text"],
                        "target_delexicalized": instance["delex_text"],
                        "references": [] if split == "train" else [instance["text"]],
                    }