1// fstpush.cc
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// Copyright 2005-2010 Google, Inc.
16// Author: riley@google.com (Michael Riley)
17// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18// \file
19// Pushes weights and/or output labels in an FST toward the initial or
20// final states.
21
22#include <fst/script/push.h>
23
24DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
25DEFINE_bool(push_weights, false, "Push weights");
26DEFINE_bool(push_labels, false, "Push output labels");
27DEFINE_bool(remove_total_weight, false,
28            "Remove total weight when pushing weights");
29DEFINE_bool(remove_common_affix, false,
30            "Remove common prefix/suffix when pushing labels");
31DEFINE_bool(to_final, false, "Push/reweight to final (vs. to initial) states");
32
33
34int main(int argc, char **argv) {
35  namespace s = fst::script;
36  using fst::script::FstClass;
37  using fst::script::VectorFstClass;
38
39  string usage = "Pushes weights and/or olabels in an FST.\n\n  Usage: ";
40  usage += argv[0];
41  usage += " [in.fst [out.fst]]\n";
42
43  std::set_new_handler(FailedNewHandler);
44  SET_FLAGS(usage.c_str(), &argc, &argv, true);
45  if (argc > 3) {
46    ShowUsage();
47    return 1;
48  }
49
50  string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
51  string out_name = argc > 2 ? argv[2] : "";
52
53  FstClass *ifst = FstClass::Read(in_name);
54  if (!ifst) return 1;
55
56  uint32 flags = 0;
57  if (FLAGS_push_weights)
58    flags |= fst::kPushWeights;
59  if (FLAGS_push_labels)
60    flags |= fst::kPushLabels;
61  if (FLAGS_remove_total_weight)
62    flags |= fst::kPushRemoveTotalWeight;
63  if (FLAGS_remove_common_affix)
64    flags |= fst::kPushRemoveCommonAffix;
65
66  VectorFstClass ofst(ifst->ArcType());
67
68  if (FLAGS_to_final) {
69    s::Push(*ifst, &ofst, flags, fst::REWEIGHT_TO_FINAL, FLAGS_delta);
70  } else {
71    s::Push(*ifst, &ofst, flags, fst::REWEIGHT_TO_INITIAL, FLAGS_delta);
72  }
73
74  ofst.Write(out_name);
75
76  return 0;
77}
78