import csv import datasets _DOWNLOAD_URL = "https://huggingface.co/datasets/mrojas/task1a/resolve/main/data.csv" class Task1a(datasets.GeneratorBasedBuilder): """Task1a classification dataset.""" def _info(self): return datasets.DatasetInfo( features=datasets.Features( { "filename": datasets.Value("string"), "text": datasets.Value("string"), "label": datasets.ClassLabel(names = ["0", "1"]), } ) ) def _split_generators(self, dl_manager): path = dl_manager.download_and_extract(_DOWNLOAD_URL) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": path, "is_test": False}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": path, "is_test": True}), ] def _generate_examples(self, filepath, is_test, test_size = 0.3): """Generate examples.""" with open(filepath, encoding="utf-8") as csv_file: train_threshold = 122 csv_reader = csv.reader( csv_file ) # next(csv_reader, None) # skip the headers for id_, row in enumerate(csv_reader): if id_ > 0: text, label = row current_row = id_, {"text": text, "label": int(label)} if (id_ < train_threshold) & (not is_test): yield current_row if (id_ >= train_threshold) & (is_test): yield current_row