File size: 5,663 Bytes
cc12d95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73de627
16bcfdf
73de627
16bcfdf
 
 
b07027d
16bcfdf
 
73de627
cc12d95
 
 
 
 
 
 
386abf8
 
 
 
 
cc12d95
 
 
 
73de627
 
 
cc12d95
 
 
 
 
 
 
 
 
 
 
 
73de627
 
 
 
 
cc12d95
73de627
 
cc12d95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386abf8
cc12d95
73de627
 
386abf8
 
73de627
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc12d95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
# coding=utf-8
# 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.
"""TODO"""


import pyarrow as pa
import pyarrow.parquet as pq
from datasets import Value, Sequence
import datasets
from datasets.config import PYARROW_VERSION
from datasets.utils.logging import get_logger

logger = get_logger(__name__)

if PYARROW_VERSION.major <= 6:
    msg = f"pyarrow version >= 7.0.0 required for this loading script, you have {PYARROW_VERSION}"
    logger.warning(msg)
    raise RuntimeError(msg)


_DESCRIPTION = "TODO"

_HOMEPAGE = "TODO"

_LANG_CONFIGS = {"fi", "sv"}

_DATA = {
    "sv": {"1900": "sv_1900.parquet", "1910": "sv_1910.parquet"},
    "fi": {"1900": "fi_1900.parquet", "1910": "fi_1910.parquet"},
}


class EuropeanaNewspapersConfig(datasets.BuilderConfig):
    """BuilderConfig for the Europeana Newspapers dataset."""

    def __init__(
        self, *args, languages=None, min_decade=None, max_decade=None, **kwargs
    ):
        """BuilderConfig for the GitHub Code dataset.

        Args:
            languages (:obj:`List[str]`): List of languages to load.
            **kwargs: keyword arguments forwarded to super.
        """

        super().__init__(
            *args,
            name="+".join(languages),
            **kwargs,
        )
        for lang in languages:
            if not lang in _LANG_CONFIGS:
                raise ValueError(
                    f"{lang} not a valid language key for this dataset, valid keys are {_LANG_CONFIGS}"
                )
        self.languages = languages
        self.min_decade = min_decade
        self.max_decade = max_decade


class EuropeanaNewspapers(datasets.GeneratorBasedBuilder):
    """TODO."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIG_CLASS = EuropeanaNewspapersConfig
    BUILDER_CONFIGS = [
        EuropeanaNewspapersConfig(languages=[lang]) for lang in _LANG_CONFIGS
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text": Value(dtype="string"),
                    "mean_ocr": Value(dtype="float64"),
                    "std_ocr": Value(dtype="float64"),
                    "bounding_boxes": Sequence(
                        feature=Sequence(
                            feature=Value(dtype="float64", id=None),
                            length=-1,
                        ),
                    ),
                    "title": Value(dtype="string"),
                    "date": Value(dtype="string"),
                    "language": Sequence(
                        feature=Value(dtype="string", id=None),
                    ),
                    "item_iiif_url": Value(
                        dtype="string",
                    ),
                    # "multi_language": Value(dtype="bool"),
                    "issue_uri": Value(dtype="string"),
                    "id": Value(dtype="string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license="Multiple: see the 'license' field of each sample.",
        )

    def _split_generators(self, dl_manager):
        # parquet_files = list(Path(".").rglob("*.parquet"))
        languages = self.config.languages
        min_decade = self.config.min_decade
        max_decade = self.config.max_decade
        data_files = []
        for language in languages:
            for decade, file in _DATA[language].items():
                decade = int(decade)
                if max_decade is None and min_decade is None:
                    data_files.append(file)
                if (
                    max_decade is not None
                    and min_decade is not None
                    and min_decade <= decade <= max_decade
                ):
                    data_files.append(file)
                if (
                    min_decade is not None
                    and max_decade is None
                    and decade >= min_decade
                ):
                    data_files.append(file)
                if (
                    min_decade is None
                    and max_decade is not None
                    and decade <= max_decade
                ):
                    data_files.append(file)

        files = dl_manager.download(data_files)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "files": files,
                },
            ),
        ]

    def _generate_examples(self, files):
        key = 0
        for file in files:
            with open(file, "rb") as f:
                parquet_file = pq.ParquetFile(f)
                for record_batch in parquet_file.iter_batches(batch_size=10_000):
                    pa_table = pa.Table.from_batches([record_batch])
                    rows = pa_table.to_pylist()
                    for row in rows:
                        row.pop("multi_language")
                        yield key, row
                        key += 1