FST  openfst-1.8.3
OpenFst Library
farextract-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 // Extracts component FSTs from an finite-state archive.
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <fst/log.h>
28 #include <fst/util.h>
29 #include <fst/script/arg-packs.h>
30 
31 DECLARE_string(filename_prefix);
32 DECLARE_string(filename_suffix);
33 DECLARE_int32(generate_filenames);
34 DECLARE_string(keys);
35 DECLARE_string(key_separator);
36 DECLARE_string(range_delimiter);
37 
38 int farextract_main(int argc, char **argv) {
39  namespace s = fst::script;
41 
42  std::string usage = "Extracts FSTs from a finite-state archive.\n\n Usage:";
43  usage += argv[0];
44  usage += " [in1.far in2.far...]\n";
45 
46  SET_FLAGS(usage.c_str(), &argc, &argv, true);
47  s::ExpandArgs(argc, argv, &argc, &argv);
48 
49  std::vector<std::string> sources;
50  for (int i = 1; i < argc; ++i) sources.push_back(argv[i]);
51  if (sources.empty()) sources.push_back("");
52  std::unique_ptr<FarReaderClass> reader(FarReaderClass::Open(sources));
53  if (!reader) return 1;
54 
55  s::Extract(*reader, FST_FLAGS_generate_filenames,
56  FST_FLAGS_keys, FST_FLAGS_key_separator,
57  FST_FLAGS_range_delimiter,
58  FST_FLAGS_filename_prefix,
59  FST_FLAGS_filename_suffix);
60 
61  if (reader->Error()) {
62  FSTERROR() << "Error reading FAR(s)";
63  return 1;
64  }
65 
66  return 0;
67 }
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:60
DECLARE_string(filename_prefix)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
#define FSTERROR()
Definition: util.h:56
DECLARE_int32(generate_filenames)
int farextract_main(int argc, char **argv)
void Extract(FarReaderClass &reader, int32_t generate_sources, const std::string &keys, const std::string &key_separator, const std::string &range_delimiter, const std::string &source_prefix, const std::string &source_suffix)
Definition: farscript.cc:123