File size: 6,044 Bytes
4770bd8
9c6cfa2
4770bd8
 
 
 
 
 
 
 
 
 
 
 
 
 
9c6cfa2
 
4770bd8
 
9c6cfa2
4770bd8
 
 
 
 
 
 
 
 
 
5d91c32
 
 
 
 
 
 
4770bd8
 
 
 
 
 
 
 
 
 
 
 
9c6cfa2
4770bd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c6cfa2
4770bd8
 
9c6cfa2
 
 
 
 
 
 
 
 
 
 
 
4770bd8
9c6cfa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4770bd8
 
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
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
# Copyright 2023 Masatoshi Suzuki (@singletongue)
#
# 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.
"""Wikipedia-Utils: Preprocessed Wikipedia Texts for NLP"""

import io
from typing import Iterator, List, Tuple

import datasets
import pyarrow as pa


_DESCRIPTION = "Preprocessed Wikipedia texts generated with scripts in singletongue/wikipedia-utils repo."

_HOMEPAGE = "https://github.com/singletongue/wikipedia-utils"

_LICENSE = "The content of Wikipedia is licensed under the CC-BY-SA 3.0 and GFDL licenses."

_URL_BASE = "https://github.com/singletongue/wikipedia-utils/releases/download"
_URLS = {
    "corpus-jawiki-20240401": f"{_URL_BASE}/2024-04-01/corpus-jawiki-20240401.txt.gz",
    "corpus-jawiki-20240401-cirrus": f"{_URL_BASE}/2024-04-01/corpus-jawiki-20240401-cirrus.txt.gz",
    "corpus-jawiki-20240401-filtered-large": f"{_URL_BASE}/2024-04-01/corpus-jawiki-20240401-filtered-large.txt.gz",
    "paragraphs-jawiki-20240401": f"{_URL_BASE}/2024-04-01/paragraphs-jawiki-20240401.json.gz",
    "passages-c300-jawiki-20240401": f"{_URL_BASE}/2024-04-01/passages-c300-jawiki-20240401.json.gz",
    "passages-c400-jawiki-20240401": f"{_URL_BASE}/2024-04-01/passages-c400-jawiki-20240401.json.gz",
    "passages-para-jawiki-20240401": f"{_URL_BASE}/2024-04-01/passages-para-jawiki-20240401.json.gz",
    "corpus-jawiki-20230403": f"{_URL_BASE}/2023-04-03/corpus-jawiki-20230403.txt.gz",
    "corpus-jawiki-20230403-cirrus": f"{_URL_BASE}/2023-04-03/corpus-jawiki-20230403-cirrus.txt.gz",
    "corpus-jawiki-20230403-filtered-large": f"{_URL_BASE}/2023-04-03/corpus-jawiki-20230403-filtered-large.txt.gz",
    "paragraphs-jawiki-20230403": f"{_URL_BASE}/2023-04-03/paragraphs-jawiki-20230403.json.gz",
    "passages-c300-jawiki-20230403": f"{_URL_BASE}/2023-04-03/passages-c300-jawiki-20230403.json.gz",
    "passages-c400-jawiki-20230403": f"{_URL_BASE}/2023-04-03/passages-c400-jawiki-20230403.json.gz",
    "passages-para-jawiki-20230403": f"{_URL_BASE}/2023-04-03/passages-para-jawiki-20230403.json.gz",
}

_VERSION = datasets.Version("1.0.0")


class WikipediaUtils(datasets.ArrowBasedBuilder):
    """Wikipedia-Utils dataset."""

    BUILDER_CONFIGS = [datasets.BuilderConfig(name=name, version=_VERSION) for name in _URLS.keys()]

    def _info(self) -> datasets.DatasetInfo:
        if self.config.name.startswith("corpus"):
            features = datasets.Features({"text": datasets.Value("string")})
        elif self.config.name.startswith("paragraphs"):
            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "pageid": datasets.Value("int64"),
                    "revid": datasets.Value("int64"),
                    "paragraph_index": datasets.Value("int64"),
                    "title": datasets.Value("string"),
                    "section": datasets.Value("string"),
                    "text": datasets.Value("string"),
                    "html_tag": datasets.Value("string"),
                }
            )
        elif self.config.name.startswith("passages"):
            features = datasets.Features(
                {
                    "id": datasets.Value("int64"),
                    "pageid": datasets.Value("int64"),
                    "revid": datasets.Value("int64"),
                    "title": datasets.Value("string"),
                    "section": datasets.Value("string"),
                    "text": datasets.Value("string"),
                }
            )
        else:
            raise ValueError("Invalid dataset config name is specified.")

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        url = _URLS[self.config.name]
        filepath = dl_manager.download_and_extract(url)
        return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": filepath})]

    def _generate_tables(self, filepath: str, chunksize: int = 10 << 20) -> Iterator[Tuple[int, pa.Table]]:
        if self.config.name.startswith("corpus"):
            with open(filepath) as f:
                batch_idx = 0
                while True:
                    batch = f.read(chunksize)
                    if not batch:
                        break

                    batch += f.readline()
                    batch = [line.rstrip("\n") for line in io.StringIO(batch).readlines()]
                    pa_table = pa.Table.from_arrays([pa.array(batch)], names=["text"])

                    yield batch_idx, pa_table
                    batch_idx += 1
        elif self.config.name.startswith(("paragraphs", "passages")):
            with open(filepath, "rb") as f:
                batch_idx = 0
                block_size = max(chunksize // 32, 16 << 10)
                while True:
                    batch = f.read(chunksize)
                    if not batch:
                        break

                    batch += f.readline()
                    pa_table = pa.json.read_json(
                        io.BytesIO(batch), read_options=pa.json.ReadOptions(block_size=block_size)
                    )

                    yield batch_idx, pa_table
                    batch_idx += 1
        else:
            raise ValueError("Invalid dataset config name is specified.")