from pathlib import Path from typing import Any, Dict, Iterator, List, Tuple import datasets _DESCRIPTION = ( "書籍『大規模言語モデル入門』で使用する JSNLI のデータセットです。" "JSNLI Version 1.1 のデータセットのうち、フィルタリング後の訓練セット (train_w_filtering) と検証セット (dev) を使用しています。" ) _HOMEPAGE = "https://nlp.ist.i.kyoto-u.ac.jp/?日本語SNLI(JSNLI)データセット" _LICENSE = "CC BY-SA 4.0" _URL = "https://nlp.ist.i.kyoto-u.ac.jp/DLcounter/lime.cgi?down=https://nlp.ist.i.kyoto-u.ac.jp/nl-resource/JSNLI/jsnli_1.1.zip&name=JSNLI.zip" class JSNLI(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") def _info(self) -> datasets.DatasetInfo: features = datasets.Features({ "premise": datasets.Value("string"), "hypothesis": datasets.Value("string"), "label": datasets.Value("string"), }) return datasets.DatasetInfo( description=_DESCRIPTION, homepage=_HOMEPAGE, license=_LICENSE, features=features, ) def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: base_filepath = dl_manager.download_and_extract(_URL) train_filepath = Path(base_filepath) / "jsnli_1.1" / "train_w_filtering.tsv" dev_filepath = Path(base_filepath) / "jsnli_1.1" / "dev.tsv" split_generators = [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_filepath}), datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": dev_filepath}), ] return split_generators def _generate_examples(self, filepath: str) -> Iterator[Tuple[int, Dict[str, Any]]]: with open(filepath, encoding="utf-8") as f: for i, line in enumerate(f): label, premise, hypothesis = line.rstrip("\n").split("\t") example = { "premise": premise, "hypothesis": hypothesis, "label": label, } yield i, example