fstaddsubsequentialloop.cc
Go to the documentation of this file.
1 // fstbin/fstaddsubsequentialloop.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"
26 #include "fstext/fstext-utils.h"
27 #include "fstext/context-fst.h"
28 #include "fstext/kaldi-fst-io.h"
29 
30 
31 
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 */
36 
37 int main(int argc, char *argv[]) {
38  try {
39  using namespace kaldi;
40  using namespace fst;
41  using kaldi::int32;
42 
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";
48 
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);
54 
55  if (po.NumArgs() < 1 || po.NumArgs() > 3) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  int32 subseq_sym;
61  if (!ConvertStringToInteger(po.GetArg(1), &subseq_sym))
62  KALDI_ERR << "Invalid subsequential symbol "<<po.GetArg(1);
63 
64  std::string fst_in_filename = po.GetOptArg(2);
65 
66  std::string fst_out_filename = po.GetOptArg(3);
67 
68  VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_filename);
69 
71  if (subseq_sym <= h) {
72  std::cerr << "fstaddsubsequentialloop.cc: subseq symbol does not seem right, "<<subseq_sym<<" <= "<<h<<'\n';
73  }
74 
75  AddSubsequentialLoop(subseq_sym, fst);
76 
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 }
85 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
Definition: text-utils.h:118
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.
#define KALDI_ERR
Definition: kaldi-error.h:147
void AddSubsequentialLoop(StdArc::Label subseq_symbol, MutableFst< StdArc > *fst)
Modifies an FST so that it transuces the same paths, but the input side of the paths can all have the...
Definition: context-fst.cc:297
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.
int main(int argc, char *argv[])
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
std::string GetOptArg(int param) const