Home | History | Annotate | Download | only in bin
      1 // fstminimize.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 // Minimizes a deterministic FSA.
     21 //
     22 
     23 #include <fst/script/minimize.h>
     24 
     25 DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta");
     26 
     27 
     28 int main(int argc, char **argv) {
     29   namespace s = fst::script;
     30   using fst::script::FstClass;
     31   using fst::script::MutableFstClass;
     32   using fst::script::VectorFstClass;
     33 
     34   string usage = "Minimizes a deterministic FST.\n\n  Usage: ";
     35   usage += argv[0];
     36   usage += " [in.fst [out1.fst [out2.fst]]]\n";
     37 
     38   std::set_new_handler(FailedNewHandler);
     39   SET_FLAGS(usage.c_str(), &argc, &argv, true);
     40   if (argc > 4) {
     41     ShowUsage();
     42     return 1;
     43   }
     44 
     45   string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
     46   string out1_name = (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
     47   string out2_name = (argc > 3 && strcmp(argv[3], "-") != 0) ? argv[3] : "";
     48 
     49   if (out1_name.empty() && out2_name.empty() && argc > 3) {
     50     LOG(ERROR) << "Both outputs can't be standard out.";
     51     return 1;
     52   }
     53 
     54   MutableFstClass *fst1 = MutableFstClass::Read(in_name, true);
     55   if (!fst1) return 1;
     56 
     57   MutableFstClass *fst2 = argc > 3 ?
     58       new VectorFstClass(fst1->ArcType()) : 0;
     59 
     60   s::Minimize(fst1, fst2, FLAGS_delta);
     61 
     62   fst1->Write(out1_name);
     63   if (fst2)
     64     fst2->Write(out2_name);
     65 
     66   return 0;
     67 }
     68