Home | History | Annotate | Download | only in bin
      1 // fstrandgen.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 // Generates random paths through an FST.
     21 
     22 #include <fst/script/randgen.h>
     23 
     24 DEFINE_int32(max_length, INT_MAX, "Maximum path length");
     25 DEFINE_int64(npath, 1, "Number of paths to generate");
     26 DEFINE_int32(seed, time(0), "Random seed");
     27 DEFINE_string(select, "uniform", "Selection type: one of: "
     28               " \"uniform\", \"log_prob\" (when appropriate),"
     29 	      " \"fast_log_prob\" (when appropriate)");
     30 DEFINE_bool(weighted, false,
     31             "Output tree weighted by path count vs. unweighted paths");
     32 DEFINE_bool(remove_total_weight, false,
     33             "Remove total weight when output weighted");
     34 
     35 int main(int argc, char **argv) {
     36   namespace s = fst::script;
     37   using fst::script::FstClass;
     38   using fst::script::VectorFstClass;
     39 
     40   string usage = "Generates random paths through an FST.\n\n  Usage: ";
     41   usage += argv[0];
     42   usage += " [in.fst [out.fst]]\n";
     43 
     44   std::set_new_handler(FailedNewHandler);
     45   SetFlags(usage.c_str(), &argc, &argv, true);
     46   if (argc > 3) {
     47     ShowUsage();
     48     return 1;
     49   }
     50 
     51   VLOG(1) << argv[0] << ": Seed = " << FLAGS_seed;
     52 
     53   string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
     54   string out_name = argc > 2 ? argv[2] : "";
     55 
     56   FstClass *ifst = FstClass::Read(in_name);
     57   if (!ifst) return 1;
     58 
     59   VectorFstClass ofst(ifst->ArcType());
     60 
     61   s::RandArcSelection ras;
     62 
     63   if (FLAGS_select == "uniform") {
     64     ras = s::UNIFORM_ARC_SELECTOR;
     65   } else if (FLAGS_select == "log_prob") {
     66     ras = s::LOG_PROB_ARC_SELECTOR;
     67   } else if (FLAGS_select == "fast_log_prob") {
     68     ras = s::FAST_LOG_PROB_ARC_SELECTOR;
     69   } else {
     70     LOG(ERROR) << argv[0] << ": Unknown selection type \""
     71                << FLAGS_select << "\"\n";
     72     return 1;
     73   }
     74 
     75   s::RandGen(*ifst, &ofst, FLAGS_seed,
     76              fst::RandGenOptions<s::RandArcSelection>(
     77                  ras, FLAGS_max_length, FLAGS_npath,
     78                  FLAGS_weighted, FLAGS_remove_total_weight));
     79 
     80   ofst.Write(out_name);
     81   return 0;
     82 }
     83