FST  openfst-1.8.3
OpenFst Library
farprintstrings-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 // Outputs as strings the string FSTs in a finite-state archive.
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <fst/log.h>
26 #include <fst/extensions/far/far.h>
29 #include <fst/string.h>
30 #include <fst/util.h>
31 #include <fst/script/arg-packs.h>
32 #include <fst/script/getters.h>
33 
34 DECLARE_string(filename_prefix);
35 DECLARE_string(filename_suffix);
36 DECLARE_int32(generate_filenames);
37 DECLARE_string(begin_key);
38 DECLARE_string(end_key);
39 DECLARE_bool(print_key);
40 DECLARE_bool(print_weight);
41 DECLARE_string(entry_type);
42 DECLARE_string(token_type);
43 DECLARE_string(symbols);
44 DECLARE_bool(initial_symbols);
45 
46 int farprintstrings_main(int argc, char **argv) {
47  namespace s = fst::script;
49 
50  std::string usage = "Prints strings in an FST archive.\n\n Usage: ";
51  usage += argv[0];
52  usage += " [in1.far in2.far ...]\n";
53 
54  SET_FLAGS(usage.c_str(), &argc, &argv, true);
55  s::ExpandArgs(argc, argv, &argc, &argv);
56 
57  std::vector<std::string> sources;
58  for (int i = 1; i < argc; ++i) sources.push_back(argv[i]);
59  if (sources.empty()) sources.push_back("");
60  std::unique_ptr<FarReaderClass> reader(FarReaderClass::Open(sources));
61  if (!reader) return 1;
62 
63  fst::FarEntryType entry_type;
64  if (!s::GetFarEntryType(FST_FLAGS_entry_type, &entry_type)) {
65  LOG(ERROR) << "Unknown or unsupported FAR entry type: "
66  << FST_FLAGS_entry_type;
67  return 1;
68  }
69 
70  fst::TokenType token_type;
71  if (!s::GetTokenType(FST_FLAGS_token_type, &token_type)) {
72  LOG(ERROR) << "Unknown or unsupported FAR token type: "
73  << FST_FLAGS_token_type;
74  return 1;
75  }
76 
78  *reader, entry_type, token_type, FST_FLAGS_begin_key,
79  FST_FLAGS_end_key, FST_FLAGS_print_key,
80  FST_FLAGS_print_weight, FST_FLAGS_symbols,
81  FST_FLAGS_initial_symbols,
82  FST_FLAGS_generate_filenames,
83  FST_FLAGS_filename_prefix,
84  FST_FLAGS_filename_suffix);
85 
86  if (reader->Error()) {
87  FSTERROR() << "Error reading FAR(s)";
88  return 1;
89  }
90 
91  return 0;
92 }
bool GetTokenType(std::string_view str, TokenType *token_type)
Definition: getters.cc:234
void PrintStrings(FarReaderClass &reader, const FarEntryType entry_type, const TokenType token_type, const std::string &begin_key, const std::string &end_key, bool print_key, bool print_weight, const std::string &symbols_source, bool initial_symbols, const int32_t generate_sources, const std::string &source_prefix, const std::string &source_suffix)
Definition: farscript.cc:166
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:60
#define LOG(type)
Definition: log.h:53
bool GetFarEntryType(std::string_view str, FarEntryType *entry_type)
Definition: getters.cc:49
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
#define FSTERROR()
Definition: util.h:56
DECLARE_string(filename_prefix)
TokenType
Definition: string.h:49
int farprintstrings_main(int argc, char **argv)
DECLARE_int32(generate_filenames)
DECLARE_bool(print_key)
FarEntryType
Definition: far.h:49