36 for(
int i = 0;
i < 100;
i++) {
37 VectorFst<Arc> *
fst = RandFst<Arc>();
38 std::cout <<
"FST before determinizing is:\n";
40 FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL,
false,
true,
"\t");
41 fstprinter.Print(&std::cout,
"standard output");
45 DeterminizeStar<Fst<Arc> >(*fst, &ofst, kDelta, NULL, max_states);
46 std::cout <<
"FST after determinizing is:\n";
48 FstPrinter<Arc> fstprinter(ofst, NULL, NULL, NULL,
false,
true,
"\t");
49 fstprinter.Print(&std::cout,
"standard output");
51 assert(RandEquivalent(*fst, ofst, 5, 0.01,
kaldi::Rand(), 100));
53 std::cout <<
"Failed to determinize *this FST (probably not determinizable)\n";
66 VectorFst<Arc> *
fst =
new VectorFst<Arc>();
68 std::cout <<
"Testing pre-determinize with "<<n_syms<<
" symbols, "<<n_states<<
" states and "<<n_arcs<<
" arcs and "<<n_final<<
" final states.\n";
69 SymbolTable *sptr = NULL;
71 std::vector<Label> all_syms;
74 all_syms.push_back(
i);
77 std::vector<StateId> all_states;
78 for (
size_t i = 0;
i < (
size_t)n_states;
i++) {
79 StateId this_state = fst->AddState();
80 if (
i == 0) fst->SetStart(
i);
81 all_states.push_back(this_state);
84 for (
size_t j = 0;
j < (
size_t)n_final;
j++) {
87 printf(
"calling SetFinal with %d and %f\n",
id, weight.Value());
88 fst->SetFinal(
id, weight);
91 for (
size_t i = 0;
i < (
size_t)n_arcs;
i++) {
97 StateId start_state = all_states[
kaldi::Rand() % n_states];
98 fst->AddArc(start_state, a);
101 std::cout <<
" printing before trimming\n";
103 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
104 fstprinter.Print(&std::cout,
"standard output");
109 std::cout <<
" printing after trimming\n";
111 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
112 fstprinter.Print(&std::cout,
"standard output");
115 VectorFst<Arc> *fst_copy_orig =
new VectorFst<Arc>(*fst);
117 std::vector<Label> extra_syms;
118 if (fst->Start() != kNoStateId) {
122 std::cout <<
" printing after predeterminization\n";
124 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
125 fstprinter.Print(&std::cout,
"standard output");
131 Weight weight_threshold = Weight::Zero();
133 double delta = kDelta;
135 RmEpsilon(fst, connect, weight_threshold, nstate, delta);
138 std::cout <<
" printing after epsilon removal\n";
140 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
141 fstprinter.Print(&std::cout,
"standard output");
143 VectorFst<Arc> ofst_orig;
144 VectorFst<Arc> ofst_star;
147 printf(
"Determinizing with baseline\n");
148 DeterminizeOptions<Arc> opts;
149 Determinize(*fst, &ofst_orig, opts);
153 printf(
"Determinizing with DeterminizeStar\n");
158 std::cout <<
" printing after determinization [baseline]\n";
159 FstPrinter<Arc> fstprinter(ofst_orig, sptr, sptr, NULL,
false,
true,
"\t");
160 fstprinter.Print(&std::cout,
"standard output");
161 assert(ofst_orig.Properties(kIDeterministic,
true) == kIDeterministic);
165 std::cout <<
" printing after determinization [star]\n";
166 FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL,
false,
true,
"\t");
167 fstprinter.Print(&std::cout,
"standard output");
168 assert(ofst_star.Properties(kIDeterministic,
true) == kIDeterministic);
171 assert(RandEquivalent(ofst_orig, ofst_star, 5, 0.01,
kaldi::Rand(), 100));
174 std::cout <<
" printing after removing "<<num_removed<<
" instances of extra symbols\n";
176 FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL,
false,
true,
"\t");
177 fstprinter.Print(&std::cout,
"standard output");
180 std::cout <<
" Checking equivalent to original FST.\n";
183 assert(RandEquivalent(ofst_star, *fst_copy_orig, 5, 0.01,
kaldi::Rand(), 100));
186 delete fst_copy_orig;
191 for(
int i = 0;
i < 10;
i++) {
194 VectorFst<Arc> *ifst = RandFst<Arc>(opts);
196 Determinize(*ifst, &ofst);
197 assert(RandEquivalent(*ifst, ofst, 5, 0.01,
kaldi::Rand(), 100));
207 VectorFst<Arc> *
fst =
new VectorFst<Arc>();
209 std::cout <<
"Testing pre-determinize with "<<n_syms<<
" symbols, "<<n_states<<
" states and "<<n_arcs<<
" arcs and "<<n_final<<
" final states.\n";
210 SymbolTable *sptr = NULL;
212 std::vector<Label> all_syms;
214 for (
size_t i = 0;
i < (
size_t)n_syms;
i++)
215 all_syms.push_back(
i);
218 std::vector<StateId> all_states;
219 for (
size_t i = 0;
i < (
size_t)n_states;
i++) {
220 StateId this_state = fst->AddState();
221 if (
i == 0) fst->SetStart(
i);
222 all_states.push_back(this_state);
225 for (
size_t j = 0;
j < (
size_t)n_final;
j++) {
228 printf(
"calling SetFinal with %d and %f\n",
id, weight.Value());
229 fst->SetFinal(
id, weight);
232 for (
size_t i = 0;
i < (
size_t)n_arcs;
i++) {
234 a.nextstate = all_states[
kaldi::Rand() % n_states];
238 StateId start_state = all_states[
kaldi::Rand() % n_states];
239 fst->AddArc(start_state, a);
242 std::cout <<
" printing before trimming\n";
244 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
245 fstprinter.Print(&std::cout,
"standard output");
250 std::cout <<
" printing after trimming\n";
252 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
253 fstprinter.Print(&std::cout,
"standard output");
256 VectorFst<Arc> *fst_copy_orig =
new VectorFst<Arc>(*fst);
258 std::vector<Label> extra_syms;
259 if (fst->Start() != kNoStateId) {
263 VectorFst<Arc> fst_pushed;
264 std::cout <<
"Pushing FST\n";
265 Push<Arc, REWEIGHT_TO_INITIAL>(*fst, &fst_pushed, kPushWeights|kPushLabels, kDelta);
267 std::cout <<
" printing after pushing\n";
269 FstPrinter<Arc> fstprinter(fst_pushed, sptr, sptr, NULL,
false,
true,
"\t");
270 fstprinter.Print(&std::cout,
"standard output");
273 assert(RandEquivalent(*fst, fst_pushed, 5, 0.01,
kaldi::Rand(), 100));
276 delete fst_copy_orig;
285 VectorFst<Arc> *
fst =
new VectorFst<Arc>();
287 std::cout <<
"Testing pre-determinize with "<<n_syms<<
" symbols, "<<n_states<<
" states and "<<n_arcs<<
" arcs and "<<n_final<<
" final states.\n";
288 SymbolTable *sptr =NULL;
290 std::vector<Label> all_syms;
292 for (
size_t i = 0;
i < (
size_t)n_syms;
i++)
293 all_syms.push_back(
i);
296 std::vector<StateId> all_states;
297 for (
size_t i = 0;
i < (
size_t)n_states;
i++) {
298 StateId this_state = fst->AddState();
299 if (
i == 0) fst->SetStart(
i);
300 all_states.push_back(this_state);
303 for (
size_t j = 0;
j < (
size_t)n_final;
j++) {
306 printf(
"calling SetFinal with %d and %f\n",
id, weight.Value());
307 fst->SetFinal(
id, weight);
310 for (
size_t i = 0;
i < (
size_t)n_arcs;
i++) {
312 a.nextstate = all_states[
kaldi::Rand() % n_states];
316 StateId start_state = all_states[
kaldi::Rand() % n_states];
317 fst->AddArc(start_state, a);
320 std::cout <<
" printing before trimming\n";
322 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
323 fstprinter.Print(&std::cout,
"standard output");
328 std::cout <<
" printing after trimming\n";
330 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
331 fstprinter.Print(&std::cout,
"standard output");
334 VectorFst<Arc> *fst_copy_orig =
new VectorFst<Arc>(*fst);
336 std::vector<Label> extra_syms;
337 if (fst->Start() != kNoStateId) {
341 std::cout <<
" printing after predeterminization\n";
343 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
344 fstprinter.Print(&std::cout,
"standard output");
350 Weight weight_threshold = Weight::Zero();
352 double delta = kDelta;
354 RmEpsilon(fst, connect, weight_threshold, nstate, delta);
357 std::cout <<
" printing after epsilon removal\n";
359 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
360 fstprinter.Print(&std::cout,
"standard output");
362 VectorFst<Arc> ofst_orig;
363 VectorFst<Arc> ofst_star;
366 printf(
"Determinizing with baseline\n");
367 DeterminizeOptions<Arc> opts;
368 Determinize(*fst, &ofst_orig, opts);
371 std::cout <<
" printing after determinization [baseline]\n";
372 FstPrinter<Arc> fstprinter(ofst_orig, sptr, sptr, NULL,
false,
true,
"\t");
373 fstprinter.Print(&std::cout,
"standard output");
378 printf(
"Determinizing with DeterminizeStar to Gallic semiring\n");
379 VectorFst<GallicArc<Arc> > gallic_fst;
383 std::cout <<
" printing after determinization by DeterminizeStar [in gallic]\n";
384 FstPrinter<GallicArc< Arc> > fstprinter(gallic_fst, sptr, sptr, NULL,
false,
true,
"\t");
385 fstprinter.Print(&std::cout,
"standard output");
389 printf(
"Pushing weights\n");
390 Push(&gallic_fst, REWEIGHT_TO_INITIAL, kDelta);
393 std::cout <<
" printing after pushing weights [in gallic]\n";
394 FstPrinter<GallicArc< Arc> > fstprinter(gallic_fst, sptr, sptr, NULL,
false,
true,
"\t");
395 fstprinter.Print(&std::cout,
"standard output");
399 printf(
"Minimizing [in Gallic]\n");
400 Minimize(&gallic_fst);
402 std::cout <<
" printing after minimization [in gallic]\n";
403 FstPrinter<GallicArc< Arc> > fstprinter(gallic_fst, sptr, sptr, NULL,
false,
true,
"\t");
404 fstprinter.Print(&std::cout,
"standard output");
407 printf(
"Converting gallic back to regular [my approach]\n");
409 typename Arc::Weight, GALLIC_LEFT> > fwfst(gallic_fst);
411 std::cout <<
" printing factor-weight FST\n";
412 FstPrinter<GallicArc< Arc> > fstprinter(fwfst, sptr, sptr, NULL,
false,
true,
"\t");
413 fstprinter.Print(&std::cout,
"standard output");
416 Map(fwfst, &ofst_star, FromGallicMapper<Arc, GALLIC_LEFT>());
419 std::cout <<
" printing after converting back to regular FST\n";
420 FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL,
false,
true,
"\t");
421 fstprinter.Print(&std::cout,
"standard output");
427 assert(RandEquivalent(ofst_orig, ofst_star, 5, 0.01,
kaldi::Rand(), 100));
431 std::cout <<
" printing after removing "<<num_removed<<
" instances of extra symbols\n";
433 FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL,
false,
true,
"\t");
434 fstprinter.Print(&std::cout,
"standard output");
437 std::cout <<
" Checking equivalent to original FST.\n";
440 assert(RandEquivalent(ofst_star, *fst_copy_orig, 5, 0.01,
kaldi::Rand(), 100));
443 delete fst_copy_orig;
453 if (
sizeof(inttype) == 1) N = 64;
454 std::vector<std::vector<Label> > strings(N);
455 std::vector<inttype> ids(N);
457 for (
int i = 0;
i < N;
i++) {
459 std::vector<Label> vec;
461 if (
i < 500 && vec.size() == 0) ids[
i] = sr.
IdOfEmpty();
462 else if (
i < 500 && vec.size() == 1) ids[
i] = sr.
IdOfLabel(vec[0]);
468 for (
int i = 0;
i < N;
i++) {
469 std::vector<Label> tmpv;
472 assert(tmpv == strings[i]);
473 assert(sr.
IdOfSeq(strings[i]) == ids[
i]);
474 if (strings[i].size() == 0) assert(ids[i] == sr.
IdOfEmpty());
475 if (strings[i].size() == 1) assert(ids[i] == sr.
IdOfLabel(strings[i][0]));
477 if (
sizeof(inttype) != 1) {
478 size_t prefix_len =
kaldi::Rand() % (strings[
i].size() + 1);
480 std::vector<Label> vec2;
482 for (
size_t j = 0;
j < strings[
i].size()-prefix_len;
j++) {
483 assert(vec2[
j] == strings[i][
j+prefix_len]);
495 for (
int i = 0;
i < 3;
i++) {
497 fst::TestStringRepository<fst::StdArc, int>();
498 fst::TestStringRepository<fst::StdArc, unsigned int>();
500 fst::TestStringRepository<fst::StdArc, unsigned char>();
501 fst::TestStringRepository<fst::StdArc, char>();
502 fst::TestDeterminizeGeneral<fst::StdArc>();
503 fst::TestDeterminize<fst::StdArc>();
505 fst::TestPush<fst::StdArc>();
506 fst::TestMinimize<fst::StdArc>();
fst::StdArc::StateId StateId
TrivialFactorWeightFst takes as template parameter a FactorIterator as defined above.
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...
StringId IdOfSeq(const std::vector< Label > &v)
void TestDeterminizeGeneral()
StringId RemovePrefix(StringId id, size_t prefix_len)
void SeqOfId(StringId id, std::vector< Label > *v)
StringId IdOfLabel(Label l)
int Rand(struct RandomState *state)
fst::StdArc::Weight Weight
void TestStringRepository()
int64 DeleteISymbols(MutableFst< Arc > *fst, std::vector< typename Arc::Label > isyms)
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...