# coding=utf-8 import collections import gzip import datasets logger = datasets.logging.get_logger(__name__) _BASE_DIR = "/home/diogo/extracted/brwac-clean/" _BASE_DATA_DIR = "/home/diogo/extracted/brwac-clean/data/" class BrwacCleanConfig(datasets.BuilderConfig): """BRWAC-clean corpus.""" def __init__(self, **kwargs): # Initialize the base class. name = "brwac-clean" description = "brwac-clean dataset" super(BrwacCleanConfig, self).__init__(name=name, description=description, **kwargs) # Additional attributes self.base_data_url = _BASE_DATA_DIR class BrwacClean(datasets.GeneratorBasedBuilder): """BRWAC corpus.""" BUILDER_CONFIGS = [ BrwacCleanConfig( version=datasets.Version("1.0.0"), ) ] BUILDER_CONFIG_CLASS = BrwacCleanConfig def _info(self): return datasets.DatasetInfo( features=datasets.Features({"id": datasets.Value("int64"), "text": datasets.Value("string")}), supervised_keys=None, ) def _split_generators(self, dl_manager): files = _BASE_DIR + "file_names.txt" with open(files, encoding="utf-8") as f: data_filenames = [line.split("\t")[0] for line in f if line] data_urls = [self.config.base_data_url + data_filename.strip() for data_filename in data_filenames] downloaded_files = dl_manager.download(data_urls) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": downloaded_files}), ] def _generate_examples(self, filepaths): """This function returns the examples in the raw (text) form by iterating on all the files.""" id_ = 0 for filepath in filepaths: with gzip.open(open(filepath, "rb"), "rt", encoding="utf-8") as f: for line in f: feature = id_, {"id": id_, "text": line.replace("", "\n").rstrip()} yield feature id_ += 1