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 #include <string>
00024 #include <vector>
00025 using std::vector;
00026 #include <utility>
00027 using std::pair; using std::make_pair;
00028
00029 #include <fst/script/relabel.h>
00030 #include <fst/script/weight-class.h>
00031 #include <fst/util.h>
00032
00033 DEFINE_string(isymbols, "", "Input label symbol table");
00034 DEFINE_string(osymbols, "", "Output label symbol table");
00035 DEFINE_string(relabel_isymbols, "", "Input symbol set to relabel to");
00036 DEFINE_string(relabel_osymbols, "", "Ouput symbol set to relabel to");
00037 DEFINE_string(relabel_ipairs, "", "Input relabel pairs (numeric)");
00038 DEFINE_string(relabel_opairs, "", "Output relabel pairs (numeric)");
00039
00040 DEFINE_bool(allow_negative_labels, false,
00041 "Allow negative labels (not recommended; may cause conflicts)");
00042
00043 int main(int argc, char **argv) {
00044 namespace s = fst::script;
00045 using fst::SymbolTable;
00046 using fst::script::FstClass;
00047 using fst::script::MutableFstClass;
00048 using fst::script::VectorFstClass;
00049
00050 string usage = "Relabel the input and/or the output labels of the Fst.\n";
00051 usage += " Usage: ";
00052 usage += argv[0];
00053 usage += " [in.fst [out.fst]]\n";
00054 usage += " Using SymbolTables flags:\n";
00055 usage += " -relabel_isymbols isyms.txt\n";
00056 usage += " -relabel_osymbols osyms.txt\n";
00057 usage += " Using numeric labels flags:\n";
00058 usage += " -relabel_ipairs ipairs.txt\n";
00059 usage += " -relabel_opairs opairs.txts\n";
00060
00061 std::set_new_handler(FailedNewHandler);
00062 SetFlags(usage.c_str(), &argc, &argv, true);
00063 if (argc > 3) {
00064 ShowUsage();
00065 return 1;
00066 }
00067
00068 string in_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
00069 string out_name = argc > 2 ? argv[2] : "";
00070
00071 FstClass *ifst = FstClass::Read(in_name);
00072 if (!ifst) {
00073 return 0;
00074 }
00075
00076 MutableFstClass *ofst = 0;
00077 if (ifst->Properties(fst::kMutable, false)) {
00078 ofst = static_cast<MutableFstClass *>(ifst);
00079 } else {
00080 ofst = new VectorFstClass(*ifst);
00081 delete ifst;
00082 }
00083
00084
00085 if (!FLAGS_relabel_isymbols.empty() || !FLAGS_relabel_osymbols.empty()) {
00086 bool attach_new_isymbols = (ifst->InputSymbols() != 0);
00087 const SymbolTable* old_isymbols = FLAGS_isymbols.empty()
00088 ? ifst->InputSymbols()
00089 : SymbolTable::ReadText(FLAGS_isymbols, FLAGS_allow_negative_labels);
00090 const SymbolTable* relabel_isymbols = FLAGS_relabel_isymbols.empty()
00091 ? NULL
00092 : SymbolTable::ReadText(FLAGS_relabel_isymbols,
00093 FLAGS_allow_negative_labels);
00094
00095 bool attach_new_osymbols = (ofst->OutputSymbols() != 0);
00096 const SymbolTable* old_osymbols = FLAGS_osymbols.empty()
00097 ? ofst->OutputSymbols()
00098 : SymbolTable::ReadText(FLAGS_osymbols, FLAGS_allow_negative_labels);
00099 const SymbolTable* relabel_osymbols = FLAGS_relabel_osymbols.empty()
00100 ? NULL
00101 : SymbolTable::ReadText(FLAGS_relabel_osymbols,
00102 FLAGS_allow_negative_labels);
00103
00104 s::Relabel(ofst,
00105 old_isymbols, relabel_isymbols, attach_new_isymbols,
00106 old_osymbols, relabel_osymbols, attach_new_osymbols);
00107 } else {
00108
00109 typedef int64 Label;
00110 vector<pair<Label, Label> > ipairs;
00111 vector<pair<Label, Label> > opairs;
00112 if (!FLAGS_relabel_ipairs.empty())
00113 fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
00114 FLAGS_allow_negative_labels);
00115 if (!FLAGS_relabel_opairs.empty())
00116 fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
00117 FLAGS_allow_negative_labels);
00118
00119 s::Relabel(ofst, ipairs, opairs);
00120 }
00121
00122 ofst->Write(out_name);
00123
00124 return 0;
00125 }
00126