|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 The TensorFlow 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 | +"""CFQ (Compositional Freebase Question) dataset.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import json |
| 23 | +import os |
| 24 | +from absl import logging |
| 25 | +import tensorflow as tf |
| 26 | +import tensorflow_datasets.public_api as tfds |
| 27 | + |
| 28 | +_CITATION = """ |
| 29 | +@inproceedings{Lake2018GeneralizationWS, |
| 30 | + title={Measuring Compositional Generalization: A Comprehensive Method on |
| 31 | + Realistic Data}, |
| 32 | + author={Daniel Keysers, et al.}, |
| 33 | + booktitle={ICLR}, |
| 34 | + year={2020}, |
| 35 | + url={https://arxiv.org/abs/1912.09713.pdf}, |
| 36 | +} |
| 37 | +""" |
| 38 | + |
| 39 | +_DESCRIPTION = """ |
| 40 | +The CFQ dataset (and it's splits) for measuring compositional generalization. |
| 41 | +
|
| 42 | +See https://arxiv.org/abs/1912.09713.pdf for background. |
| 43 | +
|
| 44 | +Example usage: |
| 45 | +data = tfds.load('cfq/mcd1') |
| 46 | +""" |
| 47 | + |
| 48 | +_DATA_URL = 'https://storage.googleapis.com/cfq_dataset/cfq.tar.gz' |
| 49 | + |
| 50 | + |
| 51 | +class CFQConfig(tfds.core.BuilderConfig): |
| 52 | + """BuilderConfig for CFQ splits.""" |
| 53 | + |
| 54 | + @tfds.core.disallow_positional_args |
| 55 | + def __init__(self, name, directory='splits', **kwargs): |
| 56 | + """BuilderConfig for CFQ. |
| 57 | +
|
| 58 | + Args: |
| 59 | + name: Unique name of the split. |
| 60 | + directory: Which subdirectory to read the split from. |
| 61 | + **kwargs: keyword arguments forwarded to super. |
| 62 | + """ |
| 63 | + # Version history: |
| 64 | + super(CFQConfig, self).__init__( |
| 65 | + name=name, |
| 66 | + version=tfds.core.Version('1.0.0'), |
| 67 | + description=_DESCRIPTION, |
| 68 | + **kwargs) |
| 69 | + self.split_file = os.path.join(directory, name + '.json') |
| 70 | + |
| 71 | + |
| 72 | +_QUESTION = 'question' |
| 73 | +_QUERY = 'query' |
| 74 | + |
| 75 | + |
| 76 | +class CFQ(tfds.core.GeneratorBasedBuilder): |
| 77 | + """CFQ task / splits.""" |
| 78 | + |
| 79 | + BUILDER_CONFIGS = [ |
| 80 | + CFQConfig(name='mcd1'), |
| 81 | + CFQConfig(name='mcd2'), |
| 82 | + CFQConfig(name='mcd3'), |
| 83 | + CFQConfig(name='question_complexity_split'), |
| 84 | + CFQConfig(name='question_pattern_split'), |
| 85 | + CFQConfig(name='query_complexity_split'), |
| 86 | + CFQConfig(name='query_pattern_split'), |
| 87 | + CFQConfig(name='random_split'), |
| 88 | + ] |
| 89 | + |
| 90 | + def _info(self): |
| 91 | + return tfds.core.DatasetInfo( |
| 92 | + builder=self, |
| 93 | + description=_DESCRIPTION, |
| 94 | + features=tfds.features.FeaturesDict({ |
| 95 | + _QUESTION: tfds.features.Text(), |
| 96 | + _QUERY: tfds.features.Text(), |
| 97 | + }), |
| 98 | + supervised_keys=(_QUESTION, _QUERY), |
| 99 | + homepage='https://github.com/google-research/google-research/tree/master/cfq', |
| 100 | + citation=_CITATION, |
| 101 | + ) |
| 102 | + |
| 103 | + def _split_generators(self, dl_manager): |
| 104 | + """Returns SplitGenerators.""" |
| 105 | + data_dir = dl_manager.download_and_extract(_DATA_URL) |
| 106 | + data_dir = os.path.join(data_dir, 'cfq') |
| 107 | + return [ |
| 108 | + tfds.core.SplitGenerator( |
| 109 | + name=tfds.Split.TRAIN, |
| 110 | + gen_kwargs={ |
| 111 | + 'base_directory': data_dir, |
| 112 | + 'splits_file': self.builder_config.split_file, |
| 113 | + 'split_id': 'trainIdxs' |
| 114 | + }), |
| 115 | + tfds.core.SplitGenerator( |
| 116 | + name=tfds.Split.TEST, |
| 117 | + gen_kwargs={ |
| 118 | + 'base_directory': data_dir, |
| 119 | + 'splits_file': self.builder_config.split_file, |
| 120 | + 'split_id': 'testIdxs' |
| 121 | + }) |
| 122 | + ] |
| 123 | + |
| 124 | + def _generate_examples(self, base_directory, splits_file, split_id): |
| 125 | + """Yields examples.""" |
| 126 | + samples_path = os.path.join(base_directory, 'dataset.json') |
| 127 | + splits_path = os.path.join(base_directory, splits_file) |
| 128 | + with tf.io.gfile.GFile(samples_path) as samples_file: |
| 129 | + with tf.io.gfile.GFile(splits_path) as splits_file: |
| 130 | + logging.info('Reading json from %s into memory...', samples_path) |
| 131 | + samples = json.load(samples_file) |
| 132 | + logging.info('Loaded json data from %s.', samples_path) |
| 133 | + splits = json.load(splits_file) |
| 134 | + for idx in splits[split_id]: |
| 135 | + sample = samples[idx] |
| 136 | + yield idx, {_QUESTION: sample['questionPatternModEntities'], |
| 137 | + _QUERY: sample['sparqlPatternModEntities']} |
0 commit comments