]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/kaldi.git/blob - src/fstbin/fstaddselfloops.cc
[src] Fix bug in fstrmymbols RE recent const-fst changes (thanks: Jon Nichols); other...
[processor-sdk/kaldi.git] / src / fstbin / fstaddselfloops.cc
1 // fstbin/fstaddselfloops.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/common-utils.h"
23 #include "fst/fstlib.h"
24 #include "fstext/determinize-star.h"
25 #include "fstext/fstext-utils.h"
26 #include "fstext/kaldi-fst-io.h"
28 /* some test examples:
29   pushd ~/tmpdir
30  ( echo 3; echo  4) > in.list
31  ( echo 5; echo  6) > out.list
32  ( echo "0 0 0 0"; echo "0 0" ) | fstcompile | fstaddselfloops in.list out.list | fstprint
33  ( echo "0 1 0 1"; echo " 0 2 1 0"; echo "1 0"; echo "2 0"; ) | fstcompile | fstaddselfloops in.list out.list | fstprint
34 */
36 int main(int argc, char *argv[]) {
37   try {
38     using namespace kaldi;
39     using namespace fst;
40     using kaldi::int32;
42     const char *usage =
43         "Adds self-loops to states of an FST to propagate disambiguation symbols through it\n"
44         "They are added on each final state and each state with non-epsilon output symbols\n"
45         "on at least one arc out of the state.  Useful in conjunction with predeterminize\n"
46         "\n"
47         "Usage:  fstaddselfloops in-disambig-list out-disambig-list  [in.fst [out.fst] ]\n"
48         "E.g:  fstaddselfloops in.list out.list < in.fst > withloops.fst\n"
49         "in.list and out.list are lists of integers, one per line, of the\n"
50         "same length.\n";
52     ParseOptions po(usage);
53     po.Read(argc, argv);
55     if (po.NumArgs() < 2 || po.NumArgs() > 4) {
56       po.PrintUsage();
57       exit(1);
58     }
60     std::string disambig_in_rxfilename = po.GetArg(1),
61         disambig_out_rxfilename = po.GetArg(2),
62         fst_in_filename = po.GetOptArg(3),
63         fst_out_filename = po.GetOptArg(4);
65     VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_filename);
67     std::vector<int32> disambig_in;
68     if (!ReadIntegerVectorSimple(disambig_in_rxfilename, &disambig_in))
69       KALDI_ERR << "fstaddselfloops: Could not read disambiguation symbols from "
70                  << kaldi::PrintableRxfilename(disambig_in_rxfilename);
72     std::vector<int32> disambig_out;
73     if (!ReadIntegerVectorSimple(disambig_out_rxfilename, &disambig_out))
74       KALDI_ERR << "fstaddselfloops: Could not read disambiguation symbols from "
75                 << kaldi::PrintableRxfilename(disambig_out_rxfilename);
77     if (disambig_in.size() != disambig_out.size())
78       KALDI_ERR << "fstaddselfloops: mismatch in size of disambiguation symbols";
80     AddSelfLoops(fst, disambig_in, disambig_out);
82     WriteFstKaldi(*fst, fst_out_filename);
84     delete fst;
86     return 0;
87   } catch(const std::exception &e) {
88     std::cerr << e.what();
89     return -1;
90   }
91   return 0;
92 }