File size: 3,590 Bytes
2ded227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""SimpleBooks dataset."""


import os

import datasets

_CITATION = """\
@misc{nguyen2019simplebooks,
    title={SimpleBooks: Long-term dependency book dataset with simplified English vocabulary for word-level language modeling}, 
    author={Huyen Nguyen},
    year={2019},
    eprint={1911.12391},
    archivePrefix={arXiv},
    primaryClass={cs.CL}
}
"""

_DESCRIPTION = """\
SimpleBooks is a small long-term dependency dataset that has the FREQ number equivalent to the 1 billion token dataset. Its small vocabulary size and small percentage of out-of-vocabulary words make it an ideal testbed and benchmark for word-level language modeling task and tutorials.
It was created from 1,573 Gutenberg books. They were selected out of 39,432 Gutenberg books using a hill-climbing algorithm to maximize FREQ.
"""

_LICENSE = "CC BY-SA"

URL = "https://dldata-public.s3.us-east-2.amazonaws.com/simplebooks.zip"


class SimpleBooks(datasets.GeneratorBasedBuilder):
    """SimpleBooks dataset."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="simplebooks-2", version=VERSION, description="2.2M tokens with the vocab size of 11,492"),
        datasets.BuilderConfig(name="simplebooks-2-raw", version=VERSION, description="2.2M tokens with the vocab size of 11,492 (raw)"),
        datasets.BuilderConfig(name="simplebooks-92", version=VERSION, description="92M tokens with the vocab size of 98,304"),
        datasets.BuilderConfig(name="simplebooks-92-raw", version=VERSION, description="92M tokens with the vocab size of 98,304 (raw)"),   
    ]

    DEFAULT_CONFIG_NAME = "simplebooks-2"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text": datasets.Value("string"),
                }
            ),
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        archive = dl_manager.download_and_extract(URL)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "path": os.path.join(archive, self.config.name, "train.txt"),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "path": os.path.join(archive, self.config.name, "valid.txt")
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "path": os.path.join(archive, self.config.name, "test.txt"),
                },
            ),
        ]
    
    def _generate_examples(self, path):
        with open(path, encoding="utf-8") as f:
            for _id, line in enumerate(f):
                yield _id, { "text": line.strip() }