FST  openfst-1.7.1
OpenFst Library
fstreweight-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 // Reweights an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <fst/flags.h>
13 #include <fst/script/getters.h>
14 #include <fst/script/reweight.h>
15 #include <fst/script/text-io.h>
16 
17 DECLARE_bool(to_final);
18 
19 int fstreweight_main(int argc, char **argv) {
20  namespace s = fst::script;
23 
24  string usage = "Reweights an FST.\n\n Usage: ";
25  usage += argv[0];
26  usage += " in.fst potential.txt [out.fst]\n";
27 
28  std::set_new_handler(FailedNewHandler);
29  SET_FLAGS(usage.c_str(), &argc, &argv, true);
30  if (argc < 3 || argc > 4) {
31  ShowUsage();
32  return 1;
33  }
34 
35  const string in_name = argv[1];
36  const string potentials_name = argv[2];
37  const string out_name = argc > 3 ? argv[3] : "";
38 
39  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
40  if (!fst) return 1;
41 
42  std::vector<WeightClass> potential;
43  if (!s::ReadPotentials(fst->WeightType(), potentials_name, &potential)) {
44  return 1;
45  }
46 
47  s::Reweight(fst.get(), potential, s::GetReweightType(FLAGS_to_final));
48 
49  return !fst->Write(out_name);
50 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
ReweightType GetReweightType(bool to_final)
Definition: getters.h:69
int fstreweight_main(int argc, char **argv)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DECLARE_bool(to_final)
bool ReadPotentials(const string &weight_type, const string &filename, std::vector< WeightClass > *potentials)
Definition: text-io.cc:20
void Reweight(MutableFst< Arc > *fst, const std::vector< typename Arc::Weight > &potential, ReweightType type)
Definition: reweight.h:28