20 #ifndef KALDI_FSTEXT_FACTOR_INL_H_ 21 #define KALDI_FSTEXT_FACTOR_INL_H_ 39 std::vector<StatePropertiesType> *props) {
42 assert(props != NULL);
44 if (fst.Start() < 0)
return;
45 props->resize(max_state+1, 0);
46 assert(fst.Start() <= max_state);
48 for (StateId s = 0; s <= max_state; s++) {
50 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
51 const Arc &arc = aiter.Value();
54 StateId nexts = arc.nextstate;
55 assert(nexts <= max_state);
62 if (fst.Final(s) != Weight::Zero()) s_info |=
kStateFinal;
68 template<
class Arc,
class I>
69 void Factor(
const Fst<Arc> &
fst, MutableFst<Arc> *ofst,
70 std::vector<std::vector<I> > *symbols_out) {
75 assert(symbols_out != NULL);
77 if (fst.Start() < 0)
return;
78 std::vector<StateId> order;
80 DfsVisit(fst, &dfs_order_visitor);
81 assert(order.size() > 0);
82 StateId max_state = *(std::max_element(order.begin(), order.end()));
83 std::vector<StatePropertiesType> state_properties;
86 std::vector<bool>
remove(max_state+1);
96 for (StateId
i = 0;
i <= max_state;
i++)
99 std::vector<StateId> state_mapping(max_state+1, kNoStateId);
102 SymbolMapType symbol_mapping;
103 Label symbol_counter = 0;
106 symbol_mapping[eps] = symbol_counter++;
108 std::vector<I> this_sym;
109 for (
size_t i = 0;
i < order.size();
i++) {
110 StateId state = order[
i];
111 if (!
remove[state]) {
112 StateId &new_state = state_mapping[state];
113 if (new_state == kNoStateId) new_state = ofst->AddState();
114 for (ArcIterator<Fst<Arc> > aiter(fst, state); !aiter.Done(); aiter.Next()) {
115 Arc arc = aiter.Value();
116 if (arc.ilabel == 0) this_sym.clear();
119 this_sym[0] = arc.ilabel;
121 while (
remove[arc.nextstate]) {
122 ArcIterator<Fst<Arc> > aiter2(fst, arc.nextstate);
123 assert(!aiter2.Done());
124 const Arc &nextarc = aiter2.Value();
125 arc.weight =
Times(arc.weight, nextarc.weight);
126 assert(nextarc.olabel == 0);
127 if (nextarc.ilabel != 0) this_sym.push_back(nextarc.ilabel);
128 assert(static_cast<Label>(static_cast<I>(nextarc.ilabel))
130 arc.nextstate = nextarc.nextstate;
132 StateId &new_nextstate = state_mapping[arc.nextstate];
133 if (new_nextstate == kNoStateId) new_nextstate = ofst->AddState();
134 arc.nextstate = new_nextstate;
135 if (symbol_mapping.count(this_sym) != 0) arc.ilabel = symbol_mapping[this_sym];
136 else arc.ilabel = symbol_mapping[this_sym] = symbol_counter++;
137 ofst->AddArc(new_state, arc);
139 if (fst.Final(state) != Weight::Zero())
140 ofst->SetFinal(new_state, fst.Final(state));
143 ofst->SetStart(state_mapping[fst.Start()]);
146 symbols_out->resize(symbol_counter);
147 for (
typename SymbolMapType::const_iterator iter = symbol_mapping.begin();
148 iter != symbol_mapping.end(); ++iter) {
149 (*symbols_out)[iter->second] = iter->first;
154 void Factor(
const Fst<Arc> &
fst, MutableFst<Arc> *ofst1,
155 MutableFst<Arc> *ofst2) {
157 std::vector<std::vector<Label> > symbols;
158 Factor(fst, ofst2, &symbols);
162 template<
class Arc,
class I>
164 MutableFst<Arc> *
fst) {
169 fst->SetInputSymbols(NULL);
170 size_t size = sequences.size();
171 if (sequences.size() > 0) assert(sequences[0].size() == 0);
172 StateId num_states_at_start = fst->NumStates();
173 for (StateId s = 0; s < num_states_at_start; s++) {
174 StateId num_arcs = fst->NumArcs(s);
175 for (StateId aidx = 0; aidx < num_arcs; aidx++) {
176 ArcIterator<MutableFst<Arc> > aiter(*fst, s);
178 Arc arc = aiter.Value();
180 Label ilabel = arc.ilabel;
181 Label dest_state = arc.nextstate;
183 assert(ilabel < static_cast<Label>(size));
184 size_t len = sequences[ilabel].size();
186 if (len == 0) arc.ilabel = 0;
187 else arc.ilabel = sequences[ilabel][0];
188 MutableArcIterator<MutableFst<Arc> > mut_aiter(fst, s);
189 mut_aiter.Seek(aidx);
190 mut_aiter.SetValue(arc);
192 StateId curstate = -1;
193 for (
size_t n = 0;
n < len;
n++) {
196 nextstate = fst->AddState();
197 assert(nextstate >= num_states_at_start);
198 }
else nextstate = dest_state;
201 arc.ilabel = sequences[ilabel][0];
202 arc.nextstate = nextstate;
203 MutableArcIterator<MutableFst<Arc> > mut_aiter(fst, s);
204 mut_aiter.Seek(aidx);
205 mut_aiter.SetValue(arc);
207 arc.ilabel = sequences[ilabel][
n];
209 arc.weight = Weight::One();
210 arc.nextstate = nextstate;
211 fst->AddArc(curstate, arc);
213 curstate = nextstate;
222 template<
class Arc,
class I>
235 uint64 to_remove = kAcceptor|kNotAcceptor|kIDeterministic|kNonIDeterministic|
236 kNoEpsilons|kNoIEpsilons|kILabelSorted|kNotILabelSorted;
237 return props & ~to_remove;
249 template<
class Arc,
class I>
251 MutableFst<Arc> *
fst) {
259 StateId loopstate = fst->AddState();
260 assert(loopstate == 0);
262 fst->SetFinal(0, Weight::One());
263 if (sequences.size() != 0) assert(sequences[0].size() == 0);
265 for (Label olabel = 1; olabel < static_cast<Label>(sequences.size()); olabel++) {
266 size_t len = sequences[olabel].size();
268 Arc arc(0, olabel, Weight::One(), loopstate);
269 fst->AddArc(loopstate, arc);
271 StateId curstate = loopstate;
272 for (
size_t i = 0;
i < len;
i++) {
273 StateId nextstate = (
i == len-1 ? loopstate : fst->AddState());
274 Arc arc(sequences[olabel][
i], (i == 0 ? olabel : 0), Weight::One(), nextstate);
275 fst->AddArc(curstate, arc);
276 curstate = nextstate;
280 fst->SetProperties(kOLabelSorted, kOLabelSorted);
284 template<
class Arc,
class I>
286 MutableFst<Arc> *
fst) {
294 StateId loopstate = fst->AddState();
295 assert(loopstate == 0);
297 fst->SetFinal(0, Weight::One());
298 assert(symbol_map.empty() || symbol_map[0] == 0);
299 for (Label olabel = 1; olabel < static_cast<Label>(symbol_map.size()); olabel++) {
300 Arc arc(symbol_map[olabel], olabel, Weight::One(), loopstate);
301 fst->AddArc(loopstate, arc);
fst::StdArc::StateId StateId
A hashing function-object for vectors.
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
void GetStateProperties(const Fst< Arc > &fst, typename Arc::StateId max_state, std::vector< StatePropertiesType > *props)
This function works out various properties of the states in the FST, using the bit properties defined...
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
unsigned char StatePropertiesType
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...
fst::StdArc::Weight Weight
void CreateMapFst(const std::vector< I > &symbol_map, MutableFst< Arc > *fst)
CreateMapFst will create an FST representing this symbol_map.
void ExpandInputSequences(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
ExpandInputSequences expands out the input symbols into sequences of input symbols.