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