FST  openfst-1.8.3
OpenFst Library
farconvert-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 // Converts FST and container type of FARs.
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>
32 
33 DECLARE_string(far_type);
34 DECLARE_string(fst_type);
35 
36 int farconvert_main(int argc, char **argv) {
37  namespace s = fst::script;
40 
41  std::string usage = "Converts FST and container types.\n\n Usage: ";
42  usage += argv[0];
43  usage += " [in.far [out.far]]\n";
44 
45  SET_FLAGS(usage.c_str(), &argc, &argv, true);
46  s::ExpandArgs(argc, argv, &argc, &argv);
47 
48  if (argc > 3) {
49  ShowUsage();
50  return 1;
51  }
52 
53  // No args: read from stdin and write to stdout.
54  // One arg: read from in.far and write to stdout.
55  // Note that only STList can be written to stdout.
56  const std::string in_name =
57  argc > 1 && std::strcmp(argv[1], "-") != 0 ? argv[1] : "";
58  const std::string out_name =
59  argc > 2 && std::strcmp(argv[2], "-") != 0 ? argv[2] : "";
60 
61  fst::FarType far_type;
62  if (!s::GetFarType(FST_FLAGS_far_type, &far_type)) {
63  LOG(ERROR) << "Unknown --far_type " << FST_FLAGS_far_type;
64  return 1;
65  }
66 
67  std::unique_ptr<FarReaderClass> reader(FarReaderClass::Open(in_name));
68  if (!reader) return 1;
69 
70  // This uses a different meaning of far_type; since DEFAULT means "same as
71  // input", we must determine the input FarType.
72  if (far_type == fst::FarType::DEFAULT) far_type = reader->Type();
73 
74  const auto arc_type = reader->ArcType();
75  if (arc_type.empty()) return 1;
76 
77  std::unique_ptr<FarWriterClass> writer(
78  FarWriterClass::Create(out_name, arc_type, far_type));
79  if (!writer) return 1;
80 
81  // An unspecified fst_type entails that the input FST types are preserved.
82  s::Convert(*reader, *writer, FST_FLAGS_fst_type);
83 
84  if (reader->Error()) {
85  FSTERROR() << "Error reading FAR: " << in_name;
86  return 1;
87  }
88  if (writer->Error()) {
89  FSTERROR() << "Error writing FAR: " << out_name;
90  return 1;
91  }
92 
93  return 0;
94 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
int farconvert_main(int argc, char **argv)
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:60
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
#define FSTERROR()
Definition: util.h:56
FarType
Definition: far.h:51
bool GetFarType(std::string_view str, FarType *far_type)
Definition: getters.cc:34
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_string(far_type)
void Convert(FarReaderClass &reader, FarWriterClass &writer, std::string_view fst_type)
Definition: farscript.cc:71