FST  openfst-1.8.4
OpenFst Library
farencode-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 // Encodes FAR labels and/or weights.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
27 #include <fst/extensions/far/far.h>
30 #include <fst/util.h>
31 #include <fst/script/arg-packs.h>
33 #include <fst/script/getters.h>
34 
35 DECLARE_bool(decode);
36 DECLARE_bool(encode_labels);
37 DECLARE_bool(encode_weights);
38 DECLARE_bool(encode_reuse);
39 DECLARE_string(far_type);
40 
41 int farencode_main(int argc, char **argv) {
42  namespace s = fst::script;
46 
47  std::string usage = "Encodes FST labels and/or weights in an FST archive.";
48  usage += "\n\n Usage: ";
49  usage += argv[0];
50  usage += " [in.far mapper [out.far]]\n";
51 
52  SET_FLAGS(usage.c_str(), &argc, &argv, true);
53  if (argc < 3 || argc > 4) {
54  ShowUsage();
55  return 1;
56  }
57 
58  const std::string in_name = (strcmp(argv[1], "-") != 0) ? argv[1] : "";
59  const std::string mapper_name = argv[2];
60  const std::string out_name =
61  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
62 
63  std::unique_ptr<FarReaderClass> reader(FarReaderClass::Open(in_name));
64  if (!reader) return 1;
65 
66  fst::FarType far_type;
67  if (!s::GetFarType(FST_FLAGS_far_type, &far_type)) {
68  LOG(ERROR) << "Unknown --far_type " << FST_FLAGS_far_type;
69  return 1;
70  }
71 
72  // This uses a different meaning of far_type; since DEFAULT means "same as
73  // input", we must determine the input FarType.
74  if (far_type == fst::FarType::DEFAULT) far_type = reader->Type();
75 
76  const auto arc_type = reader->ArcType();
77  if (arc_type.empty()) return 1;
78 
79  std::unique_ptr<FarWriterClass> writer(
80  FarWriterClass::Create(out_name, arc_type, far_type));
81  if (!writer) return 1;
82 
83  if (FST_FLAGS_decode) {
84  std::unique_ptr<EncodeMapperClass> mapper(
85  EncodeMapperClass::Read(mapper_name));
86  if (!mapper) return 1;
87  s::Decode(*reader, *writer, *mapper);
88  } else if (FST_FLAGS_encode_reuse) {
89  std::unique_ptr<EncodeMapperClass> mapper(
90  EncodeMapperClass::Read(mapper_name));
91  if (!mapper) return 1;
92  s::Encode(*reader, *writer, mapper.get());
93  } else {
94  const auto flags = s::GetEncodeFlags(FST_FLAGS_encode_labels,
95  FST_FLAGS_encode_weights);
96  EncodeMapperClass mapper(reader->ArcType(), flags);
97  s::Encode(*reader, *writer, &mapper);
98  if (!mapper.Write(mapper_name)) return 1;
99  }
100 
101  if (reader->Error()) {
102  FSTERROR() << "Error reading FAR: " << in_name;
103  return 1;
104  }
105  if (writer->Error()) {
106  FSTERROR() << "Error writing FAR: " << out_name;
107  return 1;
108  }
109 
110  return 0;
111 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
#define LOG(type)
Definition: log.h:53
void Decode(FarReaderClass &reader, FarWriterClass &writer, const EncodeMapperClass &encoder)
Definition: farscript.cc:88
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
#define FSTERROR()
Definition: util.h:57
FarType
Definition: far.h:51
DECLARE_string(far_type)
void Encode(FarReaderClass &reader, FarWriterClass &writer, EncodeMapperClass *encoder)
Definition: farscript.cc:100
bool GetFarType(std::string_view str, FarType *far_type)
Definition: getters.cc:34
uint8_t GetEncodeFlags(bool encode_labels, bool encode_weights)
Definition: getters.h:61
void Create(const std::vector< std::string > &sources, FarWriterClass &writer, const int32_t generate_keys, const std::string &key_prefix, const std::string &key_suffix)
Definition: farscript.cc:79
DECLARE_bool(decode)
int farencode_main(int argc, char **argv)