FST  openfst-1.8.2
OpenFst Library
farcreate-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 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 // Creates a finite-state archive from input FSTs.
19 
20 #include <string>
21 #include <vector>
22 
23 #include <fst/flags.h>
26 #include <fst/arc.h>
27 #include <fstream>
28 
29 DECLARE_string(key_prefix);
30 DECLARE_string(key_suffix);
31 DECLARE_int32(generate_keys);
32 DECLARE_string(far_type);
33 DECLARE_bool(file_list_input);
34 
35 int farcreate_main(int argc, char **argv) {
36  namespace s = fst::script;
38 
39  std::string usage =
40  "Creates a finite-state archive from input FSTs.\n\n Usage:";
41  usage += argv[0];
42  usage += " [in1.fst [[in2.fst ...] out.far]]\n";
43 
44  std::set_new_handler(FailedNewHandler);
45  SET_FLAGS(usage.c_str(), &argc, &argv, true);
46  s::ExpandArgs(argc, argv, &argc, &argv);
47 
48  std::vector<std::string> sources;
49  if (FST_FLAGS_file_list_input) {
50  for (int i = 1; i < argc - 1; ++i) {
51  std::ifstream istrm(argv[i]);
52  std::string str;
53  while (std::getline(istrm, str)) sources.push_back(str);
54  }
55  } else {
56  for (int i = 1; i < argc - 1; ++i)
57  sources.push_back(strcmp(argv[i], "-") != 0 ? argv[i] : "");
58  if (sources.empty()) {
59  // argc == 1 || argc == 2. This cleverly handles both the no-file case
60  // and the one (input) file case together.
61  sources.push_back(argc == 2 && strcmp(argv[1], "-") != 0 ? argv[1] : "");
62  }
63  }
64 
65  // argc <= 2 means the file (if any) is an input file, so write to stdout.
66  const std::string out_far =
67  argc > 2 && strcmp(argv[argc - 1], "-") != 0 ? argv[argc - 1] : "";
68 
69  fst::FarType far_type;
70  if (!s::GetFarType(FST_FLAGS_far_type, &far_type)) {
71  LOG(ERROR) << "Unknown or unsupported FAR type: "
72  << FST_FLAGS_far_type;
73  return 1;
74  }
75 
76  std::string arc_type = fst::ErrorArc::Type();
77  if (!sources.empty()) {
78  arc_type = s::LoadArcTypeFromFst(sources[0]);
79  if (arc_type.empty()) return 1;
80  }
81 
82  std::unique_ptr<FarWriterClass> writer(
83  FarWriterClass::Create(out_far, arc_type, far_type));
84  if (!writer) return 1;
85 
86  s::Create(sources, *writer, FST_FLAGS_generate_keys,
87  FST_FLAGS_key_prefix, FST_FLAGS_key_suffix);
88 
89  if (writer->Error()) {
90  FSTERROR() << "Error writing FAR: " << out_far;
91  return 1;
92  }
93 
94  return 0;
95 }
DECLARE_int32(generate_keys)
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:60
int farcreate_main(int argc, char **argv)
#define LOG(type)
Definition: log.h:49
static const std::string & Type()
Definition: arc.h:68
std::string LoadArcTypeFromFst(const std::string &fst_source)
Definition: script-impl.cc:40
void FailedNewHandler()
Definition: compat.cc:26
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:224
#define FSTERROR()
Definition: util.h:53
FarType
Definition: far.h:43
DECLARE_string(key_prefix)
bool GetFarType(std::string_view str, FarType *far_type)
Definition: getters.cc:34
DECLARE_bool(file_list_input)
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:72