40 SymbolTable symtab(
"my-symbol-table"), *sptr = &symtab;
42 vector<Label> all_syms;
44 for (
size_t i = 0;
i < (
size_t)n_syms;
i++) {
46 if (
i == 0) ss <<
"<eps>";
48 Label cur_lab = sptr->AddSymbol(ss.str());
49 assert(cur_lab == (Label)i);
50 all_syms.push_back(cur_lab);
52 assert(all_syms[0] == 0);
55 int cur_num_states = 1;
56 for (
int i = 0;
i < n_arcs;
i++) {
61 dst_state = cur_num_states++; fst.AddState();
69 arc.nextstate = dst_state;
70 fst.AddArc(src_state, arc);
72 for (
int i = 0;
i < n_final;
i++) {
79 std::cout <<
" printing before trimming\n";
81 FstPrinter<Arc> fstprinter(fst, sptr, sptr, NULL,
false,
true,
"\t");
82 fstprinter.Print(&std::cout,
"standard output");
87 std::cout <<
" printing after trimming\n";
89 FstPrinter<Arc> fstprinter(fst, sptr, sptr, NULL,
false,
true,
"\t");
90 fstprinter.Print(&std::cout,
"standard output");
93 if (fst.Start() == kNoStateId)
return;
95 VectorFst<Arc> fst_pushed;
96 Push<Arc, REWEIGHT_TO_INITIAL>(fst, &fst_pushed, kPushLabels);
98 VectorFst<Arc> fst_factored;
99 vector<vector<typename Arc::Label> > symbols;
101 Factor(fst, &fst_factored, &symbols);
104 for (
size_t i = 0;
i < symbols.size();
i++)
105 assert(symbols[
i].size() == 0 || *(std::min(symbols[
i].begin(), symbols[
i].end())) > 0);
107 VectorFst<Arc> fst_factored_pushed;
108 vector<vector<typename Arc::Label> > symbols_pushed;
109 Factor(fst_pushed, &fst_factored_pushed, &symbols_pushed);
111 std::cout <<
"Unfactored has "<<fst.NumStates()<<
" states, factored has "<<fst_factored.NumStates()<<
", and pushed+factored has "<<fst_factored_pushed.NumStates()<<
'\n';
113 assert(fst_factored.NumStates() <= fst.NumStates());
115 assert(fst_factored_pushed.NumStates() <= fst_pushed.NumStates());
117 VectorFst<Arc> fst_factored_copy(fst_factored);
119 VectorFst<Arc> fst_factored_unfactored(fst_factored);
122 VectorFst<Arc> factor_fst;
124 VectorFst<Arc> fst_factored_unfactored2;
125 Compose(factor_fst, fst_factored, &fst_factored_unfactored2);
129 assert(RandEquivalent(fst, fst_factored_unfactored, 5, 0.01,
kaldi::Rand(), 100));
131 assert(RandEquivalent(fst, fst_factored_unfactored2, 5, 0.01,
kaldi::Rand(), 100));
133 assert(RandEquivalent(fst, fst_factored_pushed, 5, 0.01,
kaldi::Rand(), 100));
138 Label max_label = *(std::max_element(all_syms.begin(), all_syms.end()));
139 vector<vector<Label> > new_labels(max_label+1);
140 for (Label l = 1; l < static_cast<Label>(new_labels.size()); l++) {
142 for (
int i = 0;
i <
n;
i++) new_labels[l].push_back(
kaldi::Rand() % 100);
144 VectorFst<Arc> fst_expanded(fst);
147 vector<vector<Label> > factors;
148 VectorFst<Arc> fst_reduced;
149 Factor(fst_expanded, &fst_reduced, &factors);
150 assert(fst_reduced.NumStates() <= fst.NumStates());
156 std::map<Label, Label> symbols_reverse_map;
157 symbols_reverse_map[0] = 0;
158 for (Label
i = 1; i < static_cast<Label>(symbols.size());
i++) {
162 }
while (symbols_reverse_map.count(new_i) == 1);
163 symbols_reverse_map[new_i] =
i;
165 vector<vector<Label> > symbols_new;
166 vector<Label> symbol_map(symbols.size());
167 typename std::map<Label, Label>::iterator iter = symbols_reverse_map.begin();
168 for (; iter != symbols_reverse_map.end(); iter++) {
169 Label new_label = iter->first, old_label = iter->second;
170 if (new_label >= static_cast<Label>(symbols_new.size())) symbols_new.resize(new_label+1);
171 symbols_new[new_label] = symbols[old_label];
172 symbol_map[old_label] = new_label;
176 assert(RandEquivalent(fst, fst_factored_copy,
188 for (
int i = 0;
i < 25;
i++) {
189 TestFactor<fst::StdArc>();
fst::StdArc::StateId StateId
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
void CreateFactorFst(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
The function CreateFactorFst will create an FST that expands out the "factors" that are the indices o...
void Factor(const Fst< Arc > &fst, MutableFst< Arc > *ofst, std::vector< std::vector< I > > *symbols_out)
Factor identifies linear chains of states with an olabel (if any) only on the first arc of the chain...
int Rand(struct RandomState *state)
fst::StdArc::Weight Weight
void MapInputSymbols(const std::vector< I > &symbol_mapping, MutableFst< Arc > *fst)
void ExpandInputSequences(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
ExpandInputSequences expands out the input symbols into sequences of input symbols.