import json import datasets from datasets import Dataset, DatasetDict, Features, ClassLabel, Value _URLS = { "seed_0_fold_0": { "train": "data/essay_0_0_train.jsonl.gz", "validation": "data/essay_0_0_vali.jsonl.gz", "test": "data/essay_0_0_test.jsonl.gz", }, "seed_0_fold_1": { "train": "data/essay_0_1_train.jsonl.gz", "validation": "data/essay_0_1_vali.jsonl.gz", "test": "data/essay_0_1_test.jsonl.gz", }, } class Essay(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ datasets.BuilderConfig(name="seed_0_fold_0"), datasets.BuilderConfig(name="seed_0_fold_1"), ] DEFAULT_CONFIG_NAME = "seed_0_fold_0" def _info(self): return datasets.DatasetInfo( features=datasets.Features({ 'essay_id': Value('string'), 'full_text': Value('string'), 'score': ClassLabel(names = [1, 2, 3, 4, 5, 6], id = [0, 1, 2, 3, 4, 5] ) })) def _split_generators(self, dl_manager): """Returns seed_0_fold_0Generators.""" paths = dl_manager.download_and_extract(_URLS[self.config.name]) if self.config.name == "seed_0_fold_0": return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": paths["train"]}), datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": paths["validation"]}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": paths["test"]}), ] else: return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": paths["train"]}), datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": paths["validation"]}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": paths["test"]}), ] def _generate_examples(self, filepath): """Generate examples.""" with open(filepath, encoding="utf-8") as f: for idx, line in enumerate(f): example = json.loads(line) yield idx, example