1// fstencode.cc
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Copyright 2005-2010 Google, Inc.
16// Author: riley@google.com (Michael Riley)
17// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18//
19// \file
20//  Encode transducer labels and/or weights.
21//
22
23#include <fst/script/encode.h>
24#include <fst/script/decode.h>
25
26/// EncodeMain specific flag definitions
27DEFINE_bool(encode_labels, false, "Encode output labels");
28DEFINE_bool(encode_weights, false, "Encode weights");
29DEFINE_bool(encode_reuse, false, "Re-use existing codex");
30DEFINE_bool(decode, false, "Decode labels and/or weights");
31
32int main(int argc, char **argv) {
33  namespace s = fst::script;
34  using fst::script::FstClass;
35  using fst::script::MutableFstClass;
36
37  string usage = "Encodes transducer labels and/or weights.\n\n  Usage: ";
38  usage += argv[0];
39  usage += " in.fst codex [out.fst]\n";
40
41  std::set_new_handler(FailedNewHandler);
42  SET_FLAGS(usage.c_str(), &argc, &argv, true);
43  if (argc < 3 || argc > 4) {
44    ShowUsage();
45    return 1;
46  }
47
48  string in_name = (strcmp(argv[1], "-") != 0) ? argv[1] : "";
49  string codex_name = argv[2];
50  string out_name = argc > 3 ? argv[3] : "";
51
52  MutableFstClass *fst = MutableFstClass::Read(in_name, true);
53  if (!fst) return 1;
54
55  if (FLAGS_decode == false) {
56    uint32 flags = 0;
57    flags |= FLAGS_encode_labels ? fst::kEncodeLabels : 0;
58    flags |= FLAGS_encode_weights ? fst::kEncodeWeights : 0;
59    s::Encode(fst, flags, FLAGS_encode_reuse, codex_name);
60    fst->Write(out_name);
61  } else {
62    s::Decode(fst, codex_name);
63    fst->Write(out_name);
64  }
65
66  delete fst;
67  return 0;
68}
69