]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/kaldi.git/blob - egs/swbd/s5c/local/swbd1_train_lms.sh
trunk: adding s5c scripts for switchboard
[processor-sdk/kaldi.git] / egs / swbd / s5c / local / swbd1_train_lms.sh
1 #!/bin/bash
3 # Copyright 2013  Arnab Ghoshal
4 #                 Johns Hopkins University (author: Daniel Povey)
5 #           2014  Guoguo Chen
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #  http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 # WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 # MERCHANTABLITY OR NON-INFRINGEMENT.
17 # See the Apache 2 License for the specific language governing permissions and
18 # limitations under the License.
21 # To be run from one directory above this script.
23 # Begin configuration section.
24 weblm=
25 # end configuration sections
27 help_message="Usage: $0 [options] <train-txt> <dict> <out-dir> [fisher-dirs]
28 Train language models for Switchboard-1, and optionally for Fisher and \n
29 web-data from University of Washington.\n
30 options: 
31   --help          # print this message and exit
32   --weblm DIR     # directory for web-data from University of Washington
33 ";
35 . utils/parse_options.sh
37 if [ $# -lt 3 ]; then
38   printf "$help_message\n";
39   exit 1;
40 fi
42 text=$1     # data/local/train/text
43 lexicon=$2  # data/local/dict/lexicon.txt
44 dir=$3      # data/local/lm
46 shift 3
47 fisher_dirs=( $@ )
49 for f in "$text" "$lexicon"; do
50   [ ! -f $x ] && echo "$0: No such file $f" && exit 1;
51 done
53 loc=`which ngram-count`;
54 if [ -z $loc ]; then
55   if uname -a | grep 64 >/dev/null; then # some kind of 64 bit...
56     sdir=`pwd`/../../../tools/srilm/bin/i686-m64 
57   else
58     sdir=`pwd`/../../../tools/srilm/bin/i686
59   fi
60   if [ -f $sdir/ngram-count ]; then
61     echo Using SRILM tools from $sdir
62     export PATH=$PATH:$sdir
63   else
64     echo You appear to not have SRILM tools installed, either on your path,
65     echo or installed in $sdir.  See tools/install_srilm.sh for installation
66     echo instructions.
67     exit 1
68   fi
69 fi
70     
72 set -o errexit
73 mkdir -p $dir
74 export LC_ALL=C 
76 heldout_sent=10000
77 cut -d' ' -f2- $text | gzip -c > $dir/train.all.gz
78 cut -d' ' -f2- $text | tail -n +$heldout_sent | gzip -c > $dir/train.gz
79 cut -d' ' -f2- $text | head -n $heldout_sent > $dir/heldout
81 cut -d' ' -f1 $lexicon > $dir/wordlist
83 # Trigram language model
84 ngram-count -text $dir/train.gz -order 3 -limit-vocab -vocab $dir/wordlist \
85   -unk -map-unk "<unk>" -kndiscount -interpolate -lm $dir/sw1.o3g.kn.gz
86 echo "PPL for SWBD1 trigram LM:"
87 ngram -unk -lm $dir/sw1.o3g.kn.gz -ppl $dir/heldout
88 ngram -unk -lm $dir/sw1.o3g.kn.gz -ppl $dir/heldout -debug 2 >& $dir/3gram.ppl2
89 # file data/local/lm/heldout: 10000 sentences, 118254 words, 0 OOVs
90 # 0 zeroprobs, logprob= -250952 ppl= 90.5071 ppl1= 132.479
92 # 4gram language model
93 ngram-count -text $dir/train.gz -order 4 -limit-vocab -vocab $dir/wordlist \
94   -unk -map-unk "<unk>" -kndiscount -interpolate -lm $dir/sw1.o4g.kn.gz
95 echo "PPL for SWBD1 4gram LM:"
96 ngram -unk -lm $dir/sw1.o4g.kn.gz -ppl $dir/heldout
97 ngram -unk -lm $dir/sw1.o4g.kn.gz -ppl $dir/heldout -debug 2 >& $dir/4gram.ppl2
98 # file data/local/lm/heldout: 10000 sentences, 118254 words, 0 OOVs
99 # 0 zeroprobs, logprob= -253747 ppl= 95.1632 ppl1= 139.887
101 mkdir -p $dir/fisher
102 rm -rf $dir/fisher/text0
103 for x in ${fisher_dirs[@]}; do
104   [ ! -d $x/data/trans ] \
105     && "$0: Cannot find transcripts in Fisher directory $x" && exit 1;
106   cat $x/data/trans/*/*.txt |\
107     grep -v ^# | grep -v ^$ | cut -d' ' -f4- >> $dir/fisher/text0
108 done
110 if [ -f $dir/fisher/text0 ]; then
111   cat $dir/fisher/text0 | local/fisher_map_words.pl \
112     | gzip -c > $dir/fisher/text1.gz
114   for x in 3 4; do
115     ngram-count -text $dir/fisher/text1.gz -order $x -limit-vocab \
116       -vocab $dir/wordlist -unk -map-unk "<unk>" -kndiscount -interpolate \
117       -lm $dir/fisher/fisher.o${x}g.kn.gz
118     echo "PPL for Fisher ${x}gram LM:"
119     ngram -unk -lm $dir/fisher/fisher.o${x}g.kn.gz -ppl $dir/heldout
120     ngram -unk -lm $dir/fisher/fisher.o${x}g.kn.gz -ppl $dir/heldout -debug 2 \
121       >& $dir/fisher/${x}gram.ppl2
122     compute-best-mix $dir/${x}gram.ppl2 \
123       $dir/fisher/${x}gram.ppl2 >& $dir/sw1_fsh_mix.${x}gram.log
124     grep 'best lambda' $dir/sw1_fsh_mix.${x}gram.log | perl -e '
125       $_=<>;
126       s/.*\(//; s/\).*//;
127       @A = split;
128       die "Expecting 2 numbers; found: $_" if(@A!=2);
129       print "$A[0]\n$A[1]\n";' > $dir/sw1_fsh_mix.${x}gram.weights
130     swb1_weight=$(head -1 $dir/sw1_fsh_mix.${x}gram.weights)
131     fisher_weight=$(tail -n 1 $dir/sw1_fsh_mix.${x}gram.weights)
132     ngram -order $x -lm $dir/sw1.o${x}g.kn.gz -lambda $swb1_weight \
133       -mix-lm $dir/fisher/fisher.o${x}g.kn.gz \
134       -unk -write-lm $dir/sw1_fsh.o${x}g.kn.gz
135     echo "PPL for SWBD1 + Fisher ${x}gram LM:"
136     ngram -unk -lm $dir/sw1_fsh.o${x}g.kn.gz -ppl $dir/heldout
137   done
138 fi
140 if [ ! -z "$weblm" ]; then
141   echo "Interpolating web-LM not implemented yet"
142 fi
144 ## The following takes about 11 minutes to download on Eddie: 
145 # wget --no-check-certificate http://ssli.ee.washington.edu/data/191M_conversational_web-filt+periods.gz