-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_data.py
223 lines (209 loc) · 7.81 KB
/
prepare_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
import argparse
import csv
import json
import os
import tempfile
import torch
from halo import Halo
from sentencepiece import SentencePieceProcessor, SentencePieceTrainer
from subword_nmt.learn_bpe import learn_bpe
seed = 1234
data_type = "leipzig"
min_prob = 0.99
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input",
dest="input",
required=True,
type=str,
help="Path to TSV file of the sentences",
)
parser.add_argument(
"-o",
"--output",
dest="out_dir",
type=str,
help="Output directory [Default: data/<basename-of-input-file>/]",
)
parser.add_argument(
"--split",
dest="split",
type=int,
help=(
"Percentage of split belonging to the training set, the rest belongs to "
"the validation set. e.g. 80 means 80%% training and 20%% validation. "
"Only if specified a train/validation split will be generated along side "
"the full data."
),
)
parser.add_argument(
"--vocab", dest="vocab", action="store_true", help="Generate a vocabulary"
)
parser.add_argument(
"-s",
"--seed",
dest="seed",
default=seed,
type=int,
help="Seed for random initialisation [Default: {}]".format(seed),
)
parser.add_argument(
"-t",
"--type",
dest="data_type",
type=str,
choices=["leipzig", "swiss-crawl"],
default=data_type,
help="Type of dataset to prepare [Default: {}]".format(data_type),
)
parser.add_argument(
"-p",
"--probability",
dest="min_prob",
type=float,
default=min_prob,
help=(
"Minimum probability to keep a line "
"(only applicable to the SwissCrawl dataset) "
"[Default: {}]".format(min_prob)
),
)
return parser.parse_args()
def main():
options = parse_args()
torch.manual_seed(options.seed)
basename = os.path.splitext(os.path.basename(options.input))[0]
out_dir = options.out_dir or "data/{}/".format(basename)
spinner = Halo(spinner="dots", placement="right")
with open(options.input, "r") as fd:
if options.data_type == "leipzig":
reader = csv.reader(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
lines = [[line[1]] for line in reader]
elif options.data_type == "swiss-crawl":
reader = csv.reader(fd, delimiter=",")
lines = []
for i, line in enumerate(reader):
# Skip the header
if i == 0:
continue
text = line[0]
probability = float(line[2])
if probability >= options.min_prob:
lines.append([text])
else:
raise Exception("Not a valid data type {}".format(options.data_type))
if not os.path.exists(out_dir):
os.makedirs(out_dir)
output_full = os.path.join(out_dir, "{}.tsv".format(basename))
with open(output_full, "w") as fd:
writer = csv.writer(fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar="")
writer.writerows(lines)
if options.split is not None:
num_lines = len(lines)
num_train = num_lines * options.split // 100
num_validation = num_lines - num_train
perms = torch.randperm(num_lines)
train_indices, validation_indices = perms.split([num_train, num_validation])
train_lines = [lines[i] for i in train_indices]
validation_lines = [lines[i] for i in validation_indices]
with open(os.path.join(out_dir, "{}-train.tsv".format(basename)), "w") as fd:
writer = csv.writer(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
writer.writerows(train_lines)
with open(
os.path.join(out_dir, "{}-validation.tsv".format(basename)), "w"
) as fd:
writer = csv.writer(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
writer.writerows(validation_lines)
if options.vocab:
vocab_size = 32000
spiece_out = os.path.join(out_dir, "spiece")
spiece_args = (
"--input={} "
"--model_prefix={} "
"--vocab_size={} "
"--character_coverage=1.0"
).format(output_full, spiece_out, vocab_size)
SentencePieceTrainer.Train(spiece_args)
# Load the generated vocabulary
with open("{}.vocab".format(spiece_out), "r") as fd:
reader = csv.reader(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
vocab = [line[0] for line in reader]
# Remove the special tokens <unk>, <s>, </s>
vocab = vocab[3:]
# Convert to BERT style
bert_vocab = [
v[1:] if v.startswith("▁") else "##{}".format(v) for v in vocab if v != "▁"
]
# Add BERT's special tokens to the beginning
bert_vocab = ["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"] + bert_vocab
# Fill up with unused tokens
pad_size = vocab_size - len(bert_vocab)
bert_vocab += ["unused{}".format(i) for i in range(pad_size)]
with open(os.path.join(out_dir, "vocab.txt"), "w") as fd:
writer = csv.writer(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
writer.writerows([[b] for b in bert_vocab])
# Convert to GPT-2 style
# Unfortunately it's slow and tedious.
spinner.start(text="Generating BPE vocabulary")
gpt2_vocab = ["Ġ{}".format(v[1:]) if v.startswith("▁") else v for v in vocab]
# Add the GPT-2 special token to the end
gpt2_vocab.append("<|endoftext|>")
with open(os.path.join(out_dir, "vocab.json"), "w") as fd:
json.dump({v: i for i, v in enumerate(gpt2_vocab)}, fd, ensure_ascii=False)
spiece_processor = SentencePieceProcessor()
spiece_processor.Load("{}.model".format(spiece_out))
# Encode the whole text
encoded = [
[" ".join(spiece_processor.EncodeAsPieces(line[0])).replace("▁", "Ġ")]
for line in lines
]
tmp_encoded_fd, tmp_encoded_path = tempfile.mkstemp()
tmp_bpe_fd, tmp_bpe_path = tempfile.mkstemp()
try:
# Write the encoded text to a temporary file.
with os.fdopen(tmp_encoded_fd, "w") as fd:
writer = csv.writer(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
writer.writerows(encoded)
learn_bpe(
open(tmp_encoded_path, "r"),
open(tmp_bpe_path, "w"),
num_symbols=vocab_size,
)
with open(tmp_bpe_path, "r") as fd:
reader = csv.reader(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
seen = set()
merges = []
for line in reader:
# Get rid of the </w> tokens
line = line[0].replace("</w>", "")
# Remove duplicates (due to </w> tokens)
if line not in seen:
seen.add(line)
merges.append([line])
with open(os.path.join(out_dir, "merges.txt"), "w") as fd:
writer = csv.writer(
fd, delimiter="\t", quoting=csv.QUOTE_NONE, quotechar=""
)
writer.writerows(merges)
finally:
os.remove(tmp_encoded_path)
os.remove(tmp_bpe_path)
spinner.stop()
if __name__ == "__main__":
main()