Commit
•
a6e91a9
1
Parent(s):
21f3804
Delete loading script
Browse files- blbooks.py +0 -234
blbooks.py
DELETED
@@ -1,234 +0,0 @@
|
|
1 |
-
# coding=utf-8
|
2 |
-
# Copyright 2022 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
3 |
-
#
|
4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
-
# you may not use this file except in compliance with the License.
|
6 |
-
# You may obtain a copy of the License at
|
7 |
-
#
|
8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
-
#
|
10 |
-
# Unless required by applicable law or agreed to in writing, software
|
11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
-
# See the License for the specific language governing permissions and
|
14 |
-
# limitations under the License.
|
15 |
-
import gzip
|
16 |
-
import json
|
17 |
-
from datetime import datetime
|
18 |
-
from functools import lru_cache
|
19 |
-
from typing import Dict, List
|
20 |
-
|
21 |
-
import datasets
|
22 |
-
from datasets.tasks import LanguageModeling
|
23 |
-
|
24 |
-
|
25 |
-
_CITATION = """\
|
26 |
-
@misc{BritishLibraryBooks2021,
|
27 |
-
author = {British Library Labs},
|
28 |
-
title = {Digitised Books. c. 1510 - c. 1900. JSONL (OCR derived text + metadata)},
|
29 |
-
year = {2021},
|
30 |
-
publisher = {British Library},
|
31 |
-
howpublished={https://doi.org/10.23636/r7w6-zy15}
|
32 |
-
"""
|
33 |
-
|
34 |
-
_DESCRIPTION = """\
|
35 |
-
A dataset comprising of text created by OCR from the 49,455 digitised books, equating to 65,227 volumes (25+ million pages), published between c. 1510 - c. 1900.
|
36 |
-
The books cover a wide range of subject areas including philosophy, history, poetry and literature.
|
37 |
-
"""
|
38 |
-
|
39 |
-
_BASE_URL = "https://bl.iro.bl.uk/downloads/"
|
40 |
-
|
41 |
-
|
42 |
-
_DATA_URLS = {
|
43 |
-
"1510_1699": _BASE_URL + "61f58234-b370-422f-8591-8f98e46c2757?locale=en",
|
44 |
-
"1700_1799": _BASE_URL + "78b4a8ec-395e-4383-831c-809faff85ad7?locale=en",
|
45 |
-
"1800_1809": _BASE_URL + "91ae15cb-e08f-4abf-8396-e4742d9d4e37?locale=en",
|
46 |
-
"1810_1819": _BASE_URL + "6d1a6e17-f28d-45b9-8f7a-a03cf3a96491?locale=en",
|
47 |
-
"1820_1829": _BASE_URL + "ec764dbd-1ed4-4fc2-8668-b4df5c8ec451?locale=en",
|
48 |
-
"1830_1839": _BASE_URL + "eab68022-0418-4df7-a401-78972514ed20?locale=en",
|
49 |
-
"1840_1849": _BASE_URL + "d16d88b0-aa3f-4dfe-b728-c58d168d7b4d?locale=en",
|
50 |
-
"1850_1859": _BASE_URL + "a6a44ea8-8d33-4880-8b17-f89c90e3d89a?locale=en",
|
51 |
-
"1860_1869": _BASE_URL + "2e17f00f-52e6-4259-962c-b88ad60dec23?locale=en",
|
52 |
-
"1870_1879": _BASE_URL + "899c3719-030c-4517-abd3-b28fdc85eed4?locale=en",
|
53 |
-
"1880_1889": _BASE_URL + "ec3b8545-775b-47bd-885d-ce895263709e?locale=en",
|
54 |
-
"1890_1899": _BASE_URL + "54ed2842-089a-439a-b751-2179b3ffba28?locale=en",
|
55 |
-
}
|
56 |
-
|
57 |
-
_ALL = list(_DATA_URLS.values())
|
58 |
-
_1800_1899 = [
|
59 |
-
_DATA_URLS.get(subset)
|
60 |
-
for subset in [
|
61 |
-
"1800_1809",
|
62 |
-
"1810_1819",
|
63 |
-
"1820_1829",
|
64 |
-
"1830_1839",
|
65 |
-
"1840_1849",
|
66 |
-
"1850_1859",
|
67 |
-
"1860_1869",
|
68 |
-
"1870_1879",
|
69 |
-
"1880_1889",
|
70 |
-
"1890_1899",
|
71 |
-
]
|
72 |
-
]
|
73 |
-
_1700_1799 = [_DATA_URLS.get(subset) for subset in ["1700_1799"]]
|
74 |
-
_1510_1699 = [_DATA_URLS.get(subset) for subset in ["1510_1699"]]
|
75 |
-
|
76 |
-
URL = "https://doi.org/10.23636/r7w6-zy15"
|
77 |
-
|
78 |
-
features = datasets.Features(
|
79 |
-
{
|
80 |
-
"record_id": datasets.Value("string"),
|
81 |
-
"date": datasets.Value("timestamp[s]"),
|
82 |
-
"raw_date": datasets.Value("string"),
|
83 |
-
"title": datasets.Value("string"),
|
84 |
-
"place": datasets.Value("string"),
|
85 |
-
"empty_pg": datasets.Value("bool"),
|
86 |
-
"text": datasets.Value("string"),
|
87 |
-
"pg": datasets.Value("int32"),
|
88 |
-
"mean_wc_ocr": datasets.Value("float32"),
|
89 |
-
"std_wc_ocr": datasets.Value("float64"),
|
90 |
-
"name": datasets.Value("string"),
|
91 |
-
"all_names": datasets.Value("string"),
|
92 |
-
"Publisher": datasets.Value("string"),
|
93 |
-
"Country of publication 1": datasets.Value("string"),
|
94 |
-
"all Countries of publication": datasets.Value("string"),
|
95 |
-
"Physical description": datasets.Value("string"),
|
96 |
-
"Language_1": datasets.Value("string"),
|
97 |
-
"Language_2": datasets.Value("string"),
|
98 |
-
"Language_3": datasets.Value("string"),
|
99 |
-
"Language_4": datasets.Value("string"),
|
100 |
-
"multi_language": datasets.Value("bool"),
|
101 |
-
}
|
102 |
-
)
|
103 |
-
|
104 |
-
|
105 |
-
class BritishLibraryBooksConfig(datasets.BuilderConfig):
|
106 |
-
"""BuilderConfig for BritishLibraryBooks."""
|
107 |
-
|
108 |
-
def __init__(self, data_urls, citation, url, skip_empty=False, **kwargs):
|
109 |
-
"""BuilderConfig for BritishLibraryBooks.
|
110 |
-
|
111 |
-
Args:
|
112 |
-
data_url: `string`, url to download the zip file from.
|
113 |
-
citation: `string`, citation for the data set.
|
114 |
-
url: `string`, url for information about the data set.
|
115 |
-
skip_empty: `bool`, whether to skip empty pages.
|
116 |
-
**kwargs: keyword arguments forwarded to super.
|
117 |
-
"""
|
118 |
-
|
119 |
-
super(BritishLibraryBooksConfig, self).__init__(version=datasets.Version("1.0.2"), **kwargs)
|
120 |
-
self.url: str = url
|
121 |
-
self.data_urls: List[str] = data_urls
|
122 |
-
self.citation: str = citation
|
123 |
-
self.skip_empty: bool = skip_empty
|
124 |
-
|
125 |
-
|
126 |
-
class BritishLibraryBooks(datasets.GeneratorBasedBuilder):
|
127 |
-
"""The BritishLibraryBooks dataset."""
|
128 |
-
|
129 |
-
BUILDER_CONFIGS = [
|
130 |
-
BritishLibraryBooksConfig(
|
131 |
-
name="1500_1899",
|
132 |
-
description="All periods of" + _DESCRIPTION,
|
133 |
-
data_urls=_ALL,
|
134 |
-
citation=_CITATION,
|
135 |
-
url=URL,
|
136 |
-
skip_empty=True,
|
137 |
-
),
|
138 |
-
BritishLibraryBooksConfig(
|
139 |
-
name="1800_1899",
|
140 |
-
description="A subset covering texts published during the 1800-1899 of" + _DESCRIPTION,
|
141 |
-
data_urls=_1800_1899,
|
142 |
-
citation=_CITATION,
|
143 |
-
url=URL,
|
144 |
-
skip_empty=True,
|
145 |
-
),
|
146 |
-
BritishLibraryBooksConfig(
|
147 |
-
name="1700_1799",
|
148 |
-
description="Subset covering 1700-1799 of" + _DESCRIPTION,
|
149 |
-
data_urls=_1700_1799,
|
150 |
-
citation=_CITATION,
|
151 |
-
url=URL,
|
152 |
-
skip_empty=True,
|
153 |
-
),
|
154 |
-
BritishLibraryBooksConfig(
|
155 |
-
name="1510_1699",
|
156 |
-
description="Subset covering 1510-1699 of " + _DESCRIPTION,
|
157 |
-
data_urls=_1510_1699,
|
158 |
-
citation=_CITATION,
|
159 |
-
url=URL,
|
160 |
-
skip_empty=True,
|
161 |
-
),
|
162 |
-
]
|
163 |
-
|
164 |
-
DEFAULT_CONFIG_NAME = "1500_1899"
|
165 |
-
|
166 |
-
def _info(self):
|
167 |
-
return datasets.DatasetInfo(
|
168 |
-
description=_DESCRIPTION,
|
169 |
-
features=features,
|
170 |
-
supervised_keys=None,
|
171 |
-
homepage="https://www.bl.uk/collection-guides/digitised-printed-books",
|
172 |
-
citation=_CITATION,
|
173 |
-
task_templates=[LanguageModeling(text_column="text")],
|
174 |
-
)
|
175 |
-
|
176 |
-
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
177 |
-
urls_to_download = self.config.data_urls
|
178 |
-
downloaded_archives = dl_manager.download(urls_to_download)
|
179 |
-
downloaded_archives = [dl_manager.iter_archive(archive) for archive in downloaded_archives]
|
180 |
-
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"data_dirs": downloaded_archives})]
|
181 |
-
|
182 |
-
@lru_cache(maxsize=512)
|
183 |
-
def _parse_date(self, date):
|
184 |
-
if date is not None:
|
185 |
-
date = datetime.strptime(str(date), "%Y")
|
186 |
-
return date
|
187 |
-
|
188 |
-
def _parse_data(self, data: Dict) -> Dict:
|
189 |
-
mean_wc_ocr = data["mean_wc_ocr"]
|
190 |
-
mean_wc_ocr = float(mean_wc_ocr) if mean_wc_ocr else None
|
191 |
-
std_wc_ocr = data["std_wc_ocr"]
|
192 |
-
std_wc_ocr = float(data["std_wc_ocr"]) if std_wc_ocr else None
|
193 |
-
date = data["date"]
|
194 |
-
if date is not None:
|
195 |
-
date = datetime.strptime(str(date), "%Y")
|
196 |
-
return {
|
197 |
-
"record_id": data["record_id"],
|
198 |
-
"date": date,
|
199 |
-
"raw_date": data["raw_date"],
|
200 |
-
"title": data["title"],
|
201 |
-
"place": data["place"],
|
202 |
-
"text": data["text"],
|
203 |
-
"pg": int(data["pg"]),
|
204 |
-
"mean_wc_ocr": data["mean_wc_ocr"],
|
205 |
-
"std_wc_ocr": std_wc_ocr,
|
206 |
-
"name": data["Name"],
|
207 |
-
"all_names": data["All names"],
|
208 |
-
"Publisher": data["Publisher"],
|
209 |
-
"Country of publication 1": data["Country of publication 1"],
|
210 |
-
"all Countries of publication": data["All Countries of publication"],
|
211 |
-
"Physical description": data["Physical description"],
|
212 |
-
"Language_1": data["Language_1"],
|
213 |
-
"Language_2": data["Language_2"],
|
214 |
-
"Language_3": data["Language_3"],
|
215 |
-
"Language_4": data["Language_4"],
|
216 |
-
"multi_language": data["multi_language"],
|
217 |
-
}
|
218 |
-
|
219 |
-
def _generate_examples(self, data_dirs):
|
220 |
-
skip_empty = self.config.skip_empty
|
221 |
-
id_ = 0
|
222 |
-
for data_dir in data_dirs:
|
223 |
-
for path, file in data_dir:
|
224 |
-
if not path.endswith(".gz"):
|
225 |
-
continue
|
226 |
-
with gzip.open(file) as json_l:
|
227 |
-
for row in json_l:
|
228 |
-
data = json.loads(row)
|
229 |
-
empty_pg = data["empty_pg"]
|
230 |
-
if skip_empty and empty_pg:
|
231 |
-
continue
|
232 |
-
parsed_data = self._parse_data(data)
|
233 |
-
yield id_, {**parsed_data, **{"empty_pg": empty_pg}}
|
234 |
-
id_ += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|