52 "Performs operations (set, clear, relabel) on the symbol" 53 " tables attached to an FST.\n\n Usage: ";
55 usage +=
" [in.fst [out.fst]]\n";
57 SET_FLAGS(usage.c_str(), &argc, &argv,
true);
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] :
"";
68 std::unique_ptr<MutableFstClass>
fst(MutableFstClass::Read(in_name,
true));
71 if (!FST_FLAGS_save_isymbols.empty()) {
72 const auto *isyms = fst->InputSymbols();
74 isyms->WriteText(FST_FLAGS_save_isymbols);
77 <<
": Saving isymbols but there are no input symbols.";
81 if (!FST_FLAGS_save_osymbols.empty()) {
82 const auto *osyms = fst->OutputSymbols();
84 osyms->WriteText(FST_FLAGS_save_osymbols);
87 <<
": Saving osymbols but there are no output symbols.";
91 std::unique_ptr<SymbolTable> isyms;
92 if (!FST_FLAGS_isymbols.empty()) {
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);
100 std::unique_ptr<SymbolTable> osyms;
101 if (!FST_FLAGS_osymbols.empty()) {
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);
110 using Label = int64_t;
111 if (!FST_FLAGS_relabel_ipairs.empty()) {
112 std::vector<std::pair<Label, Label>> ipairs;
114 std::unique_ptr<SymbolTable> isyms_relabel(
116 fst->SetInputSymbols(isyms_relabel.get());
118 if (!FST_FLAGS_relabel_opairs.empty()) {
119 std::vector<std::pair<Label, Label>> opairs;
121 std::unique_ptr<SymbolTable> osyms_relabel(
123 fst->SetOutputSymbols(osyms_relabel.get());
126 if (FST_FLAGS_verify && !
s::Verify(*fst))
return 1;
128 return !fst->Write(out_name);
void ShowUsage(bool long_usage=true)
void Verify(FstVerifyArgs *args)
int fstsymbols_main(int argc, char **argv)
#define SET_FLAGS(usage, argc, argv, rmflags)
DECLARE_bool(clear_isymbols)
bool ReadLabelPairs(std::string_view source, std::vector< std::pair< Label, Label >> *pairs)
SymbolTable * RelabelSymbolTable(const SymbolTable *table, const std::vector< std::pair< Label, Label >> &pairs)