# -*- coding: utf-8 -*- """CLUTRR_Dataset Loading Script.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1q9DdeHA5JbgTHkH6kfZe_KWHQOwHZA97 """ # coding=utf-8 # Copyright 2019 The CLUTRR Datasets Authors and the HuggingFace Datasets Authors. # # CLUTRR is CC-BY-NC 4.0 (Attr Non-Commercial Inter.) licensed, as found in the LICENSE file. # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Lint as: python3 """The CLUTRR (Compositional Language Understanding and Text-based Relational Reasoning) benchmark.""" import csv import os import textwrap import numpy as np import datasets _CLUTRR_CITATION = """\ @article{sinha2019clutrr, Author = {Koustuv Sinha and Shagun Sodhani and Jin Dong and Joelle Pineau and William L. Hamilton}, Title = {CLUTRR: A Diagnostic Benchmark for Inductive Reasoning from Text}, Year = {2019}, journal = {Empirical Methods of Natural Language Processing (EMNLP)}, arxiv = {1908.06177} } """ _CLUTRR_DESCRIPTION = """\ CLUTRR (Compositional Language Understanding and Text-based Relational Reasoning), a diagnostic benchmark suite, is first introduced in (https://arxiv.org/abs/1908.06177) to test the systematic generalization and inductive reasoning capabilities of NLU systems. """ _URL = "https://github.com/kliang5/CLUTRR_huggingface_dataset/tree/main/" _TASK = ["gen_train23_test2to10", "gen_train234_test2to10", "rob_train_clean_23_test_all_23", "rob_train_disc_23_test_all_23", "rob_train_irr_23_test_all_23","rob_train_sup_23_test_all_23"] class CLUTRR_v1(datasets.GeneratorBasedBuilder): """BuilderConfig for CLUTRR.""" BUILDER_CONFIGS = [ datasets.BuilderConfig( name=task, version=datasets.Version("1.0.0"), description="", ) for task in _TASK ] def _info(self): return datasets.DatasetInfo( description=_CLUTRR_DESCRIPTION, features=datasets.Features( { "id": datasets.Value("string"), "story": datasets.Value("string"), "query": datasets.Value("string"), "target": datasets.Value("string"), "clean_story": datasets.Value("string"), "proof_state": datasets.Value("string"), "f_comb": datasets.Value("string"), "task_name": datasets.Value("string"), "story_edges": datasets.Value("string"), "edge_types": datasets.Value("string"), "query_edge": datasets.Value("string"), "genders": datasets.Value("string"), "task_split": datasets.Value("string"), } ), # No default supervised_keys (as we have to pass both premise # and hypothesis as input). supervised_keys=None, homepage="https://www.cs.mcgill.ca/~ksinha4/clutrr/", citation=_CLUTRR_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # dl_manager is a datasets.download.DownloadManager that can be used to # download and extract URLs task = str(self.config.name) urls_to_download = { "test": _URL + task + "/test.csv", "train": _URL + task + "/train.csv", "validation": _URL + task + "/validation.csv", } downloaded_files = dl_manager.download_and_extract(urls_to_download) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepath": os.path.join(downloaded_files["train"], task + "_train.jsonl"), "task": task, }, ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepath": os.path.join(downloaded_files["validation"], task + "_val.jsonl"), "task": task, }, ), datasets.SplitGenerator( name=datasets.Split.TEST, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepath": os.path.join(downloaded_files["test"], task + "_test.jsonl"), "task": task, }, ), ] def _generate_examples(self, filepath, task): """Yields examples.""" with open(filepath, encoding="utf-8") as f: i = 0 for line in f: data = json.loads(line) i += 1 yield i, { "id": data["id"], "story": data["story"], "query": data["query"], "target": data["target"], "clean_story": data["clean_story"], "proof_state": data["proof_state"], "f_comb": data["f_comb"], "task_name": data["task_name"], "story_edges": data["story_edges"], "edge_types": data["edge_types"], "query_edge": data["query_edge"], "genders": data["genders"], "task_split": data["task_split"], }