1 // fstbin/fstaddsubsequentialloop.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 "util/text-utils.h"
25 #include "fst/fstlib.h"
26 #include "fstext/fstext-utils.h"
27 #include "fstext/context-fst.h"
28 #include "fstext/kaldi-fst-io.h"
32 /* some test examples:
33 ( echo "0 0 0 0"; echo "0 0" ) | fstcompile | fstaddsubsequentialloop 1 | fstprint
34 ( echo "0 1 0 0"; echo " 0 2 0 0"; echo "1 0"; echo "2 0"; ) | fstcompile | fstaddsubsequentialloop 1 | fstprint
35 */
37 int main(int argc, char *argv[]) {
38 try {
39 using namespace kaldi;
40 using namespace fst;
41 using kaldi::int32;
43 const char *usage =
44 "Minimizes FST after encoding [this algorithm applicable to all FSTs in tropical semiring]\n"
45 "\n"
46 "Usage: fstaddsubsequentialloop subseq_sym [in.fst [out.fst] ]\n"
47 "E.g.: fstaddsubsequentialloop 52 < LG.fst > LG_sub.fst\n";
49 float delta = kDelta;
50 ParseOptions po(usage);
51 po.Register("delta", &delta,
52 "Delta likelihood used for quantization of weights");
53 po.Read(argc, argv);
55 if (po.NumArgs() < 1 || po.NumArgs() > 3) {
56 po.PrintUsage();
57 exit(1);
58 }
60 int32 subseq_sym;
61 if (!ConvertStringToInteger(po.GetArg(1), &subseq_sym))
62 KALDI_ERR << "Invalid subsequential symbol "<<po.GetArg(1);
64 std::string fst_in_filename = po.GetOptArg(2);
66 std::string fst_out_filename = po.GetOptArg(3);
68 VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_filename);
70 int32 h = HighestNumberedInputSymbol(*fst);
71 if (subseq_sym <= h) {
72 std::cerr << "fstaddsubsequentialloop.cc: subseq symbol does not seem right, "<<subseq_sym<<" <= "<<h<<'\n';
73 }
75 AddSubsequentialLoop(subseq_sym, fst);
77 WriteFstKaldi(*fst, fst_out_filename);
78 delete fst;
79 return 0;
80 } catch(const std::exception &e) {
81 std::cerr << e.what();
82 return -1;
83 }
84 }