49 "Relabels the input and/or the output labels of the FST.\n\n" 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";
60 SET_FLAGS(usage.c_str(), &argc, &argv,
true);
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] :
"";
71 std::unique_ptr<MutableFstClass>
fst(MutableFstClass::Read(in_name,
true));
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()
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()
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()
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()
96 : SymbolTable::ReadText(FST_FLAGS_relabel_osymbols,
97 FST_FLAGS_fst_field_separator));
99 old_isymbols ? old_isymbols.get() : fst->InputSymbols(),
100 relabel_isymbols.get(), FST_FLAGS_unknown_isymbol,
102 old_osymbols ? old_osymbols.get() : fst->OutputSymbols(),
103 relabel_osymbols.get(), FST_FLAGS_unknown_osymbol,
104 attach_new_osymbols);
107 std::vector<std::pair<int64_t, int64_t>> ipairs;
108 if (!FST_FLAGS_relabel_ipairs.empty() &&
112 std::vector<std::pair<int64_t, int64_t>> opairs;
113 if (!FST_FLAGS_relabel_opairs.empty() &&
120 return !fst->Write(out_name);
void ShowUsage(bool long_usage=true)
#define SET_FLAGS(usage, argc, argv, rmflags)
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)
bool ReadLabelPairs(std::string_view source, std::vector< std::pair< Label, Label >> *pairs)
int fstrelabel_main(int argc, char **argv)