1// fstreweight.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: allauzen@google.com (Cyril Allauzen)
17// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18//
19// \file
20// Reweights an FST.
21//
22
23#include <fst/script/reweight.h>
24#include <fst/script/text-io.h>
25
26DEFINE_bool(to_final, false, "Push/reweight to final (vs. to initial) states");
27
28int main(int argc, char **argv) {
29  namespace s = fst::script;
30  using fst::script::FstClass;
31  using fst::script::MutableFstClass;
32
33  string usage = "Reweights an FST.\n\n  Usage: ";
34  usage += argv[0];
35  usage += " in.fst potential.txt [out.fst]\n";
36
37  std::set_new_handler(FailedNewHandler);
38  SetFlags(usage.c_str(), &argc, &argv, true);
39  if (argc < 3 || argc > 4) {
40    ShowUsage();
41    return 1;
42  }
43
44  string in_fname = argv[1];
45  string potentials_fname = argv[2];
46  string out_fname = argc > 3 ? argv[3] : "";
47
48  MutableFstClass *fst = MutableFstClass::Read(in_fname, true);
49  if (!fst) return 1;
50
51  vector<s::WeightClass> potential;
52  if (!s::ReadPotentials(fst->WeightType(), potentials_fname, &potential))
53    return 1;
54
55  fst::ReweightType reweight_type = FLAGS_to_final ?
56      fst::REWEIGHT_TO_FINAL :
57      fst::REWEIGHT_TO_INITIAL;
58
59  s::Reweight(fst, potential, reweight_type);
60  fst->Write(out_fname);
61
62  return 0;
63}
64