From 60fa22578a5db81109b03b261307c3a29931e0fd Mon Sep 17 00:00:00 2001 From: Karel Vesely Date: Mon, 21 Jun 2021 16:20:55 +0200 Subject: [PATCH] Apply suggestions from code review Adding suggestions from code review. Co-authored-by: Cy 'kkm' Katsnelson --- egs/wsj/s5/steps/nnet3/decode_compose.sh | 4 +-- src/latbin/lattice-compose-fsts.cc | 28 ++++++++++------- src/nnet3bin/nnet3-latgen-faster-compose.cc | 35 +++++++++++---------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/egs/wsj/s5/steps/nnet3/decode_compose.sh b/egs/wsj/s5/steps/nnet3/decode_compose.sh index 8d003b60eac..5e3b80adf09 100755 --- a/egs/wsj/s5/steps/nnet3/decode_compose.sh +++ b/egs/wsj/s5/steps/nnet3/decode_compose.sh @@ -48,7 +48,7 @@ echo "$0 $@" # Print the command line for logging if [ $# -ne 3 ]; then echo "Usage: $0 [options] " echo "e.g.: steps/nnet3/decode.sh --nj 8 \\" - echo "--online-ivector-dir exp/nnet2_online/ivectors_test_eval92 \\" + echo " --online-ivector-dir exp/nnet2_online/ivectors_test_eval92 \\" echo " exp/tri4b/graph_bg data/test_eval92_hires $dir/decode_bg_eval92" echo "main options (for others, see top of script file)" echo " --config # config containing options" @@ -67,7 +67,7 @@ fi graphdir=$1 data=$2 dir=$3 -srcdir=`dirname $dir`; # Assume model directory one level up from decoding directory. +srcdir=$(dirname $dir) # Assume model directory one level up from decoding directory. model=$srcdir/$iter.mdl [ -z "$boosting_graphs" ] && echo "Error, \$boosting_graphs have to be set !" && exit 1 diff --git a/src/latbin/lattice-compose-fsts.cc b/src/latbin/lattice-compose-fsts.cc index bbdcc670f01..8fd889e192f 100644 --- a/src/latbin/lattice-compose-fsts.cc +++ b/src/latbin/lattice-compose-fsts.cc @@ -47,12 +47,14 @@ int main(int argc, char *argv[]) { bool write_compact = true; int32 num_states_cache = 50000; - int32 phi_label = fst::kNoLabel; // == -1 - po.Register("write-compact", &write_compact, "If true, write in normal (compact) form."); - po.Register("phi-label", &phi_label, "If >0, the label on backoff arcs of the LM"); + int32 phi_label = fst::kNoLabel; // == -1 + po.Register("write-compact", &write_compact, + "If true, write in normal (compact) form."); + po.Register("phi-label", &phi_label, + "If >0, the label on backoff arcs of the LM"); po.Register("num-states-cache", &num_states_cache, - "Number of states we cache when mapping LM FST to lattice type. " - "More -> more memory but faster."); + "Number of states we cache when mapping LM FST to lattice type." + " More -> more memory but faster."); po.Read(argc, argv); if (po.NumArgs() != 3) { @@ -72,10 +74,11 @@ int main(int argc, char *argv[]) { CompactLatticeWriter compact_lattice_writer; LatticeWriter lattice_writer; - if (write_compact) + if (write_compact) { compact_lattice_writer.Open(lats_wspecifier); - else + } else { lattice_writer.Open(lats_wspecifier); + } if (ClassifyRspecifier(arg2, NULL, NULL) == kNoRspecifier) { std::string fst_rxfilename = arg2; @@ -105,8 +108,11 @@ int main(int argc, char *argv[]) { Lattice lat1 = lattice_reader1.Value(); ArcSort(&lat1, fst::OLabelCompare()); Lattice composed_lat; - if (phi_label > 0) PhiCompose(lat1, mapped_fst2, phi_label, &composed_lat); - else Compose(lat1, mapped_fst2, &composed_lat); + if (phi_label > 0) { + PhiCompose(lat1, mapped_fst2, phi_label, &composed_lat); + } else { + Compose(lat1, mapped_fst2, &composed_lat); + } if (composed_lat.Start() == fst::kNoStateId) { KALDI_WARN << "Empty lattice for utterance " << key << " (incompatible LM?)"; n_fail++; @@ -123,7 +129,7 @@ int main(int argc, char *argv[]) { } delete fst2; } else { - // composing with each utterance with different fst, + // Compose each utterance with its matching (by key) FST. std::string fst_rspecifier2 = arg2; RandomAccessTableReader fst_reader2(fst_rspecifier2); @@ -135,7 +141,7 @@ int main(int argc, char *argv[]) { if (!fst_reader2.HasKey(key)) { KALDI_WARN << "Not producing output for utterance " << key - << " because not present in second table."; + << " because it's not present in second table."; n_fail++; continue; } diff --git a/src/nnet3bin/nnet3-latgen-faster-compose.cc b/src/nnet3bin/nnet3-latgen-faster-compose.cc index ad230540584..7d9a5081dc5 100644 --- a/src/nnet3bin/nnet3-latgen-faster-compose.cc +++ b/src/nnet3bin/nnet3-latgen-faster-compose.cc @@ -20,19 +20,20 @@ // limitations under the License. +#include +#include + +#include + #include "base/kaldi-common.h" -#include "util/common-utils.h" -#include "tree/context-dep.h" -#include "hmm/transition-model.h" -#include "fstext/fstext-lib.h" +#include "base/timer.h" #include "decoder/decoder-wrappers.h" +#include "fstext/fstext-lib.h" +#include "hmm/transition-model.h" #include "nnet3/nnet-am-decodable-simple.h" #include "nnet3/nnet-utils.h" -#include "base/timer.h" - -#include -#include -#include +#include "tree/context-dep.h" +#include "util/common-utils.h" int main(int argc, char *argv[]) { @@ -121,9 +122,10 @@ int main(int argc, char *argv[]) { CompactLatticeWriter compact_lattice_writer; LatticeWriter lattice_writer; if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier) - : lattice_writer.Open(lattice_wspecifier))) + : lattice_writer.Open(lattice_wspecifier))) { KALDI_ERR << "Could not open table for writing lattices: " - << lattice_wspecifier; + << lattice_wspecifier; + } RandomAccessBaseFloatMatrixReader online_ivector_reader( online_ivector_rspecifier); @@ -213,13 +215,13 @@ int main(int argc, char *argv[]) { timer_compose.Reset(); - // RmEpsilon saved 30% of composition runtime... + // RmEpsilon saved 30% of composition runtime. // - Note: we are loading 2-state graphs with eps back-link to the initial state. if (boosting_fst.Properties(fst::kIEpsilons, true) != 0) { fst::RmEpsilon(&boosting_fst); } - // make sure boosting graph is sorted on ilabel, + // Make sure boosting graph is sorted on ilabel. if (boosting_fst.Properties(fst::kILabelSorted, true) == 0) { fst::ILabelCompare ilabel_comp; fst::ArcSort(&boosting_fst, ilabel_comp); @@ -267,7 +269,9 @@ int main(int argc, char *argv[]) { tot_like += like; frame_count += nnet_decodable.NumFramesReady(); num_success++; - } else num_fail++; + } else { + ++num_fail; + } } } @@ -286,8 +290,7 @@ int main(int argc, char *argv[]) { << (tot_like / frame_count) << " over " << frame_count << " frames."; - if (num_success != 0) return 0; - else return 1; + return num_success != 0 ? 0 : 1; } catch(const std::exception &e) { std::cerr << e.what(); return -1;