]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/kaldi.git/blob - src/fstbin/fstisstochastic.cc
[src] Fix bug in fstrmymbols RE recent const-fst changes (thanks: Jon Nichols); other...
[processor-sdk/kaldi.git] / src / fstbin / fstisstochastic.cc
1 // fstbin/fstisstochastic.cc
3 // Copyright 2009-2011  Microsoft Corporation
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.
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"
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.
43 int main(int argc, char *argv[]) {
44   try {
45     using namespace kaldi;
46     using namespace fst;
47     using kaldi::int32;
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";
55     float delta = 0.01;
56     bool test_in_log = true;
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);
63     if (po.NumArgs() > 1) {
64       po.PrintUsage();
65       exit(1);
66     }
68     std::string fst_in_filename = po.GetOptArg(1);
70     Fst<StdArc> *fst = ReadFstKaldiGeneric(fst_in_filename);
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);
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 }