-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmulti_preprocess.sh
284 lines (233 loc) · 8.88 KB
/
multi_preprocess.sh
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
281
282
283
284
#!/bin/bash
# LOC='/home' # lab machine
LOC='/path/to/your/folder' # set your root project location
ROOT="${LOC}/user/cipherdaug-nmt"
DATAROOT="${ROOT}/data/iwslt14" # set your data root
DATABIN="${ROOT}/data-bin/iwslt14"
mkdir -p $DATABIN
FAIRSEQ="${ROOT}/fairseq-cipherdaug"
FAIRSCRIPTS="${FAIRSEQ}/scripts"
#####################
#### REAL LANGS ####
#####################
SRCS=(
"de"
)
TGTS=(
"en"
)
#####################
### CIPHER LANGS ###
#####################
CIPHER_LANGS=(
"dex"
)
CIPHER_SRCS=(
"de1"
"de2"
)
CIPHER_TGTS=(
"de"
"en"
)
KEYS=(1 2)
#####################
### SPM Parameters ##
#####################
SPM_TRAIN="${FAIRSCRIPTS}/spm_train.py"
SPM_ENCODE="${FAIRSCRIPTS}/spm_encode.py"
SUBWORD_TYPE="bpe"
BPESIZE=16000
TRAIN_MAXLEN=500
#####################
# join all SRC and TGT arrays for further preprocessing
# source langs
# Declare an associative array
T=(${SRCS[@]} ${CIPHER_SRCS[@]})
declare -A TEMP_SRCS
# Store the values of arr3 in arr4 as keys.
for k in "${T[@]}"; do TEMP_SRCS["$k"]=1; done
# Extract the keys.
ALL_SRCS=("${!TEMP_SRCS[@]}")
# target langs
T=(${TGTS[@]} ${CIPHER_TGTS[@]})
declare -A TEMP_TGTS
# Store the values of arr3 in arr4 as keys.
for k in "${T[@]}"; do TEMP_TGTS["$k"]=1; done
# Extract the keys.
ALL_TGTS=("${!TEMP_TGTS[@]}")
#####################
PRIMARY=$1
echo "${PRIMARY}"
if [ ! ${#PRIMARY} -gt 0 ]; then
echo "PRIMARY can not be empty. This is just a check to ensure you know the source language you want to encipher."
echo "USAGE: bash multi_preprocess.sh de [dest_folder]"
exit 1
fi
if [ ${#2} -gt 0 ]; then
echo "Destination Input: $2"
echo "Setting destination directory to $2 .."
DEST="${DATABIN}/${2}"
mkdir -p $DEST/bpe
else
echo "Destination folder must be set."
echo "try this USAGE: bash multi_preprocess.sh [src lang to encipher] [dest_folder]"
fi
# summarizes the final data config for record keeping
get_config() {
echo "SRCS = ${SRCS[@]} | TGTS = ${TGTS[@]} | CIPHER_LANGS = ${CIPHER_LANGS[@]} | CIPHER_SRCS = ${CIPHER_SRCS[@]} | CIPHER_TGTS = ${CIPHER_TGTS[@]} | PRIMARY_SRC = ${PRIMARY} | DEST_DIR = "${DEST}" | BPESIZE = ${BPESIZE}"
}
echo "Saving data config to $DEST"
get_config > "${DEST}/data.config.txt"
#####################
# 1. copy relevant data files from DATAROOT to DATA-BIN
# 'data-bin' is the working directory for experimentr data
# 'data' is the original data-store; don't touch it.
copy() {
for SRC in ${SRCS[@]}; do
for TGT in ${TGTS[@]}; do
if [ ! -f "${DEST}/train.${SRC}-${TGT}.${TGT}" ]; then
echo "Copying ${SRC} and ${TGT} data .."
cp "${DATAROOT}/${SRC}-${TGT}/"*".${SRC}-${TGT}."* "${DEST}"
echo "Done Copying!"
else
echo "Found ${SRC} and ${TGT} data .."
fi
done
done
for CIPHER_DIR in ${CIPHER_LANGS[@]}; do
for TGT in ${TGTS[@]}; do
CIPHER_DIRECTORY="${CIPHER_DIR}-${TGT}"
done
done
if [ ${#CIPHER_LANGS[@]} -gt 0 ]; then
echo "Copying cipher files .."
for CIPHER_DIR in ${CIPHER_LANGS[@]}; do
for SRC in ${CIPHER_SRCS[@]}; do
for TGT in ${CIPHER_TGTS[@]}; do
if [ ! -f "${DEST}/train.${SRC}-${TGT}.${TGT}" ]; then
echo "Copying ${SRC} and ${TGT} data .."
cp "${DATAROOT}/${CIPHER_DIRECTORY}/"*".${SRC}-${TGT}."* "${DEST}"
echo "Done Copying!"
else
echo "Found ${SRC} and ${TGT} data at ${DEST} .."
fi
done
done
done
fi
}
# 2. Optionally, apply SWAP_OUT : randomly swaps out tokens in the source text.
# 'data-bin' is the working directory for experimentr data
# 'data' is the original data-store; don't touch it.
swapout_self(){
PROB=$1
if (( ! $(echo "${PROB} > 0.0" |bc -l) )); then
# if [ ! ${PROB} -gt 0.0 ]; then
echo "PROB must be greater than 0."
echo "function usage: swapout [prob value]"
exit 1
fi
if [ ${#CIPHER_LANGS[@]} -gt 0 ]; then
echo "Applying SWAPOUT to cipher files .."
for SRC in ${SRCS[@]}; do
for KEY in ${KEYS[@]}; do
# for TGT in ${CIPHER_TGTS[@]}; do
# adding a check to avoid swapping existing file
if [ -f "${DEST}/train.swap.${SRC}${KEY}-${SRC}.${SRC}${KEY}" ]; then
echo "swap file exists: train.swap.${SRC}${KEY}-${SRC}.${SRC}${KEY} !"
else
if [ -f "${DEST}/train.${SRC}${KEY}-${SRC}.${SRC}${KEY}" ]; then
echo ""
echo "Swapping ${SRC}${KEY} data .."
python cipher/utils.py --swapout -i "${DEST}/train.${SRC}${KEY}-${SRC}.${SRC}${KEY}" \
--original "${DEST}/train.${SRC}${KEY}-${SRC}.${SRC}" --prob ${PROB} \
> "${DEST}/train.swap.${SRC}${KEY}-${SRC}.${SRC}${KEY}"
if [ ! $(wc -l < "${DEST}/train.swap.${SRC}${KEY}-${SRC}.${SRC}${KEY}") -eq \
$(wc -l < "${DEST}/train.${SRC}${KEY}-${SRC}.${SRC}${KEY}") ]; then
echo "Cipher and Swapped file lengths do not match!"
exit 1
else
echo "Changing contents of ciphertext with swapped ciphertext."
cat "${DEST}/train.swap.${SRC}${KEY}-${SRC}.${SRC}${KEY}" > "${DEST}/train.${SRC}${KEY}-${SRC}.${SRC}${KEY}"
fi
echo "SwapOut Applied!"
fi
fi
# done
done
done
fi
}
# 3. learn BPE with sentencepiece
learn_bpe(){
RAW_TRAIN_FILES=$(
for SRC in "${ALL_SRCS[@]}"; do
for TGT in "${ALL_TGTS[@]}"; do
if [ ! ${SRC} = ${TGT} ]; then
echo $DEST/train.${SRC}-${TGT}.${SRC}; echo $DEST/train.${SRC}-${TGT}.${TGT};
fi
done
done | tr "\n" ",")
TRAIN_FILES="$(python ${ROOT}/cipher/utils.py -i ${RAW_TRAIN_FILES} --dedup )"
echo "learning joint BPE over ${TRAIN_FILES} .."
python "${SPM_TRAIN}" \
--input=${TRAIN_FILES} \
--model_prefix="${DEST}/bpe/spm.${SUBWORD_TYPE}" \
--vocab_size=${BPESIZE} \
--character_coverage=1.0 \
--model_type="${SUBWORD_TYPE}"
}
######################################
# 4. Optionally, call functions here #
######################################
copy
# swapout_self 0.2
learn_bpe
###################################
# 5. apply BPE to train/valid/test
# encode train/valid
echo "encoding train data with learned BPE..."
for SRC in "${ALL_SRCS[@]}"; do
for TGT in "${ALL_TGTS[@]}"; do
if [ ! ${SRC} = ${TGT} ]; then
python "${SPM_ENCODE}" \
--model "${DEST}/bpe/spm.${SUBWORD_TYPE}.model" \
--output_format=piece \
--inputs "${DEST}/train.${SRC}-${TGT}.${SRC}" "${DEST}/train.${SRC}-${TGT}.${TGT}" \
--outputs "${DEST}/bpe/train.bpe.${SRC}-${TGT}.${SRC}" "${DEST}/bpe/train.bpe.${SRC}-${TGT}.${TGT}" \
--min-len 1 --max-len "${TRAIN_MAXLEN}"
fi
done
done
echo "encoding valid data with learned BPE..."
for SRC in "${ALL_SRCS[@]}"; do
for TGT in "${ALL_TGTS[@]}"; do
if [ ! ${SRC} = ${TGT} ]; then
python "${SPM_ENCODE}" \
--model "${DEST}/bpe/spm.${SUBWORD_TYPE}.model" \
--output_format=piece \
--inputs "${DEST}/valid.${SRC}-${TGT}.${SRC}" "${DEST}/valid.${SRC}-${TGT}.${TGT}" \
--outputs "${DEST}/bpe/valid.bpe.${SRC}-${TGT}.${SRC}" "${DEST}/bpe/valid.bpe.${SRC}-${TGT}.${TGT}" \
--min-len 1 --max-len "${TRAIN_MAXLEN}"
fi
done
done
echo "encoding test data with learned BPE ..."
for SRC in "${ALL_SRCS[@]}"; do
for TGT in "${ALL_TGTS[@]}"; do
if [ ! ${SRC} = ${TGT} ]; then
python "${SPM_ENCODE}" \
--model "${DEST}/bpe/spm.${SUBWORD_TYPE}.model" \
--output_format=piece \
--inputs "${DEST}/test.${SRC}-${TGT}.${SRC}" "${DEST}/test.${SRC}-${TGT}.${TGT}" \
--outputs "${DEST}/bpe/test.bpe.${SRC}-${TGT}.${SRC}" "${DEST}/bpe/test.bpe.${SRC}-${TGT}.${TGT}" \
--min-len 1 --max-len "${TRAIN_MAXLEN}"
fi
done
done
echo ""
echo "Finished encoding train/valid/test with Sentencepiece ${SUBWORD_TYPE} ."
get_config
# echo "|- ${SUBWORD_TYPE} - ${BPESIZE}" > "${DEST}/bpe/sp.log"
echo "Dir: ${DEST}/bpe"