Home | History | Annotate | Download | only in bin
      1 // fstrelabel.cc
      2 
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 //
     15 // Copyright 2005-2010 Google, Inc.
     16 // Author: johans (at) google.com (Johan Schalkwyk)
     17 // Modified: jpr (at) google.com (Jake Ratkiewicz) to use FstClass
     18 //
     19 // \file
     20 // Relabel input or output space of Fst
     21 //
     22 
     23 #include <string>
     24 #include <vector>
     25 using std::vector;
     26 #include <utility>
     27 using std::pair; using std::make_pair;
     28 
     29 #include <fst/script/relabel.h>
     30 #include <fst/script/weight-class.h>
     31 #include <fst/util.h>
     32 
     33 DEFINE_string(isymbols, "", "Input label symbol table");
     34 DEFINE_string(osymbols, "", "Output label symbol table");
     35 DEFINE_string(relabel_isymbols, "", "Input symbol set to relabel to");
     36 DEFINE_string(relabel_osymbols, "", "Ouput symbol set to relabel to");
     37 DEFINE_string(relabel_ipairs, "", "Input relabel pairs (numeric)");
     38 DEFINE_string(relabel_opairs, "", "Output relabel pairs (numeric)");
     39 
     40 DEFINE_bool(allow_negative_labels, false,
     41             "Allow negative labels (not recommended; may cause conflicts)");
     42 
     43 int main(int argc, char **argv) {
     44   namespace s = fst::script;
     45   using fst::SymbolTable;
     46   using fst::SymbolTableTextOptions;
     47   using fst::script::FstClass;
     48   using fst::script::MutableFstClass;
     49 
     50   string usage = "Relabels the input and/or the output labels of the FST.\n\n"
     51       "  Usage: ";
     52   usage += argv[0];
     53   usage += " [in.fst [out.fst]]\n";
     54   usage += " Using SymbolTables flags:\n";
     55   usage += "  -relabel_isymbols isyms.txt\n";
     56   usage += "  -relabel_osymbols osyms.txt\n";
     57   usage += " Using numeric labels flags:\n";
     58   usage += "  -relabel_ipairs   ipairs.txt\n";
     59   usage += "  -relabel_opairs   opairs.txts\n";
     60 
     61   std::set_new_handler(FailedNewHandler);
     62   SET_FLAGS(usage.c_str(), &argc, &argv, true);
     63   if (argc > 3) {
     64     ShowUsage();
     65     return 1;
     66   }
     67 
     68   string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
     69   string out_name = argc > 2 ? argv[2] : "";
     70 
     71   MutableFstClass *fst = MutableFstClass::Read(in_name, true);
     72   if (!fst) return 1;
     73 
     74   // Relabel with symbol tables
     75   SymbolTableTextOptions opts;
     76   opts.allow_negative = FLAGS_allow_negative_labels;
     77   if (!FLAGS_relabel_isymbols.empty() || !FLAGS_relabel_osymbols.empty()) {
     78     bool attach_new_isymbols = (fst->InputSymbols() != 0);
     79     const SymbolTable* old_isymbols = FLAGS_isymbols.empty()
     80         ? fst->InputSymbols()
     81         : SymbolTable::ReadText(FLAGS_isymbols, opts);
     82     const SymbolTable* relabel_isymbols = FLAGS_relabel_isymbols.empty()
     83         ? NULL
     84         : SymbolTable::ReadText(FLAGS_relabel_isymbols, opts);
     85 
     86     bool attach_new_osymbols = (fst->OutputSymbols() != 0);
     87     const SymbolTable* old_osymbols = FLAGS_osymbols.empty()
     88         ? fst->OutputSymbols()
     89         : SymbolTable::ReadText(FLAGS_osymbols, opts);
     90     const SymbolTable* relabel_osymbols = FLAGS_relabel_osymbols.empty()
     91         ? NULL
     92         : SymbolTable::ReadText(FLAGS_relabel_osymbols, opts);
     93 
     94     s::Relabel(fst,
     95                old_isymbols, relabel_isymbols, attach_new_isymbols,
     96                old_osymbols, relabel_osymbols, attach_new_osymbols);
     97   } else {
     98     // read in relabel pairs and parse
     99     typedef int64 Label;
    100     vector<pair<Label, Label> > ipairs;
    101     vector<pair<Label, Label> > opairs;
    102     if (!FLAGS_relabel_ipairs.empty()) {
    103       if(!fst::ReadLabelPairs(FLAGS_relabel_ipairs, &ipairs,
    104                                   FLAGS_allow_negative_labels))
    105         return 1;
    106     }
    107     if (!FLAGS_relabel_opairs.empty()) {
    108       if (!fst::ReadLabelPairs(FLAGS_relabel_opairs, &opairs,
    109                                    FLAGS_allow_negative_labels))
    110         return 1;
    111     }
    112     s::Relabel(fst, ipairs, opairs);
    113   }
    114 
    115   fst->Write(out_name);
    116 
    117   return 0;
    118 }
    119