File size: 1,553 Bytes
c36d17c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66cc7f1
 
c36d17c
66cc7f1
c36d17c
 
 
 
 
 
 
 
 
 
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
import json
import gzip

import datasets


logger = datasets.logging.get_logger(__name__)


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

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

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


class Shu(datasets.GeneratorBasedBuilder):
    """shu: Chinese book dataset."""

    BUILDER_CONFIGS = [
        ShuConfig(
            name="plain_text",
            version=datasets.Version("0.1.0", ""),
            description="Plain text",
        )
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                "name": datasets.Value("string"),
                "text": datasets.Value("string"),
            }),
            homepage="https://github.com/shjwudp/shu",
        )

    def _split_generators(self, dl_manager):
        train_text = dl_manager.download("books.jsonl.gz")

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_text}),
        ]

    def _generate_examples(self, filepath):
        logger.info(f"generating examples from = {filepath}")
        key = 0
        print("filepath", filepath)
        for line in gzip.open(filepath, "rt", encoding="utf-8"):
            j = json.loads(line)
            yield key, j
            key += 1