File size: 25,413 Bytes
9e8df72
 
 
 
 
 
 
 
 
de1f0ae
9e8df72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fedb50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de1f0ae
7fedb50
9e8df72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee3f33b
 
 
 
 
9e8df72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00c9ca
9e8df72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00c9ca
9e8df72
14292ab
9e8df72
 
 
 
 
 
 
 
 
 
 
 
 
 
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import os
import re
import ast
import json
import random
from pathlib import Path
from itertools import product
from dataclasses import dataclass
from typing import Dict, List, Tuple
import requests

import datasets
import numpy as np

_CITATION = """\
@inproceedings{grouin-etal-2021-classification,
    title = "Classification de cas cliniques et {\'e}valuation automatique de r{\'e}ponses d{'}{\'e}tudiants : pr{\'e}sentation de la campagne {DEFT} 2021 (Clinical cases classification and automatic evaluation of student answers : Presentation of the {DEFT} 2021 Challenge)",
    author = "Grouin, Cyril  and
      Grabar, Natalia  and
      Illouz, Gabriel",
    booktitle = "Actes de la 28e Conf{\'e}rence sur le Traitement Automatique des Langues Naturelles. Atelier D{\'E}fi Fouille de Textes (DEFT)",
    month = "6",
    year = "2021",
    address = "Lille, France",
    publisher = "ATALA",
    url = "https://aclanthology.org/2021.jeptalnrecital-deft.1",
    pages = "1--13",
    abstract = "Le d{\'e}fi fouille de textes (DEFT) est une campagne d{'}{\'e}valuation annuelle francophone. Nous pr{\'e}sentons les corpus et baselines {\'e}labor{\'e}es pour trois t{\^a}ches : (i) identifier le profil clinique de patients d{\'e}crits dans des cas cliniques, (ii) {\'e}valuer automatiquement les r{\'e}ponses d{'}{\'e}tudiants sur des questionnaires en ligne (Moodle) {\`a} partir de la correction de l{'}enseignant, et (iii) poursuivre une {\'e}valuation de r{\'e}ponses d{'}{\'e}tudiants {\`a} partir de r{\'e}ponses d{\'e}j{\`a} {\'e}valu{\'e}es par l{'}enseignant. Les r{\'e}sultats varient de 0,394 {\`a} 0,814 de F-mesure sur la premi{\`e}re t{\^a}che (7 {\'e}quipes), de 0,448 {\`a} 0,682 de pr{\'e}cision sur la deuxi{\`e}me (3 {\'e}quipes), et de 0,133 {\`a} 0,510 de pr{\'e}cision sur la derni{\`e}re (3 {\'e}quipes).",
    language = "French",
}
"""

_DESCRIPTION = """\
ddd
"""

_HOMEPAGE = "ddd"

_LICENSE = "unknown"

_SPECIALITIES = ['immunitaire', 'endocriniennes', 'blessures', 'chimiques', 'etatsosy', 'nutritionnelles', 'infections', 'virales', 'parasitaires', 'tumeur', 'osteomusculaires', 'stomatognathique', 'digestif', 'respiratoire', 'ORL', 'nerveux', 'oeil', 'homme', 'femme', 'cardiovasculaires', 'hemopathies', 'genetique', 'peau']

_LABELS_BASE = ['anatomie', 'date', 'dose', 'duree', 'examen', 'frequence', 'mode', 'moment', 'pathologie', 'sosy', 'substance', 'traitement', 'valeur']

class StringIndex:

    def __init__(self, vocab):

        self.vocab_struct = {}

        print("Start building the index!")
        for t in vocab:

            if len(t) == 0:
                continue

            # Index terms by their first letter and length
            key = (t[0], len(t))

            if (key in self.vocab_struct) == False:
                self.vocab_struct[key] = []
            
            self.vocab_struct[key].append(t)

        print("Finished building the index!")

    def find(self, t):
        
        key = (t[0], len(t))
        
        if (key in self.vocab_struct) == False:
            return "is_oov"
        
        return "is_not_oov" if t in self.vocab_struct[key] else "is_oov"

_VOCAB = StringIndex(vocab=requests.get("https://huggingface.co/datasets/BioMedTok/vocabulary_nachos_lowercased/resolve/main/vocabulary_nachos_lowercased.txt").text.split("\n"))

class DEFT2021(datasets.GeneratorBasedBuilder):

    DEFAULT_CONFIG_NAME = "ner"

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="cls", version="1.0.0", description="DEFT 2021 corpora - Classification task"),
        datasets.BuilderConfig(name="ner", version="1.0.0", description="DEFT 2021 corpora - Named-entity recognition task"),
    ]

    def _info(self):
        
        if self.config.name.find("cls") != -1:

            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "document_id": datasets.Value("string"),
                    "text": datasets.Value("string"),
                    "specialities": datasets.Sequence(
                        datasets.features.ClassLabel(names=_SPECIALITIES),
                    ),
                    "specialities_one_hot": datasets.Sequence(
                        datasets.Value("float"),
                    ),
                }
            )

        elif self.config.name.find("ner") != -1:

            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "document_id": datasets.Value("string"),
                    "tokens": datasets.Sequence(datasets.Value("string")),
                    "ner_tags": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names = ['O', 'B-anatomie', 'I-anatomie', 'B-date', 'I-date', 'B-dose', 'I-dose', 'B-duree', 'I-duree', 'B-examen', 'I-examen', 'B-frequence', 'I-frequence', 'B-mode', 'I-mode', 'B-moment', 'I-moment', 'B-pathologie', 'I-pathologie', 'B-sosy', 'I-sosy', 'B-substance', 'I-substance', 'B-traitement', 'I-traitement', 'B-valeur', 'I-valeur'],
                        )
                    ),
                    "is_oov": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=['is_not_oov', 'is_oov'],
                        ),
                    ),
                }
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=str(_LICENSE),
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):

        if self.config.data_dir is None:
            raise ValueError("This is a local dataset. Please pass the data_dir kwarg to load_dataset.")
        
        else:
            data_dir = self.config.data_dir
            
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_dir": data_dir,
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_dir": data_dir,
                    "split": "validation",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "data_dir": data_dir,
                    "split": "test",
                },
            ),
        ]

    def remove_prefix(self, a: str, prefix: str) -> str:
        if a.startswith(prefix):
            a = a[len(prefix) :]
        return a
		
    def parse_brat_file(self, txt_file: Path, annotation_file_suffixes: List[str] = None, parse_notes: bool = False) -> Dict:
    
        example = {}
        example["document_id"] = txt_file.with_suffix("").name
        with txt_file.open() as f:
            example["text"] = f.read()
    
        # If no specific suffixes of the to-be-read annotation files are given - take standard suffixes
        # for event extraction
        if annotation_file_suffixes is None:
            annotation_file_suffixes = [".a1", ".a2", ".ann"]
    
        if len(annotation_file_suffixes) == 0:
            raise AssertionError(
                "At least one suffix for the to-be-read annotation files should be given!"
            )
    
        ann_lines = []
        for suffix in annotation_file_suffixes:
            annotation_file = txt_file.with_suffix(suffix)
            if annotation_file.exists():
                with annotation_file.open() as f:
                    ann_lines.extend(f.readlines())
    
        example["text_bound_annotations"] = []
        example["events"] = []
        example["relations"] = []
        example["equivalences"] = []
        example["attributes"] = []
        example["normalizations"] = []
    
        if parse_notes:
            example["notes"] = []
    
        for line in ann_lines:
            line = line.strip()
            if not line:
                continue
    
            if line.startswith("T"):  # Text bound
                ann = {}
                fields = line.split("\t")
    
                ann["id"] = fields[0]
                ann["type"] = fields[1].split()[0]
                ann["offsets"] = []
                span_str = self.remove_prefix(fields[1], (ann["type"] + " "))
                text = fields[2]
                for span in span_str.split(";"):
                    start, end = span.split()
                    ann["offsets"].append([int(start), int(end)])
    
                # Heuristically split text of discontiguous entities into chunks
                ann["text"] = []
                if len(ann["offsets"]) > 1:
                    i = 0
                    for start, end in ann["offsets"]:
                        chunk_len = end - start
                        ann["text"].append(text[i : chunk_len + i])
                        i += chunk_len
                        while i < len(text) and text[i] == " ":
                            i += 1
                else:
                    ann["text"] = [text]
    
                example["text_bound_annotations"].append(ann)
    
            elif line.startswith("E"):
                ann = {}
                fields = line.split("\t")
    
                ann["id"] = fields[0]
    
                ann["type"], ann["trigger"] = fields[1].split()[0].split(":")
    
                ann["arguments"] = []
                for role_ref_id in fields[1].split()[1:]:
                    argument = {
                        "role": (role_ref_id.split(":"))[0],
                        "ref_id": (role_ref_id.split(":"))[1],
                    }
                    ann["arguments"].append(argument)
    
                example["events"].append(ann)
    
            elif line.startswith("R"):
                ann = {}
                fields = line.split("\t")
    
                ann["id"] = fields[0]
                ann["type"] = fields[1].split()[0]
    
                ann["head"] = {
                    "role": fields[1].split()[1].split(":")[0],
                    "ref_id": fields[1].split()[1].split(":")[1],
                }
                ann["tail"] = {
                    "role": fields[1].split()[2].split(":")[0],
                    "ref_id": fields[1].split()[2].split(":")[1],
                }
    
                example["relations"].append(ann)
    
            # '*' seems to be the legacy way to mark equivalences,
            # but I couldn't find any info on the current way
            # this might have to be adapted dependent on the brat version
            # of the annotation
            elif line.startswith("*"):
                ann = {}
                fields = line.split("\t")
    
                ann["id"] = fields[0]
                ann["ref_ids"] = fields[1].split()[1:]
    
                example["equivalences"].append(ann)
    
            elif line.startswith("A") or line.startswith("M"):
                ann = {}
                fields = line.split("\t")
    
                ann["id"] = fields[0]
    
                info = fields[1].split()
                ann["type"] = info[0]
                ann["ref_id"] = info[1]
    
                if len(info) > 2:
                    ann["value"] = info[2]
                else:
                    ann["value"] = ""
    
                example["attributes"].append(ann)
    
            elif line.startswith("N"):
                ann = {}
                fields = line.split("\t")
    
                ann["id"] = fields[0]
                ann["text"] = fields[2]
    
                info = fields[1].split()
    
                ann["type"] = info[0]
                ann["ref_id"] = info[1]
                ann["resource_name"] = info[2].split(":")[0]
                ann["cuid"] = info[2].split(":")[1]
                example["normalizations"].append(ann)
    
            elif parse_notes and line.startswith("#"):
                ann = {}
                fields = line.split("\t")
    
                ann["id"] = fields[0]
                ann["text"] = fields[2] if len(fields) == 3 else "<BB_NULL_STR>"
    
                info = fields[1].split()
    
                ann["type"] = info[0]
                ann["ref_id"] = info[1]
                example["notes"].append(ann)
        return example

    def _to_source_example(self, brat_example: Dict) -> Dict:
    
        source_example = {
            "document_id": brat_example["document_id"],
            "text": brat_example["text"],
        }
    
        source_example["entities"] = []
    
        for entity_annotation in brat_example["text_bound_annotations"]:
            entity_ann = entity_annotation.copy()
    
            # Change id property name
            entity_ann["entity_id"] = entity_ann["id"]
            entity_ann.pop("id")
    
            # Add entity annotation to sample
            source_example["entities"].append(entity_ann)
    
        return source_example

    def convert_to_prodigy(self, json_object, list_label):
    
        def prepare_split(text):
    
            rep_before = ['?', '!', ';', '*']
            rep_after = ['’', "'"]
            rep_both = ['-', '/', '[', ']', ':', ')', '(', ',', '.']
    
            for i in rep_before:
                text = text.replace(i, ' '+i)
    
            for i in rep_after:
                text = text.replace(i, i+' ')
    
            for i in rep_both:
                text = text.replace(i, ' '+i+' ')
    
            text_split = text.split()
    
            punctuations = [',', '.']
            for j in range(0, len(text_split)-1):
                if j-1 >= 0 and j+1 <= len(text_split)-1 and text_split[j-1][-1].isdigit() and text_split[j+1][0].isdigit():
                    if text_split[j] in punctuations:
                        text_split[j-1:j+2] = [''.join(text_split[j-1:j+2])]
    
            text = ' '.join(text_split)
    
            return text
        
        new_json = []
    
        for ex in [json_object]:
            
            text = prepare_split(ex['text'])
    
            tokenized_text = text.split()
    
            list_spans = []
    
            for a in ex['entities']:
    
                for o in range(len(a['offsets'])):
    
                    text_annot = prepare_split(a['text'][o])
    
                    offset_start = a['offsets'][o][0]
                    offset_end = a['offsets'][o][1]
    
                    nb_tokens_annot = len(text_annot.split())
    
                    txt_offsetstart = prepare_split(ex['text'][:offset_start])
    
                    nb_tokens_before_annot = len(txt_offsetstart.split())
    
                    token_start = nb_tokens_before_annot
                    token_end = token_start + nb_tokens_annot - 1
    
                    if a['type'] in list_label:
                        list_spans.append({
                            'start': offset_start,
                            'end': offset_end,
                            'token_start': token_start,
                            'token_end': token_end,
                            'label': a['type'],
                            'id': a['entity_id'],
                            'text': a['text'][o],
                        })
    
            res = {
                'id': ex['document_id'],
                'document_id': ex['document_id'],
                'text': ex['text'],
                'tokens': tokenized_text,
                'spans': list_spans
            }
    
            new_json.append(res)
            
        return new_json

    def convert_to_hf_format(self, json_object):
    
        dict_out = []
    
        for i in json_object:
    
            # Filter annotations to keep the longest annotated spans when there is nested annotations
            selected_annotations = []
    
            if 'spans' in i:
    
                for idx_j, j in enumerate(i['spans']):
    
                    len_j = int(j['end'])-int(j['start'])
                    range_j = [l for l in range(int(j['start']),int(j['end']),1)]
    
                    keep = True
    
                    for idx_k, k in enumerate(i['spans'][idx_j+1:]):
    
                        len_k = int(k['end'])-int(k['start'])
                        range_k = [l for l in range(int(k['start']),int(k['end']),1)]
    
                        inter = list(set(range_k).intersection(set(range_j)))
                        if len(inter) > 0 and len_j < len_k:
                            keep = False
    
                    if keep:
                        selected_annotations.append(j)
    
            # Create list of labels + id to separate different annotation and prepare IOB2 format
            nb_tokens = len(i['tokens'])
            ner_tags = ['O']*nb_tokens
            
            for slct in selected_annotations:
    
                for x in range(slct['token_start'], slct['token_end']+1, 1):
    
                    if i['tokens'][x] not in slct['text']:
                        if ner_tags[x-1] == 'O':
                            ner_tags[x-1] = slct['label']+'-'+slct['id']
                    else:
                        if ner_tags[x] == 'O':
                            ner_tags[x] = slct['label']+'-'+slct['id']
    
            # Make IOB2 format
            ner_tags_IOB2 = []
            for idx_l, label in enumerate(ner_tags):
    
                if label == 'O':
                    ner_tags_IOB2.append('O')
                else:
                    current_label = label.split('-')[0]
                    current_id = label.split('-')[1]
                    if idx_l == 0:
                        ner_tags_IOB2.append('B-'+current_label)
                    elif current_label in ner_tags[idx_l-1]:
                        if current_id == ner_tags[idx_l-1].split('-')[1]:
                            ner_tags_IOB2.append('I-'+current_label)
                        else:
                            ner_tags_IOB2.append('B-'+current_label)
                    else:
                        ner_tags_IOB2.append('B-'+current_label)
    
            dict_out.append({
                'id': i['id'],
                'document_id': i['document_id'],
                "ner_tags": ner_tags_IOB2,
                "tokens": i['tokens'],
            })
        
        return dict_out


    def split_sentences(self, json_o):
        """
            Split each document in sentences to fit the 512 maximum tokens of BERT.
    
        """
        
        final_json = []
        
        for i in json_o:
    
            ind_punc = [index for index, value in enumerate(i['tokens']) if value=='.'] + [len(i['tokens'])]
            
            for index, value in enumerate(ind_punc):
                
                if index==0:
                    final_json.append({'id': i['id']+'_'+str(index),
                                    'document_id': i['document_id'],
                                    'ner_tags': i['ner_tags'][:value+1],
                                    'tokens': i['tokens'][:value+1]
                                    })
                else:
                    prev_value = ind_punc[index-1]
                    final_json.append({'id': i['id']+'_'+str(index),
                                    'document_id': i['document_id'],
                                    'ner_tags': i['ner_tags'][prev_value+1:value+1],
                                    'tokens': i['tokens'][prev_value+1:value+1]
                                    }) 
        
        return final_json

    def _generate_examples(self, data_dir, split):
        
        if self.config.name.find("cls") != -1:

            all_res = {}

            key = 0

            if split == 'train' or split == 'validation':
                split_eval = 'train'
            else:
                split_eval = 'test'

            path_labels = Path(data_dir)  / 'evaluations' / f"ref-{split_eval}-deft2021.txt"

            with open(os.path.join(data_dir, 'distribution-corpus.txt')) as f_dist:

                doc_specialities_ = {}

                with open(path_labels) as f_spec:

                    doc_specialities = [line.strip() for line in f_spec.readlines()]

                    for raw in doc_specialities:

                        raw_split = raw.split('\t')

                        if len(raw_split) == 3 and raw_split[0] in doc_specialities_:
                            doc_specialities_[raw_split[0]].append(raw_split[1])
                        
                        elif len(raw_split) == 3 and raw_split[0] not in doc_specialities_:
                            doc_specialities_[raw_split[0]] = [raw_split[1]]

                ann_path = Path(data_dir) / "DEFT-cas-cliniques"

                for guid, txt_file in enumerate(sorted(ann_path.glob("*.txt"))):

                    ann_file = txt_file.with_suffix("").name.split('.')[0]+'.ann'

                    if ann_file in doc_specialities_:

                        res = {}
                        res['document_id'] = txt_file.with_suffix("").name
                        with txt_file.open() as f:
                            res["text"] = f.read()

                        specialities = doc_specialities_[ann_file]

                        # Empty one hot vector
                        one_hot = [0.0 for i in _SPECIALITIES]

                        # Fill up the one hot vector
                        for s in specialities:
                            one_hot[_SPECIALITIES.index(s)] = 1.0

                        all_res[res['document_id']] = {
                            "id": str(key),
                            "document_id": res['document_id'],
                            "text": res["text"].lower(),
                            "specialities": specialities,
                            "specialities_one_hot": one_hot,
                        }

                        key += 1

                distribution = [line.strip() for line in f_dist.readlines()]
                
                random.seed(4)
                train = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'train 2021']
                random.shuffle(train)
                random.shuffle(train)
                random.shuffle(train)
                train, validation = np.split(train, [int(len(train)*0.7096)])
                
                test = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'test 2021']

                if split == "train":
                    allowed_ids = list(train)
                elif split == "test":
                    allowed_ids = list(test)
                elif split == "validation":
                    allowed_ids = list(validation)

                for r in all_res.values():
                    if r["document_id"]+'.txt' in allowed_ids:
                        yield r["id"], r

        elif self.config.name.find("ner") != -1:

            all_res = []

            key = 0

            with open(os.path.join(data_dir, 'distribution-corpus.txt')) as f_dist:

                distribution = [line.strip() for line in f_dist.readlines()]

                random.seed(4)
                train = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'train 2021']
                random.shuffle(train)
                random.shuffle(train)
                random.shuffle(train)
                train, validation = np.split(train, [int(len(train)*0.73)])
                test = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'test 2021']

                ann_path = Path(data_dir) / "DEFT-cas-cliniques"

                for guid, txt_file in enumerate(sorted(ann_path.glob("*.txt"))):

                    brat_example = self.parse_brat_file(txt_file, parse_notes=True)

                    source_example = self._to_source_example(brat_example)

                    prod_format = self.convert_to_prodigy(source_example, _LABELS_BASE)

                    hf_format = self.convert_to_hf_format(prod_format)

                    hf_split = self.split_sentences(hf_format)

                    for h in hf_split:

                        if len(h['tokens']) > 0 and len(h['ner_tags']) > 0:
                            
                            all_res.append({
                                "id": str(key),
                                "document_id": h['document_id'],
                                "tokens": [tok.lower() for tok in h['tokens']],
                                "ner_tags": h['ner_tags'],
                                "is_oov": [_VOCAB.find(tt.lower()) for tt in h['tokens']]
                            })

                            key += 1

                if split == "train":
                    allowed_ids = list(train)
                elif split == "validation":
                    allowed_ids = list(validation)
                elif split == "test":
                    allowed_ids = list(test)

                for r in all_res:
                    if r["document_id"]+'.txt' in allowed_ids:
                        yield r["id"], r