File size: 1,608 Bytes
d73a877 3ca46c1 d73a877 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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(
{
"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:
print(row)
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 |