FST  openfst-1.7.1
OpenFst Library
fstcompress.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 // Compresses/decompresses an FST.
5 
6 #include <cstring>
7 #include <memory>
8 #include <string>
9 
10 #include <fst/flags.h>
11 #include <fst/log.h>
13 #include <fst/script/arg-packs.h>
14 #include <fst/script/fst-class.h>
15 
16 DEFINE_string(arc_type, "standard", "Output arc type");
17 DEFINE_bool(decode, false, "Decode");
18 DEFINE_bool(gzip, false,
19  "Applies gzip compression after LZA compression and "
20  "gzip decompression before LZA decompression "
21  "(recommended)"
22  "");
23 
24 int main(int argc, char **argv) {
25  namespace s = fst::script;
26 
27  using s::FstClass;
28  using s::VectorFstClass;
29 
30  string usage = "Compresses/decompresses an FST.\n\n Usage: ";
31  usage += argv[0];
32  usage += " [in.fst [out.fstz]]\n";
33  usage += " --decode [in.fstz [out.fst]]\n";
34 
35  std::set_new_handler(FailedNewHandler);
36  SET_FLAGS(usage.c_str(), &argc, &argv, true);
37  if (argc > 3) {
38  ShowUsage();
39  return 1;
40  }
41 
42  string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
43  string out_name = argc > 2 ? argv[2] : "";
44 
45  if (FLAGS_decode == false) {
46  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
47  if (!ifst) return 1;
48  s::Compress(*ifst, out_name, FLAGS_gzip);
49  } else {
50  VectorFstClass ofst(FLAGS_arc_type);
51  s::Decompress(in_name, &ofst, FLAGS_gzip);
52  ofst.Write(out_name);
53  }
54  return 0;
55 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
int main(int argc, char **argv)
Definition: fstcompress.cc:24
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DEFINE_bool(decode, false,"Decode")
DEFINE_string(arc_type,"standard","Output arc type")
void Compress(const FstClass &fst, const string &filename, const bool gzip)
void Decompress(const string &filename, MutableFstClass *fst, const bool gzip)