FST  openfst-1.7.1
OpenFst Library
fstloglinearapply.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 #include <fst/compat.h>
7 #include <fst/vector-fst.h>
8 
9 #include <fst/flags.h>
10 #include <fst/log.h>
11 
12 DEFINE_bool(normalize, true, "Normalize to get posterior");
13 
14 int main(int argc, char **argv) {
15  string usage =
16  "Applies an FST to another FST, treating the second as a log-linear "
17  "model.\n\n "
18  "Usage: ";
19  usage += argv[0];
20  usage += " in.fst linear.fst [out.fst]\n";
21 
22  std::set_new_handler(FailedNewHandler);
23  SET_FLAGS(usage.c_str(), &argc, &argv, true);
24  if (argc < 3 || argc > 4) {
25  ShowUsage();
26  return 1;
27  }
28 
29  string in_name = strcmp(argv[1], "-") != 0 ? argv[1] : "";
30  string linear_name = (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
31  string out_name = argc > 3 ? argv[3] : "";
32 
33  if (in_name.empty() && linear_name.empty()) {
34  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
35  return 1;
36  }
37 
38  fst::StdFst *ifst1 = fst::StdFst::Read(in_name);
39  if (!ifst1) return 1;
40 
41  fst::StdFst *ifst2 = fst::StdFst::Read(linear_name);
42  if (!ifst2) return 1;
43 
44  fst::StdVectorFst ofst;
45 
46  LogLinearApply(*ifst1, *ifst2, &ofst, FLAGS_normalize);
47 
48  ofst.Write(out_name);
49 
50  return 0;
51 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
int main(int argc, char **argv)
bool Write(std::ostream &strm, const FstWriteOptions &opts) const override
Definition: vector-fst.h:554
#define LOG(type)
Definition: log.h:48
void LogLinearApply(const Fst< A > &ifst, const Fst< A > &lfst, MutableFst< A > *ofst, bool normalize=true)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
static Fst< Arc > * Read(std::istream &strm, const FstReadOptions &opts)
Definition: fst.h:234
DEFINE_bool(normalize, true,"Normalize to get posterior")