weight-silence-post.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "hmm/hmm-utils.h"
#include "hmm/posterior.h"
Include dependency graph for weight-silence-post.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 29 of file weight-silence-post.cc.

References kaldi::ConvertStringToReal(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), kaldi::SplitStringToIntegers(), SequentialTableReader< Holder >::Value(), kaldi::WeightSilencePost(), kaldi::WeightSilencePostDistributed(), and TableWriter< Holder >::Write().

29  {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  try {
33  const char *usage =
34  "Apply weight to silences in posts\n"
35  "Usage: weight-silence-post [options] <silence-weight> <silence-phones> "
36  "<model> <posteriors-rspecifier> <posteriors-wspecifier>\n"
37  "e.g.:\n"
38  " weight-silence-post 0.0 1:2:3 1.mdl ark:1.post ark:nosil.post\n";
39 
40  ParseOptions po(usage);
41 
42  bool distribute = false;
43 
44  po.Register("distribute", &distribute, "If true, rather than weighting the "
45  "individual posteriors, apply the weighting to the whole frame: "
46  "i.e. on time t, scale all posterior entries by "
47  "p(sil)*silence-weight + p(non-sil)*1.0");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 5) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56 
57  std::string silence_weight_str = po.GetArg(1),
58  silence_phones_str = po.GetArg(2),
59  model_rxfilename = po.GetArg(3),
60  posteriors_rspecifier = po.GetArg(4),
61  posteriors_wspecifier = po.GetArg(5);
62 
63  BaseFloat silence_weight = 0.0;
64  if (!ConvertStringToReal(silence_weight_str, &silence_weight))
65  KALDI_ERR << "Invalid silence-weight parameter: expected float, got \""
66  << silence_weight_str << '"';
67  std::vector<int32> silence_phones;
68  if (!SplitStringToIntegers(silence_phones_str, ":", false, &silence_phones))
69  KALDI_ERR << "Invalid silence-phones string " << silence_phones_str;
70  if (silence_phones.empty())
71  KALDI_WARN <<"No silence phones, this will have no effect";
72  ConstIntegerSet<int32> silence_set(silence_phones); // faster lookup.
73 
74  TransitionModel trans_model;
75  ReadKaldiObject(model_rxfilename, &trans_model);
76 
77  int32 num_posteriors = 0;
78  SequentialPosteriorReader posterior_reader(posteriors_rspecifier);
79  PosteriorWriter posterior_writer(posteriors_wspecifier);
80 
81  for (; !posterior_reader.Done(); posterior_reader.Next()) {
82  num_posteriors++;
83  // Posterior is vector<vector<pair<int32, BaseFloat> > >
84  Posterior post = posterior_reader.Value();
85  // Posterior is vector<vector<pair<int32, BaseFloat> > >
86  if (distribute)
87  WeightSilencePostDistributed(trans_model, silence_set,
88  silence_weight, &post);
89  else
90  WeightSilencePost(trans_model, silence_set,
91  silence_weight, &post);
92 
93  posterior_writer.Write(posterior_reader.Key(), post);
94  }
95  KALDI_LOG << "Done " << num_posteriors << " posteriors.";
96  return (num_posteriors != 0 ? 0 : 1);
97  } catch(const std::exception &e) {
98  std::cerr << e.what();
99  return -1;
100  }
101 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
void WeightSilencePost(const TransitionModel &trans_model, const ConstIntegerSet< int32 > &silence_set, BaseFloat silence_scale, Posterior *post)
Weight any silence phones in the posterior (i.e.
Definition: posterior.cc:375
float BaseFloat
Definition: kaldi-types.h:29
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:42
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void WeightSilencePostDistributed(const TransitionModel &trans_model, const ConstIntegerSet< int32 > &silence_set, BaseFloat silence_scale, Posterior *post)
This is similar to WeightSilencePost, except that on each frame it works out the amount by which the ...
Definition: posterior.cc:398
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:147
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
Definition: text-utils.cc:238
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_LOG
Definition: kaldi-error.h:153