FST  openfst-1.8.3
OpenFst Library
fstrelabel-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 // Relabels input or output space of an FST.
19 
20 #include <cstdint>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <fst/flags.h>
28 #include <fst/symbol-table.h>
29 #include <fst/util.h>
30 #include <fst/script/fst-class.h>
31 #include <fst/script/relabel.h>
32 
33 DECLARE_string(isymbols);
34 DECLARE_string(osymbols);
35 DECLARE_string(relabel_isymbols);
36 DECLARE_string(relabel_osymbols);
37 DECLARE_string(relabel_ipairs);
38 DECLARE_string(relabel_opairs);
39 DECLARE_string(unknown_isymbol);
40 DECLARE_string(unknown_osymbol);
41 
42 int fstrelabel_main(int argc, char **argv) {
43  namespace s = fst::script;
44  using fst::ReadLabelPairs;
45  using fst::SymbolTable;
47 
48  std::string usage =
49  "Relabels the input and/or the output labels of the FST.\n\n"
50  " Usage: ";
51  usage += argv[0];
52  usage += " [in.fst [out.fst]]\n";
53  usage += "\n Using SymbolTables flags:\n";
54  usage += " --relabel_isymbols isyms.map\n";
55  usage += " --relabel_osymbols osyms.map\n";
56  usage += "\n Using numeric labels flags:\n";
57  usage += " --relabel_ipairs ipairs.txt\n";
58  usage += " --relabel_opairs opairs.txt\n";
59 
60  SET_FLAGS(usage.c_str(), &argc, &argv, true);
61  if (argc > 3) {
62  ShowUsage();
63  return 1;
64  }
65 
66  const std::string in_name =
67  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
68  const std::string out_name =
69  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
70 
71  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
72  if (!fst) return 1;
73 
74  if (!FST_FLAGS_relabel_isymbols.empty() ||
75  !FST_FLAGS_relabel_osymbols.empty()) {
76  bool attach_new_isymbols = (fst->InputSymbols() != nullptr);
77  std::unique_ptr<const SymbolTable> old_isymbols(
78  FST_FLAGS_isymbols.empty()
79  ? nullptr
80  : SymbolTable::ReadText(FST_FLAGS_isymbols,
81  FST_FLAGS_fst_field_separator));
82  const std::unique_ptr<const SymbolTable> relabel_isymbols(
83  FST_FLAGS_relabel_isymbols.empty()
84  ? nullptr
85  : SymbolTable::ReadText(FST_FLAGS_relabel_isymbols,
86  FST_FLAGS_fst_field_separator));
87  bool attach_new_osymbols = (fst->OutputSymbols() != nullptr);
88  std::unique_ptr<const SymbolTable> old_osymbols(
89  FST_FLAGS_osymbols.empty()
90  ? nullptr
91  : SymbolTable::ReadText(FST_FLAGS_osymbols,
92  FST_FLAGS_fst_field_separator));
93  const std::unique_ptr<const SymbolTable> relabel_osymbols(
94  FST_FLAGS_relabel_osymbols.empty()
95  ? nullptr
96  : SymbolTable::ReadText(FST_FLAGS_relabel_osymbols,
97  FST_FLAGS_fst_field_separator));
98  s::Relabel(fst.get(),
99  old_isymbols ? old_isymbols.get() : fst->InputSymbols(),
100  relabel_isymbols.get(), FST_FLAGS_unknown_isymbol,
101  attach_new_isymbols,
102  old_osymbols ? old_osymbols.get() : fst->OutputSymbols(),
103  relabel_osymbols.get(), FST_FLAGS_unknown_osymbol,
104  attach_new_osymbols);
105  } else {
106  // Reads in relabeling pairs.
107  std::vector<std::pair<int64_t, int64_t>> ipairs;
108  if (!FST_FLAGS_relabel_ipairs.empty() &&
109  !ReadLabelPairs(FST_FLAGS_relabel_ipairs, &ipairs)) {
110  return 1;
111  }
112  std::vector<std::pair<int64_t, int64_t>> opairs;
113  if (!FST_FLAGS_relabel_opairs.empty() &&
114  !ReadLabelPairs(FST_FLAGS_relabel_opairs, &opairs)) {
115  return 1;
116  }
117  s::Relabel(fst.get(), ipairs, opairs);
118  }
119 
120  return !fst->Write(out_name);
121 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_string(isymbols)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void Relabel(MutableFst< Arc > *fst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &ipairs, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &opairs)
Definition: relabel.h:49
bool ReadLabelPairs(std::string_view source, std::vector< std::pair< Label, Label >> *pairs)
Definition: util.h:422
int fstrelabel_main(int argc, char **argv)