File size: 2,030 Bytes
c36d17c
 
 
 
 
 
 
 
 
 
 
 
 
81ba15f
 
 
c8b2dae
9f80775
81ba15f
 
 
c36d17c
 
 
81ba15f
c36d17c
 
 
 
81ba15f
 
 
 
 
 
c36d17c
 
 
c8b2dae
c36d17c
9f80775
81ba15f
 
 
c8b2dae
c36d17c
 
 
 
 
 
 
 
81ba15f
c36d17c
 
 
81ba15f
 
66cc7f1
c36d17c
81ba15f
 
 
 
65d9772
81ba15f
 
 
c36d17c
 
81ba15f
c36d17c
81ba15f
 
 
 
 
 
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
import json
import gzip

import datasets


logger = datasets.logging.get_logger(__name__)


_DESCRIPTION = """\
shu is a chinese book dataset.
"""

_HOMEPAGE = "https://github.com/shjwudp/shu"

_DATA_URLS = {
    "books": "books.jsonl.gz",
    "clean text": "clean_text.jsonl.gz",
}


class ShuConfig(datasets.BuilderConfig):
    """BuilderConfig for shu."""

    def __init__(self, *args, subsets, **kwargs) -> None:
        """BuilderConfig for shu.
        Args:
            **kwargs: keyword arguments forwarded to super.
        """
        super(ShuConfig, self).__init__(
            *args,
            name="+".join(subsets),
            **kwargs
        )
        self.subsets = subsets


class Shu(datasets.GeneratorBasedBuilder):
    """A chinese book dataset."""

    VERSION = datasets.Version("0.2.0")

    BUILDER_CONFIG_CLASS = ShuConfig
    BUILDER_CONFIGS = [ShuConfig(subsets=[subset]) for subset in _DATA_URLS]
    DEFAULT_CONFIG_NAME = "books"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                "name": datasets.Value("string"),
                "text": datasets.Value("string"),
            }),
            homepage=_HOMEPAGE,
        )

    def _split_generators(self, dl_manager):
        data_urls = {subset: _DATA_URLS[subset] for subset in self.config.subsets}
        archive = dl_manager.download(data_urls)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "files": {
                        subset: archive[subset] for subset in self.config.subsets
                    },
                },
            ),
        ]

    def _generate_examples(self, files):
        key = 0
        for subset in files:
            filepath = files[subset]
            for line in gzip.open(filepath, "rt", encoding="utf-8"):
                j = json.loads(line)
                yield key, j
                key += 1