Update PxCorpus.py
Browse files- PxCorpus.py +124 -115
PxCorpus.py
CHANGED
@@ -1,170 +1,179 @@
|
|
1 |
-
#
|
2 |
-
# Copyright 2020 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 |
-
"""DIAMED"""
|
16 |
|
17 |
import os
|
18 |
-
import
|
19 |
-
import math
|
20 |
|
21 |
import datasets
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
|
|
32 |
|
33 |
-
|
|
|
|
|
|
|
34 |
|
|
|
|
|
35 |
"""
|
36 |
|
37 |
-
|
38 |
-
"""DIAMED"""
|
39 |
|
40 |
-
|
41 |
|
42 |
BUILDER_CONFIGS = [
|
43 |
-
datasets.BuilderConfig(name=f"default", version="1.0.0", description=f"
|
44 |
]
|
45 |
|
46 |
DEFAULT_CONFIG_NAME = "default"
|
47 |
|
48 |
def _info(self):
|
49 |
-
|
50 |
features = datasets.Features(
|
51 |
{
|
52 |
-
"
|
53 |
-
"
|
54 |
-
"
|
55 |
-
|
56 |
-
"keywords": datasets.Sequence(
|
57 |
-
datasets.Value("string"),
|
58 |
),
|
59 |
-
"
|
60 |
-
|
|
|
|
|
|
|
61 |
),
|
62 |
-
"collected_at": datasets.Value("string"),
|
63 |
-
"published_at": datasets.Value("string"),
|
64 |
-
"source_url": datasets.Value("string"),
|
65 |
-
"source_name": datasets.Value("string"),
|
66 |
-
"license": datasets.Value("string"),
|
67 |
-
"figures_urls": datasets.Sequence(
|
68 |
-
datasets.Value("string"),
|
69 |
-
),
|
70 |
-
"figures_paths": datasets.Sequence(
|
71 |
-
datasets.Value("string"),
|
72 |
-
),
|
73 |
-
"figures": datasets.Sequence(
|
74 |
-
datasets.Image(),
|
75 |
-
),
|
76 |
-
"icd-10": datasets.features.ClassLabel(names=[
|
77 |
-
'A00-B99 Certain infectious and parasitic diseases',
|
78 |
-
'C00-D49 Neoplasms',
|
79 |
-
'D50-D89 Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism',
|
80 |
-
'E00-E89 Endocrine, nutritional and metabolic diseases',
|
81 |
-
'F01-F99 Mental, Behavioral and Neurodevelopmental disorders',
|
82 |
-
'G00-G99 Diseases of the nervous system',
|
83 |
-
'H00-H59 Diseases of the eye and adnexa',
|
84 |
-
'H60-H95 Diseases of the ear and mastoid process',
|
85 |
-
'I00-I99 Diseases of the circulatory system',
|
86 |
-
'J00-J99 Diseases of the respiratory system',
|
87 |
-
'K00-K95 Diseases of the digestive system',
|
88 |
-
'L00-L99 Diseases of the skin and subcutaneous tissue',
|
89 |
-
'M00-M99 Diseases of the musculoskeletal system and connective tissue',
|
90 |
-
'N00-N99 Diseases of the genitourinary system',
|
91 |
-
'O00-O9A Pregnancy, childbirth and the puerperium',
|
92 |
-
'P00-P96 Certain conditions originating in the perinatal period',
|
93 |
-
'Q00-Q99 Congenital malformations, deformations and chromosomal abnormalities',
|
94 |
-
'R00-R99 Symptoms, signs and abnormal clinical and laboratory findings, not elsewhere classified',
|
95 |
-
'S00-T88 Injury, poisoning and certain other consequences of external causes',
|
96 |
-
'U00-U85 Codes for special purposes',
|
97 |
-
'V00-Y99 External causes of morbidity',
|
98 |
-
'Z00-Z99 Factors influencing health status and contact with health services',
|
99 |
-
]),
|
100 |
}
|
101 |
)
|
102 |
|
103 |
return datasets.DatasetInfo(
|
104 |
description=_DESCRIPTION,
|
105 |
features=features,
|
106 |
-
homepage=_HOMEPAGE,
|
107 |
-
license=_LICENSE,
|
108 |
citation=_CITATION,
|
|
|
109 |
)
|
110 |
|
111 |
def _split_generators(self, dl_manager):
|
112 |
-
"""Returns SplitGenerators."""
|
113 |
|
114 |
data_dir = dl_manager.download_and_extract(_URL)
|
115 |
-
print("#"*50)
|
116 |
-
print(data_dir)
|
117 |
-
# data_dir = "./splits/"
|
118 |
|
|
|
|
|
119 |
return [
|
120 |
datasets.SplitGenerator(
|
121 |
name=datasets.Split.TRAIN,
|
122 |
gen_kwargs={
|
123 |
-
"
|
124 |
-
"
|
|
|
|
|
125 |
},
|
126 |
),
|
127 |
datasets.SplitGenerator(
|
128 |
name=datasets.Split.VALIDATION,
|
129 |
gen_kwargs={
|
130 |
-
"
|
131 |
-
"
|
|
|
|
|
132 |
},
|
133 |
),
|
134 |
datasets.SplitGenerator(
|
135 |
name=datasets.Split.TEST,
|
136 |
gen_kwargs={
|
137 |
-
"
|
138 |
-
"
|
|
|
|
|
139 |
},
|
140 |
),
|
141 |
]
|
142 |
|
143 |
-
def
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pip install bs4 syntok
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
import os
|
4 |
+
import random
|
|
|
5 |
|
6 |
import datasets
|
7 |
|
8 |
+
import numpy as np
|
9 |
+
from bs4 import BeautifulSoup, ResultSet
|
10 |
+
from syntok.tokenizer import Tokenizer
|
11 |
|
12 |
+
tokenizer = Tokenizer()
|
13 |
|
14 |
+
_CITATION = """\
|
15 |
+
@InProceedings{Kocabiyikoglu2022,
|
16 |
+
author = "Alican Kocabiyikoglu and Fran{\c c}ois Portet and Prudence Gibert and Hervé Blanchon and Jean-Marc Babouchkine and Gaëtan Gavazzi",
|
17 |
+
title = "A Spoken Drug Prescription Dataset in French for Spoken Language Understanding",
|
18 |
+
booktitle = "13th Language Resources and Evaluation Conference (LREC 2022)",
|
19 |
+
year = "2022",
|
20 |
+
location = "Marseille, France"
|
21 |
+
}
|
22 |
+
"""
|
23 |
|
24 |
+
_DESCRIPTION = """\
|
25 |
+
PxSLU is to the best of our knowledge, the first spoken medical drug prescriptions corpus to be distributed. It contains 4 hours of transcribed
|
26 |
+
and annotated dialogues of drug prescriptions in French acquired through an experiment with 55 participants experts and non-experts in drug prescriptions.
|
27 |
|
28 |
+
The automatic transcriptions were verified by human effort and aligned with semantic labels to allow training of NLP models. The data acquisition
|
29 |
+
protocol was reviewed by medical experts and permit free distribution without breach of privacy and regulation.
|
30 |
+
|
31 |
+
Overview of the Corpus
|
32 |
|
33 |
+
The experiment has been performed in wild conditions with naive participants and medical experts. In total, the dataset includes 1981 recordings
|
34 |
+
of 55 participants (38% non-experts, 25% doctors, 36% medical practitioners), manually transcribed and semantically annotated.
|
35 |
"""
|
36 |
|
37 |
+
_URL = "https://zenodo.org/record/6524162/files/pxslu.zip?download=1"
|
|
|
38 |
|
39 |
+
class PxCorpus(datasets.GeneratorBasedBuilder):
|
40 |
|
41 |
BUILDER_CONFIGS = [
|
42 |
+
datasets.BuilderConfig(name=f"default", version="1.0.0", description=f"PxCorpus data"),
|
43 |
]
|
44 |
|
45 |
DEFAULT_CONFIG_NAME = "default"
|
46 |
|
47 |
def _info(self):
|
48 |
+
|
49 |
features = datasets.Features(
|
50 |
{
|
51 |
+
"id": datasets.Value("string"),
|
52 |
+
"text": datasets.Value("string"),
|
53 |
+
"label": datasets.features.ClassLabel(
|
54 |
+
names=["medical_prescription", "negate", "none", "replace"],
|
|
|
|
|
55 |
),
|
56 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
57 |
+
"ner_tags": datasets.Sequence(
|
58 |
+
datasets.features.ClassLabel(
|
59 |
+
names=['O', 'B-A', 'B-cma_event', 'B-d_dos_form', 'B-d_dos_form_ext', 'B-d_dos_up', 'B-d_dos_val', 'B-dos_cond', 'B-dos_uf', 'B-dos_val', 'B-drug', 'B-dur_ut', 'B-dur_val', 'B-fasting', 'B-freq_days', 'B-freq_int_v1', 'B-freq_int_v1_ut', 'B-freq_int_v2', 'B-freq_int_v2_ut', 'B-freq_startday', 'B-freq_ut', 'B-freq_val', 'B-inn', 'B-max_unit_uf', 'B-max_unit_ut', 'B-max_unit_val', 'B-min_gap_ut', 'B-min_gap_val', 'B-qsp_ut', 'B-qsp_val', 'B-re_ut', 'B-re_val', 'B-rhythm_hour', 'B-rhythm_perday', 'B-rhythm_rec_ut', 'B-rhythm_rec_val', 'B-rhythm_tdte', 'B-roa', 'I-cma_event', 'I-d_dos_form', 'I-d_dos_form_ext', 'I-d_dos_up', 'I-d_dos_val', 'I-dos_cond', 'I-dos_uf', 'I-dos_val', 'I-drug', 'I-fasting', 'I-freq_startday', 'I-inn', 'I-rhythm_tdte', 'I-roa'],
|
60 |
+
),
|
61 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
}
|
63 |
)
|
64 |
|
65 |
return datasets.DatasetInfo(
|
66 |
description=_DESCRIPTION,
|
67 |
features=features,
|
|
|
|
|
68 |
citation=_CITATION,
|
69 |
+
supervised_keys=None,
|
70 |
)
|
71 |
|
72 |
def _split_generators(self, dl_manager):
|
|
|
73 |
|
74 |
data_dir = dl_manager.download_and_extract(_URL)
|
|
|
|
|
|
|
75 |
|
76 |
+
print(data_dir)
|
77 |
+
|
78 |
return [
|
79 |
datasets.SplitGenerator(
|
80 |
name=datasets.Split.TRAIN,
|
81 |
gen_kwargs={
|
82 |
+
"filepath_1": os.path.join(data_dir, "seq.in"),
|
83 |
+
"filepath_2": os.path.join(data_dir, "seq.label"),
|
84 |
+
"filepath_3": os.path.join(data_dir, "PxSLU_conll.txt"),
|
85 |
+
"split": "train",
|
86 |
},
|
87 |
),
|
88 |
datasets.SplitGenerator(
|
89 |
name=datasets.Split.VALIDATION,
|
90 |
gen_kwargs={
|
91 |
+
"filepath_1": os.path.join(data_dir, "seq.in"),
|
92 |
+
"filepath_2": os.path.join(data_dir, "seq.label"),
|
93 |
+
"filepath_3": os.path.join(data_dir, "PxSLU_conll.txt"),
|
94 |
+
"split": "validation",
|
95 |
},
|
96 |
),
|
97 |
datasets.SplitGenerator(
|
98 |
name=datasets.Split.TEST,
|
99 |
gen_kwargs={
|
100 |
+
"filepath_1": os.path.join(data_dir, "seq.in"),
|
101 |
+
"filepath_2": os.path.join(data_dir, "seq.label"),
|
102 |
+
"filepath_3": os.path.join(data_dir, "PxSLU_conll.txt"),
|
103 |
+
"split": "test",
|
104 |
},
|
105 |
),
|
106 |
]
|
107 |
|
108 |
+
def getTokenTags(self, document):
|
109 |
+
|
110 |
+
tokens = []
|
111 |
+
ner_tags = []
|
112 |
+
|
113 |
+
for pair in document.split("\n"):
|
114 |
+
|
115 |
+
if len(pair) <= 0:
|
116 |
+
continue
|
117 |
+
|
118 |
+
text, label = pair.split("\t")
|
119 |
+
tokens.append(text)
|
120 |
+
ner_tags.append(label)
|
121 |
+
|
122 |
+
return tokens, ner_tags
|
123 |
+
|
124 |
+
def _generate_examples(self, filepath_1, filepath_2, filepath_3, split):
|
125 |
+
|
126 |
+
key = 0
|
127 |
+
all_res = []
|
128 |
+
|
129 |
+
f_seq_in = open(filepath_1, "r")
|
130 |
+
seq_in = f_seq_in.read().split("\n")
|
131 |
+
f_seq_in.close()
|
132 |
+
|
133 |
+
f_seq_label = open(filepath_2, "r")
|
134 |
+
seq_label = f_seq_label.read().split("\n")
|
135 |
+
f_seq_label.close()
|
136 |
+
|
137 |
+
f_in_ner = open(filepath_3, "r")
|
138 |
+
docs = f_in_ner.read().split("\n\n")
|
139 |
+
f_in_ner.close()
|
140 |
+
|
141 |
+
for idx, doc in enumerate(docs):
|
142 |
+
|
143 |
+
text = seq_in[idx]
|
144 |
+
label = seq_label[idx]
|
145 |
+
|
146 |
+
tokens, ner_tags = self.getTokenTags(docs[idx])
|
147 |
+
|
148 |
+
if len(text) <= 0 or len(label) <= 0:
|
149 |
+
continue
|
150 |
+
|
151 |
+
all_res.append({
|
152 |
+
"id": key,
|
153 |
+
"text": text,
|
154 |
+
"label": label,
|
155 |
+
"tokens": tokens,
|
156 |
+
"ner_tags": ner_tags,
|
157 |
+
})
|
158 |
+
|
159 |
+
key += 1
|
160 |
+
|
161 |
+
ids = [r["id"] for r in all_res]
|
162 |
+
|
163 |
+
random.seed(4)
|
164 |
+
random.shuffle(ids)
|
165 |
+
random.shuffle(ids)
|
166 |
+
random.shuffle(ids)
|
167 |
+
|
168 |
+
train, validation, test = np.split(ids, [int(len(ids)*0.70), int(len(ids)*0.80)])
|
169 |
+
|
170 |
+
if split == "train":
|
171 |
+
allowed_ids = list(train)
|
172 |
+
elif split == "validation":
|
173 |
+
allowed_ids = list(validation)
|
174 |
+
elif split == "test":
|
175 |
+
allowed_ids = list(test)
|
176 |
+
|
177 |
+
for r in all_res:
|
178 |
+
if r["id"] in allowed_ids:
|
179 |
+
yield r["id"], r
|