-
Notifications
You must be signed in to change notification settings - Fork 6
/
preprocess.py
executable file
·280 lines (226 loc) · 9.48 KB
/
preprocess.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 os
import argparse
import logging
import data
import json
import random
import re
import numpy as np
from utils.tokenizer import Tokenizer
from data import DataTriple
from collections import defaultdict
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO, datefmt='%H:%M:%S')
logger = logging.getLogger(__name__)
class Preprocessor:
def __init__(self, dataset, out_dirname):
self.dataset = dataset
self.out_dirname = out_dirname
self.tokenizer = Tokenizer()
def fill_template(self, template, triple):
"""
Fills a template with the data from the triple
"""
for item, placeholder in [
(triple.subj, "<subject>"),
(triple.pred, "<predicate>"),
(triple.obj, "<object>")
]:
template = template.replace(placeholder,
self.tokenizer.normalize(item,
remove_quotes=True,
remove_parentheses=True)
)
return template
def create_examples(self, entry, dataset, shuffle, keep_separate_sents):
"""
Generates training examples from an entry in the dataset
"""
examples = []
lexs = entry.lexs
triples = entry.triples
sentences = []
for t in entry.triples:
template = dataset.get_template(t)
sentence = self.fill_template(template, t)
sentence = self.tokenizer.detokenize(sentence)
sentences.append(sentence)
if shuffle:
random.shuffle(sentences)
if keep_separate_sents:
inp = sentences
else:
inp = " ".join(sentences)
for lex in entry.lexs:
example = {
"sents" : inp,
"text" : lex["text"]
}
examples.append(example)
return examples
def extract_refs(self, out_dirname, split):
with open(f"{out_dirname}/{split}.ref", "w") as f:
data = self.dataset.data[split]
for entry in data:
for lex in entry.lexs:
f.write(lex["text"] + "\n")
f.write("\n")
# def extract_mrs(self, out_dirname, split):
# with open(f"{out_dirname}/{split}.ref", "w") as f:
# data = self.dataset.data[split]
# for entry in data:
# # same mr for all lexs
# mr = entry.lexs[0]["orig_mr"]
# f.write(mr + "\n")
def extract_agg(self, entry, dataset):
examples = []
triples = entry.triples
if len(triples) == 1:
return examples
# there is one way to aggregate sentences for every possible order
for lex in entry.lexs:
if not lex["order"] or not lex["agg"]:
continue
# get the "reordering indices" for the given order
order = np.argsort(lex["order"])
# reorder the triples
triples_reordered = np.array(triples)[order].tolist()
triples_reordered = [DataTriple(*x) for x in triples_reordered]
sentences = []
prev_sent = 0
agg = []
for i, t in enumerate(triples_reordered):
template = dataset.get_template(t)
sentence = self.fill_template(template, t)
sentence = self.tokenizer.detokenize(sentence)
sentences.append(sentence)
if i < len(triples_reordered) - 1:
if lex["agg"][i+1] != prev_sent:
agg.append(1)
prev_sent = lex["agg"][i+1]
else:
agg.append(0)
example = {
"sents" : sentences,
"sep" : agg
}
examples.append(example)
return examples
def extract_order(self, split, extract_copy_baseline):
with open(os.path.join(self.out_dirname, f"{split}.order.out"), "w") as f:
for i, entry in enumerate(data):
if len(entry.triples) == 1:
# skip trivial examples
continue
if extract_copy_baseline:
order = list(range(len(entry.triples)))
f.write(" ".join([str(x) for x in order]) + "\n")
continue
entry_ok = False
for lex in entry.lexs:
order = lex["order"]
if order:
assert len(order) == len(entry.triples)
entry_ok = True
f.write(" ".join([str(x) for x in order]) + "\n")
if not entry_ok:
# no valid order for the entry
# -> generate a default order
order = list(range(len(entry.triples)))
f.write(" ".join([str(x) for x in order]) + "\n")
f.write("\n")
return
def process(self, split, shuffle, extract_copy_baseline, extract_order, extract_agg, keep_separate_sents):
"""
Processes and outputs training data for the sentence fusion model
"""
output = {"data" : []}
data = self.dataset.data[split]
if extract_order:
self.extract_order(self, split, extract_copy_baseline)
return
for i, entry in enumerate(data):
if extract_agg:
examples = self.extract_agg(entry, dataset)
else:
examples = self.create_examples(entry, dataset, shuffle, keep_separate_sents)
if examples and split != "train" and not extract_agg:
# keep just one example per tripleset
examples = [examples[0]]
for example in examples:
output["data"].append(example)
with open(os.path.join(self.out_dirname, f"{split}.json"), "w") as f:
json.dump(output, f, indent=4, ensure_ascii=False)
if extract_copy_baseline and split != "train":
with open(os.path.join(self.out_dirname, f"{split}_triples.out"), "w") as f:
for example in output["data"]:
f.write(example["text"] + "\n")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str, required=True,
help="Name of the dataset to preprocess.")
parser.add_argument("--dataset_dir", type=str, default=None,
help="Path to the dataset")
parser.add_argument("--templates", type=str, default=None,
help="Path to the JSON file with templates")
parser.add_argument("--output", type=str, required=True,
help="Name of the output directory")
parser.add_argument("--output_refs", type=str, default=None,
help="Name of the output directory for references.")
parser.add_argument('--splits', type=str, nargs='+', default=["train", "dev", "test"],
help='Dataset splits (e.g. train dev test)')
parser.add_argument("--seed", type=int, default=42,
help="Random seed.")
parser.add_argument("--shuffle", action="store_true",
help="Shuffle input sentences.")
parser.add_argument("--keep_separate_sents", action="store_true",
help="Keep a list of individual sentences as the input.")
parser.add_argument("--extract_copy_baseline", action="store_true",
help="Extract inputs to a separate file for the copy baseline.")
parser.add_argument("--extract_order", action="store_true",
help="Extract ordering information (evaluation, WebNLG only).")
parser.add_argument("--extract_agg", action="store_true",
help="Extract aggregation information (evaluation, WebNLG only).")
# parser.add_argument("--extract_mrs", action="store_true",
# help="Extract meaning representations for the slot error script (evaluation, E2E only).")
args = parser.parse_args()
random.seed(args.seed)
logger.info(args)
dataset_name = args.dataset
# Load data
logger.info(f"Loading dataset {dataset_name}")
dataset = data.get_dataset_class(dataset_name)()
path = args.dataset_dir
try:
dataset.load_from_dir(path=path, template_path=args.templates, splits=args.splits)
except FileNotFoundError as err:
logger.error(f"Dataset not found in {path}")
raise err
# Create output directory
try:
out_dirname = os.path.join(args.output)
os.makedirs(out_dirname, exist_ok=True)
except OSError as err:
logger.error(f"Output directory {out_dirname} can not be created")
raise err
os.makedirs(out_dirname, exist_ok=True)
preprocessor = Preprocessor(dataset=dataset,
out_dirname=out_dirname,
)
# if args.extract_mrs:
# for split in args.splits:
# preprocessor.extract_mrs(out_dirname, split)
for split in args.splits:
preprocessor.process(split,
shuffle=args.shuffle,
extract_copy_baseline=args.extract_copy_baseline,
extract_order=args.extract_order,
extract_agg=args.extract_agg,
keep_separate_sents=args.keep_separate_sents
)
if args.output_refs:
os.makedirs(args.output_refs, exist_ok=True)
for split in args.splits:
logger.info(f"Extracting {split} references")
preprocessor.extract_refs(args.output_refs, split)
logger.info(f"Preprocessing finished.")