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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
@report{Magnini2021, \
|
| 16 |
+
author = {Bernardo Magnini and Begoña Altuna and Alberto Lavelli and Manuela Speranza \
|
| 17 |
+
and Roberto Zanoli and Fondazione Bruno Kessler}, \
|
| 18 |
+
keywords = {Clinical data,clinical enti-ties,corpus,multilingual,temporal information}, \
|
| 19 |
+
title = {The E3C Project: \
|
| 20 |
+
European Clinical Case Corpus El proyecto E3C: European Clinical Case Corpus}, \
|
| 21 |
+
url = {https://uts.nlm.nih.gov/uts/umls/home}, \
|
| 22 |
+
year = {2021}, \
|
| 23 |
+
}
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
_DESCRIPTION = """\
|
| 27 |
+
E3C is a freely available multilingual corpus (English, French, Italian, Spanish, and Basque) \
|
| 28 |
+
of semantically annotated clinical narratives to allow for the linguistic analysis, benchmarking, \
|
| 29 |
+
and training of information extraction systems. It consists of two types of annotations: \
|
| 30 |
+
(i) clinical entities (e.g., pathologies), (ii) temporal information and factuality (e.g., events). \
|
| 31 |
+
Researchers can use the benchmark training and test splits of our corpus to develop and test \
|
| 32 |
+
their own models.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
_URL = "https://github.com/hltfbk/E3C-Corpus/archive/refs/tags/v2.0.0.zip"
|
| 36 |
+
|
| 37 |
+
_LANGUAGES = ["English","Spanish","Basque","French","Italian"]
|
| 38 |
+
|
| 39 |
+
class E3C(datasets.GeneratorBasedBuilder):
|
| 40 |
+
|
| 41 |
+
BUILDER_CONFIGS = [
|
| 42 |
+
datasets.BuilderConfig(name=f"{lang}", version="1.0.0", description=f"The {lang} subset of the E3C corpus") for lang in _LANGUAGES
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
def _info(self):
|
| 46 |
+
|
| 47 |
+
features = datasets.Features(
|
| 48 |
+
{
|
| 49 |
+
"id": datasets.Value("string"),
|
| 50 |
+
"text": datasets.Value("string"),
|
| 51 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
| 52 |
+
"ner_clinical_tags": datasets.Sequence(
|
| 53 |
+
datasets.features.ClassLabel(
|
| 54 |
+
names=["O","B-CLINENTITY","I-CLINENTITY"],
|
| 55 |
+
),
|
| 56 |
+
),
|
| 57 |
+
"ner_temporal_tags": datasets.Sequence(
|
| 58 |
+
datasets.features.ClassLabel(
|
| 59 |
+
names=["O", "B-EVENT", "B-ACTOR", "B-BODYPART", "B-TIMEX3", "B-RML", "I-EVENT", "I-ACTOR", "I-BODYPART", "I-TIMEX3", "I-RML"],
|
| 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 |
+
return [
|
| 77 |
+
datasets.SplitGenerator(
|
| 78 |
+
name=datasets.Split.TRAIN,
|
| 79 |
+
gen_kwargs={
|
| 80 |
+
"filepath": os.path.join(data_dir, "E3C-Corpus-2.0.0/data_annotation", self.config.name, "layer1"),
|
| 81 |
+
"split": "train",
|
| 82 |
+
},
|
| 83 |
+
),
|
| 84 |
+
datasets.SplitGenerator(
|
| 85 |
+
name=datasets.Split.VALIDATION,
|
| 86 |
+
gen_kwargs={
|
| 87 |
+
"filepath": os.path.join(data_dir, "E3C-Corpus-2.0.0/data_annotation", self.config.name, "layer1"),
|
| 88 |
+
"split": "validation",
|
| 89 |
+
},
|
| 90 |
+
),
|
| 91 |
+
datasets.SplitGenerator(
|
| 92 |
+
name=datasets.Split.TEST,
|
| 93 |
+
gen_kwargs={
|
| 94 |
+
"filepath": os.path.join(data_dir, "E3C-Corpus-2.0.0/data_validation", self.config.name, "layer2"),
|
| 95 |
+
"split": "test",
|
| 96 |
+
},
|
| 97 |
+
),
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
@staticmethod
|
| 101 |
+
def get_annotations(entities: ResultSet, text: str) -> list:
|
| 102 |
+
|
| 103 |
+
return [[
|
| 104 |
+
int(entity.get("begin")),
|
| 105 |
+
int(entity.get("end")),
|
| 106 |
+
text[int(entity.get("begin")) : int(entity.get("end"))],
|
| 107 |
+
] for entity in entities]
|
| 108 |
+
|
| 109 |
+
def get_clinical_annotations(self, entities: ResultSet, text: str) -> list:
|
| 110 |
+
|
| 111 |
+
return [[
|
| 112 |
+
int(entity.get("begin")),
|
| 113 |
+
int(entity.get("end")),
|
| 114 |
+
text[int(entity.get("begin")) : int(entity.get("end"))],
|
| 115 |
+
entity.get("entityID"),
|
| 116 |
+
] for entity in entities]
|
| 117 |
+
|
| 118 |
+
def get_parsed_data(self, filepath: str):
|
| 119 |
+
|
| 120 |
+
for root, _, files in os.walk(filepath):
|
| 121 |
+
|
| 122 |
+
for file in files:
|
| 123 |
+
|
| 124 |
+
with open(f"{root}/{file}") as soup_file:
|
| 125 |
+
|
| 126 |
+
soup = BeautifulSoup(soup_file, "xml")
|
| 127 |
+
text = soup.find("cas:Sofa").get("sofaString")
|
| 128 |
+
|
| 129 |
+
yield {
|
| 130 |
+
"CLINENTITY": self.get_clinical_annotations(soup.find_all("custom:CLINENTITY"), text),
|
| 131 |
+
"EVENT": self.get_annotations(soup.find_all("custom:EVENT"), text),
|
| 132 |
+
"ACTOR": self.get_annotations(soup.find_all("custom:ACTOR"), text),
|
| 133 |
+
"BODYPART": self.get_annotations(soup.find_all("custom:BODYPART"), text),
|
| 134 |
+
"TIMEX3": self.get_annotations(soup.find_all("custom:TIMEX3"), text),
|
| 135 |
+
"RML": self.get_annotations(soup.find_all("custom:RML"), text),
|
| 136 |
+
"SENTENCE": self.get_annotations(soup.find_all("type4:Sentence"), text),
|
| 137 |
+
"TOKENS": self.get_annotations(soup.find_all("type4:Token"), text),
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
def _generate_examples(self, filepath, split):
|
| 141 |
+
|
| 142 |
+
all_res = []
|
| 143 |
+
|
| 144 |
+
key = 0
|
| 145 |
+
|
| 146 |
+
for content in self.get_parsed_data(filepath):
|
| 147 |
+
|
| 148 |
+
for sentence in content["SENTENCE"]:
|
| 149 |
+
|
| 150 |
+
tokens = [(
|
| 151 |
+
token.offset + sentence[0],
|
| 152 |
+
token.offset + sentence[0] + len(token.value),
|
| 153 |
+
token.value,
|
| 154 |
+
) for token in list(tokenizer.tokenize(sentence[-1]))]
|
| 155 |
+
|
| 156 |
+
filtered_tokens = list(
|
| 157 |
+
filter(
|
| 158 |
+
lambda token: token[0] >= sentence[0] and token[1] <= sentence[1],
|
| 159 |
+
tokens,
|
| 160 |
+
)
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
tokens_offsets = [
|
| 164 |
+
[token[0] - sentence[0], token[1] - sentence[0]] for token in filtered_tokens
|
| 165 |
+
]
|
| 166 |
+
|
| 167 |
+
clinical_labels = ["O"] * len(filtered_tokens)
|
| 168 |
+
clinical_cuid = ["CUI_LESS"] * len(filtered_tokens)
|
| 169 |
+
temporal_information_labels = ["O"] * len(filtered_tokens)
|
| 170 |
+
|
| 171 |
+
for entity_type in ["CLINENTITY","EVENT","ACTOR","BODYPART","TIMEX3","RML"]:
|
| 172 |
+
|
| 173 |
+
if len(content[entity_type]) != 0:
|
| 174 |
+
|
| 175 |
+
for entities in list(content[entity_type]):
|
| 176 |
+
|
| 177 |
+
annotated_tokens = [
|
| 178 |
+
idx_token
|
| 179 |
+
for idx_token, token in enumerate(filtered_tokens)
|
| 180 |
+
if token[0] >= entities[0] and token[1] <= entities[1]
|
| 181 |
+
]
|
| 182 |
+
|
| 183 |
+
for idx_token in annotated_tokens:
|
| 184 |
+
|
| 185 |
+
if entity_type == "CLINENTITY":
|
| 186 |
+
if idx_token == annotated_tokens[0]:
|
| 187 |
+
clinical_labels[idx_token] = f"B-{entity_type}"
|
| 188 |
+
else:
|
| 189 |
+
clinical_labels[idx_token] = f"I-{entity_type}"
|
| 190 |
+
clinical_cuid[idx_token] = entities[-1]
|
| 191 |
+
else:
|
| 192 |
+
if idx_token == annotated_tokens[0]:
|
| 193 |
+
temporal_information_labels[idx_token] = f"B-{entity_type}"
|
| 194 |
+
else:
|
| 195 |
+
temporal_information_labels[idx_token] = f"I-{entity_type}"
|
| 196 |
+
|
| 197 |
+
all_res.append({
|
| 198 |
+
"id": key,
|
| 199 |
+
"text": sentence[-1],
|
| 200 |
+
"tokens": list(map(lambda token: token[2], filtered_tokens)),
|
| 201 |
+
"ner_clinical_tags": clinical_labels,
|
| 202 |
+
"ner_temporal_tags": temporal_information_labels,
|
| 203 |
+
})
|
| 204 |
+
|
| 205 |
+
key += 1
|
| 206 |
+
|
| 207 |
+
if split != "test":
|
| 208 |
+
|
| 209 |
+
ids = [r["id"] for r in all_res]
|
| 210 |
+
|
| 211 |
+
random.seed(4)
|
| 212 |
+
random.shuffle(ids)
|
| 213 |
+
random.shuffle(ids)
|
| 214 |
+
random.shuffle(ids)
|
| 215 |
+
|
| 216 |
+
train, validation = np.split(ids, [int(len(ids)*0.8738)])
|
| 217 |
+
|
| 218 |
+
if split == "train":
|
| 219 |
+
allowed_ids = list(train)
|
| 220 |
+
elif split == "validation":
|
| 221 |
+
allowed_ids = list(validation)
|
| 222 |
+
|
| 223 |
+
for r in all_res:
|
| 224 |
+
if r["id"] in allowed_ids:
|
| 225 |
+
yield r["id"], r
|
| 226 |
+
else:
|
| 227 |
+
|
| 228 |
+
for r in all_res:
|
| 229 |
+
yield r["id"], r
|
test_e3c.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
|
| 5 |
+
dataset = load_dataset("./E3C.py", name="French")
|
| 6 |
+
print(dataset)
|
| 7 |
+
# print(dataset["train"][0])
|
| 8 |
+
print(json.dumps(dataset["train"][0], sort_keys=True, indent=4))
|