fstdeterminizestar.cc
Go to the documentation of this file.
1 // fstbin/fstdeterminizestar.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/common-utils.h"
23 #include "fst/fstlib.h"
25 #include "fstext/fstext-utils.h"
26 #include "fstext/kaldi-fst-io.h"
27 #if !defined(_MSC_VER) && !defined(__APPLE__)
28 #include <signal.h> // Comment this line and the call to signal below if
29 // it causes compilation problems. It is only to enable a debugging procedure
30 // when determinization does not terminate. We are disabling this code if
31 // compiling on Windows because signal.h is not available there, and on
32 // MacOS due to a problem with <signal.h> in the initial release of Sierra.
33 #endif
34 
35 /* some test examples:
36  ( echo "0 0 0 0"; echo "0 0" ) | fstcompile | fstdeterminizestar | fstprint
37  ( echo "0 0 1 0"; echo "0 0" ) | fstcompile | fstdeterminizestar | fstprint
38  ( echo "0 0 1 0"; echo "0 1 1 0"; echo "0 0" ) | fstcompile | fstdeterminizestar | fstprint
39  # this last one fails [correctly]:
40  ( echo "0 0 0 1"; echo "0 0" ) | fstcompile | fstdeterminizestar | fstprint
41 
42  cd ~/tmpdir
43  while true; do
44  fstrand > 1.fst
45  fstpredeterminize out.lst 1.fst | fstdeterminizestar | fstrmsymbols out.lst > 2.fst
46  fstequivalent --random=true 1.fst 2.fst || echo "Test failed"
47  echo -n "."
48  done
49 
50  Test of debugging [with non-determinizable input]:
51  ( echo " 0 0 1 0 1.0"; echo "0 1 1 0"; echo "1 1 1 0 0"; echo "0 2 2 0"; echo "2"; echo "1" ) | fstcompile | fstdeterminizestar
52  kill -SIGUSR1 [the process-id of fstdeterminizestar]
53  # prints out a bunch of debugging output showing the mess it got itself into.
54 */
55 
56 
57 bool debug_location = false;
58 void signal_handler(int) {
59  debug_location = true;
60 }
61 
62 
63 
64 int main(int argc, char *argv[]) {
65  try {
66  using namespace kaldi;
67  using namespace fst;
68  using kaldi::int32;
69 
70  const char *usage =
71  "Removes epsilons and determinizes in one step\n"
72  "\n"
73  "Usage: fstdeterminizestar [in.fst [out.fst] ]\n"
74  "\n"
75  "See also: fstdeterminizelog, lattice-determinize\n";
76 
77  float delta = kDelta;
78  int max_states = -1;
79  bool use_log = false;
80  ParseOptions po(usage);
81  po.Register("use-log", &use_log, "Determinize in log semiring.");
82  po.Register("delta", &delta, "Delta value used to determine equivalence of weights.");
83  po.Register("max-states", &max_states, "Maximum number of states in determinized FST before it will abort.");
84  po.Read(argc, argv);
85 
86  if (po.NumArgs() > 2) {
87  po.PrintUsage();
88  exit(1);
89  }
90 
91  std::string fst_in_str = po.GetOptArg(1),
92  fst_out_str = po.GetOptArg(2);
93 
94  // This enables us to get traceback info from determinization that is
95  // not seeming to terminate.
96 #if !defined(_MSC_VER) && !defined(__APPLE__)
97  signal(SIGUSR1, signal_handler);
98 #endif
99  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
100  // Normal case: just files.
101  VectorFst<StdArc> *fst = ReadFstKaldi(fst_in_str);
102 
103  ArcSort(fst, ILabelCompare<StdArc>()); // improves speed.
104  if (use_log) {
105  DeterminizeStarInLog(fst, delta, &debug_location, max_states);
106  } else {
107  VectorFst<StdArc> det_fst;
108  DeterminizeStar(*fst, &det_fst, delta, &debug_location, max_states);
109  *fst = det_fst; // will do shallow copy and then det_fst goes
110  // out of scope anyway.
111  }
112  WriteFstKaldi(*fst, fst_out_str);
113  delete fst;
114  } else { // Dealing with archives.
115  SequentialTableReader<VectorFstHolder> fst_reader(fst_in_str);
116  TableWriter<VectorFstHolder> fst_writer(fst_out_str);
117  for (; !fst_reader.Done(); fst_reader.Next()) {
118  std::string key = fst_reader.Key();
119  VectorFst<StdArc> fst(fst_reader.Value());
120  fst_reader.FreeCurrent();
121  ArcSort(&fst, ILabelCompare<StdArc>()); // improves speed.
122  try {
123  if (use_log) {
124  DeterminizeStarInLog(&fst, delta, &debug_location, max_states);
125  } else {
126  VectorFst<StdArc> det_fst;
127  DeterminizeStar(fst, &det_fst, delta, &debug_location, max_states);
128  fst = det_fst; // will do shallow copy and then det_fst goes out
129  // of scope anyway.
130  }
131  fst_writer.Write(key, fst);
132  } catch (const std::runtime_error &e) {
133  KALDI_WARN << "Error during determinization for key " << key;
134  }
135  }
136  }
137  return 0;
138  } catch(const std::exception &e) {
139  std::cerr << e.what();
140  return -1;
141  }
142 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
void DeterminizeStarInLog(VectorFst< StdArc > *fst, float delta, bool *debug_ptr, int max_states)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
void signal_handler(int)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_WARN
Definition: kaldi-error.h:150
bool debug_location
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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)
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...
std::string GetOptArg(int param) const