Upload _hitab.py
Browse files
_hitab.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
The script used to load the dataset from the original source.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import json
|
| 8 |
+
import datasets
|
| 9 |
+
import glob
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
_CITATION = """\
|
| 13 |
+
@article{cheng2021hitab,
|
| 14 |
+
title={HiTab: A Hierarchical Table Dataset for Question Answering and Natural Language Generation},
|
| 15 |
+
author={Cheng, Zhoujun and Dong, Haoyu and Wang, Zhiruo and Jia, Ran and Guo, Jiaqi and Gao, Yan and Han, Shi and Lou, Jian-Guang and Zhang, Dongmei},
|
| 16 |
+
journal={arXiv preprint arXiv:2108.06712},
|
| 17 |
+
year={2021}
|
| 18 |
+
}
|
| 19 |
+
"""
|
| 20 |
+
_DESCRIPTION = """\
|
| 21 |
+
HiTab is a dataset for question answering and data-to-text over hierarchical tables. It contains 10,672 samples and 3,597 tables from statistical reports (StatCan, NSF) and Wikipedia (ToTTo). 98.1% of the tables in HiTab are with hierarchies.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
_URL = "https://github.com/microsoft/HiTab"
|
| 25 |
+
_LICENSE = "C-UDA 1.0"
|
| 26 |
+
|
| 27 |
+
class HiTab(datasets.GeneratorBasedBuilder):
|
| 28 |
+
VERSION = datasets.Version("2022.2.7")
|
| 29 |
+
|
| 30 |
+
def _info(self):
|
| 31 |
+
return datasets.DatasetInfo(
|
| 32 |
+
description=_DESCRIPTION,
|
| 33 |
+
features=datasets.Features({
|
| 34 |
+
'id' : datasets.Value(dtype='string'),
|
| 35 |
+
'table_id' : datasets.Value(dtype='string'),
|
| 36 |
+
'table_source' : datasets.Value(dtype='string'),
|
| 37 |
+
'sentence_id' : datasets.Value(dtype='string'),
|
| 38 |
+
'sub_sentence_id' : datasets.Value(dtype='string'),
|
| 39 |
+
'sub_sentence' : datasets.Value(dtype='string'),
|
| 40 |
+
'question' : datasets.Value(dtype='string'),
|
| 41 |
+
'answer' : datasets.Value(dtype='large_string'),
|
| 42 |
+
'aggregation' : datasets.Value(dtype='large_string'),
|
| 43 |
+
'linked_cells' : datasets.Value(dtype='large_string'),
|
| 44 |
+
'answer_formulas' : datasets.Value(dtype='large_string'),
|
| 45 |
+
'reference_cells_map' : datasets.Value(dtype='large_string'),
|
| 46 |
+
'table_content' : datasets.Value(dtype='large_string'),
|
| 47 |
+
}),
|
| 48 |
+
supervised_keys=None,
|
| 49 |
+
homepage="https://www.microsoft.com/en-us/research/publication/hitab-a-hierarchical-table-dataset-for-question-answering-and-natural-language-generation/",
|
| 50 |
+
citation=_CITATION,
|
| 51 |
+
license=_LICENSE
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def _split_generators(self, dl_manager):
|
| 55 |
+
"""Returns SplitGenerators."""
|
| 56 |
+
return [
|
| 57 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": "data", "split" : "train"}),
|
| 58 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": "data", "split" : "dev"}),
|
| 59 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": "data", "split" : "test"}),
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
def _generate_examples(self, filepath, split):
|
| 63 |
+
table_content = {}
|
| 64 |
+
data = []
|
| 65 |
+
|
| 66 |
+
for filename in glob.glob(os.path.join(filepath, "tables", "raw", "*.json")):
|
| 67 |
+
with open(filename) as f:
|
| 68 |
+
j = json.load(f)
|
| 69 |
+
table_name = os.path.basename(filename).rstrip(".json")
|
| 70 |
+
table_content[table_name] = j
|
| 71 |
+
|
| 72 |
+
with open(os.path.join(filepath, f"{split}_samples.jsonl")) as f:
|
| 73 |
+
for i, line in enumerate(f.readlines()):
|
| 74 |
+
j = json.loads(line)
|
| 75 |
+
data.append(j)
|
| 76 |
+
|
| 77 |
+
for example_idx, entry in enumerate(data):
|
| 78 |
+
entry["table_content"] = table_content.get(entry["table_id"])
|
| 79 |
+
yield example_idx, {key: str(value) for key, value in entry.items()}
|
| 80 |
+
|
| 81 |
+
if __name__ == '__main__':
|
| 82 |
+
dataset = datasets.load_dataset(__file__)
|
| 83 |
+
dataset.push_to_hub("kasnerz/hitab")
|