ygorg commited on
Commit
f5a6977
·
1 Parent(s): 881cfcd

Keep original files for reproduction.

Browse files
Files changed (2) hide show
  1. _attic/GSC-v1.1.zip +3 -0
  2. _attic/MANTRAGSC.py +306 -0
_attic/GSC-v1.1.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c4fe4f4848d928cb75178df63208cbc56b156b613bdd7353477bb8e4bedb053
3
+ size 37191204
_attic/MANTRAGSC.py ADDED
@@ -0,0 +1,306 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
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
+
16
+ # pip install xmltodict
17
+
18
+ import random
19
+ from pathlib import Path
20
+ from itertools import product
21
+ from dataclasses import dataclass
22
+ from typing import Dict, List, Tuple
23
+
24
+ import xmltodict
25
+ import numpy as np
26
+
27
+ import datasets
28
+
29
+ _CITATION = """\
30
+ @article{10.1093/jamia/ocv037,
31
+ author = {Kors, Jan A and Clematide, Simon and Akhondi,
32
+ Saber A and van Mulligen, Erik M and Rebholz-Schuhmann, Dietrich},
33
+ title = "{A multilingual gold-standard corpus for biomedical concept recognition: the Mantra GSC}",
34
+ journal = {Journal of the American Medical Informatics Association},
35
+ volume = {22},
36
+ number = {5},
37
+ pages = {948-956},
38
+ year = {2015},
39
+ month = {05},
40
+ abstract = "{Objective To create a multilingual gold-standard corpus for biomedical concept recognition.Materials
41
+ and methods We selected text units from different parallel corpora (Medline abstract titles, drug labels,
42
+ biomedical patent claims) in English, French, German, Spanish, and Dutch. Three annotators per language
43
+ independently annotated the biomedical concepts, based on a subset of the Unified Medical Language System and
44
+ covering a wide range of semantic groups. To reduce the annotation workload, automatically generated
45
+ preannotations were provided. Individual annotations were automatically harmonized and then adjudicated, and
46
+ cross-language consistency checks were carried out to arrive at the final annotations.Results The number of final
47
+ annotations was 5530. Inter-annotator agreement scores indicate good agreement (median F-score 0.79), and are
48
+ similar to those between individual annotators and the gold standard. The automatically generated harmonized
49
+ annotation set for each language performed equally well as the best annotator for that language.Discussion The use
50
+ of automatic preannotations, harmonized annotations, and parallel corpora helped to keep the manual annotation
51
+ efforts manageable. The inter-annotator agreement scores provide a reference standard for gauging the performance
52
+ of automatic annotation techniques.Conclusion To our knowledge, this is the first gold-standard corpus for
53
+ biomedical concept recognition in languages other than English. Other distinguishing features are the wide variety
54
+ of semantic groups that are being covered, and the diversity of text genres that were annotated.}",
55
+ issn = {1067-5027},
56
+ doi = {10.1093/jamia/ocv037},
57
+ url = {https://doi.org/10.1093/jamia/ocv037},
58
+ eprint = {https://academic.oup.com/jamia/article-pdf/22/5/948/34146393/ocv037.pdf},
59
+ }
60
+ """
61
+
62
+ _DESCRIPTION = """\
63
+ We selected text units from different parallel corpora (Medline abstract titles, drug labels, biomedical patent claims)
64
+ in English, French, German, Spanish, and Dutch. Three annotators per language independently annotated the biomedical
65
+ concepts, based on a subset of the Unified Medical Language System and covering a wide range of semantic groups.
66
+ """
67
+
68
+ _HOMEPAGE = "https://biosemantics.erasmusmc.nl/index.php/resources/mantra-gsc"
69
+
70
+ _LICENSE = "CC_BY_4p0"
71
+
72
+ _URL = "https://huggingface.co/datasets/DrBenchmark/MANTRAGSC/resolve/main/GSC-v1.1.zip"
73
+
74
+ _LANGUAGES_2 = {
75
+ "es": "Spanish",
76
+ "fr": "French",
77
+ "de": "German",
78
+ "nl": "Dutch",
79
+ "en": "English",
80
+ }
81
+
82
+ _DATASET_TYPES = {
83
+ "emea": "EMEA",
84
+ "medline": "Medline",
85
+ "patents": "Patent",
86
+ }
87
+
88
+
89
+ @dataclass
90
+ class DrBenchmarkConfig(datasets.BuilderConfig):
91
+ name: str = None
92
+ version: datasets.Version = None
93
+ description: str = None
94
+ schema: str = None
95
+ subset_id: str = None
96
+
97
+
98
+ class MANTRAGSC(datasets.GeneratorBasedBuilder):
99
+
100
+ SOURCE_VERSION = datasets.Version("1.0.0")
101
+
102
+ BUILDER_CONFIGS = []
103
+
104
+ for language, dataset_type in product(_LANGUAGES_2, _DATASET_TYPES):
105
+
106
+ name = f"{language}_{dataset_type}"
107
+ if name in ['nl_patents', 'es_patents', 'en_medline']:
108
+ continue
109
+
110
+ BUILDER_CONFIGS.append(
111
+ DrBenchmarkConfig(
112
+ name=name,
113
+ version=SOURCE_VERSION,
114
+ description=f"Mantra GSC {_LANGUAGES_2[language]} {_DATASET_TYPES[dataset_type]} source schema",
115
+ schema="source",
116
+ subset_id=f"{language}_{_DATASET_TYPES[dataset_type]}",
117
+ )
118
+ )
119
+
120
+ DEFAULT_CONFIG_NAME = "fr_medline"
121
+
122
+ def _info(self):
123
+ # Label definition for each task
124
+ # Goals:
125
+ # - Tasks must not have extra labels (not present in their corpus)
126
+ # - Labels should have (roughly) the same index
127
+
128
+ # Labels common to every task (ordered by name and B-I)
129
+ common_names = ['O', 'B-ANAT', 'B-CHEM', 'I-CHEM', 'B-DEVI', 'B-DISO', 'I-DISO', 'B-LIVB', 'I-LIVB', 'B-OBJC', 'B-PHEN', 'B-PHYS', 'I-PHYS', 'B-PROC', 'I-PROC']
130
+ # Adding labels not common to every task (in an order that maximises labels having the same index accross tasks)
131
+ names = common_names + ["I-ANAT", "I-DEVI", "B-GEOG", "I-PHEN", "I-OBJC"]
132
+ unused_name_map = {
133
+ 'de_emea': {'B-GEOG', 'I-OBJC'},
134
+ 'en_emea': {'B-GEOG', 'I-OBJC'},
135
+ 'es_emea': {'B-GEOG', 'I-OBJC'},
136
+ 'fr_emea': {'B-GEOG', 'I-OBJC'},
137
+ 'nl_emea': {'B-GEOG', 'I-OBJC'},
138
+
139
+ 'de_medline': {'I-DEVI', 'I-PHEN'},
140
+ 'es_medline': {'I-DEVI', 'I-OBJC'},
141
+ 'fr_medline': {'I-OBJC', 'I-PHEN'},
142
+ 'nl_medline': {'I-DEVI'},
143
+
144
+ 'fr_patents': {'B-GEOG', 'I-OBJC', 'I-PHEN'},
145
+ 'de_patents': {'B-GEOG', 'I-OBJC', 'I-PHEN', 'I-ANAT', 'I-DEVI'},
146
+ 'en_patents': {'B-GEOG', 'I-OBJC', 'I-PHEN'}
147
+ }
148
+ names = [n for n in names if n not in unused_name_map.get(self.config.name, {})]
149
+
150
+ features = datasets.Features(
151
+ {
152
+ "id": datasets.Value("string"),
153
+ "tokens": [datasets.Value("string")],
154
+ "ner_tags": datasets.Sequence(
155
+ datasets.features.ClassLabel(
156
+ names=names,
157
+ )
158
+ ),
159
+ }
160
+ )
161
+
162
+ return datasets.DatasetInfo(
163
+ description=_DESCRIPTION,
164
+ features=features,
165
+ homepage=_HOMEPAGE,
166
+ license=str(_LICENSE),
167
+ citation=_CITATION,
168
+ )
169
+
170
+ def _split_generators(self, dl_manager):
171
+
172
+ language, dataset_type = self.config.name.split("_")
173
+
174
+ data_dir = dl_manager.download_and_extract(_URL)
175
+ data_dir = Path(data_dir) / "GSC-v1.1" / f"{_DATASET_TYPES[dataset_type]}_GSC_{language}_man.xml"
176
+
177
+ return [
178
+ datasets.SplitGenerator(
179
+ name=datasets.Split.TRAIN,
180
+ gen_kwargs={
181
+ "data_dir": data_dir,
182
+ "split": "train",
183
+ },
184
+ ),
185
+ datasets.SplitGenerator(
186
+ name=datasets.Split.VALIDATION,
187
+ gen_kwargs={
188
+ "data_dir": data_dir,
189
+ "split": "validation",
190
+ },
191
+ ),
192
+ datasets.SplitGenerator(
193
+ name=datasets.Split.TEST,
194
+ gen_kwargs={
195
+ "data_dir": data_dir,
196
+ "split": "test",
197
+ },
198
+ ),
199
+ ]
200
+
201
+ def _generate_examples(self, data_dir, split):
202
+
203
+ with open(data_dir) as fd:
204
+ doc = xmltodict.parse(fd.read())
205
+
206
+ all_res = []
207
+
208
+ for d in doc["Corpus"]["document"]:
209
+
210
+ if not isinstance(d["unit"], list):
211
+ d["unit"] = [d["unit"]]
212
+
213
+ for u in d["unit"]:
214
+
215
+ text = u["text"]
216
+
217
+ if "e" in u.keys():
218
+
219
+ if not isinstance(u["e"], list):
220
+ u["e"] = [u["e"]]
221
+
222
+ tags = [{
223
+ "label": current["@grp"].upper(),
224
+ "offset_start": int(current["@offset"]),
225
+ "offset_end": int(current["@offset"]) + int(current["@len"]),
226
+ } for current in u["e"]]
227
+
228
+ else:
229
+ tags = []
230
+
231
+ _tokens = text.split(" ")
232
+ tokens = []
233
+ for i, t in enumerate(_tokens):
234
+
235
+ concat = " ".join(_tokens[0:i + 1])
236
+
237
+ offset_start = len(concat) - len(t)
238
+ offset_end = len(concat)
239
+
240
+ tokens.append({
241
+ "token": t,
242
+ "offset_start": offset_start,
243
+ "offset_end": offset_end,
244
+ })
245
+
246
+ ner_tags = [["O", 0] for o in tokens]
247
+
248
+ for tag in tags:
249
+
250
+ cpt = 0
251
+
252
+ for idx, token in enumerate(tokens):
253
+
254
+ rtok = range(token["offset_start"], token["offset_end"] + 1)
255
+ rtag = range(tag["offset_start"], tag["offset_end"] + 1)
256
+
257
+ # Check if the ranges are overlapping
258
+ if bool(set(rtok) & set(rtag)):
259
+
260
+ # if ner_tags[idx] != "O" and ner_tags[idx] != tag['label']:
261
+ # print(f"{token} - currently: {ner_tags[idx]} - after: {tag['label']}")
262
+
263
+ if ner_tags[idx][0] == "O":
264
+ cpt += 1
265
+ ner_tags[idx][0] = tag["label"]
266
+ ner_tags[idx][1] = cpt
267
+
268
+ for i in range(len(ner_tags)):
269
+
270
+ tag = ner_tags[i][0]
271
+
272
+ if tag == "O":
273
+ continue
274
+ elif tag != "O" and ner_tags[i][1] == 1:
275
+ ner_tags[i][0] = "B-" + tag
276
+ elif tag != "O" and ner_tags[i][1] != 1:
277
+ ner_tags[i][0] = "I-" + tag
278
+
279
+ obj = {
280
+ "id": u["@id"],
281
+ "tokens": [t["token"] for t in tokens],
282
+ "ner_tags": [n[0] for n in ner_tags],
283
+ }
284
+
285
+ all_res.append(obj)
286
+
287
+ ids = [r["id"] for r in all_res]
288
+
289
+ random.seed(4)
290
+ random.shuffle(ids)
291
+ random.shuffle(ids)
292
+ random.shuffle(ids)
293
+
294
+ train, validation, test = np.split(ids, [int(len(ids) * 0.70), int(len(ids) * 0.80)])
295
+
296
+ if split == "train":
297
+ allowed_ids = list(train)
298
+ elif split == "validation":
299
+ allowed_ids = list(validation)
300
+ elif split == "test":
301
+ allowed_ids = list(test)
302
+
303
+ for r in all_res:
304
+ identifier = r["id"]
305
+ if identifier in allowed_ids:
306
+ yield identifier, r