File size: 16,295 Bytes
dab57fa
08e8e81
dab57fa
 
08e8e81
dab57fa
00399e7
 
 
 
dab57fa
 
 
08e8e81
 
 
 
 
 
 
dab57fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e957396
dab57fa
 
e957396
dab57fa
 
 
 
 
 
 
 
 
 
08e8e81
dab57fa
e957396
dab57fa
 
 
 
 
 
 
e957396
dab57fa
e957396
cad01c5
dab57fa
 
 
ff1d52b
cad01c5
 
 
 
1424af7
 
 
cad01c5
 
 
 
ff1d52b
 
 
 
 
 
 
 
 
 
dab57fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08e8e81
dab57fa
 
08e8e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc963f
 
 
 
 
 
 
 
 
 
 
08e8e81
 
 
 
 
 
 
 
 
 
 
 
 
dab57fa
 
 
 
08e8e81
 
 
 
 
ccc963f
 
 
 
 
 
 
 
 
 
 
08e8e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc963f
 
 
 
 
 
 
 
 
 
 
08e8e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc963f
 
 
 
 
 
 
 
 
 
 
08e8e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab57fa
 
 
ccc963f
 
 
 
 
 
 
 
 
 
 
dab57fa
 
 
 
 
 
1424af7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab57fa
 
 
 
 
 
 
 
 
 
 
1424af7
dab57fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1424af7
dab57fa
 
 
 
 
 
 
 
 
 
 
08e8e81
dab57fa
 
 
 
e957396
 
dab57fa
 
 
 
00399e7
ccc963f
 
 
 
 
00399e7
 
 
dab57fa
 
 
00399e7
dab57fa
 
e957396
 
 
cad01c5
1424af7
cad01c5
dab57fa
 
 
 
 
 
 
 
8354e18
dab57fa
8354e18
dab57fa
 
 
 
 
 
 
cad01c5
 
 
 
 
1424af7
dab57fa
cad01c5
 
 
 
dab57fa
e957396
ccc963f
cad01c5
1424af7
cad01c5
e957396
dab57fa
cad01c5
1424af7
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import os
from typing import Iterator

import datasets
from bs4 import BeautifulSoup, ResultSet
from datasets import DownloadManager
from syntok.tokenizer import Tokenizer

tok = Tokenizer()


_CITATION = """\
@report{Magnini2021,
author = {Bernardo Magnini and Begoña Altuna and Alberto Lavelli and Manuela Speranza
and Roberto Zanoli and Fondazione Bruno Kessler},
keywords = {Clinical data,clinical enti-ties,corpus,multilingual,temporal information},
title = {The E3C Project:
European Clinical Case Corpus El proyecto E3C: European Clinical Case Corpus},
url = {https://uts.nlm.nih.gov/uts/umls/home},
year = {2021},
}

"""

_DESCRIPTION = """\
The European Clinical Case Corpus (E3C) project aims at collecting and \
annotating a large corpus of clinical documents in five European languages (Spanish, \
Basque, English, French and Italian), which will be freely distributed. Annotations \
include temporal information, to allow temporal reasoning on chronologies, and \
information about clinical entities based on medical taxonomies, to be used for semantic reasoning.
"""

_URL = "https://github.com/hltfbk/E3C-Corpus/archive/refs/tags/v2.0.0.zip"


class E3CConfig(datasets.BuilderConfig):
    """BuilderConfig for E3C."""

    def __init__(self, **kwargs):
        """BuilderConfig for E3C.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(E3CConfig, self).__init__(**kwargs)


class E3C(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.1.0")
    BUILDER_CONFIGS = [
        E3CConfig(
            name="e3c",
            version=VERSION,
            description="this is an implementation of the E3C dataset",
        ),
    ]

    def _info(self):
        """This method specifies the DatasetInfo which contains information and typings."""
        features = datasets.Features(
            {
                "text": datasets.Value("string"),
                "tokens": datasets.Sequence(datasets.Value("string")),
                "tokens_offsets": datasets.Sequence(datasets.Sequence(datasets.Value("int32"))),
                "clinical_entity_tags": datasets.Sequence(
                    datasets.features.ClassLabel(
                        names=[
                            "O",
                            "B-CLINENTITY",
                            "I-CLINENTITY",
                        ],
                    ),
                ),
                "clinical_entity_cuid": datasets.Sequence(
                    datasets.Value("string"),
                ),
                "temporal_information_tags": datasets.Sequence(
                    datasets.features.ClassLabel(
                        names=[
                            "O",
                            "B-EVENT",
                            "B-ACTOR",
                            "B-BODYPART",
                            "B-TIMEX3",
                            "B-RML",
                            "I-EVENT",
                            "I-ACTOR",
                            "I-BODYPART",
                            "I-TIMEX3",
                            "I-RML",
                        ],
                    ),
                ),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            citation=_CITATION,
            supervised_keys=None,
        )

    def _split_generators(self, dl_manager: DownloadManager) -> list[datasets.SplitGenerator]:
        """Returns SplitGenerators who contains all the difference splits of the dataset.
        Each language has its own split and each split has 3 different layers (sub-split):
            - layer 1: full manual annotation of clinical entities, temporal information and
                factuality, for benchmarking and linguistic analysis.
            - layer 2: semi-automatic annotation of clinical entities
            - layer 3: non-annotated documents
        Args:
            dl_manager: A `datasets.utils.DownloadManager` that can be used to download and
            extract URLs.
        Returns:
            A list of `datasets.SplitGenerator`. Contains all subsets of the dataset depending on
            the language and the layer.
        """
        url = _URL
        data_dir = dl_manager.download_and_extract(url)

        return [
            datasets.SplitGenerator(
                name="en.layer1",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "English",
                        "layer1",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="en.layer2",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "English",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="en.layer2.validation",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_validation",
                        "English",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="es.layer1",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "Spanish",
                        "layer1",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="es.layer2",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "Spanish",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="es.layer2.validation",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_validation",
                        "Spanish",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="eu.layer1",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "Basque",
                        "layer1",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="eu.layer2",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "Basque",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="eu.layer2.validation",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_validation",
                        "Basque",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="fr.layer1",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "French",
                        "layer1",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="fr.layer2",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "French",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="fr.layer2.validation",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_validation",
                        "French",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="it.layer1",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "Italian",
                        "layer1",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="it.layer2",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_annotation",
                        "Italian",
                        "layer2",
                    ),
                },
            ),
            datasets.SplitGenerator(
                name="it.layer2.validation",
                gen_kwargs={
                    "filepath": os.path.join(
                        data_dir,
                        "E3C-Corpus-2.0.0/data_validation",
                        "Italian",
                        "layer2",
                    ),
                },
            ),
        ]

    @staticmethod
    def get_annotations(entities: ResultSet, text: str) -> list:
        """Extract the offset, the text and the type of the entity.

        Args:
            entities: The entities to extract.
            text: The text of the document.
        Returns:
            A list of list containing the offset, the text and the type of the entity.
        """
        return [

            [
                int(entity.get("begin")),
                int(entity.get("end")),
                text[int(entity.get("begin")) : int(entity.get("end"))],
            ]
            for entity in entities
        ]

    def get_clinical_annotations(self, entities: ResultSet, text: str) -> list:
        """Extract the offset, the text and the type of the entity.

        Args:
            entities: The entities to extract.
            text: The text of the document.
        Returns:
            A list of list containing the offset, the text and the type of the entity.
        """
        return [
            [
                int(entity.get("begin")),
                int(entity.get("end")),
                text[int(entity.get("begin")) : int(entity.get("end"))],
                entity.get("entityID"),
            ]
            for entity in entities
        ]

    def get_parsed_data(self, filepath: str):
        """Parse the data from the E3C dataset and store it in a dictionary.
        Iterate over the files in the dataset and parse for each file the following entities:
            - CLINENTITY
            - EVENT
            - ACTOR
            - BODYPART
            - TIMEX3
            - RML
        for each entity, we extract the offset, the text and the type of the entity.

        Args:
            filepath: The path to the folder containing the files to parse.
        """
        for root, _, files in os.walk(filepath):
            for file in files:
                with open(f"{root}/{file}") as soup_file:
                    soup = BeautifulSoup(soup_file, "xml")
                    text = soup.find("cas:Sofa").get("sofaString")
                    yield {
                        "CLINENTITY": self.get_clinical_annotations(
                            soup.find_all("custom:CLINENTITY"), text
                        ),
                        "EVENT": self.get_annotations(soup.find_all("custom:EVENT"), text),
                        "ACTOR": self.get_annotations(soup.find_all("custom:ACTOR"), text),
                        "BODYPART": self.get_annotations(soup.find_all("custom:BODYPART"), text),
                        "TIMEX3": self.get_annotations(soup.find_all("custom:TIMEX3"), text),
                        "RML": self.get_annotations(soup.find_all("custom:RML"), text),
                        "SENTENCE": self.get_annotations(soup.find_all("type4:Sentence"), text),
                        "TOKENS": self.get_annotations(soup.find_all("type4:Token"), text),
                    }

    def _generate_examples(self, filepath) -> Iterator:
        """Yields examples as (key, example) tuples.
        Args:
            filepath: The path to the folder containing the files to parse.
        Yields:
            an example containing four fields: the text, the annotations, the tokens offsets and
            the sentences.
        """
        guid = 0
        for content in self.get_parsed_data(filepath):
            for sentence in content["SENTENCE"]:
                tokens = [
                    (
                        token.offset + sentence[0],
                        token.offset + sentence[0] + len(token.value),
                        token.value,
                    )
                    for token in list(tok.tokenize(sentence[-1]))
                ]

                filtered_tokens = list(
                    filter(
                        lambda token: token[0] >= sentence[0] and token[1] <= sentence[1],
                        tokens,
                    )
                )
                tokens_offsets = [
                    [token[0] - sentence[0], token[1] - sentence[0]] for token in filtered_tokens
                ]
                clinical_labels = ["O"] * len(filtered_tokens)
                clinical_cuid = ["CUI_LESS"] * len(filtered_tokens)
                temporal_information_labels = ["O"] * len(filtered_tokens)
                for entity_type in [
                    "CLINENTITY",
                    "EVENT",
                    "ACTOR",
                    "BODYPART",
                    "TIMEX3",
                    "RML",
                ]:
                    if len(content[entity_type]) != 0:
                        for entities in list(
                            content[entity_type],
                        ):
                            annotated_tokens = [
                                idx_token
                                for idx_token, token in enumerate(filtered_tokens)
                                if token[0] >= entities[0] and token[1] <= entities[1]
                            ]
                            for idx_token in annotated_tokens:
                                if entity_type == "CLINENTITY":
                                    if idx_token == annotated_tokens[0]:
                                        clinical_labels[idx_token] = f"B-{entity_type}"
                                    else:
                                        clinical_labels[idx_token] = f"I-{entity_type}"
                                    clinical_cuid[idx_token] = entities[-1]
                                else:
                                    if idx_token == annotated_tokens[0]:
                                        temporal_information_labels[idx_token] = f"B-{entity_type}"
                                    else:
                                        temporal_information_labels[idx_token] = f"I-{entity_type}"
                yield guid, {
                    "text": sentence[-1],
                    "tokens": list(map(lambda token: token[2], filtered_tokens)),
                    "clinical_entity_tags": clinical_labels,
                    "clinical_entity_cuid": clinical_cuid,
                    "temporal_information_tags": temporal_information_labels,
                    "tokens_offsets": tokens_offsets,
                }
                guid += 1

if __name__ == "__main__":
    builder = E3C()
    builder.download_and_prepare()