Home | History | Annotate | Download | only in bin
      1 // fstrmepsilon.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: riley (at) google.com (Michael Riley)
     17 // Modified: jpr (at) google.com (Jake Ratkiewicz) to use FstClass
     18 //
     19 // \file
     20 // Removes epsilons from an FST.
     21 //
     22 
     23 #include <fst/script/rmepsilon.h>
     24 
     25 DEFINE_bool(connect, true, "Trim output");
     26 DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
     27 DEFINE_int64(nstate, fst::kNoStateId, "State number threshold");
     28 DEFINE_bool(reverse, false, "Perform in the reverse direction");
     29 DEFINE_string(weight, "", "Weight threshold");
     30 DEFINE_string(queue_type, "auto", "Queue type: one of: \"auto\", "
     31               "\"fifo\", \"lifo\", \"shortest\", \"state\", \"top\"");
     32 
     33 int main(int argc, char **argv) {
     34   namespace s = fst::script;
     35   using fst::script::FstClass;
     36   using fst::script::MutableFstClass;
     37   using fst::script::VectorFstClass;
     38   using fst::script::WeightClass;
     39 
     40   string usage = "Removes epsilons from an FST.\n\n  Usage: ";
     41   usage += argv[0];
     42   usage += " [in.fst [out.fst]]\n";
     43 
     44   std::set_new_handler(FailedNewHandler);
     45   SET_FLAGS(usage.c_str(), &argc, &argv, true);
     46   if (argc > 3) {
     47     ShowUsage();
     48     return 1;
     49   }
     50 
     51   string in_fname = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
     52   string out_fname = argc > 2 ? argv[2] : "";
     53 
     54   FstClass *ifst = FstClass::Read(in_fname);
     55   if (!ifst) return 1;
     56 
     57   WeightClass weight_threshold = FLAGS_weight.empty() ?
     58       WeightClass::Zero() :
     59       WeightClass(ifst->WeightType(), FLAGS_weight);
     60 
     61   fst::QueueType qt;
     62 
     63   if (FLAGS_queue_type == "auto") {
     64     qt = fst::AUTO_QUEUE;
     65   } else if (FLAGS_queue_type == "fifo") {
     66     qt = fst::FIFO_QUEUE;
     67   } else if (FLAGS_queue_type == "lifo") {
     68     qt = fst::LIFO_QUEUE;
     69   } else if (FLAGS_queue_type == "shortest") {
     70     qt = fst::SHORTEST_FIRST_QUEUE;
     71   } else if (FLAGS_queue_type == "state") {
     72     qt = fst::STATE_ORDER_QUEUE;
     73   } else if (FLAGS_queue_type == "top") {
     74     qt = fst::TOP_ORDER_QUEUE;
     75   } else {
     76     LOG(ERROR) << "Unknown or unsupported queue type: " << FLAGS_queue_type;
     77     return 1;
     78   }
     79 
     80   s::RmEpsilonOptions opts(qt, FLAGS_delta, FLAGS_connect,
     81                            weight_threshold, FLAGS_nstate);
     82 
     83   MutableFstClass *ofst = new VectorFstClass(ifst->ArcType());
     84   s::RmEpsilon(*ifst, ofst, FLAGS_reverse, opts);
     85 
     86   ofst->Write(out_fname);
     87 
     88   return 0;
     89 }
     90