-
Notifications
You must be signed in to change notification settings - Fork 10
/
build-lm.sh
executable file
·215 lines (184 loc) · 5.26 KB
/
build-lm.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
#!/usr/bin/env bash
set -e
set -o pipefail
scriptDir=$(dirname $0)
function usage {
echo >&2 "Usage: $0 order corpusHdfs lmName finalArpaFile [filterVocabHdfs] [skipOpts...]"
echo >&2 ""
echo >&2 "TIP: To obtain a gzipped final LM file use a process substitution >(gzip > lm.gz)"
echo >&2 "TIP: To obtain a gzipped final LM file on a remote machine, use a process substitution >(bzip2 | ssh my.machine.com 'bunzip2 | gzip > /path/lm.gz' )"
echo >&2 "TIP: To obtain a KenLM mmappable final LM file use a process substitution >($KENLM/build-binary /dev/stdin lm.mmap)"
exit 1
}
if (( $# < 4 )); then
usage
fi
set -x
order=$1
corpusHdfs=$2
name=$3 # HDFS directory where temporary files will be placed
finalArpaGz=$4
shift 4
if [[ "$1" != "" && "$1" != --* ]]; then
noFilter=0
filterVocabHdfs=$1
shift
else
noFilter=1
fi
function die {
echo >&2 "$@"
exit 1
}
[[ "$order" != --* ]] || die "invalid order: $order"
[[ "$corpusHdfs" != --* ]] || die "invalid corpusHdfs: $corpusHdfs"
[[ "$name" != --* ]] || die "invalid name: $name"
[[ "$finalArpaGz" != --* ]] || die "invalid finalArpaGz: $finalArpaGz"
localDir=$name
function parseArgs {
while [[ $# > 0 ]]; do
case "$1" in
--skipVocab) skipVocab=1 ;;
--skipExtract) skipExtract=1 ;;
--skipDiscounts) skipDiscounts=1 ;;
--skipUninterp) skipUninterp=1 ;;
--skipInterpOrders) skipInterp=1 ;;
--skipRenorm) skipRenorm=1 ;;
--skipArpaFormat) skipArpaFormat=1 ;;
--skipArpaFilter) skipArpaFilter=1 ;;
--noFilter) noFilter=1 ;;
--skipArpaMerge) skipArpaMerge=1 ;;
--vocabHdfs) shift; vocabHdfs=$1 ;;
*) echo "Unrecognized flag: $1"; usage
esac
shift
done
}
# A statically linked (possibly non-threaded) version of Kenneth's filter
filterVocabHdfs=vocabMT06.txt
testOut=$name
vocabHdfs=$testOut/vocabIds.txt
countsHdfs=$testOut/countsDir
countCountsTmp=$testOut/countCounts.txt
discountsHdfs=$testOut/discounts.txt
uninterpStatsHdfs=$testOut/uninterpStatsDir
interpModelHdfs=$testOut/interpModelDir
renormModelHdfs=$testOut/renormModelDir
dArpaModelHdfs=$testOut/dArpaModelDir
filterModelHdfs=$testOut/filterModelDir
arpaFileGz=$finalArpaGz
parseArgs $@
# Do we have a proper hadoop installation on this machine?
if which hadoop; then
haveHadoop=1
else
haveHadoop=""
fi
function hrm {
file=$1
if [ $haveHadoop ]; then (set +e; hadoop dfs -rmr $file); fi
rm -rf $file
}
function hmkdir {
dir=$1
mkdir -p $dir
if [ $haveHadoop ]; then hadoop dfs -mkdir $dir; fi
}
mkdir -p $localDir
if [ -z $skipVocab ]; then
hmkdir $testOut
hrm $vocabHdfs
rm -rf $vocab
$scriptDir/bigfat vocab \
--corpusIn $corpusHdfs \
--vocabIdsOut $vocabHdfs \
2>&1 | tee $testOut/vocab.log
fi
if [ -z $skipExtract ]; then
rm -rf $countsHdfs
hrm $countsHdfs
$scriptDir/bigfat extract \
--vocabIds $vocabHdfs \
--corpusIn $corpusHdfs \
--countsOut $countsHdfs \
--order $order \
--maxOrder $order \
--reverseKeys true \
2>&1 | tee $testOut/extract.log
fi
### # This step is for debugging only
###rm -f $testOut/counts.txt
###java -cp bigfat.jar util.DebugSequenceFileReader adjusted-counts $vocabHdfs $countsHdfs $testOut/counts.txt
if [ -z $skipDiscounts ]; then
hrm $countCountsTmp
hrm $discountsHdfs
$scriptDir/bigfat discounts \
--countsIn $countsHdfs \
--order $order \
--maxOrder $order \
--discountsOut $discountsHdfs \
--countOfCountsTmpOut $countCountsTmp \
2>&1 | tee $testOut/discounts.log
fi
if [ -z $skipUninterp ]; then
hrm $uninterpStatsHdfs
$scriptDir/bigfat uninterp \
--adjustedCountsIn $countsHdfs \
--statsOut $uninterpStatsHdfs \
--order $order \
--maxOrder $order \
--discountFile $discountsHdfs \
--minCountsPre 1,1,1,1,1 \
--minCountsPost 1,1,1,1,1 \
--debug false \
2>&1 | tee $testOut/uninterp.log
fi
if [ -z $skipInterp ]; then
hrm $interpModelHdfs
# bigfat script will add numReducers for us
$scriptDir/bigfat interpOrders \
--uninterpolatedIn $uninterpStatsHdfs \
--interpolatedOut $interpModelHdfs \
--maxOrder $order \
--order $order \
--debug false \
2>&1 | tee $testOut/interp.log
fi
if [ -z $skipRenorm ]; then
hrm $renormModelHdfs
$scriptDir/bigfat renorm \
--modelIn $interpModelHdfs \
--modelOut $renormModelHdfs \
--maxOrder $order \
--order $order \
--debug false \
2>&1 | tee $testOut/renorm.log
fi
if [ -z $skipArpaFormat ]; then
hrm $dArpaModelHdfs
$scriptDir/bigfat darpa \
--modelIn $renormModelHdfs \
--dArpaOut $dArpaModelHdfs \
--order $order \
--vocabFile $vocabHdfs \
2>&1 | tee $testOut/darpa.log
fi
countsByOrder=$testOut/countsByOrder.txt
if [[ $noFilter == 1 ]]; then
filterModelHdfs=$dArpaModelHdfs
for i in $(seq 1 $order); do echo $i $(fgrep "$i-gram dARPA entries written" $testOut/darpa.log | cut -d= -f2); done > $countsByOrder
elif [ -z $skipArpaFilter ]; then
$scriptDir/bigfat filter $dArpaModelHdfs $filterVocabHdfs $filterModelHdfs \
2>&1 | tee $testOut/filter.log
fi
if [ -z $skipArpaMerge ]; then
# Don't try to remove named pipes or process subsitution commands
if [ ! -p $arpaFileGz ]; then
rm -f $arpaFileGz
fi
$scriptDir/bigfat mergearpa \
--dArpaIn $filterModelHdfs \
--countsByOrder $countsByOrder \
--arpaFileOut $arpaFileGz \
2>&1 | tee $testOut/merge.log
fi