38 VectorFst<Arc> *
fst =
new VectorFst<Arc>();
40 cout <<
"Testing pre-determinize with "<<n_syms<<
" symbols, "<<n_states<<
" states and "<<n_arcs<<
" arcs and "<<n_final<<
" final states.\n";
41 SymbolTable *sptr = NULL;
43 vector<Label> all_syms;
46 all_syms.push_back(
i);
49 vector<StateId> all_states;
50 for (
size_t i = 0;
i < (
size_t)n_states;
i++) {
51 StateId this_state = fst->AddState();
52 if (
i == 0) fst->SetStart(
i);
53 all_states.push_back(this_state);
56 for (
size_t j = 0;
j < (
size_t)n_final;
j++) {
59 printf(
"calling SetFinal with %d and %f\n",
id, weight.Value());
60 fst->SetFinal(
id, weight);
63 for (
size_t i = 0;
i < (
size_t)n_arcs;
i++) {
69 StateId start_state = all_states[
kaldi::Rand() % n_states];
70 fst->AddArc(start_state, a);
73 std::cout <<
" printing before trimming\n";
75 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
76 fstprinter.Print(&std::cout,
"standard output");
81 std::cout <<
" printing after trimming\n";
83 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
84 fstprinter.Print(&std::cout,
"standard output");
87 VectorFst<Arc> *fst_copy_orig =
new VectorFst<Arc>(*fst);
89 vector<Label> extra_syms;
90 if (fst->Start() != kNoStateId) {
95 std::cout <<
" printing after predeterminization\n";
97 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
98 fstprinter.Print(&std::cout,
"standard output");
104 Weight weight_threshold = Weight::Zero();
106 double delta = kDelta;
108 RmEpsilon(fst, connect, weight_threshold, nstate, delta);
111 std::cout <<
" printing after epsilon removal\n";
113 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
114 fstprinter.Print(&std::cout,
"standard output");
119 DeterminizeOptions<Arc> opts;
120 Determinize(*fst, &ofst, opts);
121 std::cout <<
" printing after determinization\n";
123 FstPrinter<Arc> fstprinter(ofst, sptr, sptr, NULL,
false,
true,
"\t");
124 fstprinter.Print(&std::cout,
"standard output");
128 std::cout <<
" printing after removing "<<num_removed<<
" instances of extra symbols\n";
130 FstPrinter<Arc> fstprinter(ofst, sptr, sptr, NULL,
false,
true,
"\t");
131 fstprinter.Print(&std::cout,
"standard output");
134 std::cout <<
" Checking equivalent to original FST.\n";
137 assert(RandEquivalent(ofst, *fst_copy_orig, 5, 0.01,
kaldi::Rand(), 100));
140 delete fst_copy_orig;
148 VectorFst<Arc> *
fst =
new VectorFst<Arc>();
149 SymbolTable *ilabels =
new SymbolTable(
"my-symbol-table");
150 SymbolTable *olabels =
new SymbolTable(
"my-symbol-table-2");
151 Label i0 = ilabels->AddSymbol(
"<eps>");
152 Label i1 = ilabels->AddSymbol(
"1");
153 Label i2 = ilabels->AddSymbol(
"2");
155 Label o0 = olabels->AddSymbol(
"<eps>");
156 Label o1 = olabels->AddSymbol(
"1");
158 assert(i0 == 0 && o0 == 0);
159 StateId s0 = fst->AddState(), s1 = fst->AddState(), s2 = fst->AddState();
163 fst->SetFinal(s2, (Weight)2);
170 fst->AddArc(s0, arc);
178 fst->AddArc(s1, arc);
180 std::cout <<
" printing before adding self-loops\n";
182 FstPrinter<Arc> fstprinter(*fst, ilabels, olabels, NULL,
false,
true,
"\t");
183 fstprinter.Print(&std::cout,
"standard output");
189 vector<Label> extra_ilabels, extra_olabels;
195 assert(fst->NumArcs(0) == 1);
196 assert(fst->NumArcs(1) == 1 + num_extra);
197 assert(fst->NumArcs(2) == num_extra);
199 std::cout <<
" printing after adding self-loops\n";
201 FstPrinter<Arc> fstprinter(*fst, ilabels, olabels, NULL,
false,
true,
"\t");
202 fstprinter.Print(&std::cout,
"standard output");
214 for (
int i = 0;
i < 10;
i++) {
215 fst::TestPreDeterminize<fst::StdArc>();
217 for (
int i = 0;
i < 5;
i++) {
218 fst::TestAddSelfLoops<fst::StdArc>();
fst::StdArc::StateId StateId
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
void CreateNewSymbols(SymbolTable *input_sym_table, int nSym, std::string prefix, std::vector< Label > *symsOut)
void TestPreDeterminize()
void AddSelfLoops(MutableFst< Arc > *fst, std::vector< typename Arc::Label > &isyms, std::vector< typename Arc::Label > &osyms)
AddSelfLoops is a function you will probably want to use alongside PreDeterminize, to add self-loops to any FSTs that you compose on the left hand side of the one modified by PreDeterminize.
int Rand(struct RandomState *state)
fst::StdArc::Weight Weight
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.
int64 DeleteISymbols(MutableFst< Arc > *fst, std::vector< typename Arc::Label > isyms)