File size: 9,324 Bytes
69bc0e9 2b58efe 69bc0e9 ad58881 cc9f1ec 2b58efe cc9f1ec ad58881 2b58efe f4ff05c 982d568 69bc0e9 2b58efe cc9f1ec f4ff05c 2b58efe f4ff05c 69bc0e9 2b58efe f4ff05c 171478c f4ff05c 2b58efe f4ff05c 982d568 2b58efe 982d568 2b58efe 982d568 f4ff05c 2b58efe f4ff05c 2b58efe 69bc0e9 2b58efe 69bc0e9 2b58efe c8fcb51 2b58efe |
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 |
import dataclasses
import logging
from typing import Union
from pie_core import AnnotationLayer, Document, annotation_field
from pie_documents.document.processing import (
RegexPartitioner,
RelationArgumentSorter,
SpansViaRelationMerger,
TextSpanTrimmer,
)
from pie_documents.documents import (
TextDocumentWithLabeledMultiSpansAndBinaryRelations,
TextDocumentWithLabeledMultiSpansBinaryRelationsAndLabeledPartitions,
TextDocumentWithLabeledSpansAndBinaryRelations,
TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions,
)
from pie_datasets.builders import BratBuilder, BratConfig
from pie_datasets.builders.brat import (
BratAttribute,
BratDocument,
BratDocumentWithMergedSpans,
BratNote,
)
from pie_datasets.core.dataset import DocumentConvertersType
from pie_datasets.document.processing import Caster, Pipeline
logger = logging.getLogger(__name__)
URL = "http://data.dws.informatik.uni-mannheim.de/sci-arg/compiled_corpus.zip"
SPLIT_PATHS = {"train": "compiled_corpus"}
@dataclasses.dataclass
class ConvertedBratDocument(TextDocumentWithLabeledMultiSpansAndBinaryRelations):
span_attributes: AnnotationLayer[BratAttribute] = annotation_field(
target="labeled_multi_spans"
)
relation_attributes: AnnotationLayer[BratAttribute] = annotation_field(
target="binary_relations"
)
notes: AnnotationLayer[BratNote] = annotation_field(
targets=[
"labeled_multi_spans",
"binary_relations",
"span_attributes",
"relation_attributes",
]
)
@dataclasses.dataclass
class ConvertedBratDocumentWithMergedSpans(TextDocumentWithLabeledSpansAndBinaryRelations):
span_attributes: AnnotationLayer[BratAttribute] = annotation_field(target="labeled_spans")
relation_attributes: AnnotationLayer[BratAttribute] = annotation_field(
target="binary_relations"
)
notes: AnnotationLayer[BratNote] = annotation_field(
targets=["labeled_spans", "binary_relations", "span_attributes", "relation_attributes"]
)
def get_common_converter_pipeline_steps(target_document_type: type[Document]) -> dict:
return dict(
cast=Caster(
document_type=target_document_type,
field_mapping={"spans": "labeled_spans", "relations": "binary_relations"},
),
trim_adus=TextSpanTrimmer(layer="labeled_spans"),
sort_symmetric_relation_arguments=RelationArgumentSorter(
relation_layer="binary_relations",
label_whitelist=["parts_of_same", "semantically_same", "contradicts"],
),
)
def get_common_converter_pipeline_steps_with_resolve_parts_of_same(
target_document_type: type[Document],
) -> dict:
return dict(
cast=Caster(
document_type=target_document_type,
field_mapping={"spans": "labeled_multi_spans", "relations": "binary_relations"},
),
trim_adus=TextSpanTrimmer(layer="labeled_multi_spans"),
sort_symmetric_relation_arguments=RelationArgumentSorter(
relation_layer="binary_relations",
label_whitelist=["semantically_same"],
),
)
def remove_duplicate_relations(document: Union[BratDocument, BratDocumentWithMergedSpans]) -> None:
if len(document.relations) > len(set(document.relations)):
added = set()
i = 0
while i < len(document.relations):
relation = document.relations[i]
if relation in added:
logger.warning(f"doc_id={document.id}: Removing duplicate relation: {relation}")
document.relations.pop(i)
else:
added.add(relation)
i += 1
class SciArgConfig(BratConfig):
def __init__(
self,
name: str,
resolve_parts_of_same: bool = False,
**kwargs,
):
super().__init__(name=name, merge_fragmented_spans=True, **kwargs)
self.resolve_parts_of_same = resolve_parts_of_same
class SciArg(BratBuilder):
BASE_DATASET_PATH = "DFKI-SLT/brat"
BASE_DATASET_REVISION = "844de61e8a00dc6a93fc29dc185f6e617131fbf1"
# Overwrite the default config to merge the span fragments.
# The span fragments in SciArg come just from the new line splits, so we can merge them.
# Actual span fragments are annotated via "parts_of_same" relations.
BUILDER_CONFIGS = [
SciArgConfig(name=BratBuilder.DEFAULT_CONFIG_NAME),
SciArgConfig(name="resolve_parts_of_same", resolve_parts_of_same=True),
]
DOCUMENT_TYPES = {
BratBuilder.DEFAULT_CONFIG_NAME: BratDocumentWithMergedSpans,
"resolve_parts_of_same": BratDocument,
}
# we need to add None to the list of dataset variants to support the default dataset variant
BASE_BUILDER_KWARGS_DICT = {
dataset_variant: {"url": URL, "split_paths": SPLIT_PATHS}
for dataset_variant in ["default", "resolve_parts_of_same", None]
}
def _generate_document(self, example, **kwargs):
document = super()._generate_document(example, **kwargs)
if self.config.resolve_parts_of_same:
# we need to convert the document to a different type to be able to merge the spans:
# SpansViaRelationMerger expects the spans to be of type LabeledSpan,
# but the document has spans of type BratSpan
converted_doc = document.as_type(
ConvertedBratDocumentWithMergedSpans,
field_mapping={
"spans": "labeled_spans",
"relations": "binary_relations",
},
keep_remaining=True,
)
merged_document = SpansViaRelationMerger(
relation_layer="binary_relations",
link_relation_label="parts_of_same",
create_multi_spans=True,
result_document_type=ConvertedBratDocument,
result_field_mapping={
"labeled_spans": "labeled_multi_spans",
"binary_relations": "binary_relations",
"span_attributes": "span_attributes",
"relation_attributes": "relation_attributes",
"notes": "notes",
},
)(converted_doc)
# convert back to BratDocument
document = merged_document.as_type(
BratDocument,
field_mapping={"labeled_multi_spans": "spans", "binary_relations": "relations"},
keep_remaining=True,
)
else:
# some documents have duplicate relations, remove them
remove_duplicate_relations(document)
return document
@property
def document_converters(self) -> DocumentConvertersType:
regex_partitioner = RegexPartitioner(
partition_layer_name="labeled_partitions",
# find matching tags, allow newlines in between (s flag) and capture the tag name
pattern="<([^>/]+)>(?s:.)*?</\\1>",
label_group_id=1,
label_whitelist=["Title", "Abstract", "H1"],
skip_initial_partition=True,
strip_whitespace=True,
)
if not self.config.resolve_parts_of_same:
return {
TextDocumentWithLabeledSpansAndBinaryRelations: Pipeline(
**get_common_converter_pipeline_steps(
TextDocumentWithLabeledSpansAndBinaryRelations
)
),
TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions: Pipeline(
**get_common_converter_pipeline_steps(
TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions
),
add_partitions=regex_partitioner,
),
}
else:
return {
# TextDocumentWithLabeledSpansAndBinaryRelations: Pipeline(
# **get_common_converter_pipeline_steps_with_resolve_parts_of_same(
# TextDocumentWithLabeledSpansAndBinaryRelations
# )
# ),
# TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions: Pipeline(
# **get_common_converter_pipeline_steps_with_resolve_parts_of_same(
# TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions
# ),
# add_partitions=regex_partitioner,
# ),
TextDocumentWithLabeledMultiSpansAndBinaryRelations: Pipeline(
**get_common_converter_pipeline_steps_with_resolve_parts_of_same(
TextDocumentWithLabeledMultiSpansAndBinaryRelations
)
),
TextDocumentWithLabeledMultiSpansBinaryRelationsAndLabeledPartitions: Pipeline(
**get_common_converter_pipeline_steps_with_resolve_parts_of_same(
TextDocumentWithLabeledMultiSpansBinaryRelationsAndLabeledPartitions
),
add_partitions=regex_partitioner,
),
}
|