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