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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import glob
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, List
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import datasets
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def remove_prefix(a: str, prefix: str) -> str:
|
| 10 |
+
if a.startswith(prefix):
|
| 11 |
+
a = a[len(prefix) :]
|
| 12 |
+
return a
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def parse_brat_file(
|
| 16 |
+
txt_file: Path,
|
| 17 |
+
annotation_file_suffixes: List[str] = None,
|
| 18 |
+
parse_notes: bool = False,
|
| 19 |
+
) -> Dict:
|
| 20 |
+
"""
|
| 21 |
+
Parse a brat file into the schema defined below.
|
| 22 |
+
`txt_file` should be the path to the brat '.txt' file you want to parse, e.g. 'data/1234.txt'
|
| 23 |
+
Assumes that the annotations are contained in one or more of the corresponding '.a1', '.a2' or '.ann' files,
|
| 24 |
+
e.g. 'data/1234.ann' or 'data/1234.a1' and 'data/1234.a2'.
|
| 25 |
+
Will include annotator notes, when `parse_notes == True`.
|
| 26 |
+
brat_features = datasets.Features(
|
| 27 |
+
{
|
| 28 |
+
"id": datasets.Value("string"),
|
| 29 |
+
"document_id": datasets.Value("string"),
|
| 30 |
+
"text": datasets.Value("string"),
|
| 31 |
+
"text_bound_annotations": [ # T line in brat, e.g. type or event trigger
|
| 32 |
+
{
|
| 33 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
| 34 |
+
"text": datasets.Sequence(datasets.Value("string")),
|
| 35 |
+
"type": datasets.Value("string"),
|
| 36 |
+
"id": datasets.Value("string"),
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
"events": [ # E line in brat
|
| 40 |
+
{
|
| 41 |
+
"trigger": datasets.Value(
|
| 42 |
+
"string"
|
| 43 |
+
), # refers to the text_bound_annotation of the trigger,
|
| 44 |
+
"id": datasets.Value("string"),
|
| 45 |
+
"type": datasets.Value("string"),
|
| 46 |
+
"arguments": datasets.Sequence(
|
| 47 |
+
{
|
| 48 |
+
"role": datasets.Value("string"),
|
| 49 |
+
"ref_id": datasets.Value("string"),
|
| 50 |
+
}
|
| 51 |
+
),
|
| 52 |
+
}
|
| 53 |
+
],
|
| 54 |
+
"relations": [ # R line in brat
|
| 55 |
+
{
|
| 56 |
+
"id": datasets.Value("string"),
|
| 57 |
+
"head": {
|
| 58 |
+
"ref_id": datasets.Value("string"),
|
| 59 |
+
"role": datasets.Value("string"),
|
| 60 |
+
},
|
| 61 |
+
"tail": {
|
| 62 |
+
"ref_id": datasets.Value("string"),
|
| 63 |
+
"role": datasets.Value("string"),
|
| 64 |
+
},
|
| 65 |
+
"type": datasets.Value("string"),
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
"equivalences": [ # Equiv line in brat
|
| 69 |
+
{
|
| 70 |
+
"id": datasets.Value("string"),
|
| 71 |
+
"ref_ids": datasets.Sequence(datasets.Value("string")),
|
| 72 |
+
}
|
| 73 |
+
],
|
| 74 |
+
"attributes": [ # M or A lines in brat
|
| 75 |
+
{
|
| 76 |
+
"id": datasets.Value("string"),
|
| 77 |
+
"type": datasets.Value("string"),
|
| 78 |
+
"ref_id": datasets.Value("string"),
|
| 79 |
+
"value": datasets.Value("string"),
|
| 80 |
+
}
|
| 81 |
+
],
|
| 82 |
+
"normalizations": [ # N lines in brat
|
| 83 |
+
{
|
| 84 |
+
"id": datasets.Value("string"),
|
| 85 |
+
"type": datasets.Value("string"),
|
| 86 |
+
"ref_id": datasets.Value("string"),
|
| 87 |
+
"resource_name": datasets.Value(
|
| 88 |
+
"string"
|
| 89 |
+
), # Name of the resource, e.g. "Wikipedia"
|
| 90 |
+
"cuid": datasets.Value(
|
| 91 |
+
"string"
|
| 92 |
+
), # ID in the resource, e.g. 534366
|
| 93 |
+
"text": datasets.Value(
|
| 94 |
+
"string"
|
| 95 |
+
), # Human readable description/name of the entity, e.g. "Barack Obama"
|
| 96 |
+
}
|
| 97 |
+
],
|
| 98 |
+
### OPTIONAL: Only included when `parse_notes == True`
|
| 99 |
+
"notes": [ # # lines in brat
|
| 100 |
+
{
|
| 101 |
+
"id": datasets.Value("string"),
|
| 102 |
+
"type": datasets.Value("string"),
|
| 103 |
+
"ref_id": datasets.Value("string"),
|
| 104 |
+
"text": datasets.Value("string"),
|
| 105 |
+
}
|
| 106 |
+
],
|
| 107 |
+
},
|
| 108 |
+
)
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
example = {}
|
| 112 |
+
example["document_id"] = txt_file.with_suffix("").name
|
| 113 |
+
with txt_file.open() as f:
|
| 114 |
+
example["text"] = f.read()
|
| 115 |
+
|
| 116 |
+
# If no specific suffixes of the to-be-read annotation files are given - take standard suffixes
|
| 117 |
+
# for event extraction
|
| 118 |
+
if annotation_file_suffixes is None:
|
| 119 |
+
annotation_file_suffixes = [".a1", ".a2", ".ann"]
|
| 120 |
+
|
| 121 |
+
if len(annotation_file_suffixes) == 0:
|
| 122 |
+
raise AssertionError(
|
| 123 |
+
"At least one suffix for the to-be-read annotation files should be given!"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
ann_lines = []
|
| 127 |
+
for suffix in annotation_file_suffixes:
|
| 128 |
+
annotation_file = txt_file.with_suffix(suffix)
|
| 129 |
+
if annotation_file.exists():
|
| 130 |
+
with annotation_file.open() as f:
|
| 131 |
+
ann_lines.extend(f.readlines())
|
| 132 |
+
|
| 133 |
+
example["text_bound_annotations"] = []
|
| 134 |
+
example["events"] = []
|
| 135 |
+
example["relations"] = []
|
| 136 |
+
example["equivalences"] = []
|
| 137 |
+
example["attributes"] = []
|
| 138 |
+
example["normalizations"] = []
|
| 139 |
+
|
| 140 |
+
if parse_notes:
|
| 141 |
+
example["notes"] = []
|
| 142 |
+
|
| 143 |
+
for line in ann_lines:
|
| 144 |
+
line = line.strip()
|
| 145 |
+
if not line:
|
| 146 |
+
continue
|
| 147 |
+
|
| 148 |
+
if line.startswith("T"): # Text bound
|
| 149 |
+
ann = {}
|
| 150 |
+
fields = line.split("\t")
|
| 151 |
+
|
| 152 |
+
ann["id"] = fields[0]
|
| 153 |
+
ann["type"] = fields[1].split()[0]
|
| 154 |
+
ann["offsets"] = []
|
| 155 |
+
span_str = remove_prefix(fields[1], (ann["type"] + " "))
|
| 156 |
+
text = fields[2]
|
| 157 |
+
for span in span_str.split(";"):
|
| 158 |
+
start, end = span.split()
|
| 159 |
+
ann["offsets"].append([int(start), int(end)])
|
| 160 |
+
|
| 161 |
+
# Heuristically split text of discontiguous entities into chunks
|
| 162 |
+
ann["text"] = []
|
| 163 |
+
if len(ann["offsets"]) > 1:
|
| 164 |
+
i = 0
|
| 165 |
+
for start, end in ann["offsets"]:
|
| 166 |
+
chunk_len = end - start
|
| 167 |
+
ann["text"].append(text[i : chunk_len + i])
|
| 168 |
+
i += chunk_len
|
| 169 |
+
while i < len(text) and text[i] == " ":
|
| 170 |
+
i += 1
|
| 171 |
+
else:
|
| 172 |
+
ann["text"] = [text]
|
| 173 |
+
|
| 174 |
+
example["text_bound_annotations"].append(ann)
|
| 175 |
+
|
| 176 |
+
elif line.startswith("E"):
|
| 177 |
+
ann = {}
|
| 178 |
+
fields = line.split("\t")
|
| 179 |
+
|
| 180 |
+
ann["id"] = fields[0]
|
| 181 |
+
|
| 182 |
+
ann["type"], ann["trigger"] = fields[1].split()[0].split(":")
|
| 183 |
+
|
| 184 |
+
ann["arguments"] = []
|
| 185 |
+
for role_ref_id in fields[1].split()[1:]:
|
| 186 |
+
argument = {
|
| 187 |
+
"role": (role_ref_id.split(":"))[0],
|
| 188 |
+
"ref_id": (role_ref_id.split(":"))[1],
|
| 189 |
+
}
|
| 190 |
+
ann["arguments"].append(argument)
|
| 191 |
+
|
| 192 |
+
example["events"].append(ann)
|
| 193 |
+
|
| 194 |
+
elif line.startswith("R"):
|
| 195 |
+
ann = {}
|
| 196 |
+
fields = line.split("\t")
|
| 197 |
+
|
| 198 |
+
ann["id"] = fields[0]
|
| 199 |
+
ann["type"] = fields[1].split()[0]
|
| 200 |
+
|
| 201 |
+
ann["head"] = {
|
| 202 |
+
"role": fields[1].split()[1].split(":")[0],
|
| 203 |
+
"ref_id": fields[1].split()[1].split(":")[1],
|
| 204 |
+
}
|
| 205 |
+
ann["tail"] = {
|
| 206 |
+
"role": fields[1].split()[2].split(":")[0],
|
| 207 |
+
"ref_id": fields[1].split()[2].split(":")[1],
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
example["relations"].append(ann)
|
| 211 |
+
|
| 212 |
+
# '*' seems to be the legacy way to mark equivalences,
|
| 213 |
+
# but I couldn't find any info on the current way
|
| 214 |
+
# this might have to be adapted dependent on the brat version
|
| 215 |
+
# of the annotation
|
| 216 |
+
elif line.startswith("*"):
|
| 217 |
+
ann = {}
|
| 218 |
+
fields = line.split("\t")
|
| 219 |
+
|
| 220 |
+
ann["id"] = fields[0]
|
| 221 |
+
ann["ref_ids"] = fields[1].split()[1:]
|
| 222 |
+
|
| 223 |
+
example["equivalences"].append(ann)
|
| 224 |
+
|
| 225 |
+
elif line.startswith("A") or line.startswith("M"):
|
| 226 |
+
ann = {}
|
| 227 |
+
fields = line.split("\t")
|
| 228 |
+
|
| 229 |
+
ann["id"] = fields[0]
|
| 230 |
+
|
| 231 |
+
info = fields[1].split()
|
| 232 |
+
ann["type"] = info[0]
|
| 233 |
+
ann["ref_id"] = info[1]
|
| 234 |
+
|
| 235 |
+
if len(info) > 2:
|
| 236 |
+
ann["value"] = info[2]
|
| 237 |
+
else:
|
| 238 |
+
ann["value"] = ""
|
| 239 |
+
|
| 240 |
+
example["attributes"].append(ann)
|
| 241 |
+
|
| 242 |
+
elif line.startswith("N"):
|
| 243 |
+
ann = {}
|
| 244 |
+
fields = line.split("\t")
|
| 245 |
+
|
| 246 |
+
ann["id"] = fields[0]
|
| 247 |
+
ann["text"] = fields[2]
|
| 248 |
+
|
| 249 |
+
info = fields[1].split()
|
| 250 |
+
|
| 251 |
+
ann["type"] = info[0]
|
| 252 |
+
ann["ref_id"] = info[1]
|
| 253 |
+
ann["resource_name"] = info[2].split(":")[0]
|
| 254 |
+
ann["cuid"] = info[2].split(":")[1]
|
| 255 |
+
example["normalizations"].append(ann)
|
| 256 |
+
|
| 257 |
+
elif parse_notes and line.startswith("#"):
|
| 258 |
+
ann = {}
|
| 259 |
+
fields = line.split("\t")
|
| 260 |
+
|
| 261 |
+
ann["id"] = fields[0]
|
| 262 |
+
ann["text"] = fields[2] if len(fields) == 3 else None
|
| 263 |
+
|
| 264 |
+
info = fields[1].split()
|
| 265 |
+
|
| 266 |
+
ann["type"] = info[0]
|
| 267 |
+
ann["ref_id"] = info[1]
|
| 268 |
+
example["notes"].append(ann)
|
| 269 |
+
|
| 270 |
+
return example
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
_CITATION = """\
|
| 274 |
+
@inproceedings{lauscher2018b,
|
| 275 |
+
title = {An argument-annotated corpus of scientific publications},
|
| 276 |
+
booktitle = {Proceedings of the 5th Workshop on Mining Argumentation},
|
| 277 |
+
publisher = {Association for Computational Linguistics},
|
| 278 |
+
author = {Lauscher, Anne and Glava\v{s}, Goran and Ponzetto, Simone Paolo},
|
| 279 |
+
address = {Brussels, Belgium},
|
| 280 |
+
year = {2018},
|
| 281 |
+
pages = {40–46}
|
| 282 |
+
}
|
| 283 |
+
"""
|
| 284 |
+
_DESCRIPTION = """\
|
| 285 |
+
The SciArg dataset is an extension of the Dr. Inventor corpus (Fisas et al., 2015, 2016) with an annotation layer containing
|
| 286 |
+
fine-grained argumentative components and relations. It is the first argument-annotated corpus of scientific
|
| 287 |
+
publications (in English), which allows for joint analyses of argumentation and other rhetorical dimensions of
|
| 288 |
+
scientific writing.
|
| 289 |
+
"""
|
| 290 |
+
_URL = "http://data.dws.informatik.uni-mannheim.de/sci-arg/compiled_corpus.zip"
|
| 291 |
+
_HOMEPAGE = "https://github.com/anlausch/ArguminSci"
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
@dataclass
|
| 295 |
+
class SciArgConfig(datasets.BuilderConfig):
|
| 296 |
+
data_url = _URL
|
| 297 |
+
subdirectory_mapping = {"compiled_corpus": datasets.Split.TRAIN}
|
| 298 |
+
filename_blacklist = [] #["A28"]
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
class SciArg(datasets.GeneratorBasedBuilder):
|
| 302 |
+
"""Scientific Argument corpus"""
|
| 303 |
+
|
| 304 |
+
DEFAULT_CONFIG_CLASS = SciArgConfig
|
| 305 |
+
|
| 306 |
+
BUILDER_CONFIGS = [
|
| 307 |
+
SciArgConfig(
|
| 308 |
+
name="full",
|
| 309 |
+
version="1.0.0",
|
| 310 |
+
),
|
| 311 |
+
]
|
| 312 |
+
|
| 313 |
+
DEFAULT_CONFIG_NAME = "full"
|
| 314 |
+
|
| 315 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 316 |
+
features = datasets.Features(
|
| 317 |
+
{
|
| 318 |
+
"document_id": datasets.Value("string"),
|
| 319 |
+
"text": datasets.Value("string"),
|
| 320 |
+
"text_bound_annotations": [
|
| 321 |
+
{
|
| 322 |
+
"offsets": datasets.Sequence([datasets.Value("int32")]),
|
| 323 |
+
"text": datasets.Value("string"),
|
| 324 |
+
"type": datasets.Value("string"),
|
| 325 |
+
"id": datasets.Value("string"),
|
| 326 |
+
}
|
| 327 |
+
],
|
| 328 |
+
"relations": [
|
| 329 |
+
{
|
| 330 |
+
"id": datasets.Value("string"),
|
| 331 |
+
"head": {
|
| 332 |
+
"ref_id": datasets.Value("string"),
|
| 333 |
+
"role": datasets.Value("string"),
|
| 334 |
+
},
|
| 335 |
+
"tail": {
|
| 336 |
+
"ref_id": datasets.Value("string"),
|
| 337 |
+
"role": datasets.Value("string"),
|
| 338 |
+
},
|
| 339 |
+
"type": datasets.Value("string"),
|
| 340 |
+
}
|
| 341 |
+
],
|
| 342 |
+
}
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
return datasets.DatasetInfo(
|
| 346 |
+
description=_DESCRIPTION,
|
| 347 |
+
features=features,
|
| 348 |
+
homepage=_HOMEPAGE,
|
| 349 |
+
citation=_CITATION,
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
|
| 353 |
+
"""Returns SplitGenerators."""
|
| 354 |
+
data_dir = self.config.data_dir or Path(dl_manager.download_and_extract(self.config.data_url))
|
| 355 |
+
|
| 356 |
+
return [
|
| 357 |
+
datasets.SplitGenerator(name=split, gen_kwargs={"filepath": data_dir / subdir})
|
| 358 |
+
for subdir, split in self.config.subdirectory_mapping.items()
|
| 359 |
+
]
|
| 360 |
+
|
| 361 |
+
def _generate_examples(self, filepath):
|
| 362 |
+
for txt_file in glob.glob(filepath / "*.txt"):
|
| 363 |
+
|
| 364 |
+
brat_parsed = parse_brat_file(Path(txt_file))
|
| 365 |
+
if brat_parsed["document_id"] in self.config.filename_blacklist:
|
| 366 |
+
continue
|
| 367 |
+
relevant_subset = {f_name: brat_parsed[f_name] for f_name in self.info.features}
|
| 368 |
+
yield brat_parsed["document_id"], relevant_subset
|