File size: 5,508 Bytes
2a69d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa884c7
c5e68a0
2a69d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa884c7
 
 
 
 
 
c5e68a0
 
 
 
 
 
aa884c7
2a69d5c
 
aa884c7
2a69d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import json
import gzip

import datasets
from datasets import Value, Sequence


logger = datasets.logging.get_logger(__name__)


_DESCRIPTION = """\
V2EX is a dataset curated by https://www.v2ex.com/ open data.
"""

_HOMEPAGE = "https://huggingface.co/datasets/Dialogue-Model-Research-Group/v2ex"

_DATA_URLS = {
    "topic": "v2ex_topic.jsonl.gz",
    "replies": "v2ex_replies.jsonl.gz",
    "conversation": "v2ex_conversation.jsonl.gz",
    "context-response": "v2ex_context_response.jsonl.gz",
}


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

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


class V2EX(datasets.GeneratorBasedBuilder):
    """V2EX: Chinese book dataset."""

    VERSION = datasets.Version("0.1.0")

    BUILDER_CONFIG_CLASS = V2EXConfig
    BUILDER_CONFIGS = [V2EXConfig(subsets=[subset]) for subset in _DATA_URLS]
    DEFAULT_CONFIG_NAME = "topic"

    def _info(self):
        if self.config.name == "topic":
            features = datasets.Features({
                "id": Value("int64"),
                "title": Value("string"),
                "content": Value("string"),
                "content_rendered": Value("string"),
                "syntax": Value("int64"),
                "url": Value("string"),
                "replies": Value("int64"),
                "last_reply_by": Value("string"),
                "created": Value("int64"),
                "last_modified": Value("int64"),
                "last_touched": Value("int64"),
                "member": {
                    "id": Value("int64"),
                    "username": Value("string"),
                    "bio": Value("string"),
                    "website": Value("string"),
                    "github": Value("string"),
                    "url": Value("string"),
                    "avatar": Value("string"),
                    "created": Value("int64"),
                },
                "node": {
                    "id": Value("int64"),
                    "url": Value("string"),
                    "name": Value("string"),
                    "title": Value("string"),
                    "header": Value("string"),
                    "footer": Value("string"),
                    "avatar": Value("string"),
                    "topics": Value("int64"),
                    "created": Value("int64"),
                    "last_modified": Value("int64"),
                },
                "supplements": Sequence({
                    "id": Value("int64"),
                    "content": Value("string"),
                    "content_rendered": Value("string"),
                    "syntax": Value("int64"),
                    "created": Value("int64"),
                }),
            })
        elif self.config.name == "replies":
            features = datasets.Features({
                "id": Value(dtype="int64", id=None),
                "content": Value(dtype="string", id=None),
                "content_rendered": Value(dtype="string", id=None),
                "created": Value(dtype="int64", id=None),
                "member": {
                    "id": Value(dtype="int64", id=None),
                    "username": Value(dtype="string", id=None),
                    "bio": Value(dtype="string", id=None),
                    "website": Value(dtype="string", id=None),
                    "github": Value(dtype="string", id=None),
                    "url": Value(dtype="string", id=None),
                    "avatar": Value(dtype="string", id=None),
                    "created": Value(dtype="int64", id=None)
                },
                "topic_id": Value(dtype="int64", id=None),
            })
        elif self.config.name == "conversation":
            features = datasets.Features({
                "conversation": Sequence({
                    "username": Value("string"),
                    "post": Value("string"),
                }),
                "url": Value(dtype="string"),
            })
        elif self.config.name == "context-response":
            features = datasets.Features({
                "context": Value("string"),
                "response": Value("string"),
            })
        else:
            raise NotImplementedError(
                "This dataset is not implemented."
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            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