Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <fst/script/fst-class.h>
00025 #include <fst/script/script-impl.h>
00026 #include <fst/util.h>
00027
00028 DEFINE_string(isymbols, "", "Input label symbol table");
00029 DEFINE_string(osymbols, "", "Output label symbol table");
00030 DEFINE_bool(clear_isymbols, false, "Clear input symbol table");
00031 DEFINE_bool(clear_osymbols, false, "Clear output symbol table");
00032 DEFINE_string(relabel_ipairs, "", "Input relabel pairs (numeric)");
00033 DEFINE_string(relabel_opairs, "", "Output relabel pairs (numeric)");
00034 DEFINE_bool(allow_negative_labels, false,
00035 "Allow negative labels (not recommended; may cause conflicts)");
00036
00037 int main(int argc, char **argv) {
00038 namespace s = fst::script;
00039 using fst::SymbolTable;
00040
00041 string usage = "Performs operations (set, clear, relabel) on the symbol"
00042 "tables attached to an FST.\n\n Usage: ";
00043 usage += argv[0];
00044 usage += " [in.fst [out.fst]]\n";
00045
00046 std::set_new_handler(FailedNewHandler);
00047 SetFlags(usage.c_str(), &argc, &argv, true);
00048 if (argc > 3) {
00049 ShowUsage();
00050 return 1;
00051 }
00052
00053 string in_fname = argc > 1 && strcmp(argv[1], "-") != 0 ? argv[1] : "";
00054 string out_fname = argc > 2 ? argv[2] : "";
00055
00056 s::FstClass *ifst = s::FstClass::Read(in_fname);
00057 if (!ifst) return 1;
00058
00059 s::MutableFstClass *ofst = 0;
00060 if (ifst->Properties(fst::kMutable, false)) {
00061 ofst = static_cast<s::MutableFstClass *>(ifst);
00062 } else {
00063 ofst = new s::VectorFstClass(*ifst);
00064 delete ifst;
00065 }
00066
00067 if (FLAGS_clear_isymbols)
00068 ofst->SetInputSymbols(0);
00069 else if (!FLAGS_isymbols.empty())
00070 ofst->SetInputSymbols(
00071 SymbolTable::ReadText(FLAGS_isymbols, FLAGS_allow_negative_labels));
00072
00073 if (FLAGS_clear_osymbols)
00074 ofst->SetOutputSymbols(0);
00075 else if (!FLAGS_osymbols.empty())
00076 ofst->SetOutputSymbols(
00077 SymbolTable::ReadText(FLAGS_osymbols, FLAGS_allow_negative_labels));
00078
00079 if (!FLAGS_relabel_ipairs.empty()) {
00080 typedef int64 Label;
00081 vector<pair<Label, Label> > ipairs;
00082 fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
00083 FLAGS_allow_negative_labels);
00084 SymbolTable *isyms = RelabelSymbolTable(ofst->InputSymbols(), ipairs);
00085 ofst->SetInputSymbols(isyms);
00086 delete isyms;
00087 }
00088
00089 if (!FLAGS_relabel_opairs.empty()) {
00090 typedef int64 Label;
00091 vector<pair<Label, Label> > opairs;
00092 fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
00093 FLAGS_allow_negative_labels);
00094 SymbolTable *osyms = RelabelSymbolTable(ofst->OutputSymbols(), opairs);
00095 ofst->SetOutputSymbols(osyms);
00096 delete osyms;
00097 }
00098
00099 ofst->Write(out_fname);
00100 return 0;
00101 }
00102