import tarfile import os import datasets _URL = "https://huggingface.co/datasets/neuclir/neumarco/resolve/main/data/neumarco.tar.gz" class Neumarco(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") def _info(self): return datasets.DatasetInfo( features=datasets.Features({ "doc_id": datasets.Value("string"), "text": datasets.Value("string"), }), ) def _split_generators(self, dl_manager): path = dl_manager.download(_URL) return [ datasets.SplitGenerator( name=lang, gen_kwargs={ "filepath": path, "tarpath": f'eng-{lang}/msmarco.collection.20210731-scale21-sockeye2-tm1.tsv' }) for lang in ['fas', 'rus', 'zho'] ] def _generate_examples(self, filepath, tarpath): with tarfile.open(filepath, 'r|gz') as tarf: for fileinfo in tarf: if fileinfo.name != tarpath: continue with tarf.extractfile(fileinfo) as f: for key, line in enumerate(f): doc_id, text = line.decode('utf8').rstrip('\n').split('\t') yield key, {'doc_id': doc_id, 'text': text} break