File size: 7,375 Bytes
d4f2789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import random

import datasets
import numpy as np

_CITATION = """\
@inproceedings{grabar-etal-2018-cas,
  title        = {{CAS}: {F}rench Corpus with Clinical Cases},
  author       = {Grabar, Natalia  and Claveau, Vincent  and Dalloux, Cl{\'e}ment},
  year         = 2018,
  month        = oct,
  booktitle    = {
    Proceedings of the Ninth International Workshop on Health Text Mining and
    Information Analysis
  },
  publisher    = {Association for Computational Linguistics},
  address      = {Brussels, Belgium},
  pages        = {122--128},
  doi          = {10.18653/v1/W18-5614},
  url          = {https://aclanthology.org/W18-5614},
  abstract     = {
    Textual corpora are extremely important for various NLP applications as
    they provide information necessary for creating, setting and testing these
    applications and the corresponding tools. They are also crucial for
    designing reliable methods and reproducible results. Yet, in some areas,
    such as the medical area, due to confidentiality or to ethical reasons, it
    is complicated and even impossible to access textual data representative of
    those produced in these areas. We propose the CAS corpus built with
    clinical cases, such as they are reported in the published scientific
    literature in French. We describe this corpus, currently containing over
    397,000 word occurrences, and the existing linguistic and semantic
    annotations.
  }
}
"""

_DESCRIPTION = """\
We manually annotated two corpora from the biomedical field. The ESSAI corpus \
contains clinical trial protocols in French. They were mainly obtained from the \
National Cancer Institute The typical protocol consists of two parts: the \
summary of the trial, which indicates the purpose of the trial and the methods \
applied; and a detailed description of the trial with the inclusion and \
exclusion criteria. The CAS corpus contains clinical cases published in \
scientific literature and training material. They are published in different \
journals from French-speaking countries (France, Belgium, Switzerland, Canada, \
African countries, tropical countries) and are related to various medical \
specialties (cardiology, urology, oncology, obstetrics, pulmonology, \
gastro-enterology). The purpose of clinical cases is to describe clinical \
situations of patients. Hence, their content is close to the content of clinical \
narratives (description of diagnoses, treatments or procedures, evolution, \
family history, expected audience, etc.). In clinical cases, the negation is \
frequently used for describing the patient signs, symptoms, and diagnosis. \
Speculation is present as well but less frequently.
This version only contain the annotated CAS corpus
"""

_HOMEPAGE = "https://clementdalloux.fr/?page_id=28"

_LICENSE = 'Data User Agreement'

class CAS(datasets.GeneratorBasedBuilder):

    DEFAULT_CONFIG_NAME = "source"

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="source", version="1.0.0", description="The CAS corpora"),
    ]

    def _info(self):

        features = datasets.Features(
            {
                "id": datasets.Value("string"),
                "document_id": datasets.Value("string"),
                "tokens": [datasets.Value("string")],
                "lemmas": [datasets.Value("string")],
                "pos_tags": [datasets.features.ClassLabel(
                    names = ['VER:ppre', 'VER:infi', 'VER:impf', 'VER:simp', 'PUN', 'DET:POS', 'ADV', 'DET:ART', 'PRO:DEM', 'INT', 'VER:futu', 'VER:subp', 'VER:cond', 'VER:pper', 'KON', 'NAM', 'PRO:IND', 'VER:con', 'PRP', 'SYM', 'SENT', 'PUN:cit', 'VER:pres', 'PRP:det', 'PRO:REL', 'PRO:PER', 'VER:subi', 'ADJ', 'NUM', 'NOM', 'ABR'],
                )],
                "label": datasets.features.ClassLabel(
                    names = ['negation', 'speculation'],
                ),
            }
        )

        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={
                    "datadir": data_dir,
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "datadir": data_dir,
                    "split": "validation",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "datadir": data_dir,
                    "split": "test",
                },
            ),
        ]

    def _generate_examples(self, datadir, split):

        all_res = []

        key = 0

        for file in ["CAS_neg.txt", "CAS_spec.txt"]:

            label = "negation" if "neg" in file else "speculation"
            id_docs = []
            id_words = []
            words = []
            lemmas = []
            POS_tags = []

            with open(os.path.join(datadir, file)) as f:

                for line in f.readlines():

                    if len(line.split("\t")) < 5:
                        continue

                    id_doc, id_word, word, lemma, tag = line.split("\t")[0:5]

                    id_docs.append(id_doc)
                    id_words.append(id_word)
                    words.append(word)
                    lemmas.append(lemma)
                    POS_tags.append(tag)

            dic = {
                "id_docs":  np.array(list(map(int, id_docs))),
                "id_words": id_words,
                "words":    words,
                "lemmas":   lemmas,
                "POS_tags": POS_tags,
            }

            for doc_id in set(dic["id_docs"]):

                indexes = np.argwhere(dic["id_docs"] == doc_id)[:, 0]
                tokens = [dic["words"][id] for id in indexes]
                text_lemmas = [dic["lemmas"][id] for id in indexes]
                pos_tags = [dic["POS_tags"][id] for id in indexes]

                all_res.append({
                    "id": str(key),
                    "document_id": str(doc_id),
                    "tokens": tokens,
                    "lemmas": text_lemmas,
                    "pos_tags": pos_tags,
                    "label": label,
                })

                key += 1

        ids = [r["id"] for r in all_res]

        random.seed(4)
        random.shuffle(ids)
        random.shuffle(ids)
        random.shuffle(ids)

        train, validation, test = np.split(ids, [int(len(ids)*0.70), int(len(ids)*0.80)])

        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["id"] in allowed_ids:
                yield r["id"], r