FST  openfst-1.7.1
OpenFst Library
fstrelabel-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 // Relabels input or output space of an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <fst/flags.h>
13 #include <fst/util.h>
14 #include <fst/script/relabel.h>
16 
17 DECLARE_string(isymbols);
18 DECLARE_string(osymbols);
19 DECLARE_string(relabel_isymbols);
20 DECLARE_string(relabel_osymbols);
21 DECLARE_string(relabel_ipairs);
22 DECLARE_string(relabel_opairs);
23 DECLARE_string(unknown_isymbol);
24 DECLARE_string(unknown_osymbol);
25 DECLARE_bool(allow_negative_labels);
26 
27 int fstrelabel_main(int argc, char **argv) {
28  namespace s = fst::script;
30  using fst::SymbolTable;
32 
33  string usage =
34  "Relabels the input and/or the output labels of the FST.\n\n"
35  " Usage: ";
36  usage += argv[0];
37  usage += " [in.fst [out.fst]]\n";
38  usage += "\n Using SymbolTables flags:\n";
39  usage += " --relabel_isymbols isyms.map\n";
40  usage += " --relabel_osymbols osyms.map\n";
41  usage += "\n Using numeric labels flags:\n";
42  usage += " --relabel_ipairs ipairs.txt\n";
43  usage += " --relabel_opairs opairs.txt\n";
44 
45  std::set_new_handler(FailedNewHandler);
46  SET_FLAGS(usage.c_str(), &argc, &argv, true);
47  if (argc > 3) {
48  ShowUsage();
49  return 1;
50  }
51 
52  const string in_name =
53  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
54  const string out_name = argc > 2 ? argv[2] : "";
55 
56  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
57  if (!fst) return 1;
58 
59  // Relabel with symbol tables.
60  const SymbolTableTextOptions opts(FLAGS_allow_negative_labels);
61 
62  if (!FLAGS_relabel_isymbols.empty() || !FLAGS_relabel_osymbols.empty()) {
63  bool attach_new_isymbols = (fst->InputSymbols() != nullptr);
64  std::unique_ptr<const SymbolTable> old_isymbols(
65  FLAGS_isymbols.empty() ? nullptr
66  : SymbolTable::ReadText(FLAGS_isymbols, opts));
67  const std::unique_ptr<const SymbolTable> relabel_isymbols(
68  FLAGS_relabel_isymbols.empty()
69  ? nullptr
70  : SymbolTable::ReadText(FLAGS_relabel_isymbols, opts));
71  bool attach_new_osymbols = (fst->OutputSymbols() != nullptr);
72  std::unique_ptr<const SymbolTable> old_osymbols(
73  FLAGS_osymbols.empty() ? nullptr
74  : SymbolTable::ReadText(FLAGS_osymbols, opts));
75  const std::unique_ptr<const SymbolTable> relabel_osymbols(
76  FLAGS_relabel_osymbols.empty()
77  ? nullptr
78  : SymbolTable::ReadText(FLAGS_relabel_osymbols, opts));
79  s::Relabel(fst.get(),
80  old_isymbols ? old_isymbols.get() : fst->InputSymbols(),
81  relabel_isymbols.get(), FLAGS_unknown_isymbol,
82  attach_new_isymbols,
83  old_osymbols ? old_osymbols.get() : fst->OutputSymbols(),
84  relabel_osymbols.get(), FLAGS_unknown_osymbol,
85  attach_new_osymbols);
86  } else {
87  // Reads in relabeling pairs.
88  std::vector<s::LabelPair> ipairs;
89  std::vector<s::LabelPair> opairs;
90  if (!FLAGS_relabel_ipairs.empty()) {
91  if (!fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
92  FLAGS_allow_negative_labels))
93  return 1;
94  }
95  if (!FLAGS_relabel_opairs.empty()) {
96  if (!fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
97  FLAGS_allow_negative_labels))
98  return 1;
99  }
100  s::Relabel(fst.get(), ipairs, opairs);
101  }
102 
103  return !fst->Write(out_name);
104 }
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
DECLARE_bool(allow_negative_labels)
DECLARE_string(isymbols)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
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:29
int fstrelabel_main(int argc, char **argv)