-
Notifications
You must be signed in to change notification settings - Fork 0
/
n-fold-validation.sh
executable file
·294 lines (231 loc) · 6.29 KB
/
n-fold-validation.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
285
286
287
288
289
290
291
292
293
# (c) Centre for Development of Advanced Computing, Mumbai
# Knowledge Based Computer Systems Division. All Rights Reserved.
#!/bin/bash
#Created By Raj Nath Patel, Nov 7, 2014
#Purpos: Automatic n-fold cross validation
CURDIR=`pwd`
SYSDIR=`dirname $0`
#Change bin directory where GIZA++ is installed
BIN=/home/anuvad/smt/decoder/bin
#Change "MOSES_HOME" according moses installation dir w.r.t. your system
MOSES_HOME=/home/raj/smt/decoder/mosesdecoder-RELEASE-2.1.1/
MOSES_BIN=$MOSES_HOME/bin
SCRIPTS=$MOSES_HOME/scripts
WORKDIR=
usage()
{
cat << EOF
usage: $0 options
This script run n-fold validation test.
OPTIONS:
-h Show this message
-t Target Corpus
-s Source Corpus
-r Directory where systems will be installed
-f Number of folds(Min=1,Max=20)
EOF
}
#BL='\e[0;31m'
BL='\e[0;34m'
NC='\e[0m' # No Color
if [ $# -eq 0 ]
then
echo -e "${BL}No arguments supplied${NC}"
usage
exit 1
fi
src=
tgt=
fold=
while getopts “hs:r:t:f:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
r)
WORKDIR=$OPTARG
;;
s)
src=$OPTARG
;;
t)
tgt=$OPTARG
;;
f)
fold=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [ -z "$src" ] || [ -z "$tgt" ] || [ -z "$fold" ] || [ -z "$WORKDIR" ]
then
usage
exit 1
fi
#Check if path is absolute or not
if [[ "$WORKDIR" = /* ]]
then
echo "Absolute path for working directory:$WORKDIR"
else
WORKDIR=$CURDIR/$WORKDIR
echo "Absolute path for working directory:$WORKDIR"
fi
#If work directory already exist delete it
if [ -d "$WORKDIR" ]; then
rm -rf $WORKDIR
fi
#CLEANING corpus
cat $src |tr -d '|' > $src.clean
cat $src.clean |tr -d '[' > $src.clean.clean
cat $src.clean.clean |tr -d ']' > $src.clean.clean.clean
cat $tgt |tr -d '|' > $tgt.clean
cat $tgt.clean |tr -d '[' > $tgt.clean.clean
cat $tgt.clean.clean |tr -d ']' > $tgt.clean.clean.clean
mv $src.clean.clean.clean $src
mv $tgt.clean.clean.clean $tgt
rm -rf $src.*
rm -rf $tgt.*
#CREATING corpus
python $SYSDIR/corpusnfold.py $src $tgt $fold
#rm -rf train test ref lm
mkdir -p $WORKDIR/train $WORKDIR/test $WORKDIR/ref $WORKDIR/lm
mv ref* $WORKDIR/ref
mv train* $WORKDIR/train
mv test* $WORKDIR/test
src=$(basename $src)
echo $src
tgt=$(basename $tgt)
echo $tgt
ext1="${src##*.}"
ext2="${tgt##*.}"
echo $ext1
echo $ext2
#CREATING Language Models
for file in $WORKDIR/train/*.$ext2
do
if [[ -f $file ]]; then
echo $file
$MOSES_HOME/bin/lmplz -o 2 -S 20% -T /tmp < $file > $file.5gram.arpa
$MOSES_HOME/bin/build_binary $file.5gram.arpa $file.5gram.binary
fi
done
mv $WORKDIR/train/*.binary $WORKDIR/lm
mv $WORKDIR/train/*.arpa $WORKDIR/lm
#Training n-fold systems
for i in `seq 1 $fold`;
do
echo $i
mkdir -p $WORKDIR/$i/corpus $WORKDIR/$i/lm
cp $WORKDIR/train/*$i.* $WORKDIR/$i/corpus
cp $WORKDIR/lm/*$i.* $WORKDIR/$i/lm
$SCRIPTS/training/train-model.perl \
-external-bin-dir $BIN \
-root-dir $WORKDIR/$i \
-corpus $WORKDIR/$i/corpus/train$i -f $ext1 -e $ext2 \
-reordering msd-bidirectional-fe \
-alignment grow-diag-final-and \
-lm 0:5:$WORKDIR/$i/lm/train$i.$ext2.5gram.binary:8 \
>& $WORKDIR/$i/trainig.out &
done
echo -e "${BL}Training Started for all $fold folds...${NC}"
for job in `jobs -p`
do
while kill -0 $job >/dev/null 2>&1
do
echo -n '.'
sleep 3
done
done
echo -e "\n${BL}Training Completed!!!${NC}"
#Unzip phrase table for binrization
for i in `seq 1 $fold`;
do
echo "Unzipping phrase table for system $i ..."
gunzip $WORKDIR/$i/model/phrase-table.gz
done
for job in `jobs -p`
do
wait $job || let "FAIL+=1"
done
#Binarizing
echo -e "\n${BL}Binarizing phrase table...${NC}"
for i in `seq 1 $fold`;
do
export LC_ALL=C
#Binarizing Reordering model
$MOSES_BIN/processLexicalTable -in $WORKDIR/$i/model/reordering-table.wbe-msd-bidirectional-fe.gz \
-out $WORKDIR/$i/model/reordering-table.wbe-msd-bidirectional-fe.gz.binary 2> $WORKDIR/$i/lex.log &
#Binarizing Phrase table
cat $WORKDIR/$i/model/phrase-table | sort | $MOSES_BIN/processPhraseTable -ttable 0 0 - -nscores 5 \
-out $WORKDIR/$i/model/phrase-table 2> $WORKDIR/$i/tm.log &
done
echo -e "\n${BL}Binarization Started for all $fold folds...${NC}"
for job in `jobs -p`
do
while kill -0 $job >/dev/null 2>&1
do
echo -n '.'
sleep 3
done
done
#Chnaging moses.ini, after binrizing
echo -e "\n${BL}Chnaging "moses.ini" for binarized tables...${NC}"
for i in `seq 1 $fold`;
do
/bin/sed -i 's,phrase-table.gz,phrase-table,' "$WORKDIR/$i/model/moses.ini"
/bin/sed -i 's,reordering-table.wbe-msd-bidirectional-fe.gz,reordering-table.wbe-msd-bidirectional-fe.gz.binary,' "$WORKDIR/$i/model/moses.ini"
/bin/sed -i 's,PhraseDictionaryMemory,PhraseDictionaryBinary,' "$WORKDIR/$i/model/moses.ini"
done
for job in `jobs -p`
do
wait $job || let "FAIL+=1"
done
#Testing
for i in `seq 1 $fold`;
do
mkdir -p $WORKDIR/$i/eval/
cp $WORKDIR/test/test$i.$ext1 $WORKDIR/$i/eval
$MOSES_BIN/moses -f $WORKDIR/$i/model/moses.ini -threads 4 < $WORKDIR/$i/eval/test$i.$ext1 > $WORKDIR/$i/eval/test$i.$ext1.out 2> $WORKDIR/$i/eval/err.log &
done
echo -e "\n${BL}Testing Started for all $fold folds... ${NC}"
for job in `jobs -p`
do
while kill -0 $job >/dev/null 2>&1
do
echo -n '.'
sleep 3
done
done
#Evaluation
echo -e "\n${BL}Calculating BLEU...${NC}"
for i in `seq 1 $fold`;
do
$SCRIPTS/generic/multi-bleu.perl $WORKDIR/ref/ref$i.$ext2 < $WORKDIR/$i/eval/test$i.$ext1.out > $WORKDIR/$i/eval/bleu.txt &
done
for job in `jobs -p`
do
wait $job || let "FAIL+=1"
done
#Consolidating Results
echo -e "\n${BL}Consolidating Results...${NC}"
mkdir -p $WORKDIR/results
let total=0
let var=0
for i in `seq 1 $fold`;
do
cat $WORKDIR/$i/eval/bleu.txt
cat $WORKDIR/$i/eval/bleu.txt >> $WORKDIR/results/CONSOLIDATED-BLEU.txt
var=$(cat $WORKDIR/$i/eval/bleu.txt |cut -d ',' -f1 |cut -d '=' -f2 |sed "s/ //g")
echo "Variable=$var"
total=$(bc <<< "scale=3;$total+$var")
done
echo "Total=$total"
average=$(bc <<< "scale=3;$total/$fold")
echo "Average=$average"
echo "Average=$average" >> $WORKDIR/results/CONSOLIDATED-BLEU.txt
echo -e "${BL}Done...${NC}"