FST  openfst-1.7.2
OpenFst Library
fstdeterminize-main.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Determinizes an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/script/determinize.h>
13 #include <fst/script/getters.h>
14 
15 DECLARE_double(delta);
16 DECLARE_string(weight);
17 DECLARE_int64(nstate);
18 DECLARE_int64(subsequential_label);
19 DECLARE_string(det_type);
20 DECLARE_bool(increment_subsequential_label);
21 
22 int fstdeterminize_main(int argc, char **argv) {
23  namespace s = fst::script;
28 
29  string usage = "Determinizes an FST.\n\n Usage: ";
30  usage += argv[0];
31  usage += " [in.fst [out.fst]]\n";
32 
33  std::set_new_handler(FailedNewHandler);
34  SET_FLAGS(usage.c_str(), &argc, &argv, true);
35  if (argc > 3) {
36  ShowUsage();
37  return 1;
38  }
39 
40  DeterminizeType det_type;
41  if (!s::GetDeterminizeType(FLAGS_det_type, &det_type)) {
42  LOG(ERROR) << argv[0] << ": Unknown or unsupported determinization type: "
43  << FLAGS_det_type;
44  return 1;
45  }
46 
47  const string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
48  const string out_name = argc > 2 ? argv[2] : "";
49 
50  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
51  if (!ifst) return 1;
52 
53  VectorFstClass ofst(ifst->ArcType());
54 
55  const auto weight_threshold =
56  FLAGS_weight.empty() ? WeightClass::Zero(ifst->WeightType())
57  : WeightClass(ifst->WeightType(), FLAGS_weight);
58 
59  const s::DeterminizeOptions opts(FLAGS_delta, weight_threshold, FLAGS_nstate,
60  FLAGS_subsequential_label, det_type,
61  FLAGS_increment_subsequential_label);
62 
63  s::Determinize(*ifst, &ofst, opts);
64 
65  return !ofst.Write(out_name);
66 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
DECLARE_double(delta)
DECLARE_string(weight)
void Determinize(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, const DeterminizeOptions< Arc > &opts=DeterminizeOptions< Arc >())
Definition: determinize.h:1061
#define LOG(type)
Definition: log.h:48
DECLARE_bool(increment_subsequential_label)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
int fstdeterminize_main(int argc, char **argv)
DECLARE_int64(nstate)
bool GetDeterminizeType(const string &str, DeterminizeType *det_type)
Definition: getters.cc:38
DeterminizeType
Definition: determinize.h:358