41 SymbolTable symtab(
"my-symbol-table"), *sptr = &symtab;
43 vector<Label> all_syms;
45 for (
size_t i = 0;
i < (
size_t)n_syms;
i++) {
47 if (
i == 0) ss <<
"<eps>";
49 Label cur_lab = sptr->AddSymbol(ss.str());
50 assert(cur_lab == (Label)i);
51 all_syms.push_back(cur_lab);
53 assert(all_syms[0] == 0);
56 int cur_num_states = 1;
57 for (
int i = 0;
i < n_arcs;
i++) {
62 dst_state = cur_num_states++; fst.AddState();
70 arc.nextstate = dst_state;
71 fst.AddArc(src_state, arc);
73 for (
int i = 0;
i < n_final;
i++) {
81 if (fst.Start() == kNoStateId)
return;
83 std::cout <<
" printing after trimming\n";
85 FstPrinter<Arc> fstprinter(fst, sptr, sptr, NULL,
false,
true,
"\t");
86 fstprinter.Print(&std::cout,
"standard output");
89 VectorFst<Arc> fst_copy1(fst);
97 std::cout <<
"copy1 = \n";
98 FstPrinter<Arc> fstprinter(fst_copy1, sptr, sptr, NULL,
false,
true,
"\t");
99 fstprinter.Print(&std::cout,
"standard output");
103 int num_states_0 = fst.NumStates();
104 int num_states_1 = fst_copy1.NumStates();
107 std::cout <<
"Number of states 0 = "<<num_states_0<<
", 1 = "<<num_states_1<<
'\n';
109 assert(RandEquivalent(fst, fst_copy1, 5, 0.01,
kaldi::Rand(), 100));
116 typedef VectorFst<LogArc> Fst;
120 VectorFst<LogArc> *logfst = RandFst<LogArc>();
123 for (StateId s = 0; s < logfst->NumStates(); s++) {
124 Weight w = logfst->Final(s);
125 for (ArcIterator<Fst> aiter(*logfst, s); !aiter.Done(); aiter.Next()) {
126 w =
Plus(w, aiter.Value().weight);
128 if (w != Weight::Zero()) {
129 logfst->SetFinal(s,
Divide(logfst->Final(s), w, DIVIDE_ANY));
130 for (MutableArcIterator<Fst> aiter(logfst, s); !aiter.Done(); aiter.Next()) {
131 Arc a = aiter.Value();
132 a.weight =
Divide(a.weight, w, DIVIDE_ANY);
142 std::cout <<
"logfst = \n";
143 FstPrinter<LogArc> fstprinter(*logfst, NULL, NULL, NULL,
false,
true,
"\t");
144 fstprinter.Print(&std::cout,
"standard output");
147 VectorFst<StdArc>
fst;
149 VectorFst<StdArc> fst_copy(fst);
152 assert(RandEquivalent(fst, fst_copy, 5, 0.01,
kaldi::Rand(), 100));
153 VectorFst<LogArc> logfst2;
157 std::cout <<
"logfst2 = \n";
158 FstPrinter<LogArc> fstprinter(logfst2, NULL, NULL, NULL,
false,
true,
"\t");
159 fstprinter.Print(&std::cout,
"standard output");
161 if (
ApproxEqual(ShortestDistance(*logfst), ShortestDistance(logfst2))) {
174 for (
int i = 0;
i < 10;
i++) {
175 TestRemoveEpsLocal<fst::StdArc>();
fst::StdArc::StateId StateId
LatticeWeightTpl< FloatType > Divide(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, DivideType typ=DIVIDE_ANY)
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
bool ApproxEqual(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, float delta=kDelta)
void RemoveEpsLocalSpecial(MutableFst< StdArc > *fst)
As RemoveEpsLocal but takes care to preserve stochasticity when cast to LogArc.
static void TestRemoveEpsLocalSpecial()
bool IsStochasticFst(const Fst< LogArc > &fst, float delta, LogArc::Weight *min_sum, LogArc::Weight *max_sum)
int Rand(struct RandomState *state)
fst::StdArc::Weight Weight
static void TestRemoveEpsLocal()