FST  openfst-1.7.3
OpenFst Library
fstpush-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 // Pushes weights and/or output labels in an FST toward the initial or final
5 // states.
6 
7 #include <cstring>
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/script/getters.h>
13 #include <fst/script/push.h>
14 
15 DECLARE_double(delta);
16 DECLARE_bool(push_weights);
17 DECLARE_bool(push_labels);
18 DECLARE_bool(remove_total_weight);
19 DECLARE_bool(remove_common_affix);
20 DECLARE_bool(to_final);
21 
22 int fstpush_main(int argc, char **argv) {
23  namespace s = fst::script;
26 
27  std::string usage = "Pushes weights and/or olabels in an FST.\n\n Usage: ";
28  usage += argv[0];
29  usage += " [in.fst [out.fst]]\n";
30 
31  std::set_new_handler(FailedNewHandler);
32  SET_FLAGS(usage.c_str(), &argc, &argv, true);
33  if (argc > 3) {
34  ShowUsage();
35  return 1;
36  }
37 
38  const std::string in_name =
39  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
40  const std::string out_name =
41  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
42 
43  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
44  if (!ifst) return 1;
45 
46  const auto flags =
47  s::GetPushFlags(FLAGS_push_weights, FLAGS_push_labels,
48  FLAGS_remove_total_weight, FLAGS_remove_common_affix);
49 
50  VectorFstClass ofst(ifst->ArcType());
51 
52  s::Push(*ifst, &ofst, flags, s::GetReweightType(FLAGS_to_final),
53  FLAGS_delta);
54 
55  return !ofst.Write(out_name);
56 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
int fstpush_main(int argc, char **argv)
Definition: fstpush-main.cc:22
ReweightType GetReweightType(bool to_final)
Definition: getters.h:68
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
DECLARE_double(delta)
uint32 GetPushFlags(bool push_weights, bool push_labels, bool remove_total_weight, bool remove_common_affix)
Definition: getters.h:53
DECLARE_bool(push_weights)
void Push(MutableFst< Arc > *fst, ReweightType type, float delta=kDelta, bool remove_total_weight=false)
Definition: push.h:77