Commit
·
26896f5
1
Parent(s):
a1a9f23
v1
Browse files- kubhist2.py +131 -0
kubhist2.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Config file by Simon Hengchen, https://hengchen.net
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import datasets
|
| 5 |
+
|
| 6 |
+
logger = datasets.logging.get_logger(__name__)
|
| 7 |
+
|
| 8 |
+
_CITATION = """
|
| 9 |
+
@misc{botenanna,
|
| 10 |
+
title = {"Jag känner en bot, hon heter [MASK]. A BERT for older Swedish, and a more usable dataset for historical newspapers"},
|
| 11 |
+
author = {Simon Hengchen}
|
| 12 |
+
year={2023},
|
| 13 |
+
}
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
_DESCRIPTION = """
|
| 17 |
+
This is a version of the Kubhist 2 dataset created, curated and made available by Språkbanken Text (SBX) at the University of Gothenburg (Sweden) under the CC BY 4.0 license.
|
| 18 |
+
This is a a corpus of OCRed newspapers from Sweden spanning the 1640s to the 1900s.
|
| 19 |
+
The original data is available with many types of annotation in XML at https://spraakbanken.gu.se/en/resources/kubhist2.
|
| 20 |
+
A good description of the data is available in this blog entry by Dana Dannélls: https://spraakbanken.gu.se/blogg/index.php/2019/09/15/the-kubhist-corpus-of-swedish-newspapers/
|
| 21 |
+
|
| 22 |
+
In a nutshell, this hugginface dataset version offers:
|
| 23 |
+
- only the OCRed text
|
| 24 |
+
- available in decadal subsets
|
| 25 |
+
|
| 26 |
+
License is CC BY 4.0 with attribution.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
_URL = "https://github.com/ChangeIsKey/kubhist2"
|
| 30 |
+
|
| 31 |
+
_URLS = {'1640': './text/1640/1640.txt.gz',
|
| 32 |
+
'1650': './text/1650/1650.txt.gz',
|
| 33 |
+
'1660': './text/1660/1660.txt.gz',
|
| 34 |
+
'1670': './text/1670/1670.txt.gz',
|
| 35 |
+
'1680': './text/1680/1680.txt.gz',
|
| 36 |
+
'1690': './text/1690/1690.txt.gz',
|
| 37 |
+
'1700': './text/1700/1700.txt.gz',
|
| 38 |
+
'1710': './text/1710/1710.txt.gz',
|
| 39 |
+
'1720': './text/1720/1720.txt.gz',
|
| 40 |
+
'1730': './text/1730/1730.txt.gz',
|
| 41 |
+
'1740': './text/1740/1740.txt.gz',
|
| 42 |
+
'1750': './text/1750/1750.txt.gz',
|
| 43 |
+
'1760': './text/1760/1760.txt.gz',
|
| 44 |
+
'1770': './text/1770/1770.txt.gz',
|
| 45 |
+
'1780': './text/1780/1780.txt.gz',
|
| 46 |
+
'1790': './text/1790/1790.txt.gz',
|
| 47 |
+
'1800': './text/1800/1800.txt.gz',
|
| 48 |
+
'1810': './text/1810/1810.txt.gz',
|
| 49 |
+
'1820': './text/1820/1820.txt.gz',
|
| 50 |
+
'1830': './text/1830/1830.txt.gz',
|
| 51 |
+
'1840': './text/1840/1840.txt.gz',
|
| 52 |
+
'1850': './text/1850/1850.txt.gz',
|
| 53 |
+
'1860': './text/1860/1860.txt.gz',
|
| 54 |
+
'1870': './text/1870/1870.txt.gz',
|
| 55 |
+
'1880': './text/1880/1880.txt.gz',
|
| 56 |
+
'1890': './text/1890/1890.txt.gz',
|
| 57 |
+
'1900': './text/1900/1900.txt.gz'
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class kubhist2Config(datasets.BuilderConfig):
|
| 62 |
+
"""BuilderConfig for kubhist2."""
|
| 63 |
+
|
| 64 |
+
def __init__(self, period="all", **kwargs):
|
| 65 |
+
"""Constructs a kubhist2Dataset.
|
| 66 |
+
Args:
|
| 67 |
+
period: can be any key in _URLS, `all` takes all
|
| 68 |
+
**kwargs: keyword arguments forwarded to super.
|
| 69 |
+
"""
|
| 70 |
+
if str(period) not in _URLS.keys():
|
| 71 |
+
#logger.warning("No period specified or wrong period, getting everything instead.")
|
| 72 |
+
self.period = "all"
|
| 73 |
+
else:
|
| 74 |
+
self.period = str(period)
|
| 75 |
+
super(kubhist2Config, self).__init__(**kwargs)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class kubhist2(datasets.GeneratorBasedBuilder):
|
| 79 |
+
BUILDER_CONFIG_CLASS = kubhist2Config
|
| 80 |
+
|
| 81 |
+
BUILDER_CONFIGS = []
|
| 82 |
+
for key in _URLS:
|
| 83 |
+
BUILDER_CONFIGS.append(
|
| 84 |
+
kubhist2Config(
|
| 85 |
+
name=key,
|
| 86 |
+
version=datasets.Version("1.0.0", ""),
|
| 87 |
+
description=f"Kubhist2: {key}",
|
| 88 |
+
period=key,
|
| 89 |
+
)
|
| 90 |
+
)
|
| 91 |
+
BUILDER_CONFIGS.append(
|
| 92 |
+
kubhist2Config(
|
| 93 |
+
name="all",
|
| 94 |
+
version=datasets.Version("1.0.0", ""),
|
| 95 |
+
description=f"Kubhist2: all",
|
| 96 |
+
period="all",
|
| 97 |
+
)
|
| 98 |
+
)
|
| 99 |
+
DEFAULT_CONFIG_NAME = "all"
|
| 100 |
+
|
| 101 |
+
def _info(self):
|
| 102 |
+
f = datasets.Features(
|
| 103 |
+
{
|
| 104 |
+
"text": datasets.Value("string")
|
| 105 |
+
}
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
return datasets.DatasetInfo(
|
| 109 |
+
features=f,
|
| 110 |
+
supervised_keys=None,
|
| 111 |
+
homepage="https://github.com/ChangeIsKey",
|
| 112 |
+
citation=_CITATION,
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
def _split_generators(self, dl_manager):
|
| 116 |
+
if self.config.period != "all":
|
| 117 |
+
url = {"train" : _URLS[self.config.period]}
|
| 118 |
+
downloaded_files = dl_manager.download_and_extract(url)
|
| 119 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]})]
|
| 120 |
+
|
| 121 |
+
elif self.config.period == "all":
|
| 122 |
+
url = {"train" : './text/all/all.txt.gz'}
|
| 123 |
+
#print(url)
|
| 124 |
+
downloaded_files = dl_manager.download_and_extract(url)
|
| 125 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]})]
|
| 126 |
+
|
| 127 |
+
def _generate_examples(self, filepath):
|
| 128 |
+
"""Yields examples."""
|
| 129 |
+
with open(filepath) as f:
|
| 130 |
+
for i, line in enumerate(f):
|
| 131 |
+
yield i, {"text" : line.rstrip()}
|