Abinaya Mahendiran
commited on
Commit
•
32f772e
1
Parent(s):
291cc38
Added data loader script - totto
Browse files
totto.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
_CITATION = """\@inproceedings{parikh2020totto,
|
7 |
+
title={{ToTTo}: A Controlled Table-To-Text Generation Dataset},
|
8 |
+
author={Parikh, Ankur P and Wang, Xuezhi and Gehrmann, Sebastian and Faruqui, Manaal and Dhingra, Bhuwan and Yang, Diyi and Das, Dipanjan},
|
9 |
+
booktitle={Proceedings of EMNLP},
|
10 |
+
year={2020}
|
11 |
+
}
|
12 |
+
"""
|
13 |
+
|
14 |
+
_DESCRIPTION = """\
|
15 |
+
ToTTo is an open-domain English table-to-text dataset with over 120,000 training examples that proposes a controlled generation task: given a Wikipedia table and a set of highlighted table cells, produce a one-sentence description.
|
16 |
+
"""
|
17 |
+
|
18 |
+
_URLs = {
|
19 |
+
"totto": {
|
20 |
+
"data": "https://storage.googleapis.com/totto/totto_data.zip",
|
21 |
+
"challenge_set": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/totto.zip",
|
22 |
+
},
|
23 |
+
}
|
24 |
+
|
25 |
+
|
26 |
+
class Mlsum(datasets.GeneratorBasedBuilder):
|
27 |
+
|
28 |
+
BUILDER_CONFIGS = [
|
29 |
+
datasets.BuilderConfig(
|
30 |
+
version=datasets.Version("1.0.0"),
|
31 |
+
description=f"GEM benchmark: struct2text task",
|
32 |
+
)
|
33 |
+
]
|
34 |
+
|
35 |
+
def _info(self):
|
36 |
+
return datasets.DatasetInfo(
|
37 |
+
description=_DESCRIPTION,
|
38 |
+
features = datasets.Features(
|
39 |
+
{
|
40 |
+
"gem_id": datasets.Value("string"),
|
41 |
+
"gem_parent_id": datasets.Value("string"),
|
42 |
+
"totto_id": datasets.Value("int32"),
|
43 |
+
"table_page_title": datasets.Value("string"),
|
44 |
+
"table_webpage_url": datasets.Value("string"),
|
45 |
+
"table_section_title": datasets.Value("string"),
|
46 |
+
"table_section_text": datasets.Value("string"),
|
47 |
+
"table": [
|
48 |
+
[
|
49 |
+
{
|
50 |
+
"column_span": datasets.Value("int32"),
|
51 |
+
"is_header": datasets.Value("bool"),
|
52 |
+
"row_span": datasets.Value("int32"),
|
53 |
+
"value": datasets.Value("string"),
|
54 |
+
}
|
55 |
+
]
|
56 |
+
],
|
57 |
+
"highlighted_cells": [[datasets.Value("int32")]],
|
58 |
+
"example_id": datasets.Value("string"),
|
59 |
+
"sentence_annotations": [
|
60 |
+
{
|
61 |
+
"original_sentence": datasets.Value("string"),
|
62 |
+
"sentence_after_deletion": datasets.Value("string"),
|
63 |
+
"sentence_after_ambiguity": datasets.Value("string"),
|
64 |
+
"final_sentence": datasets.Value("string"),
|
65 |
+
}
|
66 |
+
],
|
67 |
+
"overlap_subset": datasets.Value("string"),
|
68 |
+
"target": datasets.Value("string"), # single target for train
|
69 |
+
"references": [datasets.Value("string")],
|
70 |
+
},
|
71 |
+
),
|
72 |
+
supervised_keys=None,
|
73 |
+
homepage="",
|
74 |
+
citation=_CITATION,
|
75 |
+
)
|
76 |
+
|
77 |
+
def _split_generators(self, dl_manager):
|
78 |
+
"""Returns SplitGenerators."""
|
79 |
+
dl_dir = dl_manager.download_and_extract(_URLs[self.config.name])
|
80 |
+
challenge_sets = [
|
81 |
+
("challenge_train_sample", "train_totto_RandomSample500.json"),
|
82 |
+
("challenge_validation_sample", "validation_totto_RandomSample500.json"),
|
83 |
+
("challenge_test_scramble", "test_totto_ScrambleInputStructure500.json"),
|
84 |
+
]
|
85 |
+
return [
|
86 |
+
datasets.SplitGenerator(
|
87 |
+
name=datasets.Split.TRAIN,
|
88 |
+
gen_kwargs={
|
89 |
+
"filepath": os.path.join(dl_dir["data"], "totto_data/totto_train_data.jsonl"),
|
90 |
+
"split": "train",
|
91 |
+
},
|
92 |
+
),
|
93 |
+
datasets.SplitGenerator(
|
94 |
+
name=datasets.Split.VALIDATION,
|
95 |
+
gen_kwargs={
|
96 |
+
"filepath": os.path.join(dl_dir["data"], "totto_data/totto_dev_data.jsonl"),
|
97 |
+
"split": "validation",
|
98 |
+
},
|
99 |
+
),
|
100 |
+
datasets.SplitGenerator(
|
101 |
+
name=datasets.Split.TEST,
|
102 |
+
gen_kwargs={
|
103 |
+
"filepath": os.path.join(dl_dir["data"], "totto_data/unlabeled_totto_test_data.jsonl"),
|
104 |
+
"split": "test",
|
105 |
+
},
|
106 |
+
),
|
107 |
+
] + [
|
108 |
+
datasets.SplitGenerator(
|
109 |
+
name=challenge_split,
|
110 |
+
gen_kwargs={
|
111 |
+
"filepath": os.path.join(dl_dir["challenge_set"], self.config.name, filename),
|
112 |
+
"split": challenge_split,
|
113 |
+
},
|
114 |
+
)
|
115 |
+
for challenge_split, filename in challenge_sets
|
116 |
+
]
|
117 |
+
|
118 |
+
def _generate_examples(self, filepath, split, filepaths=None, lang=None):
|
119 |
+
"""Yields examples."""
|
120 |
+
if "challenge" in split:
|
121 |
+
exples = json.load(open(filepath, encoding="utf-8"))
|
122 |
+
if isinstance(exples, dict):
|
123 |
+
assert len(exples) == 1, "multiple entries found"
|
124 |
+
exples = list(exples.values())[0]
|
125 |
+
for id_, exple in enumerate(exples):
|
126 |
+
if len(exple) == 0:
|
127 |
+
continue
|
128 |
+
exple["gem_parent_id"] = exple["gem_id"]
|
129 |
+
exple["gem_id"] = f"{self.config.name}-{split}-{id_}"
|
130 |
+
yield id_, exple
|
131 |
+
else:
|
132 |
+
with open(filepath, "r", encoding="utf-8") as json_file:
|
133 |
+
json_list = list(json_file)
|
134 |
+
id_ = -1
|
135 |
+
i = -1
|
136 |
+
for json_str in json_list:
|
137 |
+
result = json.loads(json_str)
|
138 |
+
if split == "train":
|
139 |
+
i += 1
|
140 |
+
for sentence in result["sentence_annotations"]:
|
141 |
+
id_ += 1
|
142 |
+
response = {
|
143 |
+
"gem_id": f"{self.config.name}-{split}-{id_}",
|
144 |
+
"gem_parent_id": f"{self.config.name}-{split}-{id_}",
|
145 |
+
"totto_id": i,
|
146 |
+
"table_page_title": result["table_page_title"],
|
147 |
+
"table_webpage_url": result["table_webpage_url"],
|
148 |
+
"table_section_title": result["table_section_title"],
|
149 |
+
"table_section_text": result["table_section_text"],
|
150 |
+
"table": result["table"],
|
151 |
+
"highlighted_cells": result["highlighted_cells"],
|
152 |
+
"example_id": str(result["example_id"]),
|
153 |
+
"overlap_subset": "none",
|
154 |
+
"sentence_annotations": [sentence],
|
155 |
+
"references": [],
|
156 |
+
"target": sentence["final_sentence"],
|
157 |
+
}
|
158 |
+
yield id_, response
|
159 |
+
else:
|
160 |
+
id_ += 1
|
161 |
+
response = {
|
162 |
+
"gem_id": f"{self.config.name}-{split}-{id_}",
|
163 |
+
"gem_parent_id": f"{self.config.name}-{split}-{id_}",
|
164 |
+
"totto_id": id_,
|
165 |
+
"table_page_title": result["table_page_title"],
|
166 |
+
"table_webpage_url": result["table_webpage_url"],
|
167 |
+
"table_section_title": result["table_section_title"],
|
168 |
+
"table_section_text": result["table_section_text"],
|
169 |
+
"table": result["table"],
|
170 |
+
"highlighted_cells": result["highlighted_cells"],
|
171 |
+
"example_id": str(result["example_id"]),
|
172 |
+
"overlap_subset": str(result["overlap_subset"]),
|
173 |
+
}
|
174 |
+
response["sentence_annotations"] = [] if split == "test" else result["sentence_annotations"]
|
175 |
+
response["references"] = [
|
176 |
+
sentence["final_sentence"] for sentence in response["sentence_annotations"]
|
177 |
+
]
|
178 |
+
response["target"] = response["references"][0] if len(response["references"]) > 0 else ""
|
179 |
+
yield id_, response
|
180 |
+
|