File size: 2,045 Bytes
59104b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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("<END>", "\n").rstrip()}
                    yield feature
                    id_ += 1