20 #ifndef KALDI_FSTEXT_LATTICE_UTILS_INL_H_ 21 #define KALDI_FSTEXT_LATTICE_UTILS_INL_H_ 32 template<
class Weight,
class Int>
34 const ExpandedFst<ArcTpl<Weight> > &ifst,
37 typedef ArcTpl<Weight>
Arc;
40 typedef ArcTpl<CompactWeight> CompactArc;
42 VectorFst<ArcTpl<Weight> > ffst;
43 std::vector<std::vector<Int> > labels;
45 Factor(ifst, &ffst, &labels);
48 VectorFst<ArcTpl<Weight> > invfst(ifst);
50 Factor(invfst, &ffst, &labels);
61 StateId num_states = ffst.NumStates();
62 for (StateId s = 0; s < num_states; s++) {
63 StateId news = ofst->AddState();
66 ofst->SetStart(ffst.Start());
67 for (StateId s = 0; s < num_states; s++) {
68 Weight final_weight = ffst.Final(s);
69 if (final_weight != Weight::Zero()) {
70 CompactWeight final_compact_weight(final_weight, std::vector<Int>());
71 ofst->SetFinal(s, final_compact_weight);
73 for (ArcIterator<ExpandedFst<Arc> > iter(ffst, s);
76 const Arc &arc = iter.Value();
80 CompactArc compact_arc(arc.olabel, arc.olabel,
81 CompactWeight(arc.weight, labels[arc.ilabel]),
83 ofst->AddArc(s, compact_arc);
88 template<
class Weight,
class Int>
91 MutableFst<ArcTpl<Weight> > *ofst,
93 typedef ArcTpl<Weight>
Arc;
97 typedef ArcTpl<CompactWeight> CompactArc;
102 StateId num_states = ifst.NumStates();
103 for (StateId s = 0; s < num_states; s++) {
104 StateId news = ofst->AddState();
107 ofst->SetStart(ifst.Start());
108 for (StateId s = 0; s < num_states; s++) {
109 CompactWeight final_weight = ifst.Final(s);
110 if (final_weight != CompactWeight::Zero()) {
111 StateId cur_state = s;
112 size_t string_length = final_weight.String().size();
113 for (
size_t n = 0;
n < string_length;
n++) {
114 StateId next_state = ofst->AddState();
116 Arc arc(ilabel, final_weight.String()[
n],
117 (
n == 0 ? final_weight.Weight() : Weight::One()),
119 if (invert)
std::swap(arc.ilabel, arc.olabel);
120 ofst->AddArc(cur_state, arc);
121 cur_state = next_state;
123 ofst->SetFinal(cur_state,
124 string_length > 0 ? Weight::One() : final_weight.Weight());
126 for (ArcIterator<ExpandedFst<CompactArc> > iter(ifst, s);
129 const CompactArc &arc = iter.Value();
130 size_t string_length = arc.weight.String().size();
131 StateId cur_state = s;
134 for (
size_t n = 0 ;
n+1 < string_length;
n++) {
135 StateId next_state = ofst->AddState();
136 Label ilabel = (
n == 0 ? arc.ilabel : 0),
137 olabel = static_cast<Label>(arc.weight.String()[
n]);
138 Weight weight = (
n == 0 ? arc.weight.Weight() : Weight::One());
139 Arc new_arc(ilabel, olabel, weight, next_state);
140 if (invert)
std::swap(new_arc.ilabel, new_arc.olabel);
141 ofst->AddArc(cur_state, new_arc);
142 cur_state = next_state;
144 Label ilabel = (string_length <= 1 ? arc.ilabel : 0),
145 olabel = (string_length > 0 ? arc.weight.String()[string_length-1] : 0);
146 Weight weight = (string_length <= 1 ? arc.weight.Weight() : Weight::One());
147 Arc new_arc(ilabel, olabel, weight, arc.nextstate);
148 if (invert)
std::swap(new_arc.ilabel, new_arc.olabel);
149 ofst->AddArc(cur_state, new_arc);
156 template<
class WeightIn,
class WeightOut>
158 const ExpandedFst<ArcTpl<WeightIn> > &ifst,
159 MutableFst<ArcTpl<WeightOut> > *ofst) {
160 typedef ArcTpl<WeightIn> ArcIn;
161 typedef ArcTpl<WeightOut> ArcOut;
163 ofst->DeleteStates();
166 StateId num_states = ifst.NumStates();
167 for (StateId s = 0; s < num_states; s++) {
168 StateId news = ofst->AddState();
171 ofst->SetStart(ifst.Start());
172 for (StateId s = 0; s < num_states; s++) {
173 WeightIn final_iweight = ifst.Final(s);
174 if (final_iweight != WeightIn::Zero()) {
175 WeightOut final_oweight;
177 ofst->SetFinal(s, final_oweight);
179 for (ArcIterator<ExpandedFst<ArcIn> > iter(ifst, s);
182 ArcIn arc = iter.Value();
186 oarc.ilabel = arc.ilabel;
187 oarc.olabel = arc.olabel;
188 oarc.nextstate = arc.nextstate;
189 ofst->AddArc(s, oarc);
196 template<
class Weight,
class ScaleFloat>
198 const std::vector<std::vector<ScaleFloat> > &scale,
199 MutableFst<ArcTpl<Weight> > *
fst) {
200 assert(scale.size() == 2 && scale[0].size() == 2 && scale[1].size() == 2);
203 typedef ArcTpl<Weight>
Arc;
204 typedef MutableFst<Arc> Fst;
206 StateId num_states =
fst->NumStates();
207 for (StateId s = 0; s < num_states; s++) {
208 for (MutableArcIterator<Fst> aiter(
fst, s);
211 Arc arc = aiter.Value();
216 if (final_weight != Weight::Zero())
221 template<
class Weight,
class Int>
225 typedef ArcTpl<W>
Arc;
226 typedef MutableFst<Arc> Fst;
228 StateId num_states =
fst->NumStates();
229 for (StateId s = 0; s < num_states; s++) {
230 for (MutableArcIterator<Fst> aiter(
fst, s);
233 Arc arc = aiter.Value();
234 arc.weight = W(arc.weight.Weight(), std::vector<Int>());
237 W final_weight =
fst->Final(s);
238 if (final_weight != W::Zero())
239 fst->SetFinal(s, W(final_weight.Weight(), std::vector<Int>()));
243 template<
class Weight,
class Int>
247 typedef ArcTpl<W>
Arc;
248 typedef ExpandedFst<Arc> Fst;
250 StateId num_states =
fst.NumStates();
251 for (StateId s = 0; s < num_states; s++) {
252 for (ArcIterator<Fst> aiter(
fst, s);
255 const Arc &arc = aiter.Value();
256 if (!arc.weight.String().empty())
return true;
258 W final_weight =
fst.Final(s);
259 if (!final_weight.String().empty())
return true;
265 template <
class Real>
267 const ExpandedFst<ArcTpl<TropicalWeight> > &ifst,
269 int32 num_states_cache = 50000;
270 fst::CacheOptions cache_opts(
true, num_states_cache);
271 fst::MapFstOptions mapfst_opts(cache_opts);
273 MapFst<StdArc, ArcTpl<LatticeWeightTpl<Real> >,
fst::StdArc::StateId StateId
void RemoveAlignmentsFromCompactLattice(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *fst)
Removes state-level alignments (the strings that are part of the weights).
LatticeWeightTpl< FloatType > ScaleTupleWeight(const LatticeWeightTpl< FloatType > &w, const std::vector< std::vector< ScaleFloatType > > &scale)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void ConvertLatticeWeight(const LatticeWeightTpl< Float1 > &w_in, LatticeWeightTpl< Float2 > *w_out)
Define some ConvertLatticeWeight functions that are used in various lattice conversions...
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
bool CompactLatticeHasAlignment(const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > &fst)
Returns true if lattice has alignments, i.e.
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
#define KALDI_PARANOID_ASSERT(cond)
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
Class StdToLatticeMapper maps a normal arc (StdArc) to a LatticeArc by putting the StdArc weight as t...
void ConvertFstToLattice(const ExpandedFst< ArcTpl< TropicalWeight > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< Real > > > *ofst)
Converts TropicalWeight to LatticeWeight (puts all the weight on the first float in the lattice's pai...
std::vector< std::vector< double > > DefaultLatticeScale()
Returns a default 2x2 matrix scaling factor for LatticeWeight.