fstisstochastic.cc
Go to the documentation of this file.
1 // fstbin/fstisstochastic.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
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.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/kaldi-io.h"
23 #include "util/parse-options.h"
24 #include "fst/fstlib.h"
25 #include "fstext/fstext-utils.h"
26 #include "fstext/kaldi-fst-io.h"
27 
28 // e.g. of test:
29 // echo " 0 0" | fstcompile | fstisstochastic
30 // should return 0 and print "0 0" [meaning, min and
31 // max weight are one = exp(0)]
32 // echo " 0 1" | fstcompile | fstisstochastic
33 // should return 1, not stochastic, and print 1 1
34 // (echo "0 0 0 0 0.693147 "; echo "0 1 0 0 0.693147 "; echo "1 0" ) | fstcompile | fstisstochastic
35 // should return 0, stochastic; it prints "0 -1.78e-07" for me
36 // (echo "0 0 0 0 0.693147 "; echo "0 1 0 0 0.693147 "; echo "1 0" ) | fstcompile | fstisstochastic --test-in-log=false
37 // should return 1, not stochastic in tropical; it prints "0 0.693147" for me
38 // (echo "0 0 0 0 0 "; echo "0 1 0 0 0 "; echo "1 0" ) | fstcompile | fstisstochastic --test-in-log=false
39 // should return 0, stochastic in tropical; it prints "0 0" for me
40 // (echo "0 0 0 0 0.693147 "; echo "0 1 0 0 0.693147 "; echo "1 0" ) | fstcompile | fstisstochastic --test-in-log=false --delta=1
41 // returns 0 even though not stochastic because we gave it an absurdly large delta.
42 
43 int main(int argc, char *argv[]) {
44  try {
45  using namespace kaldi;
46  using namespace fst;
47  using kaldi::int32;
48 
49  const char *usage =
50  "Checks whether an FST is stochastic and exits with success if so.\n"
51  "Prints out maximum error (in log units).\n"
52  "\n"
53  "Usage: fstisstochastic [ in.fst ]\n";
54 
55  float delta = 0.01;
56  bool test_in_log = true;
57 
58  ParseOptions po(usage);
59  po.Register("delta", &delta, "Maximum error to accept.");
60  po.Register("test-in-log", &test_in_log, "Test stochasticity in log semiring.");
61  po.Read(argc, argv);
62 
63  if (po.NumArgs() > 1) {
64  po.PrintUsage();
65  exit(1);
66  }
67 
68  std::string fst_in_filename = po.GetOptArg(1);
69 
70  Fst<StdArc> *fst = ReadFstKaldiGeneric(fst_in_filename);
71 
72  bool ans;
73  StdArc::Weight min, max;
74  if (test_in_log) ans = IsStochasticFstInLog(*fst, delta, &min, &max);
75  else ans = IsStochasticFst(*fst, delta, &min, &max);
76 
77  std::cout << min.Value() << " " << max.Value() << '\n';
78  delete fst;
79  if (ans) return 0; // success;
80  else return 1;
81  } catch(const std::exception &e) {
82  std::cerr << e.what();
83  return -1;
84  }
85 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
bool IsStochasticFstInLog(const Fst< StdArc > &fst, float delta, StdArc::Weight *min_sum, StdArc::Weight *max_sum)
bool IsStochasticFst(const Fst< LogArc > &fst, float delta, LogArc::Weight *min_sum, LogArc::Weight *max_sum)
fst::StdArc::Weight Weight
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
std::string GetOptArg(int param) const