Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef FST_SCRIPT_RANDGEN_H_
00018 #define FST_SCRIPT_RANDGEN_H_
00019
00020 #include <fst/script/arg-packs.h>
00021 #include <fst/script/fst-class.h>
00022 #include <fst/randgen.h>
00023
00024 namespace fst {
00025 namespace script {
00026
00027 enum RandArcSelection {
00028 UNIFORM_ARC_SELECTOR,
00029 LOG_PROB_ARC_SELECTOR,
00030 FAST_LOG_PROB_ARC_SELECTOR
00031 };
00032
00033
00034
00035 template<class A>
00036 struct LogProbArcSelectorGuard {
00037 typedef typename A::StateId StateId;
00038 typedef typename A::Weight Weight;
00039
00040 explicit LogProbArcSelectorGuard(int seed) {
00041 LOG(FATAL) << "LogProbArcSelectorGuard: bad weight type: "
00042 << Weight::Type();
00043 }
00044 size_t operator()(const Fst<A> &fst, StateId s) const { return 0; }
00045 };
00046
00047 template<class T>
00048 struct LogProbArcSelectorGuard<ArcTpl<TropicalWeightTpl<T> > >
00049 : public LogProbArcSelector<ArcTpl<TropicalWeightTpl<T> > > {
00050 LogProbArcSelectorGuard(int seed = time(0))
00051 : LogProbArcSelector<ArcTpl<TropicalWeightTpl<T> > >(seed) {}
00052 };
00053
00054 template<class T>
00055 struct LogProbArcSelectorGuard<ArcTpl<LogWeightTpl<T> > >
00056 : public LogProbArcSelector<ArcTpl<LogWeightTpl<T> > > {
00057 LogProbArcSelectorGuard(int seed = time(0))
00058 : LogProbArcSelector<ArcTpl<LogWeightTpl<T> > >(seed) {}
00059 };
00060
00061
00062
00063 template<class A>
00064 struct FastLogProbArcSelectorGuard {
00065 typedef typename A::StateId StateId;
00066 typedef typename A::Weight Weight;
00067
00068 explicit FastLogProbArcSelectorGuard(int seed) {
00069 LOG(FATAL) << "LogProbArcSelectorGuard: bad weight type: "
00070 << Weight::Type();
00071 }
00072 size_t operator()(const Fst<A> &fst, StateId s) const { return 0; }
00073 };
00074
00075 template<class T>
00076 struct FastLogProbArcSelectorGuard<ArcTpl<TropicalWeightTpl<T> > >
00077 : public FastLogProbArcSelector<ArcTpl<TropicalWeightTpl<T> > > {
00078 FastLogProbArcSelectorGuard(int seed = time(0))
00079 : FastLogProbArcSelector<ArcTpl<TropicalWeightTpl<T> > >(seed) {}
00080 };
00081
00082 template<class T>
00083 struct FastLogProbArcSelectorGuard<ArcTpl<LogWeightTpl<T> > >
00084 : public FastLogProbArcSelector<ArcTpl<LogWeightTpl<T> > > {
00085 FastLogProbArcSelectorGuard(int seed = time(0))
00086 : FastLogProbArcSelector<ArcTpl<LogWeightTpl<T> > >(seed) {}
00087 };
00088
00089 typedef args::Package<const FstClass &, MutableFstClass*, int32,
00090 const RandGenOptions<RandArcSelection> &> RandGenArgs;
00091
00092 template<class Arc>
00093 void RandGen(RandGenArgs *args) {
00094 const Fst<Arc> &ifst = *(args->arg1.GetFst<Arc>());
00095 MutableFst<Arc> *ofst = args->arg2->GetMutableFst<Arc>();
00096 int32 seed = args->arg3;
00097 const RandGenOptions<RandArcSelection> &opts = args->arg4;
00098
00099 if (opts.arc_selector == UNIFORM_ARC_SELECTOR) {
00100 UniformArcSelector<Arc> arc_selector(seed);
00101 RandGenOptions< UniformArcSelector<Arc> >
00102 ropts(arc_selector, opts.max_length,
00103 opts.npath);
00104 RandGen(ifst, ofst, ropts);
00105 } else if (opts.arc_selector == FAST_LOG_PROB_ARC_SELECTOR) {
00106 FastLogProbArcSelectorGuard<Arc> arc_selector(seed);
00107 RandGenOptions< FastLogProbArcSelectorGuard<Arc> >
00108 ropts(arc_selector, opts.max_length,
00109 opts.npath);
00110 RandGen(ifst, ofst, ropts);
00111 } else {
00112 LogProbArcSelectorGuard<Arc> arc_selector(seed);
00113 RandGenOptions< LogProbArcSelectorGuard<Arc> >
00114 ropts(arc_selector, opts.max_length,
00115 opts.npath);
00116 RandGen(ifst, ofst, ropts);
00117 }
00118 }
00119
00120
00121
00122 void RandGen(const FstClass &ifst, MutableFstClass *ofst, int32 seed = time(0),
00123 const RandGenOptions<RandArcSelection> &opts =
00124 fst::RandGenOptions<fst::script::RandArcSelection>(
00125 fst::script::UNIFORM_ARC_SELECTOR));
00126
00127 }
00128 }
00129
00130
00131
00132 #endif /// FST_SCRIPT_RANDGEN_H_
00133