Home | History | Annotate | Download | only in script
      1 
      2 // Licensed under the Apache License, Version 2.0 (the "License");
      3 // you may not use this file except in compliance with the License.
      4 // You may obtain a copy of the License at
      5 //
      6 //     http://www.apache.org/licenses/LICENSE-2.0
      7 //
      8 // Unless required by applicable law or agreed to in writing, software
      9 // distributed under the License is distributed on an "AS IS" BASIS,
     10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11 // See the License for the specific language governing permissions and
     12 // limitations under the License.
     13 //
     14 // Copyright 2005-2010 Google, Inc.
     15 // Author: jpr (at) google.com (Jake Ratkiewicz)
     16 
     17 #ifndef FST_SCRIPT_RANDGEN_H_
     18 #define FST_SCRIPT_RANDGEN_H_
     19 
     20 #include <fst/script/arg-packs.h>
     21 #include <fst/script/fst-class.h>
     22 #include <fst/randgen.h>
     23 
     24 namespace fst {
     25 namespace script {
     26 
     27 enum RandArcSelection {
     28   UNIFORM_ARC_SELECTOR,
     29   LOG_PROB_ARC_SELECTOR,
     30   FAST_LOG_PROB_ARC_SELECTOR
     31 };
     32 
     33 typedef args::Package<const FstClass &, MutableFstClass*, int32,
     34                       const RandGenOptions<RandArcSelection> &> RandGenArgs;
     35 
     36 template<class Arc>
     37 void RandGen(RandGenArgs *args) {
     38   const Fst<Arc> &ifst = *(args->arg1.GetFst<Arc>());
     39   MutableFst<Arc> *ofst = args->arg2->GetMutableFst<Arc>();
     40   int32 seed = args->arg3;
     41   const RandGenOptions<RandArcSelection> &opts = args->arg4;
     42 
     43   if (opts.arc_selector == UNIFORM_ARC_SELECTOR) {
     44     UniformArcSelector<Arc> arc_selector(seed);
     45     RandGenOptions< UniformArcSelector<Arc> >
     46         ropts(arc_selector, opts.max_length,
     47               opts.npath, opts.weighted);
     48     RandGen(ifst, ofst, ropts);
     49   } else if (opts.arc_selector == FAST_LOG_PROB_ARC_SELECTOR) {
     50     FastLogProbArcSelector<Arc> arc_selector(seed);
     51     RandGenOptions< FastLogProbArcSelector<Arc> >
     52         ropts(arc_selector, opts.max_length,
     53               opts.npath, opts.weighted);
     54     RandGen(ifst, ofst, ropts);
     55   } else {
     56     LogProbArcSelector<Arc> arc_selector(seed);
     57     RandGenOptions< LogProbArcSelector<Arc> >
     58         ropts(arc_selector, opts.max_length,
     59               opts.npath, opts.weighted);
     60     RandGen(ifst, ofst, ropts);
     61   }
     62 }
     63 
     64 
     65 // Client-facing prototype
     66 void RandGen(const FstClass &ifst, MutableFstClass *ofst, int32 seed = time(0),
     67              const RandGenOptions<RandArcSelection> &opts =
     68              fst::RandGenOptions<fst::script::RandArcSelection>(
     69                  fst::script::UNIFORM_ARC_SELECTOR));
     70 
     71 }  // namespace script
     72 }  // namespace fst
     73 
     74 
     75 
     76 #endif  // FST_SCRIPT_RANDGEN_H_
     77