-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata.py
executable file
·281 lines (219 loc) · 8.43 KB
/
data.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
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
#!/usr/bin/env python3
import json
import csv
import os
import logging
import re
import random
from collections import defaultdict, namedtuple
from utils import webnlg_parsing
from utils.corpus_reader.benchmark_reader import Benchmark, select_files
from utils.tokenizer import Tokenizer
logger = logging.getLogger(__name__)
DataTriple = namedtuple('DataTriple', ['subj', 'pred', 'obj'])
def get_dataset_class(dataset_class):
"""
A wrapper for easier introduction of new datasets.
Returns class "MyDataset" for a parameter "--dataset mydataset"
"""
try:
# case-insensitive
available_classes = {o.name.lower() : o for o in globals().values()
if type(o)==type(D2TDataset) and hasattr(o, "name")}
return available_classes[dataset_class.lower()]
except AttributeError:
logger.error(f"Unknown dataset: '{args.dataset}'. Please create \
a class with an attribute name='{args.dataset}' in 'data.py'.")
return None
class DataEntry:
"""
A single D2T dataset example: a set of triples & its possible lexicalizations
"""
def __init__(self, triples, lexs):
self.triples = triples
self.lexs = lexs
def __repr__(self):
return str(self.__dict__)
class D2TDataset:
def __init__(self):
self.data = {split: [] for split in ["train", "dev", "test"]}
self.fallback_template = "The <predicate> of <subject> is <object> ."
def load_from_dir(self, path, template_path, splits):
"""
Load the dataset
"""
raise NotImplementedError
def load_templates(self, templates_filename):
"""
Load existing templates from a JSON file
"""
if not templates_filename:
logger.warning(f"Templates will not be loaded")
return
logger.info(f"Loaded templates from {templates_filename}")
with open(templates_filename) as f:
self.templates = json.load(f)
class WebNLG(D2TDataset):
name="webnlg"
def __init__(self):
super().__init__()
def get_template(self, triple):
"""
Return the template for the triple
"""
pred = triple.pred
if pred in self.templates:
# Using just a single template
assert len(self.templates[pred]) == 1
template = self.templates[pred][0]
else:
logger.warning(f"No template for {pred}, using a fallback")
template = self.fallback_template
return template
def load_from_dir(self, path, template_path, splits):
"""
Load the dataset
"""
self.load_templates(template_path)
for split in splits:
logger.info(f"Loading {split} split")
data_dir = os.path.join(path, split)
err = 0
xml_entryset = webnlg_parsing.run_parser(data_dir)
for xml_entry in xml_entryset:
triples = [DataTriple(e.subject, e.predicate, e.object)
for e in xml_entry.modifiedtripleset]
lexs = self._extract_lexs(xml_entry.lexEntries, triples)
if not any([lex for lex in lexs]):
err += 1
continue
entry = DataEntry(triples=triples, lexs=lexs)
self.data[split].append(entry)
if err > 0:
logger.warning(f"Skipping {err} entries without lexicalizations...")
def _extract_lexs(self, lex_entries, triples):
"""
Use `orderedtripleset` in the WebNLG dataset to determine the "ground-truth" order
of the triples (based on human references).
"""
lexs = []
for entry in lex_entries:
order, agg = self._extract_ord_agg(triples, entry.orderedtripleset)
lex = {
"text" : entry.text,
"order" : order,
"agg" : agg
}
lexs.append(lex)
return lexs
def _extract_ord_agg(self, triples, ordered_triples):
"""
Determine the permutation indices and aggregation markers from
the ground truth.
"""
# if ordered triples do not match the actual triples -> fail
ordered_triples_flattened = [x for sent in ordered_triples for x in sent]
if len(ordered_triples_flattened) != len(triples):
return None, None
order = []
for t in triples:
for i, o in enumerate(ordered_triples_flattened):
if t.subj == o.subject and \
t.pred == o.predicate and \
t.obj == o.object:
order.append(i)
break
else:
# ordered triples do not match the actual triples
return None, None
agg = []
for i, triples_in_sent in enumerate(ordered_triples):
if triples_in_sent:
agg += [i] * len(triples_in_sent)
return order, agg
class E2E(D2TDataset):
name="e2e"
def __init__(self):
super().__init__()
def load_from_dir(self, path, template_path, splits):
"""
Load the dataset
"""
self.load_templates(template_path)
for split in splits:
logger.info(f"Loading {split} split")
triples_to_lex = defaultdict(list)
with open(os.path.join(path, f"{split}.csv")) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
# skip header
next(csv_reader)
err = 0
for i, line in enumerate(csv_reader):
triples = self._mr_to_triples(line[0])
# probably a corrupted sample
if not triples or len(triples) == 1:
err += 1
# cannot skip for dev and test
if split == "train":
continue
lex = {"text" : line[1]}
triples_to_lex[triples].append(lex)
# triples are not sorted, complete entries can be created only after the dataset is processed
for triples, lex_list in triples_to_lex.items():
entry = DataEntry(triples, lex_list)
self.data[split].append(entry)
logger.warn(f"{err} corrupted instances")
def _mr_to_triples(self, mr):
"""
Transforms E2E meaning representation into RDF triples.
"""
triples = []
# cannot be dictionary, slot keys can be duplicated
items = [x.strip() for x in mr.split(",")]
subj = None
keys = []
vals = []
for item in items:
key, val = item.split("[")
val = val[:-1]
keys.append(key)
vals.append(val)
name_idx = None if "name" not in keys else keys.index("name")
eatType_idx = None if "eatType" not in keys else keys.index("eatType")
# primary option: use `name` as a subject
if name_idx is not None:
subj = vals[name_idx]
del keys[name_idx]
del vals[name_idx]
# corrupted case hotfix
if not keys:
keys.append("eatType")
vals.append("restaurant")
# in some cases, that does not work -> use `eatType` as a subject
elif eatType_idx is not None:
subj = vals[eatType_idx]
del keys[eatType_idx]
del vals[eatType_idx]
# still in some cases, there is not even an eatType
#-> hotfix so that we do not lose data
else:
# logger.warning(f"Cannot recognize subject in mr: {mr}")
subj = "restaurant"
for key, val in zip(keys, vals):
triples.append(DataTriple(subj, key, val))
# will be used as a key in a dictionary
return tuple(triples)
def get_template(self, triple):
"""
Return the template for the triple
"""
if triple.pred in self.templates:
templates = self.templates[triple.pred]
# special templates for familyFriendly yes / no
if type(templates) is dict and triple.obj in templates:
template = templates[triple.obj][0]
else:
template = templates[0]
else:
template = self.fallback_template
return template