import csv import json import logging as logger import os import datasets _CITATION = """\ @misc{welleck2019dialogue, title={Dialogue Natural Language Inference}, author={Sean Welleck and Jason Weston and Arthur Szlam and Kyunghyun Cho}, year={2019}, eprint={1811.00671}, archivePrefix={arXiv}, primaryClass={cs.CL} } """ _DESCRIPTION = """\ Dialogue NLI is a dataset that addresses the issue of consistency in dialogue models. """ _URL = "dnli.zip" _HOMEPAGE = "https://wellecks.github.io/dialogue_nli/" _LICENSE = "MIT" class Dialogue_NLI(datasets.GeneratorBasedBuilder): """DNLI""" VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ datasets.BuilderConfig(name="dialogue_nli", version=VERSION, description="dnli"), ] DEFAULT_CONFIG_NAME = "dialogue_nli" # It's not mandatory to have a default configuration. Just use one if it make sense. def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "id": datasets.Value("string"), "label": datasets.Value("string"), "premise": datasets.Value("string"), "hypothesis": datasets.Value("string"), # "triple1": datasets.Value("string"), # "triple2": datasets.Value("string"), "dtype": datasets.Value("string"), } ), # No default supervised_keys (as we have to pass both question # and context as input). supervised_keys=None, homepage=_HOMEPAGE, citation=_CITATION, ) def _split_generators(self, dl_manager): # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files. # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive data_dir = dl_manager.download_and_extract(_URL) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepath": os.path.join(data_dir, "dnli/dialogue_nli/dialogue_nli_train.jsonl"), "split": "train", }, ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepath": os.path.join(data_dir, "dnli/dialogue_nli/dialogue_nli_dev.jsonl"), "split": "dev", }, ), datasets.SplitGenerator( name=datasets.Split.TEST, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepath": os.path.join(data_dir, "dnli/dialogue_nli/dialogue_nli_test.jsonl"), "split": "test" }, ), ] def _generate_examples(self, filepath, split): """This function returns the examples in the raw (text) form.""" logger.info("generating examples from = %s", filepath) with open(filepath) as f: data = json.load(f) # print(data) for example in data: label = example.get("label", "").strip() id_ = example.get("id", "").strip() hypothesis = example.get("sentence1", "").strip() premise = example.get("sentence2", "").strip() dtype = example.get("dtype", "").strip() # Features currently used are "context", "question", and "answers". # Others are extracted here for the ease of future expansions. yield id_, { "premise": premise, "hypothesis": hypothesis, "id": id_, "label": label, "dtype": dtype, }