Update DEFT2021.py
Browse files- 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 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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 |
-
|
| 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 |
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 |
|