scale-post.cc
Go to the documentation of this file.
1 // bin/scale-post.cc
2 
3 // Copyright 2011 Chao Weng
4 // 2013 Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "hmm/posterior.h"
25 
26 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31 
32  const char *usage =
33  "Scale posteriors with either a global scale, or a different scale for "
34  " each utterance.\n"
35  "Usage: scale-post <post-rspecifier> (<scale-rspecifier>|<scale>) <post-wspecifier>\n";
36 
37  ParseOptions po(usage);
38  po.Read(argc, argv);
39 
40  if (po.NumArgs() != 3) {
41  po.PrintUsage();
42  exit(1);
43  }
44 
45  std::string post_rspecifier = po.GetArg(1),
46  scale_or_scale_rspecifier = po.GetArg(2),
47  post_wspecifier = po.GetArg(3);
48 
49  double global_scale = 0.0;
50  if (ClassifyRspecifier(scale_or_scale_rspecifier, NULL, NULL) == kNoRspecifier) {
51  // treat second arg as a floating-point scale.
52  if (!ConvertStringToReal(scale_or_scale_rspecifier, &global_scale))
53  KALDI_ERR << "Bad second argument " << scale_or_scale_rspecifier
54  << " (expected scale or scale rspecifier)";
55  scale_or_scale_rspecifier = ""; // So the archive won't be opened.
56  }
57 
58 
59  SequentialPosteriorReader posterior_reader(post_rspecifier);
60  RandomAccessBaseFloatReader scale_reader(scale_or_scale_rspecifier);
61  PosteriorWriter posterior_writer(post_wspecifier);
62 
63  int32 num_scaled = 0, num_no_scale = 0;
64 
65  for (; !posterior_reader.Done(); posterior_reader.Next()) {
66  std::string key = posterior_reader.Key();
67  Posterior posterior = posterior_reader.Value();
68  posterior_reader.FreeCurrent();
69  if (scale_or_scale_rspecifier != "" && !scale_reader.HasKey(key)) {
70  num_no_scale++;
71  } else {
72  BaseFloat post_scale = (scale_or_scale_rspecifier == "" ? global_scale
73  : scale_reader.Value(key));
74  ScalePosterior(post_scale, &posterior);
75  num_scaled++;
76  posterior_writer.Write(key, posterior);
77  }
78  }
79  KALDI_LOG << "Done " << num_scaled << " posteriors; " << num_no_scale
80  << " had no scales.";
81  return (num_scaled != 0 ? 0 : 1);
82  } catch(const std::exception &e) {
83  std::cerr << e.what();
84  return -1;
85  }
86 }
87 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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 Write(const std::string &key, const T &value) const
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#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
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
void ScalePosterior(BaseFloat scale, Posterior *post)
Scales the BaseFloat (weight) element in the posterior entries.
Definition: posterior.cc:218
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
Definition: scale-post.cc:27
#define KALDI_LOG
Definition: kaldi-error.h:153