31 template<
class Arc,
class I>
39 vector<I> vec_nozeros;
40 for (
int i = 0;
i < len;
i++) {
43 if (j != 0) vec_nozeros.push_back(j);
53 assert(w == Weight::One());
54 assert(vec_nozeros == vec2);
55 assert(vec_nozeros == vec3);
57 if (vec2.size() != 0 || vec3.size() != 0) {
60 vector<VectorFst<Arc> > fstvec;
63 assert(RandEquivalent(vfst, fstvec[0], 2, 0.01,
68 if (!include_eps) vec = vec_nozeros;
80 VectorFst<Arc> *
fst = RandFst<Arc>();
81 VectorFst<Arc> fst_copy(fst);
83 vector<typename Arc::Label> syms;
95 VectorFst<Arc> *
fst =
new VectorFst<Arc>();
97 cout <<
"Testing pre-determinize with "<<n_syms<<
" symbols, "<<n_states<<
" states and "<<n_arcs<<
" arcs and "<<n_final<<
" final states.\n";
98 SymbolTable *sptr =
new SymbolTable(
"my-symbol-table");
99 sptr->AddSymbol(
"<eps>");
101 sptr =
new SymbolTable(
"my-symbol-table");
103 vector<Label> all_syms;
105 for (
size_t i = 0;
i < (
size_t)n_syms;
i++) {
106 std::stringstream ss;
107 if (
i == 0) ss <<
"<eps>";
109 Label cur_lab = sptr->AddSymbol(ss.str());
110 assert(cur_lab == (Label)i);
111 all_syms.push_back(cur_lab);
113 assert(all_syms[0] == 0);
116 vector<StateId> all_states;
117 for (
size_t i = 0;
i < (
size_t)n_states;
i++) {
118 StateId this_state = fst->AddState();
119 if (
i == 0) fst->SetStart(
i);
120 all_states.push_back(this_state);
123 for (
size_t j = 0;
j < (
size_t)n_final;
j++) {
126 printf(
"calling SetFinal with %d and %f\n",
id, weight.Value());
127 fst->SetFinal(
id, weight);
130 for (
size_t i = 0;
i < (
size_t)n_arcs;
i++) {
132 a.nextstate = all_states[
kaldi::Rand() % n_states];
136 StateId start_state = all_states[
kaldi::Rand() % n_states];
137 fst->AddArc(start_state, a);
140 std::cout <<
" printing before trimming\n";
142 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
143 fstprinter.Print(&std::cout,
"standard output");
148 std::cout <<
" printing after trimming\n";
150 FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL,
false,
true,
"\t");
151 fstprinter.Print(&std::cout,
"standard output");
154 VectorFst<Arc> *fst_copy_orig =
new VectorFst<Arc>(*fst);
156 VectorFst<Arc> *fst_det =
new VectorFst<Arc>;
158 vector<Label> extra_syms;
159 if (fst->Start() != kNoStateId) {
171 KALDI_LOG <<
"Num states [orig]: " << fst->NumStates() <<
"[det]" << fst_det->NumStates();
172 assert(RandEquivalent(*fst, *fst_det, 5, 0.01,
kaldi::Rand(), 100));
175 delete fst_copy_orig;
188 VectorFst<Arc> *
fst = RandFst<Arc>();
189 VectorFst<Arc> fst2(*fst);
190 PushInLog<REWEIGHT_TO_INITIAL>(&fst2, kPushLabels|kPushWeights, 0.01);
191 assert(RandEquivalent(*fst, fst2, 5, 0.01,
kaldi::Rand(), 100));
203 VectorFst<Arc> *
fst = RandFst<Arc>();
205 Project(fst, PROJECT_INPUT);
208 VectorFst<Arc> fst2(*fst);
209 internal::AcceptorMinimize(&fst2);
211 assert(RandEquivalent(*fst, fst2, 5, 0.01,
kaldi::Rand(), 100));
219 VectorFst<Arc> *
fst = RandFst<Arc>();
224 VectorFst<Arc> fst2(*fst);
235 assert(RandEquivalent(*fst, fst2, 5, 0.01,
kaldi::Rand(), 100));
246 if (a == kNoLabel)
return -1;
247 else if (a == 0)
return 0;
249 return 1 + ((a-1) % 10);
256 VectorFst<Arc> *
fst = RandFst<Arc>();
261 VectorFst<Arc> fst2(*fst);
272 assert(RandEquivalent(*fst, fst2, 5, 0.01,
kaldi::Rand(), 100));
282 VectorFst<Arc> *ans =
new VectorFst<Arc>;
287 for (Label
i = 0;
i < fsts.size();
i++) {
288 if (fsts[
i] != NULL) {
289 VectorFst<Arc> i_fst;
290 i_fst.AddState(); i_fst.AddState();
291 i_fst.SetStart(0); i_fst.SetFinal(1, Weight::One());
292 i_fst.AddArc(0,
Arc(0,
i, Weight::One(), 1));
293 VectorFst<Arc> other_fst(*(fsts[
i]));
296 Concat(&i_fst, other_fst);
308 vector<const ExpandedFst<Arc>* > fsts(num_fsts, (
const ExpandedFst<Arc>*)NULL);
309 for (
int i = 0;
i < num_fsts;
i++) {
311 VectorFst<Arc> *
fst = RandFst<Arc>();
312 Project(fst, PROJECT_INPUT);
322 assert(fst1->Properties(kOLabelSorted, kOLabelSorted) != 0);
324 assert(RandEquivalent(*fst1, *fst2, 5, 0.01,
kaldi::Rand(), 100));
327 std::sort(fsts.begin(), fsts.end());
328 fsts.erase(std::unique(fsts.begin(), fsts.end()), fsts.end());
329 for (
int i = 0;
i < (int)fsts.size();
i++)
337 for (
size_t i = 0;
i < 4;
i++) {
340 VectorFst<Arc> *
fst = RandFst<Arc>();
343 VectorFst<Arc> fst_path;
345 std::cout <<
"EqualAlign succeeded\n";
346 vector<int32> isymbol_seq, osymbol_seq;
349 assert(isymbol_seq.size() == length);
351 VectorFst<Arc> fst_composed;
352 Compose(fst_path, *fst, &fst_composed);
353 assert(fst_composed.Start() != kNoStateId);
355 std::cout <<
"EqualAlign did not generate alignment\n";
362 template<
class Arc>
void Print(
const Fst<Arc> &
fst, std::string message) {
363 std::cout << message <<
"\n";
364 FstPrinter<Arc> fstprinter(fst, NULL, NULL, NULL,
false,
true,
"\t");
365 fstprinter.Print(&std::cout,
"standard output");
371 for (
size_t i = 0;
i < 4;
i++) {
374 VectorFst<Arc> *
fst = RandFst<Arc>();
376 UniformArcSelector<Arc> selector;
377 RandGenOptions<UniformArcSelector<Arc> > randgen_opts(selector);
378 VectorFst<Arc> fst_path;
379 RandGen(*fst, &fst_path, randgen_opts);
380 Project(&fst_path, PROJECT_INPUT);
383 VectorFst<Arc> fst_nouseless(*fst);
387 VectorFst<Arc> orig_composed,
389 Compose(fst_path, *fst, &orig_composed);
390 Compose(fst_path, fst_nouseless, &nouseless_composed);
395 VectorFst<Arc> orig_bestpath,
397 ShortestPath(orig_composed, &orig_bestpath);
398 ShortestPath(nouseless_composed, &nouseless_bestpath);
403 GetLinearSymbolSequence<Arc, int>(orig_bestpath, NULL, NULL, &worig);
404 GetLinearSymbolSequence<Arc, int>(nouseless_bestpath, NULL, NULL, &wnouseless);
418 for (
int i = 0;
i < 5;
i++) {
419 fst::TestMakeLinearAcceptor<fst::StdArc, int>();
420 fst::TestMakeLinearAcceptor<fst::StdArc, int32>();
421 fst::TestMakeLinearAcceptor<fst::StdArc, uint32>();
422 fst::TestSafeDeterminizeWrapper<fst::StdArc>();
423 fst::TestAcceptorMinimize<fst::StdArc>();
424 fst::TestMakeSymbolsSame<fst::StdArc>();
425 fst::TestMakeSymbolsSame<fst::LogArc>();
426 fst::TestMakeSymbolsSameClass<fst::StdArc>();
427 fst::TestMakeSymbolsSameClass<fst::LogArc>();
428 fst::TestMakeLoopFst<fst::StdArc>();
429 fst::TestMakeLoopFst<fst::LogArc>();
430 fst::TestEqualAlign<fst::StdArc>();
431 fst::TestEqualAlign<fst::LogArc>();
432 fst::TestRemoveUselessArcs<fst::StdArc>();
fst::StdArc::StateId StateId
void TestSafeDeterminizeWrapper()
void MakePrecedingInputSymbolsSame(bool start_is_epsilon, MutableFst< Arc > *fst)
MakePrecedingInputSymbolsSame ensures that all arcs entering any given fst state have the same input ...
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 TestMakeSymbolsSameClass()
VectorFst< Arc > * MakeLoopFstCompare(const vector< const ExpandedFst< Arc > *> &fsts)
void TestMakeLinearAcceptor()
bool ApproxEqual(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, float delta=kDelta)
Result operator()(Arg a) const
void ClearSymbols(bool clear_input, bool clear_output, MutableFst< Arc > *fst)
ClearSymbols sets all the symbols on the input and/or output side of the FST to zero, as specified.
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq's (removes duplicates) from a vector.
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
void TestDeterminizeStarInLog()
void RemoveUselessArcs(MutableFst< Arc > *fst)
void MakeLinearAcceptor(const std::vector< I > &labels, MutableFst< Arc > *ofst)
Creates unweighted linear acceptor from symbol sequence.
bool EqualAlign(const Fst< Arc > &ifst, typename Arc::StateId length, int rand_seed, MutableFst< Arc > *ofst, int num_retries)
EqualAlign is similar to RandGen, but it generates a sequence with exactly "length" input symbols...
void NbestAsFsts(const Fst< Arc > &fst, size_t n, std::vector< VectorFst< Arc > > *fsts_out)
Takes the n-shortest-paths (using ShortestPath), but outputs the result as a vector of up to n fsts...
void Closure(MutableFst< Arc > *fst, std::set< typename Arc::StateId > *S, const std::vector< bool > &pVec)
VectorFst< Arc > * MakeLoopFst(const std::vector< const ExpandedFst< Arc > *> &fsts)
MakeLoopFst creates an FST that has a state that is both initial and final (weight == Weight::One())...
bool PrecedingInputSymbolsAreSame(bool start_is_epsilon, const Fst< Arc > &fst)
Returns true if and only if the FST is such that the input symbols on arcs entering any given state a...
int Rand(struct RandomState *state)
bool PrecedingInputSymbolsAreSameClass(bool start_is_epsilon, const Fst< Arc > &fst, const F &f)
This is as PrecedingInputSymbolsAreSame, but with a functor f that maps labels to classes...
void MakePrecedingInputSymbolsSameClass(bool start_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakePrecedingInputSymbolsSame, but takes a functor object that maps labels to classes.
fst::StdArc::Weight Weight
bool FollowingInputSymbolsAreSame(bool end_is_epsilon, const Fst< Arc > &fst)
Returns true if and only if the FST is such that the input symbols on arcs exiting any given state al...
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.
#define KALDI_ASSERT(cond)
void RemoveWeights(MutableFst< Arc > *ifst)
void SafeDeterminizeWrapper(MutableFst< Arc > *ifst, MutableFst< Arc > *ofst, float delta)
Does PreDeterminize and DeterminizeStar and then removes the disambiguation symbols.
void Print(const Fst< Arc > &fst, std::string message)
void SafeDeterminizeMinimizeWrapperInLog(VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, float delta)
SafeDeterminizeMinimizeWapperInLog is as SafeDeterminizeMinimizeWrapper except it first casts tothe l...
void SafeDeterminizeMinimizeWrapper(MutableFst< Arc > *ifst, VectorFst< Arc > *ofst, float delta)
SafeDeterminizeMinimizeWapper is as SafeDeterminizeWrapper except that it also minimizes (encoded min...
bool FollowingInputSymbolsAreSameClass(bool end_is_epsilon, const Fst< Arc > &fst, const F &f)
void MakeFollowingInputSymbolsSameClass(bool end_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakeFollowingInputSymbolsSame, but takes a functor object that maps labels to classes.
void MakeFollowingInputSymbolsSame(bool end_is_epsilon, MutableFst< Arc > *fst)
MakeFollowingInputSymbolsSame ensures that all arcs exiting any given fst state have the same input s...
void TestMakeSymbolsSame()
void TestRemoveUselessArcs()
void TestAcceptorMinimize()