Datasets:
Tasks:
Table to Text
Modalities:
Text
Languages:
English
Size:
100K - 1M
ArXiv:
Tags:
data-to-text
License:
File size: 13,231 Bytes
cbc53cf 32f772e cbc53cf 32f772e ce3fe91 32f772e cbc53cf 32f772e c166902 32f772e cbc53cf 32f772e cbc53cf 32f772e cbc53cf 89635c5 cbc53cf 32f772e ca65b5a 7e08b55 cbc53cf 7e08b55 32f772e cbc53cf 32f772e cbc53cf 32f772e cbc53cf 32f772e cbc53cf 32f772e cbc53cf 32f772e cbc53cf 32f772e cbc53cf 32f772e |
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
import copy
import json
import os
import datasets
_CITATION = """\@inproceedings{parikh2020totto,
title={{ToTTo}: A Controlled Table-To-Text Generation Dataset},
author={Parikh, Ankur P and Wang, Xuezhi and Gehrmann, Sebastian and Faruqui, Manaal and Dhingra, Bhuwan and Yang, Diyi and Das, Dipanjan},
booktitle={Proceedings of EMNLP},
year={2020}
}
"""
_DESCRIPTION = """\
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.
"""
_URLs = {
"totto": {
"data": "https://storage.googleapis.com/totto-public/totto_data.zip",
"challenge_set": "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/totto.zip",
},
}
def _add_adjusted_col_offsets(table):
"""Add adjusted column offsets to take into account multi-column cells."""
adjusted_table = []
for row in table:
real_col_index = 0
adjusted_row = []
for cell in row:
adjusted_cell = copy.deepcopy(cell)
adjusted_cell["adjusted_col_start"] = real_col_index
adjusted_cell["adjusted_col_end"] = (
adjusted_cell["adjusted_col_start"] + adjusted_cell["column_span"]
)
real_col_index += adjusted_cell["column_span"]
adjusted_row.append(adjusted_cell)
adjusted_table.append(adjusted_row)
return adjusted_table
def _get_heuristic_row_headers(adjusted_table, row_index, col_index):
"""Heuristic to find row headers."""
row_headers = []
row = adjusted_table[row_index]
for i in range(0, col_index):
if row[i]["is_header"]:
row_headers.append(row[i])
return row_headers
def _get_heuristic_col_headers(adjusted_table, row_index, col_index):
"""Heuristic to find column headers."""
adjusted_cell = adjusted_table[row_index][col_index]
adjusted_col_start = adjusted_cell["adjusted_col_start"]
adjusted_col_end = adjusted_cell["adjusted_col_end"]
col_headers = []
for r in range(0, row_index):
row = adjusted_table[r]
for cell in row:
if (
cell["adjusted_col_start"] < adjusted_col_end
and cell["adjusted_col_end"] > adjusted_col_start
):
if cell["is_header"]:
col_headers.append(cell)
return col_headers
def get_highlighted_subtable(table, cell_indices, with_heuristic_headers=False):
"""Extract out the highlighted part of a table."""
highlighted_table = []
adjusted_table = _add_adjusted_col_offsets(table)
for (row_index, col_index) in cell_indices:
cell = table[row_index][col_index]
if with_heuristic_headers:
row_headers = _get_heuristic_row_headers(
adjusted_table, row_index, col_index
)
col_headers = _get_heuristic_col_headers(
adjusted_table, row_index, col_index
)
else:
row_headers = []
col_headers = []
highlighted_cell = {
"cell": cell,
"row_headers": row_headers,
"col_headers": col_headers,
}
highlighted_table.append(highlighted_cell)
return highlighted_table
def linearize_subtable(subtable, table_page_title, table_section_title):
"""Linearize the highlighted subtable and return a string of its contents."""
table_str = ""
if table_page_title:
table_str += "<page_title> " + table_page_title + " </page_title> "
if table_section_title:
table_str += "<section_title> " + table_section_title + " </section_title> "
table_str += "<table> "
for item in subtable:
cell = item["cell"]
row_headers = item["row_headers"]
col_headers = item["col_headers"]
# The value of the cell.
item_str = "<cell> " + cell["value"] + " "
# All the column headers associated with this cell.
for col_header in col_headers:
item_str += "<col_header> " + col_header["value"] + " </col_header> "
# All the row headers associated with this cell.
for row_header in row_headers:
item_str += "<row_header> " + row_header["value"] + " </row_header> "
item_str += "</cell> "
table_str += item_str
table_str += "</table>"
return table_str
def linearize(example):
table = example["table"]
table_page_title = example["table_page_title"]
table_section_title = example["table_section_title"]
cell_indices = example["highlighted_cells"]
subtable = get_highlighted_subtable(
table=table, cell_indices=cell_indices, with_heuristic_headers=True
)
subtable_metadata_str = linearize_subtable(
subtable=subtable,
table_page_title=table_page_title,
table_section_title=table_section_title,
)
return subtable_metadata_str
class Totto(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="totto",
version=datasets.Version("1.0.0"),
description=f"GEM benchmark: struct2text task",
)
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"gem_id": datasets.Value("string"),
"gem_parent_id": datasets.Value("string"),
"totto_id": datasets.Value("int32"),
"table_page_title": datasets.Value("string"),
"table_webpage_url": datasets.Value("string"),
"table_section_title": datasets.Value("string"),
"table_section_text": datasets.Value("string"),
"table": [
[
{
"column_span": datasets.Value("int32"),
"is_header": datasets.Value("bool"),
"row_span": datasets.Value("int32"),
"value": datasets.Value("string"),
}
]
],
"highlighted_cells": [[datasets.Value("int32")]],
"example_id": datasets.Value("string"),
"sentence_annotations": [
{
"original_sentence": datasets.Value("string"),
"sentence_after_deletion": datasets.Value("string"),
"sentence_after_ambiguity": datasets.Value("string"),
"final_sentence": datasets.Value("string"),
}
],
"overlap_subset": datasets.Value("string"),
"target": datasets.Value("string"), # single target for train
"references": [datasets.Value("string")],
"linearized_input": datasets.Value("string"),
},
),
supervised_keys=None,
homepage="",
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
dl_dir = dl_manager.download_and_extract(_URLs[self.config.name])
challenge_sets = [
("challenge_train_sample", "train_totto_RandomSample500.json"),
("challenge_validation_sample", "validation_totto_RandomSample500.json"),
# ("challenge_test_scramble", "test_totto_ScrambleInputStructure500.json"),
]
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": os.path.join(
dl_dir["data"], "totto_data/totto_train_data.jsonl"
),
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepath": os.path.join(
dl_dir["data"], "totto_data/totto_dev_data.jsonl"
),
"split": "validation",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": os.path.join(
dl_dir["data"], "totto_data/unlabeled_totto_test_data.jsonl"
),
"split": "test",
},
),
] + [
datasets.SplitGenerator(
name=challenge_split,
gen_kwargs={
"filepath": os.path.join(
dl_dir["challenge_set"], self.config.name, filename
),
"split": challenge_split,
},
)
for challenge_split, filename in challenge_sets
]
def _generate_examples(self, filepath, split, filepaths=None, lang=None):
"""Yields examples."""
if "challenge" in split:
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"{self.config.name}-{split}-{id_}"
exple["linearized_input"] = linearize(exple)
yield id_, exple
else:
with open(filepath, "r", encoding="utf-8") as json_file:
json_list = list(json_file)
id_ = -1
i = -1
for json_str in json_list:
result = json.loads(json_str)
linearized_input = linearize(result)
if split == "train":
i += 1
for sentence in result["sentence_annotations"]:
id_ += 1
response = {
"gem_id": f"{self.config.name}-{split}-{id_}",
"gem_parent_id": f"{self.config.name}-{split}-{id_}",
"totto_id": i,
"table_page_title": result["table_page_title"],
"table_webpage_url": result["table_webpage_url"],
"table_section_title": result["table_section_title"],
"table_section_text": result["table_section_text"],
"table": result["table"],
"highlighted_cells": result["highlighted_cells"],
"example_id": str(result["example_id"]),
"overlap_subset": "none",
"sentence_annotations": [sentence],
"references": [sentence["final_sentence"]],
"target": sentence["final_sentence"],
"linearized_input": linearized_input,
}
yield id_, response
else:
id_ += 1
response = {
"gem_id": f"{self.config.name}-{split}-{id_}",
"gem_parent_id": f"{self.config.name}-{split}-{id_}",
"totto_id": id_,
"table_page_title": result["table_page_title"],
"table_webpage_url": result["table_webpage_url"],
"table_section_title": result["table_section_title"],
"table_section_text": result["table_section_text"],
"table": result["table"],
"highlighted_cells": result["highlighted_cells"],
"example_id": str(result["example_id"]),
"overlap_subset": str(result["overlap_subset"]),
"linearized_input": linearized_input,
}
response["sentence_annotations"] = (
[] if split == "test" else result["sentence_annotations"]
)
response["references"] = [
sentence["final_sentence"]
for sentence in response["sentence_annotations"]
]
response["target"] = (
response["references"][0]
if len(response["references"]) > 0
else ""
)
yield id_, response
|