fstrmepslocal.cc
Go to the documentation of this file.
1 // fstbin/fstrmepslocal.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 "util/text-utils.h"
25 #include "fst/fstlib.h"
27 #include "fstext/fstext-utils.h"
28 #include "fstext/kaldi-fst-io.h"
29 
30 
31 /*
32  A test example:
33  ( echo "0 1 1 0"; echo "1 2 0 2"; echo "2 0"; ) | fstcompile | fstrmepslocal | fstprint
34 # prints:
35 # 0 1 1 2
36 # 1
37  ( echo "0 1 0 0"; echo "0 0"; echo "1 0" ) | fstcompile | fstrmepslocal | fstprint
38 # 0
39  ( echo "0 1 0 0"; echo "0 0"; echo "1 0" ) | fstcompile | fstrmepslocal | fstprint
40  ( echo "0 1 0 0"; echo "0 0"; echo "1 0" ) | fstcompile | fstrmepslocal --use-log=true | fstprint
41 # 0 -0.693147182
42 
43 */
44 
45 int main(int argc, char *argv[]) {
46  try {
47  using namespace kaldi;
48  using namespace fst;
49  using kaldi::int32;
50 
51  const char *usage =
52  "Removes some (but not all) epsilons in an algorithm that will always reduce the number of\n"
53  "arcs+states. Option to preserves equivalence in tropical or log semiring, and\n"
54  "if in tropical, stochasticit in either log or tropical.\n"
55  "\n"
56  "Usage: fstrmepslocal [in.fst [out.fst] ]\n";
57 
58  ParseOptions po(usage);
59  bool use_log = false;
60  bool stochastic_in_log = true;
61  po.Register("use-log", &use_log,
62  "Preserve equivalence in log semiring [false->tropical]\n");
63  po.Register("stochastic-in-log", &stochastic_in_log,
64  "Preserve stochasticity in log semiring [false->tropical]\n");
65  po.Read(argc, argv);
66 
67  if (po.NumArgs() > 2) {
68  po.PrintUsage();
69  exit(1);
70  }
71 
72  std::string fst_in_filename = po.GetOptArg(1),
73  fst_out_filename = po.GetOptArg(2);
74 
75  VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_filename);
76 
77  if (!use_log && stochastic_in_log) {
79  } else if (use_log && !stochastic_in_log) {
80  std::cerr << "fstrmsymbols: invalid combination of flags\n";
81  return 1;
82  } else if (use_log) {
83  VectorFst<LogArc> log_fst;
84  Cast(*fst, &log_fst);
85  delete fst;
86  RemoveEpsLocal(&log_fst);
87  fst = new VectorFst<StdArc>;
88  Cast(log_fst, fst);
89  } else {
90  RemoveEpsLocal(fst);
91  }
92 
93  WriteFstKaldi(*fst, fst_out_filename);
94  delete fst;
95  return 0;
96  } catch(const std::exception &e) {
97  std::cerr << e.what();
98  return -1;
99  }
100 }
101 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
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 RemoveEpsLocalSpecial(MutableFst< StdArc > *fst)
As RemoveEpsLocal but takes care to preserve stochasticity when cast to LogArc.
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
int main(int argc, char *argv[])
std::string GetOptArg(int param) const