qanastek commited on
Commit
dcf6f21
1 Parent(s): d7b0a56

Update DEFT2021.py

Browse files
Files changed (1) hide show
  1. DEFT2021.py +119 -107
DEFT2021.py CHANGED
@@ -1,7 +1,12 @@
1
  import os
 
 
2
  import json
3
  import random
4
  from pathlib import Path
 
 
 
5
 
6
  import datasets
7
  import numpy as np
@@ -46,37 +51,37 @@ class DEFT2021(datasets.GeneratorBasedBuilder):
46
  ]
47
 
48
  def _info(self):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- if self.config.name.find("cls") != -1:
51
-
52
- features = datasets.Features(
53
- {
54
- "id": datasets.Value("string"),
55
- "document_id": datasets.Value("string"),
56
- "text": datasets.Value("string"),
57
- "specialities": datasets.Sequence(
58
- datasets.features.ClassLabel(names=_SPECIALITIES),
59
- ),
60
- "specialities_one_hot": datasets.Sequence(
61
- datasets.Value("float"),
62
- ),
63
- }
64
- )
65
-
66
- elif self.config.name.find("ner") != -1:
67
-
68
- features = datasets.Features(
69
- {
70
- "id": datasets.Value("string"),
71
- "document_id": datasets.Value("string"),
72
- "tokens": datasets.Sequence(datasets.Value("string")),
73
- "ner_tags": datasets.Sequence(
74
- datasets.features.ClassLabel(
75
- 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'],
76
- )
77
- ),
78
- }
79
- ),
80
 
81
  return datasets.DatasetInfo(
82
  description=_DESCRIPTION,
@@ -492,84 +497,91 @@ class DEFT2021(datasets.GeneratorBasedBuilder):
492
  return final_json
493
 
494
  def _generate_examples(self, data_dir, split):
 
 
495
 
496
- if self.config.name.find("cls") != -1:
497
- all_res = {}
498
-
499
- key = 0
500
-
501
- if split == 'train' or split == 'validation':
502
- split_eval = 'train'
503
- else:
504
- split_eval = 'test'
505
-
506
- path_labels = Path(data_dir) / 'evaluations' / f"ref-{split_eval}-deft2021.txt"
507
-
508
- with open(os.path.join(data_dir, 'distribution-corpus.txt')) as f_dist:
509
-
510
- doc_specialities_ = {}
511
- with open(path_labels) as f_spec:
512
- doc_specialities = [line.strip() for line in f_spec.readlines()]
513
- for raw in doc_specialities:
514
- raw_split = raw.split('\t')
515
- if len(raw_split) == 3 and raw_split[0] in doc_specialities_:
516
- doc_specialities_[raw_split[0]].append(raw_split[1])
517
- elif len(raw_split) == 3 and raw_split[0] not in doc_specialities_:
518
- doc_specialities_[raw_split[0]] = [raw_split[1]]
519
-
520
- ann_path = Path(data_dir) / "DEFT-cas-cliniques"
521
-
522
- for guid, txt_file in enumerate(sorted(ann_path.glob("*.txt"))):
523
-
524
- ann_file = txt_file.with_suffix("").name.split('.')[0]+'.ann'
525
-
526
- if ann_file in doc_specialities_:
527
-
528
- res = {}
529
- res['document_id'] = txt_file.with_suffix("").name
530
- with txt_file.open() as f:
531
- res["text"] = f.read()
532
-
533
- specialities = doc_specialities_[ann_file]
534
-
535
- # Empty one hot vector
536
- one_hot = [0.0 for i in _SPECIALITIES]
537
-
538
- # Fill up the one hot vector
539
- for s in specialities:
540
- one_hot[_SPECIALITIES.index(s)] = 1.0
541
-
542
- all_res[res['document_id']] = {
543
- "id": str(key),
544
- "document_id": res['document_id'],
545
- "text": res["text"],
546
- "specialities": specialities,
547
- "specialities_one_hot": one_hot,
548
- }
549
-
550
- key += 1
551
-
552
- distribution = [line.strip() for line in f_dist.readlines()]
553
-
554
- random.seed(4)
555
- train = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'train 2021']
556
- random.shuffle(train)
557
- random.shuffle(train)
558
- random.shuffle(train)
559
- train, validation = np.split(train, [int(len(train)*0.7096)])
560
-
561
- test = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'test 2021']
562
-
563
- if split == "train":
564
- allowed_ids = list(train)
565
- elif split == "test":
566
- allowed_ids = list(test)
567
- elif split == "validation":
568
- allowed_ids = list(validation)
569
-
570
- for r in all_res.values():
571
- if r["document_id"]+'.txt' in allowed_ids:
572
- yield r["id"], r
 
 
 
 
 
573
 
574
  elif self.config.name.find("ner") != -1:
575
 
 
1
  import os
2
+ import re
3
+ import ast
4
  import json
5
  import random
6
  from pathlib import Path
7
+ from itertools import product
8
+ from dataclasses import dataclass
9
+ from typing import Dict, List, Tuple
10
 
11
  import datasets
12
  import numpy as np
 
51
  ]
52
 
53
  def _info(self):
54
+
55
+ if self.config.name.find("cls") != -1:
56
+
57
+ features = datasets.Features(
58
+ {
59
+ "id": datasets.Value("string"),
60
+ "document_id": datasets.Value("string"),
61
+ "text": datasets.Value("string"),
62
+ "specialities": datasets.Sequence(
63
+ datasets.features.ClassLabel(names=_SPECIALITIES),
64
+ ),
65
+ "specialities_one_hot": datasets.Sequence(
66
+ datasets.Value("float"),
67
+ ),
68
+ }
69
+ )
70
 
71
+ elif self.config.name.find("ner") != -1:
72
+
73
+ features = datasets.Features(
74
+ {
75
+ "id": datasets.Value("string"),
76
+ "document_id": datasets.Value("string"),
77
+ "tokens": datasets.Sequence(datasets.Value("string")),
78
+ "ner_tags": datasets.Sequence(
79
+ datasets.features.ClassLabel(
80
+ 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'],
81
+ )
82
+ ),
83
+ }
84
+ ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  return datasets.DatasetInfo(
87
  description=_DESCRIPTION,
 
497
  return final_json
498
 
499
  def _generate_examples(self, data_dir, split):
500
+
501
+ if self.config.name.find("cls") != -1:
502
 
503
+ all_res = {}
504
+
505
+ key = 0
506
+
507
+ if split == 'train' or split == 'validation':
508
+ split_eval = 'train'
509
+ else:
510
+ split_eval = 'test'
511
+
512
+ path_labels = Path(data_dir) / 'evaluations' / f"ref-{split_eval}-deft2021.txt"
513
+
514
+ with open(os.path.join(data_dir, 'distribution-corpus.txt')) as f_dist:
515
+
516
+ doc_specialities_ = {}
517
+
518
+ with open(path_labels) as f_spec:
519
+
520
+ doc_specialities = [line.strip() for line in f_spec.readlines()]
521
+
522
+ for raw in doc_specialities:
523
+
524
+ raw_split = raw.split('\t')
525
+
526
+ if len(raw_split) == 3 and raw_split[0] in doc_specialities_:
527
+ doc_specialities_[raw_split[0]].append(raw_split[1])
528
+
529
+ elif len(raw_split) == 3 and raw_split[0] not in doc_specialities_:
530
+ doc_specialities_[raw_split[0]] = [raw_split[1]]
531
+
532
+ ann_path = Path(data_dir) / "DEFT-cas-cliniques"
533
+
534
+ for guid, txt_file in enumerate(sorted(ann_path.glob("*.txt"))):
535
+
536
+ ann_file = txt_file.with_suffix("").name.split('.')[0]+'.ann'
537
+
538
+ if ann_file in doc_specialities_:
539
+
540
+ res = {}
541
+ res['document_id'] = txt_file.with_suffix("").name
542
+ with txt_file.open() as f:
543
+ res["text"] = f.read()
544
+
545
+ specialities = doc_specialities_[ann_file]
546
+
547
+ # Empty one hot vector
548
+ one_hot = [0.0 for i in _SPECIALITIES]
549
+
550
+ # Fill up the one hot vector
551
+ for s in specialities:
552
+ one_hot[_SPECIALITIES.index(s)] = 1.0
553
+
554
+ all_res[res['document_id']] = {
555
+ "id": str(key),
556
+ "document_id": res['document_id'],
557
+ "text": res["text"],
558
+ "specialities": specialities,
559
+ "specialities_one_hot": one_hot,
560
+ }
561
+
562
+ key += 1
563
+
564
+ distribution = [line.strip() for line in f_dist.readlines()]
565
+
566
+ random.seed(4)
567
+ train = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'train 2021']
568
+ random.shuffle(train)
569
+ random.shuffle(train)
570
+ random.shuffle(train)
571
+ train, validation = np.split(train, [int(len(train)*0.7096)])
572
+
573
+ test = [raw.split('\t')[0] for raw in distribution if len(raw.split('\t')) == 4 and raw.split('\t')[3] == 'test 2021']
574
+
575
+ if split == "train":
576
+ allowed_ids = list(train)
577
+ elif split == "test":
578
+ allowed_ids = list(test)
579
+ elif split == "validation":
580
+ allowed_ids = list(validation)
581
+
582
+ for r in all_res.values():
583
+ if r["document_id"]+'.txt' in allowed_ids:
584
+ yield r["id"], r
585
 
586
  elif self.config.name.find("ner") != -1:
587