Delete loading script
Browse files
cfq.py
DELETED
|
@@ -1,167 +0,0 @@
|
|
| 1 |
-
# coding=utf-8
|
| 2 |
-
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
-
# you may not use this file except in compliance with the License.
|
| 6 |
-
# You may obtain a copy of the License at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
-
# See the License for the specific language governing permissions and
|
| 14 |
-
# limitations under the License.
|
| 15 |
-
|
| 16 |
-
# Lint as: python3
|
| 17 |
-
"""CFQ (Compositional Freebase Questions) dataset."""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
import json
|
| 21 |
-
import re
|
| 22 |
-
|
| 23 |
-
import datasets
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
logger = datasets.logging.get_logger(__name__)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
_HOMEPAGE = "https://github.com/google-research/google-research/tree/master/cfq"
|
| 30 |
-
|
| 31 |
-
_LICENSE = "CC BY 4.0"
|
| 32 |
-
|
| 33 |
-
_CITATION = """
|
| 34 |
-
@inproceedings{Keysers2020,
|
| 35 |
-
title={Measuring Compositional Generalization: A Comprehensive Method on
|
| 36 |
-
Realistic Data},
|
| 37 |
-
author={Daniel Keysers and Nathanael Sch\"{a}rli and Nathan Scales and
|
| 38 |
-
Hylke Buisman and Daniel Furrer and Sergii Kashubin and
|
| 39 |
-
Nikola Momchev and Danila Sinopalnikov and Lukasz Stafiniak and
|
| 40 |
-
Tibor Tihon and Dmitry Tsarkov and Xiao Wang and Marc van Zee and
|
| 41 |
-
Olivier Bousquet},
|
| 42 |
-
booktitle={ICLR},
|
| 43 |
-
year={2020},
|
| 44 |
-
url={https://arxiv.org/abs/1912.09713.pdf},
|
| 45 |
-
}
|
| 46 |
-
"""
|
| 47 |
-
|
| 48 |
-
_DESCRIPTION = """
|
| 49 |
-
The CFQ dataset (and it's splits) for measuring compositional generalization.
|
| 50 |
-
|
| 51 |
-
See https://arxiv.org/abs/1912.09713.pdf for background.
|
| 52 |
-
|
| 53 |
-
Example usage:
|
| 54 |
-
data = datasets.load_dataset('cfq/mcd1')
|
| 55 |
-
"""
|
| 56 |
-
|
| 57 |
-
_DATA_URL = "https://storage.googleapis.com/cfq_dataset/cfq.tar.gz"
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
class CfqConfig(datasets.BuilderConfig):
|
| 61 |
-
"""BuilderConfig for CFQ splits."""
|
| 62 |
-
|
| 63 |
-
def __init__(self, name, directory="splits", **kwargs):
|
| 64 |
-
"""BuilderConfig for CFQ.
|
| 65 |
-
|
| 66 |
-
Args:
|
| 67 |
-
name: Unique name of the split.
|
| 68 |
-
directory: Which subdirectory to read the split from.
|
| 69 |
-
**kwargs: keyword arguments forwarded to super.
|
| 70 |
-
"""
|
| 71 |
-
# Version history:
|
| 72 |
-
super(CfqConfig, self).__init__(
|
| 73 |
-
name=name, version=datasets.Version("1.0.1"), description=_DESCRIPTION, **kwargs
|
| 74 |
-
)
|
| 75 |
-
self.splits_path = f"cfq/{directory}/{name}.json"
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
_QUESTION = "question"
|
| 79 |
-
_QUERY = "query"
|
| 80 |
-
_QUESTION_FIELD = "questionPatternModEntities"
|
| 81 |
-
_QUERY_FIELD = "sparqlPatternModEntities"
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
class Cfq(datasets.GeneratorBasedBuilder):
|
| 85 |
-
"""CFQ task / splits."""
|
| 86 |
-
|
| 87 |
-
BUILDER_CONFIGS = [
|
| 88 |
-
CfqConfig(name="mcd1"),
|
| 89 |
-
CfqConfig(name="mcd2"),
|
| 90 |
-
CfqConfig(name="mcd3"),
|
| 91 |
-
CfqConfig(name="question_complexity_split"),
|
| 92 |
-
CfqConfig(name="question_pattern_split"),
|
| 93 |
-
CfqConfig(name="query_complexity_split"),
|
| 94 |
-
CfqConfig(name="query_pattern_split"),
|
| 95 |
-
CfqConfig(name="random_split"),
|
| 96 |
-
]
|
| 97 |
-
|
| 98 |
-
def _info(self):
|
| 99 |
-
return datasets.DatasetInfo(
|
| 100 |
-
description=_DESCRIPTION,
|
| 101 |
-
features=datasets.Features(
|
| 102 |
-
{
|
| 103 |
-
_QUESTION: datasets.Value("string"),
|
| 104 |
-
_QUERY: datasets.Value("string"),
|
| 105 |
-
}
|
| 106 |
-
),
|
| 107 |
-
supervised_keys=(_QUESTION, _QUERY),
|
| 108 |
-
homepage=_HOMEPAGE,
|
| 109 |
-
license=_LICENSE,
|
| 110 |
-
citation=_CITATION,
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
def _split_generators(self, dl_manager):
|
| 114 |
-
"""Returns SplitGenerators."""
|
| 115 |
-
archive_path = dl_manager.download(_DATA_URL)
|
| 116 |
-
return [
|
| 117 |
-
datasets.SplitGenerator(
|
| 118 |
-
name=split,
|
| 119 |
-
gen_kwargs={
|
| 120 |
-
"data_files": dl_manager.iter_archive(archive_path),
|
| 121 |
-
"split_id": f"{split}Idxs",
|
| 122 |
-
},
|
| 123 |
-
)
|
| 124 |
-
for split in [datasets.Split.TRAIN, datasets.Split.TEST]
|
| 125 |
-
]
|
| 126 |
-
|
| 127 |
-
def _scrub_json(self, content):
|
| 128 |
-
"""Reduce JSON by filtering out only the fields of interest."""
|
| 129 |
-
# Loading of json data with the standard Python library is very inefficient:
|
| 130 |
-
# For the 4GB dataset file it requires more than 40GB of RAM and takes 3min.
|
| 131 |
-
# There are more efficient libraries but in order to avoid additional
|
| 132 |
-
# dependencies we use a simple (perhaps somewhat brittle) regexp to reduce
|
| 133 |
-
# the content to only what is needed.
|
| 134 |
-
question_regex = re.compile(r'("%s":\s*"[^"]*")' % _QUESTION_FIELD)
|
| 135 |
-
query_regex = re.compile(r'("%s":\s*"[^"]*")' % _QUERY_FIELD)
|
| 136 |
-
question_match = None
|
| 137 |
-
for line in content:
|
| 138 |
-
line = line.decode("utf-8")
|
| 139 |
-
if not question_match:
|
| 140 |
-
question_match = question_regex.match(line)
|
| 141 |
-
else:
|
| 142 |
-
query_match = query_regex.match(line)
|
| 143 |
-
if query_match:
|
| 144 |
-
yield json.loads("{" + question_match.group(1) + "," + query_match.group(1) + "}")
|
| 145 |
-
question_match = None
|
| 146 |
-
|
| 147 |
-
def _generate_examples(self, data_files, split_id):
|
| 148 |
-
"""Yields examples."""
|
| 149 |
-
samples_path = "cfq/dataset.json"
|
| 150 |
-
for path, file in data_files:
|
| 151 |
-
if path == self.config.splits_path:
|
| 152 |
-
splits = json.load(file)[split_id]
|
| 153 |
-
elif path == samples_path:
|
| 154 |
-
# The samples_path is the last path inside the archive
|
| 155 |
-
generator = enumerate(self._scrub_json(file))
|
| 156 |
-
samples = {}
|
| 157 |
-
splits_set = set(splits)
|
| 158 |
-
for split_idx in splits:
|
| 159 |
-
if split_idx in samples:
|
| 160 |
-
sample = samples.pop(split_idx)
|
| 161 |
-
else:
|
| 162 |
-
for sample_idx, sample in generator:
|
| 163 |
-
if sample_idx == split_idx:
|
| 164 |
-
break
|
| 165 |
-
elif sample_idx in splits_set:
|
| 166 |
-
samples[sample_idx] = sample
|
| 167 |
-
yield split_idx, {_QUESTION: sample[_QUESTION_FIELD], _QUERY: sample[_QUERY_FIELD]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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