stefan-it commited on
Commit
0de18a0
1 Parent(s): 4e02e43

dataset: add initial version of loader

Browse files
Files changed (1) hide show
  1. HisGermaNER.py +113 -0
HisGermaNER.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+
3
+ # Coming soon!
4
+ _CITATION = ""
5
+
6
+ _DESCRIPTION = """\
7
+ HisGermaNER is another NER dataset from historical German newspapers.
8
+
9
+ In the first release of our dataset, 11 newspapers from 1710 to 1840 from the Austrian National Library (ONB) are selected, resulting in 100 pages.
10
+ """
11
+
12
+
13
+ class HisGermaNERConfig(datasets.BuilderConfig):
14
+ """BuilderConfig for HisGermaNER"""
15
+
16
+ def __init__(self, data_url, **kwargs):
17
+ super(HisGermaNERConfig, self).__init__(**kwargs)
18
+ self.data_url = data_url
19
+
20
+
21
+ class HisGermaNER(datasets.GeneratorBasedBuilder):
22
+ BUILDER_CONFIGS = [
23
+ HisGermaNERConfig(
24
+ name="HisGermaNER",
25
+ version=datasets.Version("0.0.1"),
26
+ description="HisGermaNER Dataset",
27
+ data_url="https://huggingface.co/datasets/stefan-it/HisGermaNER/resolve/main/splits/HisGermaNER_v0_",
28
+ )
29
+ ]
30
+
31
+ def _info(self):
32
+ return datasets.DatasetInfo(
33
+ description=_DESCRIPTION,
34
+ features=datasets.Features(
35
+ {
36
+ "id": datasets.Value("string"),
37
+ "tokens": datasets.Sequence(datasets.Value("string")),
38
+ "ner_tags": datasets.Sequence(
39
+ datasets.features.ClassLabel(
40
+ names=[
41
+ "O",
42
+ "B-PER",
43
+ "I-PER",
44
+ "B-ORG",
45
+ "I-ORG",
46
+ "B-LOC",
47
+ "I-LOC",
48
+ ]
49
+ )
50
+ )
51
+ }
52
+ ),
53
+ supervised_keys=None,
54
+ homepage="https://huggingface.co/datasets/stefan-it/HisGermaNER",
55
+ citation=_CITATION,
56
+ )
57
+
58
+ def _split_generators(self, dl_manager):
59
+ """Returns generator for dataset splits."""
60
+ download_urls = {
61
+ split: self.config.data_url + split + ".tsv" for split in ["train", "dev", "test"]
62
+ }
63
+
64
+ downloaded_files = dl_manager.download_and_extract(download_urls)
65
+
66
+ splits = [
67
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
68
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
69
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]})
70
+ ]
71
+
72
+ return splits
73
+
74
+ def _generate_examples(self, filepath):
75
+ with open(filepath, "rt", encoding="utf-8") as f_p:
76
+ current_tokens = []
77
+ current_tags = []
78
+
79
+ sentence_counter = 0
80
+
81
+ for line in f_p:
82
+ line = line.strip()
83
+ if not line:
84
+ if len(current_tokens) > 0:
85
+ sentence = (
86
+ sentence_counter, {
87
+ "id": str(sentence_counter),
88
+ "tokens": current_tokens,
89
+ "ner_tags": current_tags,
90
+ }
91
+ )
92
+ sentence_counter += 1
93
+ current_tokens = []
94
+ current_tags = []
95
+ yield sentence
96
+ continue
97
+
98
+ if line.startswith("TOKEN"):
99
+ continue
100
+
101
+ if line.startswith("# "):
102
+ continue
103
+
104
+ token, tag, misc = line.split("\t")
105
+ current_tokens.append(token)
106
+ current_tags.append(tag)
107
+
108
+ if len(current_tokens) > 0:
109
+ yield sentence_counter, {
110
+ "id": str(sentence_counter),
111
+ "tokens": current_tokens,
112
+ "ner_tags": current_tags,
113
+ }