-
Notifications
You must be signed in to change notification settings - Fork 0
/
mspt_dataset.py
228 lines (205 loc) · 9.18 KB
/
mspt_dataset.py
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
# Experiment resources related to the MuLMS corpus (WIESP 2023).
# Copyright (c) 2023 Robert Bosch GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
This module contains the dataset class for the MSPT dataset.
"""
import os
from collections import namedtuple
from puima.collection_utils import DocumentCollection
from source.constants.mulms_constants import (
MSPT_PATH,
mspt_ne_label2id,
mspt_rel_label2id,
)
SENTENCE_TYPE: str = "webanno.custom.Sentence"
TOKEN_TYPE: str = "webanno.custom.Token"
RELATION_TYPE: str = "webanno.custom.Relation"
NE_TYPE: str = "webanno.custom.NamedEntity"
Sentence: namedtuple = namedtuple("Sentence", ["doc_id", "sent_id", "begin_offset", "end_offset"])
Token: namedtuple = namedtuple(
"Token", ["doc_id", "sent_id", "token_id", "begin_idx", "end_idx", "text"]
)
Entity: namedtuple = namedtuple(
"Entity",
[
"doc_id",
"sent_id",
"ent_id",
"begin_idx",
"end_idx",
"begin_token",
"end_token",
"label",
"label_id",
],
)
Relation: namedtuple = namedtuple(
"Relation", ["doc_id", "sent_id", "rel_id", "gov_span_id", "dep_span_id", "label", "label_id"]
)
class MSPT_Dataset:
"""
MSPT dataset class.
"""
def __init__(self, split: str) -> None:
"""
Initializes the MSPT dataset by reading it from the disk and preparing it for BERT-based training.
Args:
split (str): Desired split; must one of [ner-train, ner-dev, ner-test, sfex-train, sfex-dev, sfex-test]
"""
assert split in [
"ner-train",
"ner-dev",
"ner-test",
"sfex-train",
"sfex-dev",
"sfex-test",
], "Invalid split provided. Split must be one of: ner-train, ner-dev, ner-test (NER), sfex-train, sfex-dev, sfex-test (Frames)"
self._sentences: dict = {} # Sentences sorted by Doc_ID
self._sentences_as_string: dict = {} # Sentences as string representation
self._tokens: dict = {} # Named Tuple Tokens sorted by Doc_ID and Sent_ID
self._token_list: dict = {} # Raw tokens sorted by Doc_ID and Sent_ID
self._relations: dict = {} # Named Tuple Relations sorted by Doc_ID and Sent_ID
self._named_entities: dict = {} # Named Tuple Entities sorted by Doc_ID and Sent_ID
self._named_entities_by_id: dict = {} # Named Tuple Entities sorted by their ID
self._ner_data: dict = {
"id": [],
"sentences": [],
"tokens": [],
"ne_labels": [],
"ne_labels_bilou": [],
"slot_labels": [],
"slot_labels_bilou": [],
"crf_mask": [],
"tensor_encoded_input": None,
} # This dict contains the data unrolled s.t. it can be iterated over
self._split: str = split
self._load_mspt_relation_dataset()
def _load_mspt_relation_dataset(self) -> None:
"""
Reads the dataset from the disk and creates BERT-based tensors.
"""
doc_collection: DocumentCollection = DocumentCollection(
xmi_dir=MSPT_PATH.__str__(), file_list=os.listdir(MSPT_PATH.__str__())
)
split_docs: list[str] = None
with open(
os.path.join(MSPT_PATH, f"../{self._split}-fnames.txt"), mode="r", encoding="utf-8"
) as f:
split_docs = f.read().splitlines()
for doc_id, doc in doc_collection.docs.items():
if not doc_id.split(".")[0] in split_docs:
continue
sentences: list = list(doc.select_annotations(SENTENCE_TYPE))
self._sentences[doc_id] = []
self._sentences_as_string[doc_id] = {}
self._tokens[doc_id] = {}
self._token_list[doc_id] = {}
self._relations[doc_id] = {}
self._named_entities[doc_id] = {}
for sent_id, sent in enumerate(sentences):
self._sentences[doc_id].append(Sentence(doc_id, sent_id, sent.begin, sent.end))
self._sentences_as_string[doc_id][sent_id] = doc.get_covered_text(sent)
sent_tokens: list = list(doc.select_covered(TOKEN_TYPE, sent))
sent_relations: list = list(doc.select_covered(RELATION_TYPE, sent))
sent_entities: list = list(doc.select_covered(NE_TYPE, sent))
self._tokens[doc_id][sent_id] = []
self._token_list[doc_id][sent_id] = []
self._relations[doc_id][sent_id] = []
self._named_entities[doc_id][sent_id] = []
for tok in sent_tokens:
self._tokens[doc_id][sent_id].append(
Token(
doc_id,
sent_id,
tok.id,
tok.begin,
tok.end,
tok.get_feature_value("value"),
)
)
self._token_list[doc_id][sent_id].append(tok.get_feature_value("value"))
for ent in sent_entities:
covered_tokens: list = list(doc.select_covered(TOKEN_TYPE, ent))
covering_tokens: list = list(doc.select_covering(TOKEN_TYPE, ent))
min_begin_idx: int = -1
max_end_idx: int = -1
if len(covered_tokens) > 0:
min_begin_idx = min([ct.begin for ct in covered_tokens])
max_end_idx = max([ct.end for ct in covered_tokens])
# Fallback if NEs do not cover whole token
elif len(covering_tokens) > 0:
min_begin_idx = min([ct.begin for ct in covering_tokens])
max_end_idx = max([ct.end for ct in covering_tokens])
begin_token_idx: int = [
(i, t)
for i, t in enumerate(self._tokens[doc_id][sent_id])
if t.begin_idx == min_begin_idx
][0][0]
end_token_idx: int = [
(i, t)
for i, t in enumerate(self._tokens[doc_id][sent_id])
if t.end_idx == max_end_idx
][0][0]
label: str = ent.get_feature_value("value").replace(
"-", "_"
) # Fix s.t. evaluation logic does not break
self._named_entities[doc_id][sent_id].append(
Entity(
doc_id,
sent_id,
ent.id,
ent.begin,
ent.end,
begin_token_idx,
end_token_idx,
label,
mspt_ne_label2id[label],
)
)
self._named_entities_by_id[ent.id] = Entity(
doc_id,
sent_id,
ent.id,
ent.begin,
ent.end,
begin_token_idx,
end_token_idx,
label,
mspt_ne_label2id[label],
)
for rel in sent_relations:
# Get correct Token ID based on offsets
dep_tokens: list = list(doc.select_covered(NE_TYPE, rel))
dep_span = list(doc.select_covering(NE_TYPE, dep_tokens[0]))[0]
gov_span_id: int = int(rel.get_feature_value("governor"))
rel_label: str = rel.get_feature_value("relationType")
# Check if relation stays within same sentence
try:
dep_ent: Entity = self._named_entities_by_id[dep_span.id]
gov_ent: Entity = self._named_entities_by_id[gov_span_id]
except KeyError:
continue
if dep_ent.doc_id == gov_ent.doc_id and dep_ent.sent_id == gov_ent.sent_id:
self._relations[doc_id][sent_id].append(
Relation(
doc_id,
sent_id,
rel.id,
gov_span_id,
dep_span.id,
rel_label,
mspt_rel_label2id[rel_label],
)
)