Sebastian Gehrmann commited on
Commit
cbc53cf
1 Parent(s): 46806f7

linearization

Browse files
Files changed (2) hide show
  1. dataset_infos.json +1 -1
  2. totto.py +188 -43
dataset_infos.json CHANGED
@@ -1 +1 @@
1
- {"totto": {"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. \n", "citation": "\\@inproceedings{parikh2020totto,\ntitle={{ToTTo}: A Controlled Table-To-Text Generation Dataset},\nauthor={Parikh, Ankur P and Wang, Xuezhi and Gehrmann, Sebastian and Faruqui, Manaal and Dhingra, Bhuwan and Yang, Diyi and Das, Dipanjan},\nbooktitle={Proceedings of EMNLP},\nyear={2020}\n}\n", "homepage": "", "license": "", "features": {"gem_id": {"dtype": "string", "id": null, "_type": "Value"}, "gem_parent_id": {"dtype": "string", "id": null, "_type": "Value"}, "totto_id": {"dtype": "int32", "id": null, "_type": "Value"}, "table_page_title": {"dtype": "string", "id": null, "_type": "Value"}, "table_webpage_url": {"dtype": "string", "id": null, "_type": "Value"}, "table_section_title": {"dtype": "string", "id": null, "_type": "Value"}, "table_section_text": {"dtype": "string", "id": null, "_type": "Value"}, "table": [[{"column_span": {"dtype": "int32", "id": null, "_type": "Value"}, "is_header": {"dtype": "bool", "id": null, "_type": "Value"}, "row_span": {"dtype": "int32", "id": null, "_type": "Value"}, "value": {"dtype": "string", "id": null, "_type": "Value"}}]], "highlighted_cells": [[{"dtype": "int32", "id": null, "_type": "Value"}]], "example_id": {"dtype": "string", "id": null, "_type": "Value"}, "sentence_annotations": [{"original_sentence": {"dtype": "string", "id": null, "_type": "Value"}, "sentence_after_deletion": {"dtype": "string", "id": null, "_type": "Value"}, "sentence_after_ambiguity": {"dtype": "string", "id": null, "_type": "Value"}, "final_sentence": {"dtype": "string", "id": null, "_type": "Value"}}], "overlap_subset": {"dtype": "string", "id": null, "_type": "Value"}, "target": {"dtype": "string", "id": null, "_type": "Value"}, "references": [{"dtype": "string", "id": null, "_type": "Value"}]}, "post_processed": null, "supervised_keys": null, "task_templates": null, "builder_name": "mlsum", "config_name": "totto", "version": {"version_str": "1.0.0", "description": null, "major": 1, "minor": 0, "patch": 0}, "splits": {"train": {"name": "train", "num_bytes": 669655077, "num_examples": 121153, "dataset_name": "mlsum"}, "validation": {"name": "validation", "num_bytes": 50334466, "num_examples": 7700, "dataset_name": "mlsum"}, "test": {"name": "test", "num_bytes": 40896774, "num_examples": 7700, "dataset_name": "mlsum"}, "challenge_train_sample": {"name": "challenge_train_sample", "num_bytes": 2262167, "num_examples": 500, "dataset_name": "mlsum"}, "challenge_validation_sample": {"name": "challenge_validation_sample", "num_bytes": 3371787, "num_examples": 500, "dataset_name": "mlsum"}, "challenge_test_scramble": {"name": "challenge_test_scramble", "num_bytes": 2612484, "num_examples": 500, "dataset_name": "mlsum"}}, "download_checksums": {"https://storage.googleapis.com/totto-public/totto_data.zip": {"num_bytes": 187724372, "checksum": "0aab72597057394514fd9659745fd2b318d1a64bf0b2ca1b2c339abe0692fdf2"}, "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/totto.zip": {"num_bytes": 1810237, "checksum": "86bcec978edc44caa7a313944cecaaeb52e4685ee05f7be073911a15665d5ac3"}}, "download_size": 189534609, "post_processing_size": null, "dataset_size": 769132755, "size_in_bytes": 958667364}}
1
+ {"totto": {"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.\n", "citation": "\\@inproceedings{parikh2020totto,\ntitle={{ToTTo}: A Controlled Table-To-Text Generation Dataset},\nauthor={Parikh, Ankur P and Wang, Xuezhi and Gehrmann, Sebastian and Faruqui, Manaal and Dhingra, Bhuwan and Yang, Diyi and Das, Dipanjan},\nbooktitle={Proceedings of EMNLP},\nyear={2020}\n}\n", "homepage": "", "license": "", "features": {"gem_id": {"dtype": "string", "id": null, "_type": "Value"}, "gem_parent_id": {"dtype": "string", "id": null, "_type": "Value"}, "totto_id": {"dtype": "int32", "id": null, "_type": "Value"}, "table_page_title": {"dtype": "string", "id": null, "_type": "Value"}, "table_webpage_url": {"dtype": "string", "id": null, "_type": "Value"}, "table_section_title": {"dtype": "string", "id": null, "_type": "Value"}, "table_section_text": {"dtype": "string", "id": null, "_type": "Value"}, "table": [[{"column_span": {"dtype": "int32", "id": null, "_type": "Value"}, "is_header": {"dtype": "bool", "id": null, "_type": "Value"}, "row_span": {"dtype": "int32", "id": null, "_type": "Value"}, "value": {"dtype": "string", "id": null, "_type": "Value"}}]], "highlighted_cells": [[{"dtype": "int32", "id": null, "_type": "Value"}]], "example_id": {"dtype": "string", "id": null, "_type": "Value"}, "sentence_annotations": [{"original_sentence": {"dtype": "string", "id": null, "_type": "Value"}, "sentence_after_deletion": {"dtype": "string", "id": null, "_type": "Value"}, "sentence_after_ambiguity": {"dtype": "string", "id": null, "_type": "Value"}, "final_sentence": {"dtype": "string", "id": null, "_type": "Value"}}], "overlap_subset": {"dtype": "string", "id": null, "_type": "Value"}, "target": {"dtype": "string", "id": null, "_type": "Value"}, "references": [{"dtype": "string", "id": null, "_type": "Value"}], "linearized_input": {"dtype": "string", "id": null, "_type": "Value"}}, "post_processed": null, "supervised_keys": null, "task_templates": null, "builder_name": "totto", "config_name": "totto", "version": {"version_str": "1.0.0", "description": null, "major": 1, "minor": 0, "patch": 0}, "splits": {"train": {"name": "train", "num_bytes": 747884464, "num_examples": 121153, "dataset_name": "totto"}, "validation": {"name": "validation", "num_bytes": 54580701, "num_examples": 7700, "dataset_name": "totto"}, "test": {"name": "test", "num_bytes": 45476943, "num_examples": 7700, "dataset_name": "totto"}, "challenge_train_sample": {"name": "challenge_train_sample", "num_bytes": 2521208, "num_examples": 500, "dataset_name": "totto"}, "challenge_validation_sample": {"name": "challenge_validation_sample", "num_bytes": 3693554, "num_examples": 500, "dataset_name": "totto"}}, "download_checksums": {"https://storage.googleapis.com/totto-public/totto_data.zip": {"num_bytes": 187724372, "checksum": "0aab72597057394514fd9659745fd2b318d1a64bf0b2ca1b2c339abe0692fdf2"}, "https://storage.googleapis.com/huggingface-nlp/datasets/gem/gem_challenge_sets/totto.zip": {"num_bytes": 1810237, "checksum": "86bcec978edc44caa7a313944cecaaeb52e4685ee05f7be073911a15665d5ac3"}}, "download_size": 189534609, "post_processing_size": null, "dataset_size": 854156870, "size_in_bytes": 1043691479}}
totto.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import json
2
  import os
3
 
@@ -12,7 +13,7 @@ year={2020}
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 = {
@@ -23,7 +24,132 @@ _URLs = {
23
  }
24
 
25
 
26
- class Mlsum(datasets.GeneratorBasedBuilder):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  BUILDER_CONFIGS = [
29
  datasets.BuilderConfig(
@@ -36,7 +162,7 @@ class Mlsum(datasets.GeneratorBasedBuilder):
36
  def _info(self):
37
  return datasets.DatasetInfo(
38
  description=_DESCRIPTION,
39
- features = datasets.Features(
40
  {
41
  "gem_id": datasets.Value("string"),
42
  "gem_parent_id": datasets.Value("string"),
@@ -68,6 +194,7 @@ class Mlsum(datasets.GeneratorBasedBuilder):
68
  "overlap_subset": datasets.Value("string"),
69
  "target": datasets.Value("string"), # single target for train
70
  "references": [datasets.Value("string")],
 
71
  },
72
  ),
73
  supervised_keys=None,
@@ -79,43 +206,51 @@ class Mlsum(datasets.GeneratorBasedBuilder):
79
  """Returns SplitGenerators."""
80
  dl_dir = dl_manager.download_and_extract(_URLs[self.config.name])
81
  challenge_sets = [
82
- ("challenge_train_sample", "train_totto_RandomSample500.json"),
83
- ("challenge_validation_sample", "validation_totto_RandomSample500.json"),
84
- ("challenge_test_scramble", "test_totto_ScrambleInputStructure500.json"),
85
- ]
86
 
87
  return [
88
- datasets.SplitGenerator(
89
- name=datasets.Split.TRAIN,
90
- gen_kwargs={
91
- "filepath": os.path.join(dl_dir["data"], "totto_data/totto_train_data.jsonl"),
92
- "split": "train",
93
- },
94
- ),
95
- datasets.SplitGenerator(
96
- name=datasets.Split.VALIDATION,
97
- gen_kwargs={
98
- "filepath": os.path.join(dl_dir["data"], "totto_data/totto_dev_data.jsonl"),
99
- "split": "validation",
100
- },
101
- ),
102
- datasets.SplitGenerator(
103
- name=datasets.Split.TEST,
104
- gen_kwargs={
105
- "filepath": os.path.join(dl_dir["data"], "totto_data/unlabeled_totto_test_data.jsonl"),
106
- "split": "test",
107
- },
108
- ),
109
- ] + [
110
- datasets.SplitGenerator(
111
- name=challenge_split,
112
- gen_kwargs={
113
- "filepath": os.path.join(dl_dir["challenge_set"], self.config.name, filename),
114
- "split": challenge_split,
115
- },
116
- )
117
- for challenge_split, filename in challenge_sets
118
- ]
 
 
 
 
 
 
 
 
119
 
120
  def _generate_examples(self, filepath, split, filepaths=None, lang=None):
121
  """Yields examples."""
@@ -129,6 +264,7 @@ class Mlsum(datasets.GeneratorBasedBuilder):
129
  continue
130
  exple["gem_parent_id"] = exple["gem_id"]
131
  exple["gem_id"] = f"{self.config.name}-{split}-{id_}"
 
132
  yield id_, exple
133
  else:
134
  with open(filepath, "r", encoding="utf-8") as json_file:
@@ -137,6 +273,7 @@ class Mlsum(datasets.GeneratorBasedBuilder):
137
  i = -1
138
  for json_str in json_list:
139
  result = json.loads(json_str)
 
140
  if split == "train":
141
  i += 1
142
  for sentence in result["sentence_annotations"]:
@@ -154,8 +291,9 @@ class Mlsum(datasets.GeneratorBasedBuilder):
154
  "example_id": str(result["example_id"]),
155
  "overlap_subset": "none",
156
  "sentence_annotations": [sentence],
157
- "references": [],
158
  "target": sentence["final_sentence"],
 
159
  }
160
  yield id_, response
161
  else:
@@ -172,11 +310,18 @@ class Mlsum(datasets.GeneratorBasedBuilder):
172
  "highlighted_cells": result["highlighted_cells"],
173
  "example_id": str(result["example_id"]),
174
  "overlap_subset": str(result["overlap_subset"]),
 
175
  }
176
- response["sentence_annotations"] = [] if split == "test" else result["sentence_annotations"]
 
 
177
  response["references"] = [
178
- sentence["final_sentence"] for sentence in response["sentence_annotations"]
 
179
  ]
180
- response["target"] = response["references"][0] if len(response["references"]) > 0 else ""
 
 
 
 
181
  yield id_, response
182
-
1
+ import copy
2
  import json
3
  import os
4
 
13
  """
14
 
15
  _DESCRIPTION = """\
16
+ 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.
17
  """
18
 
19
  _URLs = {
24
  }
25
 
26
 
27
+ def _add_adjusted_col_offsets(table):
28
+ """Add adjusted column offsets to take into account multi-column cells."""
29
+ adjusted_table = []
30
+ for row in table:
31
+ real_col_index = 0
32
+ adjusted_row = []
33
+ for cell in row:
34
+ adjusted_cell = copy.deepcopy(cell)
35
+ adjusted_cell["adjusted_col_start"] = real_col_index
36
+ adjusted_cell["adjusted_col_end"] = (
37
+ adjusted_cell["adjusted_col_start"] + adjusted_cell["column_span"]
38
+ )
39
+ real_col_index += adjusted_cell["column_span"]
40
+ adjusted_row.append(adjusted_cell)
41
+ adjusted_table.append(adjusted_row)
42
+ return adjusted_table
43
+
44
+
45
+ def _get_heuristic_row_headers(adjusted_table, row_index, col_index):
46
+ """Heuristic to find row headers."""
47
+ row_headers = []
48
+ row = adjusted_table[row_index]
49
+ for i in range(0, col_index):
50
+ if row[i]["is_header"]:
51
+ row_headers.append(row[i])
52
+ return row_headers
53
+
54
+
55
+ def _get_heuristic_col_headers(adjusted_table, row_index, col_index):
56
+ """Heuristic to find column headers."""
57
+ adjusted_cell = adjusted_table[row_index][col_index]
58
+ adjusted_col_start = adjusted_cell["adjusted_col_start"]
59
+ adjusted_col_end = adjusted_cell["adjusted_col_end"]
60
+ col_headers = []
61
+ for r in range(0, row_index):
62
+ row = adjusted_table[r]
63
+ for cell in row:
64
+ if (
65
+ cell["adjusted_col_start"] < adjusted_col_end
66
+ and cell["adjusted_col_end"] > adjusted_col_start
67
+ ):
68
+ if cell["is_header"]:
69
+ col_headers.append(cell)
70
+
71
+ return col_headers
72
+
73
+
74
+ def get_highlighted_subtable(table, cell_indices, with_heuristic_headers=False):
75
+ """Extract out the highlighted part of a table."""
76
+ highlighted_table = []
77
+
78
+ adjusted_table = _add_adjusted_col_offsets(table)
79
+
80
+ for (row_index, col_index) in cell_indices:
81
+ cell = table[row_index][col_index]
82
+ if with_heuristic_headers:
83
+ row_headers = _get_heuristic_row_headers(
84
+ adjusted_table, row_index, col_index
85
+ )
86
+ col_headers = _get_heuristic_col_headers(
87
+ adjusted_table, row_index, col_index
88
+ )
89
+ else:
90
+ row_headers = []
91
+ col_headers = []
92
+
93
+ highlighted_cell = {
94
+ "cell": cell,
95
+ "row_headers": row_headers,
96
+ "col_headers": col_headers,
97
+ }
98
+ highlighted_table.append(highlighted_cell)
99
+
100
+ return highlighted_table
101
+
102
+
103
+ def linearize_subtable(subtable, table_page_title, table_section_title):
104
+ """Linearize the highlighted subtable and return a string of its contents."""
105
+ table_str = ""
106
+ if table_page_title:
107
+ table_str += "<page_title> " + table_page_title + " </page_title> "
108
+ if table_section_title:
109
+ table_str += "<section_title> " + table_section_title + " </section_title> "
110
+ table_str += "<table> "
111
+
112
+ for item in subtable:
113
+ cell = item["cell"]
114
+ row_headers = item["row_headers"]
115
+ col_headers = item["col_headers"]
116
+
117
+ # The value of the cell.
118
+ item_str = "<cell> " + cell["value"] + " "
119
+
120
+ # All the column headers associated with this cell.
121
+ for col_header in col_headers:
122
+ item_str += "<col_header> " + col_header["value"] + " </col_header> "
123
+
124
+ # All the row headers associated with this cell.
125
+ for row_header in row_headers:
126
+ item_str += "<row_header> " + row_header["value"] + " </row_header> "
127
+
128
+ item_str += "</cell> "
129
+ table_str += item_str
130
+
131
+ table_str += "</table>"
132
+ return table_str
133
+
134
+
135
+ def linearize(example):
136
+ table = example["table"]
137
+ table_page_title = example["table_page_title"]
138
+ table_section_title = example["table_section_title"]
139
+ cell_indices = example["highlighted_cells"]
140
+ subtable = get_highlighted_subtable(
141
+ table=table, cell_indices=cell_indices, with_heuristic_headers=True
142
+ )
143
+
144
+ subtable_metadata_str = linearize_subtable(
145
+ subtable=subtable,
146
+ table_page_title=table_page_title,
147
+ table_section_title=table_section_title,
148
+ )
149
+ return subtable_metadata_str
150
+
151
+
152
+ class Totto(datasets.GeneratorBasedBuilder):
153
 
154
  BUILDER_CONFIGS = [
155
  datasets.BuilderConfig(
162
  def _info(self):
163
  return datasets.DatasetInfo(
164
  description=_DESCRIPTION,
165
+ features=datasets.Features(
166
  {
167
  "gem_id": datasets.Value("string"),
168
  "gem_parent_id": datasets.Value("string"),
194
  "overlap_subset": datasets.Value("string"),
195
  "target": datasets.Value("string"), # single target for train
196
  "references": [datasets.Value("string")],
197
+ "linearized_input": datasets.Value("string"),
198
  },
199
  ),
200
  supervised_keys=None,
206
  """Returns SplitGenerators."""
207
  dl_dir = dl_manager.download_and_extract(_URLs[self.config.name])
208
  challenge_sets = [
209
+ ("challenge_train_sample", "train_totto_RandomSample500.json"),
210
+ ("challenge_validation_sample", "validation_totto_RandomSample500.json"),
211
+ # ("challenge_test_scramble", "test_totto_ScrambleInputStructure500.json"),
212
+ ]
213
 
214
  return [
215
+ datasets.SplitGenerator(
216
+ name=datasets.Split.TRAIN,
217
+ gen_kwargs={
218
+ "filepath": os.path.join(
219
+ dl_dir["data"], "totto_data/totto_train_data.jsonl"
220
+ ),
221
+ "split": "train",
222
+ },
223
+ ),
224
+ datasets.SplitGenerator(
225
+ name=datasets.Split.VALIDATION,
226
+ gen_kwargs={
227
+ "filepath": os.path.join(
228
+ dl_dir["data"], "totto_data/totto_dev_data.jsonl"
229
+ ),
230
+ "split": "validation",
231
+ },
232
+ ),
233
+ datasets.SplitGenerator(
234
+ name=datasets.Split.TEST,
235
+ gen_kwargs={
236
+ "filepath": os.path.join(
237
+ dl_dir["data"], "totto_data/unlabeled_totto_test_data.jsonl"
238
+ ),
239
+ "split": "test",
240
+ },
241
+ ),
242
+ ] + [
243
+ datasets.SplitGenerator(
244
+ name=challenge_split,
245
+ gen_kwargs={
246
+ "filepath": os.path.join(
247
+ dl_dir["challenge_set"], self.config.name, filename
248
+ ),
249
+ "split": challenge_split,
250
+ },
251
+ )
252
+ for challenge_split, filename in challenge_sets
253
+ ]
254
 
255
  def _generate_examples(self, filepath, split, filepaths=None, lang=None):
256
  """Yields examples."""
264
  continue
265
  exple["gem_parent_id"] = exple["gem_id"]
266
  exple["gem_id"] = f"{self.config.name}-{split}-{id_}"
267
+ exple["linearized_input"] = linearize(exple)
268
  yield id_, exple
269
  else:
270
  with open(filepath, "r", encoding="utf-8") as json_file:
273
  i = -1
274
  for json_str in json_list:
275
  result = json.loads(json_str)
276
+ linearized_input = linearize(result)
277
  if split == "train":
278
  i += 1
279
  for sentence in result["sentence_annotations"]:
291
  "example_id": str(result["example_id"]),
292
  "overlap_subset": "none",
293
  "sentence_annotations": [sentence],
294
+ "references": [sentence["final_sentence"]],
295
  "target": sentence["final_sentence"],
296
+ "linearized_input": linearized_input,
297
  }
298
  yield id_, response
299
  else:
310
  "highlighted_cells": result["highlighted_cells"],
311
  "example_id": str(result["example_id"]),
312
  "overlap_subset": str(result["overlap_subset"]),
313
+ "linearized_input": linearized_input,
314
  }
315
+ response["sentence_annotations"] = (
316
+ [] if split == "test" else result["sentence_annotations"]
317
+ )
318
  response["references"] = [
319
+ sentence["final_sentence"]
320
+ for sentence in response["sentence_annotations"]
321
  ]
322
+ response["target"] = (
323
+ response["references"][0]
324
+ if len(response["references"]) > 0
325
+ else ""
326
+ )
327
  yield id_, response