import json import datasets as ds _DESCRIPTION = """ Description of the dataset. """ _CITATION = """ Citation for the dataset. """ _LICENCE = """ License information for the dataset. """ _FEATURES = ds.Features({ "x": ds.Value(dtype="string"), "y": ds.Value(dtype="string") }) class HIMYMConfig(ds.BuilderConfig): def __init__(self, **kwargs): super(HIMYMConfig, self).__init__(**kwargs) self.version = ds.Version("1.0.3") self.features = _FEATURES self.citation = _CITATION class HIMYM(ds.GeneratorBasedBuilder): BUILDER_CONFIGS = [ HIMYMConfig(name="ted"), HIMYMConfig(name="barney"), HIMYMConfig(name="marshall"), HIMYMConfig(name="rily"), HIMYMConfig(name="robin"), ] def _info(self) -> ds.DatasetInfo: """Returns the dataset metadata.""" return ds.DatasetInfo( description=_DESCRIPTION, features=self.config.features, citation=self.config.citation, license=_LICENCE, supervised_keys=None ) def _split_generators(self, dl_manager: ds.DownloadManager): """Returns SplitGenerators""" base_url = "https://huggingface.co/datasets/moon23k/HIMYM_Script/resolve/main/data/" data_files = { "ted": { "train": base_url + "ted/train.json", "validation": base_url + "ted/valid.json", "test": base_url + "ted/test.json" }, "barney": { "train": base_url + "barney/train.json", "validation": base_url + "barney/valid.json", "test": base_url + "barney/test.json" }, "marshall": { "train": base_url + "marshall/train.json", "validation": base_url + "marshall/valid.json", "test": base_url + "marshall/test.json" }, "rily": { "train": base_url + "rily/train.json", "validation": base_url + "rily/valid.json", "test": base_url + "rily/test.json" }, "robin": { "train": base_url + "robin/train.json", "validation": base_url + "robin/valid.json", "test": base_url + "robin/test.json" }, } downloaded_files = dl_manager.download_and_extract(data_files[self.config.name]) return [ ds.SplitGenerator( name=ds.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}, ), ds.SplitGenerator( name=ds.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["validation"]}, ), ds.SplitGenerator( name=ds.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}, ), ] def _generate_examples(self, filepath): with open(filepath, encoding="utf-8") as f: data = json.load(f) for idx, example in enumerate(data): yield idx, example