FST  openfst-1.8.3
OpenFst Library
fstencode-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Encode transducer labels and/or weights.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/script/decode.h>
26 #include <fst/script/encode.h>
28 #include <fst/script/fst-class.h>
29 #include <fst/script/getters.h>
30 
31 DECLARE_bool(decode);
32 DECLARE_bool(encode_labels);
33 DECLARE_bool(encode_weights);
34 DECLARE_bool(encode_reuse);
35 
36 int fstencode_main(int argc, char **argv) {
37  namespace s = fst::script;
40 
41  std::string usage = "Encodes transducer labels and/or weights.\n\n Usage: ";
42  usage += argv[0];
43  usage += " in.fst mapper [out.fst]\n";
44 
45  SET_FLAGS(usage.c_str(), &argc, &argv, true);
46  if (argc < 3 || argc > 4) {
47  ShowUsage();
48  return 1;
49  }
50 
51  const std::string in_name = (strcmp(argv[1], "-") != 0) ? argv[1] : "";
52  const std::string mapper_name = argv[2];
53  const std::string out_name =
54  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
55 
56  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
57  if (!fst) return 1;
58 
59  if (FST_FLAGS_decode) {
60  std::unique_ptr<EncodeMapperClass> mapper(
61  EncodeMapperClass::Read(mapper_name));
62  if (!mapper) return 1;
63  s::Decode(fst.get(), *mapper);
64  } else if (FST_FLAGS_encode_reuse) {
65  std::unique_ptr<EncodeMapperClass> mapper(
66  EncodeMapperClass::Read(mapper_name));
67  if (!mapper) return 1;
68  s::Encode(fst.get(), mapper.get());
69  } else {
70  const auto flags = s::GetEncodeFlags(FST_FLAGS_encode_labels,
71  FST_FLAGS_encode_weights);
72  EncodeMapperClass mapper(fst->ArcType(), flags);
73  s::Encode(fst.get(), &mapper);
74  if (!mapper.Write(mapper_name)) return 1;
75  }
76 
77  return !fst->Write(out_name);
78 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_bool(decode)
void Decode(FarReaderClass &reader, FarWriterClass &writer, const EncodeMapperClass &encoder)
Definition: farscript.cc:88
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void Encode(FarReaderClass &reader, FarWriterClass &writer, EncodeMapperClass *encoder)
Definition: farscript.cc:100
uint8_t GetEncodeFlags(bool encode_labels, bool encode_weights)
Definition: getters.h:61
int fstencode_main(int argc, char **argv)