File size: 4,242 Bytes
63abb80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import json
import os
import datasets

_CITATION = """\
@inproceedings{e2e_cleaned,
	address = {Tokyo, Japan},
	title = {Semantic {Noise} {Matters} for {Neural} {Natural} {Language} {Generation}},
	url = {https://www.aclweb.org/anthology/W19-8652/},
	booktitle = {Proceedings of the 12th {International} {Conference} on {Natural} {Language} {Generation} ({INLG} 2019)},
	author = {Dušek, Ondřej and Howcroft, David M and Rieser, Verena},
	year = {2019},
	pages = {421--426},
}
"""

_DESCRIPTION = """\
The E2E dataset is designed for a limited-domain data-to-text task --
generation of restaurant descriptions/recommendations based on up to 8 different
attributes (name, area, price range etc.).
"""

_URLs = {
    "train": "https://github.com/tuetschek/e2e-cleaning/raw/master/cleaned-data/train-fixed.no-ol.csv",
    "validation": "https://github.com/tuetschek/e2e-cleaning/raw/master/cleaned-data/devel-fixed.no-ol.csv",
    "test": "https://github.com/tuetschek/e2e-cleaning/raw/master/cleaned-data/test-fixed.csv",
    "challenge_set": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/e2e_nlg.zip",
}


class E2ENlg(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    DEFAULT_CONFIG_NAME = "e2e_nlg"

    def _info(self):
        features = datasets.Features(
            {
                "gem_id": datasets.Value("string"),
                "gem_parent_id": datasets.Value("string"),
                "meaning_representation": datasets.Value("string"),
                "target": datasets.Value("string"),
                "references": [datasets.Value("string")],
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=datasets.info.SupervisedKeysData(
                input="meaning_representation", output="target"
            ),
            homepage="http://www.macs.hw.ac.uk/InteractionLab/E2E/",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        dl_dir = dl_manager.download_and_extract(_URLs)
        challenge_sets = [
            ("challenge_train_sample", "train_e2e_nlg_RandomSample500.json"),
            ("challenge_validation_sample", "validation_e2e_nlg_RandomSample500.json"),
            ("challenge_test_scramble", "test_e2e_nlg_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"], "e2e_nlg", 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"e2e_nlg-{split}-{id_}"
                yield id_, exple
        else:
            with open(filepath, encoding="utf-8") as f:
                reader = csv.DictReader(f)
                for id_, example in enumerate(reader):
                    yield id_, {
                        "gem_id": f"e2e_nlg-{split}-{id_}",
                        "gem_parent_id": f"e2e_nlg-{split}-{id_}",
                        "meaning_representation": example["mr"],
                        "target": example["ref"],
                        "references": [] if split == "train" else [example["ref"]],
                    }