FST  openfst-1.7.2
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 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/script/decode.h>
13 #include <fst/script/encode.h>
14 #include <fst/script/getters.h>
15 
16 DECLARE_bool(encode_labels);
17 DECLARE_bool(encode_weights);
18 DECLARE_bool(encode_reuse);
19 DECLARE_bool(decode);
20 
21 int fstencode_main(int argc, char **argv) {
22  namespace s = fst::script;
25 
26  string usage = "Encodes transducer labels and/or weights.\n\n Usage: ";
27  usage += argv[0];
28  usage += " in.fst codex [out.fst]\n";
29 
30  std::set_new_handler(FailedNewHandler);
31  SET_FLAGS(usage.c_str(), &argc, &argv, true);
32  if (argc < 3 || argc > 4) {
33  ShowUsage();
34  return 1;
35  }
36 
37  const string in_name = (strcmp(argv[1], "-") != 0) ? argv[1] : "";
38  const string codex_name = argv[2];
39  const string out_name = argc > 3 ? 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:124
void Encode(MutableFst< Arc > *fst, EncodeMapper< Arc > *mapper)
Definition: encode.h:419
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
uint32 GetEncodeFlags(bool encode_labels, bool encode_weights)
Definition: getters.h:39
DECLARE_bool(encode_labels)
int fstencode_main(int argc, char **argv)
void Decode(MutableFst< Arc > *fst, const EncodeMapper< Arc > &mapper)
Definition: encode.h:426