FST  openfst-1.7.3
OpenFst Library
fstencode-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 // Encode transducer labels and/or weights.
5 
6 #include <cstring>
7 #include <memory>
8 #include <string>
9 
10 #include <fst/flags.h>
11 #include <fst/script/decode.h>
12 #include <fst/script/encode.h>
13 #include <fst/script/getters.h>
14 
15 DECLARE_bool(encode_labels);
16 DECLARE_bool(encode_weights);
17 DECLARE_bool(encode_reuse);
18 DECLARE_bool(decode);
19 
20 int fstencode_main(int argc, char **argv) {
21  namespace s = fst::script;
24 
25  std::string usage = "Encodes transducer labels and/or weights.\n\n Usage: ";
26  usage += argv[0];
27  usage += " in.fst codex [out.fst]\n";
28 
29  std::set_new_handler(FailedNewHandler);
30  SET_FLAGS(usage.c_str(), &argc, &argv, true);
31  if (argc < 3 || argc > 4) {
32  ShowUsage();
33  return 1;
34  }
35 
36  const std::string in_name = (strcmp(argv[1], "-") != 0) ? argv[1] : "";
37  const std::string codex_name = argv[2];
38  const std::string out_name =
39  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
40 
41  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
42  if (!fst) return 1;
43 
44  if (FLAGS_decode) {
45  s::Decode(fst.get(), codex_name);
46  return !fst->Write(out_name);
47  } else {
48  const auto flags =
49  s::GetEncodeFlags(FLAGS_encode_labels, FLAGS_encode_weights);
50  s::Encode(fst.get(), flags, FLAGS_encode_reuse, codex_name);
51  return !fst->Write(out_name);
52  }
53 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
void Encode(MutableFst< Arc > *fst, EncodeMapper< Arc > *mapper)
Definition: encode.h:419
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
uint32 GetEncodeFlags(bool encode_labels, bool encode_weights)
Definition: getters.h:38
DECLARE_bool(encode_labels)
int fstencode_main(int argc, char **argv)
void Decode(MutableFst< Arc > *fst, const EncodeMapper< Arc > &mapper)
Definition: encode.h:426