FST  openfst-1.8.3
OpenFst Library
fstsymbols-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 // Performs operations (set, clear, relabel) on the symbols table attached to an
19 // input FST.
20 
21 #include <cstdint>
22 #include <cstring>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include <fst/flags.h>
29 #include <fst/log.h>
30 #include <fst/symbol-table.h>
31 #include <fst/util.h>
32 #include <fst/script/fst-class.h>
33 #include <fst/script/verify.h>
34 
35 DECLARE_string(isymbols);
36 DECLARE_string(osymbols);
37 DECLARE_bool(clear_isymbols);
38 DECLARE_bool(clear_osymbols);
39 DECLARE_string(relabel_ipairs);
40 DECLARE_string(relabel_opairs);
41 DECLARE_string(save_isymbols);
42 DECLARE_string(save_osymbols);
43 DECLARE_bool(verify);
44 
45 int fstsymbols_main(int argc, char **argv) {
46  namespace s = fst::script;
47  using fst::ReadLabelPairs;
48  using fst::SymbolTable;
50 
51  std::string usage =
52  "Performs operations (set, clear, relabel) on the symbol"
53  " tables attached to an FST.\n\n Usage: ";
54  usage += argv[0];
55  usage += " [in.fst [out.fst]]\n";
56 
57  SET_FLAGS(usage.c_str(), &argc, &argv, true);
58  if (argc > 3) {
59  ShowUsage();
60  return 1;
61  }
62 
63  const std::string in_name =
64  argc > 1 && strcmp(argv[1], "-") != 0 ? argv[1] : "";
65  const std::string out_name =
66  argc > 2 && strcmp(argv[2], "-") != 0 ? argv[2] : "";
67 
68  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
69  if (!fst) return 1;
70 
71  if (!FST_FLAGS_save_isymbols.empty()) {
72  const auto *isyms = fst->InputSymbols();
73  if (isyms) {
74  isyms->WriteText(FST_FLAGS_save_isymbols);
75  } else {
76  LOG(ERROR) << argv[0]
77  << ": Saving isymbols but there are no input symbols.";
78  }
79  }
80 
81  if (!FST_FLAGS_save_osymbols.empty()) {
82  const auto *osyms = fst->OutputSymbols();
83  if (osyms) {
84  osyms->WriteText(FST_FLAGS_save_osymbols);
85  } else {
86  LOG(ERROR) << argv[0]
87  << ": Saving osymbols but there are no output symbols.";
88  }
89  }
90 
91  std::unique_ptr<SymbolTable> isyms;
92  if (!FST_FLAGS_isymbols.empty()) {
93  isyms.reset(
94  SymbolTable::ReadText(FST_FLAGS_isymbols,
95  FST_FLAGS_fst_field_separator));
96  fst->SetInputSymbols(isyms.get());
97  } else if (FST_FLAGS_clear_isymbols) {
98  fst->SetInputSymbols(nullptr);
99  }
100  std::unique_ptr<SymbolTable> osyms;
101  if (!FST_FLAGS_osymbols.empty()) {
102  osyms.reset(
103  SymbolTable::ReadText(FST_FLAGS_osymbols,
104  FST_FLAGS_fst_field_separator));
105  fst->SetOutputSymbols(osyms.get());
106  } else if (FST_FLAGS_clear_osymbols) {
107  fst->SetOutputSymbols(nullptr);
108  }
109 
110  using Label = int64_t;
111  if (!FST_FLAGS_relabel_ipairs.empty()) {
112  std::vector<std::pair<Label, Label>> ipairs;
113  ReadLabelPairs(FST_FLAGS_relabel_ipairs, &ipairs);
114  std::unique_ptr<SymbolTable> isyms_relabel(
115  RelabelSymbolTable(fst->InputSymbols(), ipairs));
116  fst->SetInputSymbols(isyms_relabel.get());
117  }
118  if (!FST_FLAGS_relabel_opairs.empty()) {
119  std::vector<std::pair<Label, Label>> opairs;
120  ReadLabelPairs(FST_FLAGS_relabel_opairs, &opairs);
121  std::unique_ptr<SymbolTable> osyms_relabel(
122  RelabelSymbolTable(fst->OutputSymbols(), opairs));
123  fst->SetOutputSymbols(osyms_relabel.get());
124  }
125 
126  if (FST_FLAGS_verify && !s::Verify(*fst)) return 1;
127 
128  return !fst->Write(out_name);
129 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
void Verify(FstVerifyArgs *args)
Definition: verify.h:32
#define LOG(type)
Definition: log.h:53
int fstsymbols_main(int argc, char **argv)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
DECLARE_bool(clear_isymbols)
DECLARE_string(isymbols)
bool ReadLabelPairs(std::string_view source, std::vector< std::pair< Label, Label >> *pairs)
Definition: util.h:422
SymbolTable * RelabelSymbolTable(const SymbolTable *table, const std::vector< std::pair< Label, Label >> &pairs)
Definition: symbol-table.h:572