FST  openfst-1.7.1
OpenFst Library
fstsymbols-main.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Performs operations (set, clear, relabel) on the symbols table attached to an
5 // input FST.
6 
7 #include <cstring>
8 
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include <fst/flags.h>
14 #include <fst/util.h>
15 #include <fst/script/fst-class.h>
16 #include <fst/script/verify.h>
17 
18 DECLARE_string(isymbols);
19 DECLARE_string(osymbols);
20 DECLARE_bool(clear_isymbols);
21 DECLARE_bool(clear_osymbols);
22 DECLARE_string(relabel_ipairs);
23 DECLARE_string(relabel_opairs);
24 DECLARE_string(save_isymbols);
25 DECLARE_string(save_osymbols);
26 DECLARE_bool(allow_negative_labels);
27 DECLARE_bool(verify);
28 
29 int fstsymbols_main(int argc, char **argv) {
30  namespace s = fst::script;
32  using fst::ReadLabelPairs;
33  using fst::SymbolTable;
35 
36  string usage =
37  "Performs operations (set, clear, relabel) on the symbol"
38  " tables attached to an FST.\n\n Usage: ";
39  usage += argv[0];
40  usage += " [in.fst [out.fst]]\n";
41 
42  std::set_new_handler(FailedNewHandler);
43  SET_FLAGS(usage.c_str(), &argc, &argv, true);
44  if (argc > 3) {
45  ShowUsage();
46  return 1;
47  }
48 
49  const string in_name = argc > 1 && strcmp(argv[1], "-") != 0 ? argv[1] : "";
50  const string out_name = argc > 2 ? argv[2] : "";
51 
52  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
53  if (!fst) return 1;
54 
55  if (!FLAGS_save_isymbols.empty()) {
56  const auto *isyms = fst->InputSymbols();
57  if (isyms) {
58  isyms->WriteText(FLAGS_save_isymbols);
59  } else {
60  LOG(ERROR) << argv[0]
61  << ": Saving isymbols but there are no input symbols.";
62  }
63  }
64 
65  if (!FLAGS_save_osymbols.empty()) {
66  const auto *osyms = fst->OutputSymbols();
67  if (osyms) {
68  osyms->WriteText(FLAGS_save_osymbols);
69  } else {
70  LOG(ERROR) << argv[0]
71  << ": Saving osymbols but there are no output symbols.";
72  }
73  }
74 
75  const SymbolTableTextOptions opts(FLAGS_allow_negative_labels);
76 
77  std::unique_ptr<SymbolTable> isyms;
78  if (!FLAGS_isymbols.empty()) {
79  isyms.reset(SymbolTable::ReadText(FLAGS_isymbols, opts));
80  fst->SetInputSymbols(isyms.get());
81  } else if (FLAGS_clear_isymbols) {
82  fst->SetInputSymbols(nullptr);
83  }
84  std::unique_ptr<SymbolTable> osyms;
85  if (!FLAGS_osymbols.empty()) {
86  osyms.reset(SymbolTable::ReadText(FLAGS_osymbols, opts));
87  fst->SetOutputSymbols(osyms.get());
88  } else if (FLAGS_clear_osymbols) {
89  fst->SetOutputSymbols(nullptr);
90  }
91 
92  using Label = int64;
93  if (!FLAGS_relabel_ipairs.empty()) {
94  std::vector<std::pair<Label, Label>> ipairs;
95  ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs, FLAGS_allow_negative_labels);
96  std::unique_ptr<SymbolTable> isyms_relabel(
97  RelabelSymbolTable(fst->InputSymbols(), ipairs));
98  fst->SetInputSymbols(isyms_relabel.get());
99  }
100  if (!FLAGS_relabel_opairs.empty()) {
101  std::vector<std::pair<Label, Label>> opairs;
102  ReadLabelPairs(FLAGS_relabel_opairs, &opairs, FLAGS_allow_negative_labels);
103  std::unique_ptr<SymbolTable> osyms_relabel(
104  RelabelSymbolTable(fst->OutputSymbols(), opairs));
105  fst->SetOutputSymbols(osyms_relabel.get());
106  }
107 
108  if (FLAGS_verify && !s::Verify(*fst)) return 1;
109 
110  return !fst->Write(out_name);
111 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
bool ReadLabelPairs(const string &filename, std::vector< std::pair< Label, Label >> *pairs, bool allow_negative=false)
Definition: util.h:331
#define LOG(type)
Definition: log.h:48
int fstsymbols_main(int argc, char **argv)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
int64_t int64
Definition: types.h:27
void Verify(VerifyArgs *args)
Definition: verify.h:17
DECLARE_bool(clear_isymbols)
DECLARE_string(isymbols)
SymbolTable * RelabelSymbolTable(const SymbolTable *table, const std::vector< std::pair< Label, Label >> &pairs)
Definition: symbol-table.h:419