15 #ifndef NLP_GRM2_BAUMWELCH_RANDOMIZE_H_ 16 #define NLP_GRM2_BAUMWELCH_RANDOMIZE_H_ 23 #include <fst/mutable-fst.h> 24 #include <fst/weight.h> 30 template <
class Weight>
36 : rand_(seed), dist_(kDelta, 1.0) {}
38 Weight
operator()()
const {
return Weight(-std::log(dist_(rand_))); }
41 mutable std::mt19937_64 rand_;
42 mutable std::uniform_real_distribution<ValueType> dist_;
48 void Randomize(MutableFst<Arc> *
fst, uint64_t seed = std::random_device()()) {
49 using Weight =
typename Arc::Weight;
51 for (StateIterator<MutableFst<Arc>> siter(*fst); !siter.Done();
53 const auto state = siter.Value();
55 for (MutableArcIterator<MutableFst<Arc>> aiter(fst, state); !aiter.Done();
57 auto arc = aiter.Value();
58 arc.weight = generator();
62 if (fst->Final(state) != Weight::Zero()) fst->SetFinal(state, generator());
68 #endif // NLP_GRM2_BAUMWELCH_RANDOMIZE_H_
void Randomize(MutableFst< Arc > *fst, uint64_t seed=std::random_device()())