FST  openfst-1.8.3
OpenFst Library
fstpush-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Pushes weights and/or output labels in an FST toward the initial or final
19 // states.
20 
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 
25 #include <fst/flags.h>
26 #include <fst/log.h>
27 #include <fst/reweight.h>
28 #include <fst/script/fst-class.h>
29 #include <fst/script/getters.h>
30 #include <fst/script/push.h>
31 
32 DECLARE_double(delta);
33 DECLARE_bool(push_weights);
34 DECLARE_bool(push_labels);
35 DECLARE_bool(remove_total_weight);
36 DECLARE_bool(remove_common_affix);
37 DECLARE_string(reweight_type);
38 
39 int fstpush_main(int argc, char **argv) {
40  namespace s = fst::script;
41  using fst::ReweightType;
44 
45  std::string usage = "Pushes weights and/or olabels in an FST.\n\n Usage: ";
46  usage += argv[0];
47  usage += " [in.fst [out.fst]]\n";
48 
49  SET_FLAGS(usage.c_str(), &argc, &argv, true);
50  if (argc > 3) {
51  ShowUsage();
52  return 1;
53  }
54 
55  const std::string in_name =
56  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
57  const std::string out_name =
58  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
59 
60  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
61  if (!ifst) return 1;
62 
63  const auto flags = s::GetPushFlags(FST_FLAGS_push_weights,
64  FST_FLAGS_push_labels,
65  FST_FLAGS_remove_total_weight,
66  FST_FLAGS_remove_common_affix);
67 
68  VectorFstClass ofst(ifst->ArcType());
69 
70  ReweightType reweight_type;
71  if (!s::GetReweightType(FST_FLAGS_reweight_type, &reweight_type)) {
72  LOG(ERROR) << argv[0] << ": Unknown or unsupported reweight type: "
73  << FST_FLAGS_reweight_type;
74  return 1;
75  }
76 
77  s::Push(*ifst, &ofst, flags, reweight_type, FST_FLAGS_delta);
78 
79  return !ofst.Write(out_name);
80 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
int fstpush_main(int argc, char **argv)
Definition: fstpush-main.cc:39
ReweightType
Definition: reweight.h:35
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
DECLARE_double(delta)
void Push(MutableFst< Arc > *fst, ReweightType type=REWEIGHT_TO_INITIAL, float delta=kShortestDelta, bool remove_total_weight=false)
Definition: push.h:94
DECLARE_string(reweight_type)
uint8_t GetPushFlags(bool push_weights, bool push_labels, bool remove_total_weight, bool remove_common_affix)
Definition: getters.h:73
DECLARE_bool(push_weights)
bool GetReweightType(std::string_view str, ReweightType *reweight_type)
Definition: getters.cc:219