Datasets:

Sub-tasks:
parsing
Languages:
English
Multilinguality:
monolingual
Size Categories:
unknown
Language Creators:
found
Annotations Creators:
expert-generated
Source Datasets:
original
File size: 12,257 Bytes
8d2e942
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import glob
import logging
from dataclasses import dataclass
from os import listdir, path
from typing import Dict, List, Optional

import datasets
from datasets import BuilderConfig, DatasetInfo, Features, Sequence, SplitGenerator, Value

logger = logging.getLogger(__name__)


@dataclass
class BratConfig(BuilderConfig):
    """BuilderConfig for BRAT."""

    url: str = None  # type: ignore
    description: Optional[str] = None
    citation: Optional[str] = None
    homepage: Optional[str] = None

    subdirectory_mapping: Optional[Dict[str, str]] = None
    file_name_blacklist: Optional[List[str]] = None
    ann_file_extension: str = "ann"
    txt_file_extension: str = "txt"


class Brat(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIG_CLASS = BratConfig

    def _info(self):
        return DatasetInfo(
            description=self.config.description,
            citation=self.config.citation,
            homepage=self.config.homepage,
            features=Features(
                {
                    "context": Value("string"),
                    "file_name": Value("string"),
                    "spans": Sequence(
                        {
                            "id": Value("string"),
                            "type": Value("string"),
                            "locations": Sequence(
                                {
                                    "start": Value("int32"),
                                    "end": Value("int32"),
                                }
                            ),
                            "text": Value("string"),
                        }
                    ),
                    "relations": Sequence(
                        {
                            "id": Value("string"),
                            "type": Value("string"),
                            "arguments": Sequence(
                                {"type": Value("string"), "target": Value("string")}
                            ),
                        }
                    ),
                    "equivalence_relations": Sequence(
                        {
                            "type": Value("string"),
                            "targets": Sequence(Value("string")),
                        }
                    ),
                    "events": Sequence(
                        {
                            "id": Value("string"),
                            "type": Value("string"),
                            "trigger": Value("string"),
                            "arguments": Sequence(
                                {"type": Value("string"), "target": Value("string")}
                            ),
                        }
                    ),
                    "attributions": Sequence(
                        {
                            "id": Value("string"),
                            "type": Value("string"),
                            "target": Value("string"),
                            "value": Value("string"),
                        }
                    ),
                    "normalizations": Sequence(
                        {
                            "id": Value("string"),
                            "type": Value("string"),
                            "target": Value("string"),
                            "resource_id": Value("string"),
                            "entity_id": Value("string"),
                        }
                    ),
                    "notes": Sequence(
                        {
                            "id": Value("string"),
                            "type": Value("string"),
                            "target": Value("string"),
                            "note": Value("string"),
                        }
                    ),
                }
            ),
        )

    @staticmethod
    def _get_location(location_string):
        parts = location_string.split(" ")
        assert (
            len(parts) == 2
        ), f"Wrong number of entries in location string. Expected 2, but found: {parts}"
        return {"start": int(parts[0]), "end": int(parts[1])}

    @staticmethod
    def _get_span_annotation(annotation_line):
        """
        example input:
        T1	Organization 0 4	Sony
        """

        _id, remaining, text = annotation_line.split("\t", maxsplit=2)
        _type, locations = remaining.split(" ", maxsplit=1)
        return {
            "id": _id,
            "text": text,
            "type": _type,
            "locations": [Brat._get_location(loc) for loc in locations.split(";")],
        }

    @staticmethod
    def _get_event_annotation(annotation_line):
        """
        example input:
        E1	MERGE-ORG:T2 Org1:T1 Org2:T3
        """
        _id, remaining = annotation_line.strip().split("\t")
        args = [dict(zip(["type", "target"], a.split(":"))) for a in remaining.split(" ")]
        return {
            "id": _id,
            "type": args[0]["type"],
            "trigger": args[0]["target"],
            "arguments": args[1:],
        }

    @staticmethod
    def _get_relation_annotation(annotation_line):
        """
        example input:
        R1	Origin Arg1:T3 Arg2:T4
        """

        _id, remaining = annotation_line.strip().split("\t")
        _type, remaining = remaining.split(" ", maxsplit=1)
        args = [dict(zip(["type", "target"], a.split(":"))) for a in remaining.split(" ")]
        return {"id": _id, "type": _type, "arguments": args}

    @staticmethod
    def _get_equivalence_relation_annotation(annotation_line):
        """
        example input:
        *	Equiv T1 T2 T3
        """
        _, remaining = annotation_line.strip().split("\t")
        parts = remaining.split(" ")
        return {"type": parts[0], "targets": parts[1:]}

    @staticmethod
    def _get_attribute_annotation(annotation_line):
        """
        example input (binary: implicit value is True, if present, False otherwise):
        A1	Negation E1
        example input (multi-value: explicit value)
        A2	Confidence E2 L1
        """

        _id, remaining = annotation_line.strip().split("\t")
        parts = remaining.split(" ")
        # if no value is present, it is implicitly "true"
        if len(parts) == 2:
            parts.append("true")
        return {
            "id": _id,
            "type": parts[0],
            "target": parts[1],
            "value": parts[2],
        }

    @staticmethod
    def _get_normalization_annotation(annotation_line):
        """
        example input:
        N1	Reference T1 Wikipedia:534366	Barack Obama
        """
        _id, remaining, text = annotation_line.split("\t", maxsplit=2)
        _type, target, ref = remaining.split(" ")
        res_id, ent_id = ref.split(":")
        return {
            "id": _id,
            "type": _type,
            "target": target,
            "resource_id": res_id,
            "entity_id": ent_id,
        }

    @staticmethod
    def _get_note_annotation(annotation_line):
        """
        example input:
        #1	AnnotatorNotes T1	this annotation is suspect
        """
        _id, remaining, note = annotation_line.split("\t", maxsplit=2)
        _type, target = remaining.split(" ")
        return {
            "id": _id,
            "type": _type,
            "target": target,
            "note": note,
        }

    @staticmethod
    def _read_annotation_file(filename):
        """
        reads a BRAT v1.3 annotations file (see https://brat.nlplab.org/standoff.html)
        """

        res = {
            "spans": [],
            "events": [],
            "relations": [],
            "equivalence_relations": [],
            "attributions": [],
            "normalizations": [],
            "notes": [],
        }

        with open(filename) as file:
            for i, line in enumerate(file):
                if len(line.strip()) == 0:
                    continue
                ann_type = line[0]

                # strip away the new line character
                if line.endswith("\n"):
                    line = line[:-1]

                if ann_type == "T":
                    res["spans"].append(Brat._get_span_annotation(line))
                elif ann_type == "E":
                    res["events"].append(Brat._get_event_annotation(line))
                elif ann_type == "R":
                    res["relations"].append(Brat._get_relation_annotation(line))
                elif ann_type == "*":
                    res["equivalence_relations"].append(
                        Brat._get_equivalence_relation_annotation(line)
                    )
                elif ann_type in ["A", "M"]:
                    res["attributions"].append(Brat._get_attribute_annotation(line))
                elif ann_type == "N":
                    res["normalizations"].append(Brat._get_normalization_annotation(line))
                elif ann_type == "#":
                    res["notes"].append(Brat._get_note_annotation(line))
                else:
                    raise ValueError(
                        f'unknown BRAT annotation id type: "{line}" (from file {filename} @line {i}). '
                        f"Annotation ids have to start with T (spans), E (events), R (relations), "
                        f"A (attributions), or N (normalizations). See "
                        f"https://brat.nlplab.org/standoff.html for the BRAT annotation file "
                        f"specification."
                    )
        return res

    def _generate_examples(self, files=None, directory=None):
        """Read context (.txt) and annotation (.ann) files."""
        if files is None:
            assert (
                directory is not None
            ), "If files is None, directory has to be provided, but it is also None."
            _files = glob.glob(f"{directory}/*.{self.config.ann_file_extension}")
            files = sorted(path.splitext(fn)[0] for fn in _files)

        for filename in files:
            basename = path.basename(filename)
            if (
                self.config.file_name_blacklist is not None
                and basename in self.config.file_name_blacklist
            ):
                logger.info(f"skip annotation file: {basename} (blacklisted)")
                continue

            ann_fn = f"{filename}.{self.config.ann_file_extension}"
            brat_annotations = Brat._read_annotation_file(ann_fn)

            txt_fn = f"{filename}.{self.config.txt_file_extension}"
            txt_content = open(txt_fn).read()
            brat_annotations["context"] = txt_content
            brat_annotations["file_name"] = basename

            yield basename, brat_annotations

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""

        subdirectory_mapping = self.config.subdirectory_mapping

        # since subclasses of BuilderConfig are not allowed to define
        # attributes without defaults, check here
        assert self.config.url is not None, "data url not specified"

        # if url points to a local directory, just point to that
        if path.exists(self.config.url) and path.isdir(self.config.url):
            data_dir = self.config.url
        # otherwise, download and extract
        else:
            data_dir = dl_manager.download_and_extract(self.config.url)
        logging.info(f"load from data dir: {data_dir}")

        # if no subdirectory mapping is provided, ...
        if subdirectory_mapping is None:
            # ... use available subdirectories as split names ...
            subdirs = [f for f in listdir(data_dir) if path.isdir(path.join(data_dir, f))]
            if len(subdirs) > 0:
                subdirectory_mapping = {subdir: subdir for subdir in subdirs}
            else:
                # ... otherwise, default to a single train split with the base directory
                subdirectory_mapping = {"": "train"}

        return [
            SplitGenerator(
                name=split,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "directory": path.join(data_dir, subdir),
                },
            )
            for subdir, split in subdirectory_mapping.items()
        ]