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