import os import json import datasets import typing as tp _LABELS = [ 'acq', 'crude', 'earn', 'grain', 'interest', 'money-fx', 'ship', 'trade', ] class R8(datasets.GeneratorBasedBuilder): def _info(self): return datasets.DatasetInfo( description=None, features=datasets.Features({ "text": datasets.Value("string"), "label": datasets.features.ClassLabel( names=_LABELS ) }), homepage=None, citation=None ) def _split_generators(self, dl_manager: datasets.DownloadManager) -> tp.List[datasets.SplitGenerator]: path = os.getcwd() train_filepath = os.path.join(path, 'train.json') test_filepath = os.path.join(path, 'test.json') return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'filepath': train_filepath}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={'filepath': test_filepath}), ] def _generate_examples(self, filepath): with open(filepath, 'r', encoding='utf-8') as fp: lines = json.load(fp) for idx, line in enumerate(lines): text = str(line['text']) label = int(line['label']) yield idx, {'text': text, 'label': label}