Skip to content

Commit 261fbb5

Browse files
sih4sing5hong5danpovey
authored andcommitted
WIP: Update docs about arpa2fst (kaldi-asr#1129)
* added some usages for format_lm.sh. updated format_lm_sri.sh and data_prep.dox * online_decoding.dox using format_lm, graph_recipe_test.dox using commands directly * copy the `topo` to new lang dir * utils/format_lm.sh instead of egs/wsj/... * fixed indent in the example
1 parent 93f2f65 commit 261fbb5

5 files changed

Lines changed: 34 additions & 69 deletions

File tree

‎egs/wsj/s5/utils/format_lm.sh‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
set -o errexit
2020

2121
if [ $# -ne 4 ]; then
22-
printf "Usage: %s lang_dir LM lexicon out_dir\n" `basename $0`
23-
echo " Convert ARPA-format language models to FSTs.";
22+
echo "Usage: $0 <lang_dir> <arpa-LM> <lexicon> <out_dir>"
23+
echo "E.g.: $0 data/lang data/local/lm/foo.kn.gz data/local/dict/lexicon.txt data/lang_test"
24+
echo "Convert ARPA-format language models to FSTs.";
2425
exit 1;
2526
fi
2627

@@ -34,7 +35,7 @@ mkdir -p $out_dir
3435

3536
echo "Converting '$lm' to FST"
3637

37-
for f in phones.txt words.txt L.fst L_disambig.fst phones/ oov.int oov.txt; do
38+
for f in phones.txt words.txt topo L.fst L_disambig.fst phones/ oov.int oov.txt; do
3839
cp -r $lang_dir/$f $out_dir
3940
done
4041

‎egs/wsj/s5/utils/format_lm_sri.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if [ $# -ne 4 ] && [ $# -ne 3 ]; then
2929
echo "E.g.: utils/format_lm_sri.sh data/lang data/local/lm/foo.kn.gz data/local/dict/lexicon.txt data/lang_test"
3030
echo "Converts ARPA-format language models to FSTs. Change the LM vocabulary using SRILM."
3131
echo "Note: if you want to just convert ARPA LMs to FSTs, there is a simpler way to do this"
32-
echo "that doesn't require SRILM: see examples in egs/wsj/s5/local/wsj_format_local_lms.sh"
32+
echo "that doesn't require SRILM: see utils/format_lm.sh"
3333
echo "options:"
3434
echo " --help # print this message and exit"
3535
echo " --srilm-opts STRING # options to pass to SRILM tools (default: '$srilm_opts')"

‎src/doc/data_prep.dox‎

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -810,44 +810,32 @@ www.openfst.org and do the FST tutorial so that you understand the basics of fin
810810
state transducers. (Note that language models would be represented as finite state
811811
acceptors, or FSAs, which can be considered as a special case of finite state transducers).
812812

813-
In the WSJ setup (like most setups), we use a statistical language model. The script <DFN>local/wsj_format_data.sh</DFN>
814-
deals with converting the ARPA-format language models supplied with the WSJ database, into
815-
an OpenFst format. Some of the key commands from that script are:
816-
\verbatim
817-
gunzip -c $lmdir/lm_${lm_suffix}.arpa.gz | \
818-
utils/find_arpa_oovs.pl $test/words.txt > $tmpdir/oovs_${lm_suffix}.txt
819-
...
820-
gunzip -c $lmdir/lm_${lm_suffix}.arpa.gz | \
821-
grep -v '<s> <s>' | \
822-
grep -v '</s> <s>' | \
823-
grep -v '</s> </s>' | \
824-
arpa2fst - | fstprint | \
825-
utils/remove_oovs.pl $tmpdir/oovs_${lm_suffix}.txt | \
826-
utils/eps2disambig.pl | utils/s2eps.pl | fstcompile --isymbols=$test/words.txt \
827-
--osymbols=$test/words.txt --keep_isymbols=false --keep_osymbols=false | \
828-
fstrmepsilon > $test/G.fst
829-
\endverbatim
830-
Here, the variable <DFN>\$test</DFN> would be something like <DFN>data/lang_test_tg</DFN>.
831-
The most important of these commands is <DFN>arpa2fst</DFN> which is a Kaldi program.
832-
This program turns the ARPA-format language model into a Weight Finite State Transducer
833-
(actually, an acceptor).
834-
The <DFN>grep</DFN> commands remove certain "invalid" n-grams from the language model, which
835-
should not have been there in the first place. The program <DFN>remove_oovs.pl</DFN> removes
836-
N-grams containing words not in our vocabulary (which would cause a crash in <DFN>fstcompile</DFN>); <DFN>eps2disambig.pl</DFN> turns the
837-
<DFN>\<eps\></DFN> (\f$\epsilon\f$) symbols on backoff arcs into a special symbol <DFN>#0</DFN> which
838-
we use to make the grammar determinizable; see \ref graph_disambig. If you don't know
839-
what a "backoff arc" is, see the literature on backoff N-grams, for instance Goodman's "A bit of progress in
840-
language modeling", and the paper by Mohri we referenced above. The command <DFN>s2eps.pl</DFN> turns the
841-
sentence-begin and sentence-end symbols <DFN>\<s\></DFN> and <DFN>\</s\></DFN> into
842-
epsilon (<DFN>\<eps\></DFN>), which means "no symbol"; <DFN>fstcompile</DFN> is an OpenFst
843-
command which turns the
844-
FST from text form into OpenFst binary form; and <DFN>fstrmepsilon</DFN> is also
845-
an OpenFst command, which removes the small number of <DFN>\<eps\></DFN> symbols from the
846-
FST, which were converted from <DFN>\<s\></DFN> and <DFN>\</s\></DFN>.
813+
The script <DFN>utils/format_lm.sh</DFN> deals with converting the ARPA-format language
814+
models into an OpenFst format. Here is the usage messages of that script:
815+
\verbatim
816+
Usage: utils/format_lm.sh <lang_dir> <arpa-LM> <lexicon> <out_dir>
817+
E.g.: utils/format_lm.sh data/lang data/local/lm/foo.kn.gz data/local/dict/lexicon.txt data/lang_test
818+
Convert ARPA-format language models to FSTs.
819+
\endverbatim
820+
Some of the key commands from that script are:
821+
\verbatim
822+
gunzip -c $lm \
823+
| arpa2fst --disambig-symbol=#0 \
824+
--read-symbol-table=$out_dir/words.txt - $out_dir/G.fst
825+
\endverbatim
826+
This Kaldi program, <DFN>arpa2fst</DFN>, turns the ARPA-format language model
827+
into a Weight Finite State Transducer (actually, an acceptor).
847828

848829
A popular toolkit for building language models is SRILM. Various language
849830
modeling toolkits are used in the Kaldi example scripts. SRILM is the best
850831
documented and most fully featured, and we generally recommend it (its only
851-
drawback is that it don't have the most free licence).
832+
drawback is that it don't have the most free licence). Here is the usage
833+
messages of <DFN>utils/format_lm_sri.sh</DFN>
834+
835+
\verbatim
836+
Usage: utils/format_lm_sri.sh [options] <lang-dir> <arpa-LM> <out-dir>
837+
E.g.: utils/format_lm_sri.sh data/lang data/local/lm/foo.kn.gz data/lang_test
838+
Converts ARPA-format language models to FSTs. Change the LM vocabulary using SRILM.
839+
\endverbatim
852840

853841
*/

‎src/doc/graph_recipe_test.dox‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,9 @@ The steps we have to do aside from just running arpa2fst are as follows:
199199

200200
A slightly simplified version of the actual script that does this is as follows:
201201
\verbatim
202-
gunzip -c data_prep/lm.arpa.gz | \
203-
scripts/find_arpa_oovs.pl data/words.txt > data/oovs.txt
204-
205-
gunzip -c data_prep/lm.arpa.gz | \
206-
grep -v '<s> <s>' | \
207-
grep -v '<s> </s>' | \
208-
grep -v '</s> </s>' | \
209-
../src/lm/arpa2fst - | fstprint | \
210-
scripts/remove_oovs.pl data/oovs.txt | \
211-
scripts/eps2disambig.pl | \
212-
fstcompile --isymbols=data/words.txt --osymbols=data/words.txt \
213-
--keep_isymbols=false --keep_osymbols=false > data/G.fst
214-
fstisstochastic data/G.fst
202+
gunzip -c data_prep/lm.arpa.gz | \
203+
arpa2fst --disambig-symbol=#0 \
204+
--read-symbol-table=data/words.txt - data/G.fst
215205
\endverbatim
216206
The last command (fstisstochastic) is a diagnostic step (see \ref fst_algo_stochastic).
217207
In one typical example, it prints out the numbers:

‎src/doc/online_decoding.dox‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -339,31 +339,16 @@ ngram-count -text train.txt -order 3 -limit-vocab -vocab wordlist -unk \
339339
Now that we have the ARPA format language model trained, we have to compile it
340340
into WFST format. Let's first define the following variables
341341
\verbatim
342+
dict_dir=data/local/dict # The dict directory provided by the online-nnet2 models
342343
lm=srilm.o3g.kn.gz # ARPA format LM you just built.
343344
lang=data/lang # Old lang directory provided by the online-nnet2 models
344345
lang_own=data/lang_own # New lang directory we are going to create, which contains the new language model
345-
lang_own_tmp=data/local/lang_own_tmp/ # Temporary directory.
346346
\endverbatim
347347

348348
Given the above variables, we can compile an ARPA format language model into
349349
WFST format using the following commands
350350
\verbatim
351-
mkdir -p $lang_own_tmp
352-
mkdir -p $lang_own
353-
cp -r $lang/* $lang_own
354-
gunzip -c $lm | utils/find_arpa_oovs.pl $lang_own/words.txt \
355-
> $lang_own_tmp/oovs.txt || exit 1
356-
gunzip -c $lm | \
357-
grep -v '<s> <s>' | \
358-
grep -v '</s> <s>' | \
359-
grep -v '</s> </s>' | \
360-
arpa2fst - | fstprint | \
361-
utils/remove_oovs.pl $lang_own_tmp/oovs.txt | \
362-
utils/eps2disambig.pl | utils/s2eps.pl | \
363-
fstcompile --isymbols=$lang_own/words.txt --osymbols=$lang_own/words.txt \
364-
--keep_isymbols=false --keep_osymbols=false | \
365-
fstrmepsilon | fstarcsort --sort_type=ilabel > $lang_own/G.fst
366-
utils/validate_lang.pl --skip-determinization-check $lang_own || exit 1;
351+
utils/format_lm.sh $lang $lm $dict_dir/lexicon.txt $lang_own
367352
\endverbatim
368353

369354
Now, we can compile the decoding graph with the new language model, using the
@@ -408,6 +393,7 @@ just created, and "<SPOKEN_NOISE>" is the word symbol that represents
408393
out-of-vocabulary words in the lexicon, we can generate the new lang directory
409394
with the updated lexicon using the following command
410395
\verbatim
396+
lang_own_tmp=data/local/lang_own_tmp/ # Temporary directory.
411397
utils/prepare_lang.sh \
412398
--phone-symbol-table $lang/phones.txt \
413399
$dict_own "<SPOKEN_NOISE>" $lang_own_tmp $lang_own

0 commit comments

Comments
 (0)