File size: 8,147 Bytes
a9a54e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import zipfile
import json
import base64
import sys
import traceback

import datasets

_CITATION = """\
    @inproceedings{lecorve2022sparql2text,
        title={SPARQL-to-Text Question Generation for Knowledge-Based Conversational Applications},
        author={Lecorv\'e, Gw\'enol\'e and Veyret, Morgan and Brabant, Quentin and Rojas-Barahona, Lina M.},
        journal={Proceedings of the Conference of the Asia-Pacific Chapter of the Association for Computational Linguistics and the International Joint Conference on Natural Language Processing (AACL-IJCNLP)},
        year={2022}
    }
"""

_HOMEPAGE = ""

_URLS = {
    "train": "train.json",
    "dev": "dev.json",
    "test": "test.json",
    "challenge": "challenge.json"
}

_DESCRIPTION = """\
Augmented version of WebNLG v3.0 English with follow-up SPARQL queries with their associated answer(s). A small portion of it also contains natural language questions associated with the queries.
"""

class WebNLGQA(datasets.GeneratorBasedBuilder):
    """
    WebNLG-QA: Augmented version of WebNLG v3.0 English with follow-up SPARQL queries with their associated answer(s). A small portion of it also contains natural language questions associated with the queries.
    """

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # datasets.features.FeatureConnectors
            features=datasets.Features(
                {
                    "category": datasets.Value("string"),
                    "size": datasets.Value("int32"),
                    "id": datasets.Value("string"),
                    "eid": datasets.Value("string"),
                    "original_triple_sets": [
                        {"subject": datasets.Value("string"),
                         "property": datasets.Value("string"),
                         "object": datasets.Value("string")}
                    ],
                    "modified_triple_sets": [
                        {"subject": datasets.Value("string"),
                         "property": datasets.Value("string"),
                         "object": datasets.Value("string")}
                    ],
                    "shape": datasets.Value("string"),
                    "shape_type": datasets.Value("string"),
                    "lex": datasets.Sequence(
                        {
                            "comment": datasets.Value("string"),
                            "lid": datasets.Value("string"),
                            "text": datasets.Value("string"),
                            "lang": datasets.Value("string"),
                        }
                    ),
                    "test_category": datasets.Value("string"),
                    "dbpedia_links": datasets.Sequence(datasets.Value("string")),
                    "links": datasets.Sequence(datasets.Value("string")),
                    "graph": [
                        [datasets.Value("string")]
                    ],
                    "main_entity": datasets.Value("string"),
                    "mappings": [
                                    {
                                        "modified": datasets.Value("string"),
                                        "readable": datasets.Value("string"),
                                        "graph": datasets.Value("string")
                                    }
                                ],
                    "dialogue": [
                            {
                                "question": [ {
                                  "source": datasets.Value("string"),
                                  "text": datasets.Value("string")
                                }],
                                "graph_query": datasets.Value("string"),
                                "readable_query": datasets.Value("string"),
                                "graph_answer": [
                                    datasets.Value("string")
                                ],
                                "readable_answer": [
                                    datasets.Value("string")
                                ],
                                "type": [ datasets.Value("string") ]
                            }
                        ]
                }
            ),
            # If there's a common (input, target) tuple from the features,
            # specify them here. They'll be used if as_supervised=True in
            # builder.as_dataset
            supervised_keys=None,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # Downloads the data and defines the splits
        # dl_manager is a datasets.download.DownloadManager that can be used to
        # download and extract URLs
        paths = dl_manager.download_and_extract(_URLS)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": paths['train'],
                            "split": "train"}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": paths['dev'],
                            "split": "dev"}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": paths['test'],
                            "split": "test"}
            ),
            datasets.SplitGenerator(
                name="challenge",
                gen_kwargs={"filepath": paths['challenge'],
                            "split": "challenge"}
            )
        ]


    def _generate_examples(self, filepath, split):
        """Yields examples."""

        def transform_sample(original_sample):
            transformed_sample = {
                    "category": "",
                    "size": -1,
                    "id": "",
                    "eid": "",
                    "original_triple_sets": [],
                    "modified_triple_sets": [],
                    "shape": "",
                    "shape_type": "",
                    "lex": [],
                    "test_category": "",
                    "dbpedia_links": [],
                    "links": [],
                    "graph": [],
                    "main_entity": "",
                    "mappings": [],
                    "dialogue": []
                }

            for (old_key, new_key) in [("modifiedtripleset", "modified_triple_sets"), ("originaltriplesets", "original_triple_sets"), ("dbpedialinks", "dbpedia_links"), ("lexicalisations", "lex"), ("xml_id", "eid")]:
                original_sample[new_key] = original_sample[old_key]
                del original_sample[old_key]

            original_sample["original_triple_sets"] = original_sample["original_triple_sets"]["originaltripleset"][0]

            for l in original_sample["lex"]:
                l["lid"] = l["xml_id"]
                del l["xml_id"]
                l["text"] = l["lex"]
                del l["lex"]

            for turn in original_sample["dialogue"]:
                if "question" in turn:
                    old_format = turn["question"]
                    new_format = []
                    for source, text in old_format.items():
                        new_format.append({"source": source, "text": text})
                    turn["question"] = new_format


            for k in transformed_sample:
                if k in original_sample:
                    transformed_sample[k] = original_sample[k]
            # transformed_sample.update(original_sample)

            return transformed_sample

        # Yields (key, example) tuples from the dataset
        with open(filepath,'r') as f:
            data = json.load(f)
            key = 0
            for it in data:
                yield key, transform_sample(it)
                key += 1