import datasets import json _CITATION = """ """ _DESCRIPTION = """ """ _HOMEPAGE = "" _LICENSE = "" _URLS = { "test": "https://raw.githubusercontent.com/wicsaax/m3exam/main/afrikaans-questions-test.json", } _NEED_KEY = ["question_text","background_description","answer_text","options","language"] class afrikaans(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("0.0.1") BUILDER_CONFIGS = [ datasets.BuilderConfig( name="m3exam-afrikaans", version=VERSION, description="The m3exam-afrikaans dataset." ), ] def _info(self): features = datasets.Features( { "answer_text": datasets.Value("string"), "question_text": datasets.Value("string"), "background_description": datasets.features.Sequence(datasets.Value("string")), "options": datasets.features.Sequence(datasets.Value("string")), "language": datasets.Value("string"), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): urls = { "test": _URLS["test"], } data_dir = dl_manager.download_and_extract(urls) return [ datasets.SplitGenerator( name=datasets.Split.TEST, # These kwargs will be passed to _generate_examples gen_kwargs={"filepath": data_dir["test"], "split": "test"}, ) ] def norm(self,dict): new_dic = {} for k,v in dict.items(): if k not in _NEED_KEY: continue new_dic[k] = v return new_dic # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` def _generate_examples(self, filepath, split): with open(filepath, 'r', encoding="utf-8") as f: print(filepath) data = json.loads(f.read()) for idx, single_data in enumerate(data): yield f"{idx}", self.norm(single_data)