All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TrivialFactorWeightFstImpl< A, F > Class Template Reference

#include <trivial-factor-weight.h>

Inheritance diagram for TrivialFactorWeightFstImpl< A, F >:
Collaboration diagram for TrivialFactorWeightFstImpl< A, F >:

Classes

struct  Element
 
class  ElementEqual
 
class  ElementKey
 

Public Types

typedef A Arc
 
typedef A::Label Label
 
typedef A::Weight Weight
 
typedef A::StateId StateId
 
typedef F FactorIterator
 

Public Member Functions

 TrivialFactorWeightFstImpl (const Fst< A > &fst, const TrivialFactorWeightOptions< A > &opts)
 
 TrivialFactorWeightFstImpl (const TrivialFactorWeightFstImpl< A, F > &impl)
 
 ~TrivialFactorWeightFstImpl ()
 
StateId Start ()
 
Weight Final (StateId s)
 
size_t NumArcs (StateId s)
 
size_t NumInputEpsilons (StateId s)
 
size_t NumOutputEpsilons (StateId s)
 
void InitArcIterator (StateId s, ArcIteratorData< A > *data)
 
StateId FindState (const Element &e)
 
void Expand (StateId s)
 

Private Types

typedef unordered_map< Element,
StateId, ElementKey,
ElementEqual
ElementMap
 

Private Member Functions

void operator= (const TrivialFactorWeightFstImpl< A, F > &)
 

Private Attributes

const Fst< A > * fst_
 
float delta_
 
uint32 mode_
 
Label extra_ilabel_
 
Label extra_olabel_
 
vector< Elementelements_
 
ElementMap element_map_
 

Detailed Description

template<class A, class F>
class fst::TrivialFactorWeightFstImpl< A, F >

Definition at line 100 of file trivial-factor-weight.h.

Member Typedef Documentation

typedef A Arc

Definition at line 114 of file trivial-factor-weight.h.

typedef unordered_map<Element, StateId, ElementKey, ElementEqual> ElementMap
private

Definition at line 308 of file trivial-factor-weight.h.

typedef F FactorIterator

Definition at line 118 of file trivial-factor-weight.h.

typedef A::Label Label

Definition at line 115 of file trivial-factor-weight.h.

typedef A::StateId StateId

Definition at line 117 of file trivial-factor-weight.h.

typedef A::Weight Weight

Definition at line 116 of file trivial-factor-weight.h.

Constructor & Destructor Documentation

TrivialFactorWeightFstImpl ( const Fst< A > &  fst,
const TrivialFactorWeightOptions< A > &  opts 
)
inline

Definition at line 134 of file trivial-factor-weight.h.

135  : CacheImpl<A>(opts),
136  fst_(fst.Copy()),
137  delta_(opts.delta),
138  extra_ilabel_(opts.extra_ilabel),
139  extra_olabel_(opts.extra_olabel) {
140  SetType("factor-weight");
141  uint64 props = fst.Properties(kFstProperties, false);
142  SetProperties(FactorWeightProperties(props), kCopyProperties);
143 
144  SetInputSymbols(fst.InputSymbols());
145  SetOutputSymbols(fst.OutputSymbols());
146  }
TrivialFactorWeightFstImpl ( const TrivialFactorWeightFstImpl< A, F > &  impl)
inline

Definition at line 148 of file trivial-factor-weight.h.

149  : CacheImpl<A>(impl),
150  fst_(impl.fst_->Copy(true)),
151  delta_(impl.delta_),
152  extra_ilabel_(impl.extra_ilabel_),
153  extra_olabel_(impl.extra_olabel_) {
154  SetType("factor-weight");
155  SetProperties(impl.Properties(), kCopyProperties);
156  SetInputSymbols(impl.InputSymbols());
157  SetOutputSymbols(impl.OutputSymbols());
158  }

Definition at line 160 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::fst_.

160  {
161  delete fst_;
162  }

Member Function Documentation

void Expand ( StateId  s)
inline

Definition at line 244 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::delta_, TrivialFactorWeightFstImpl< A, F >::elements_, TrivialFactorWeightFstImpl< A, F >::extra_ilabel_, TrivialFactorWeightFstImpl< A, F >::extra_olabel_, TrivialFactorWeightFstImpl< A, F >::FindState(), and TrivialFactorWeightFstImpl< A, F >::fst_.

Referenced by TrivialFactorWeightFstImpl< A, F >::InitArcIterator(), TrivialFactorWeightFstImpl< A, F >::NumArcs(), TrivialFactorWeightFstImpl< A, F >::NumInputEpsilons(), and TrivialFactorWeightFstImpl< A, F >::NumOutputEpsilons().

244  {
245  CHECK(static_cast<size_t>(s) < elements_.size());
246  Element e = elements_[s];
247  if (e.weight != Weight::One()) {
248  FactorIterator fit(e.weight);
249  if (fit.Done()) { // Cannot be factored-> create a link to dest state directly
250  if (e.state != kNoStateId) {
251  StateId dest = FindState(Element(e.state, Weight::One()));
252  PushArc(s, Arc(extra_ilabel_, extra_olabel_, e.weight, dest));
253  } // else we're done. This is a final state.
254  } else { // Can be factored.
255  const pair<Weight, Weight> &p = fit.Value();
256  StateId dest = FindState(Element(e.state, p.second.Quantize(delta_)));
257  PushArc(s, Arc(extra_ilabel_, extra_olabel_, p.first, dest));
258  }
259  } else { // Unit weight. This corresponds to a "real" state.
260  CHECK(e.state != kNoStateId);
261  for (ArcIterator< Fst<A> > ait(*fst_, e.state);
262  !ait.Done();
263  ait.Next()) {
264  const A &arc = ait.Value();
265  FactorIterator fit(arc.weight);
266  if (fit.Done()) { // cannot be factored->just link directly to dest.
267  StateId dest = FindState(Element(arc.nextstate, Weight::One()));
268  PushArc(s, Arc(arc.ilabel, arc.olabel, arc.weight, dest));
269  } else {
270  const pair<Weight, Weight> &p = fit.Value();
271  StateId dest = FindState(Element(arc.nextstate, p.second.Quantize(delta_)));
272  PushArc(s, Arc(arc.ilabel, arc.olabel, p.first, dest));
273  }
274  }
275  // See if we have to add arcs for final-states [only if final-weight is factorable].
276  Weight final_w = fst_->Final(e.state);
277  if (final_w != Weight::Zero()) {
278  FactorIterator fit(final_w);
279  if (!fit.Done()) {
280  const pair<Weight, Weight> &p = fit.Value();
281  StateId dest = FindState(Element(kNoStateId, p.second.Quantize(delta_)));
282  PushArc(s, Arc(extra_ilabel_, extra_olabel_, p.first, dest));
283  }
284  }
285  }
286  this->SetArcs(s);
287  }
StateId FindState(const Element &e)
Weight Final ( StateId  s)
inline

Definition at line 175 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::elements_, TrivialFactorWeightFstImpl< A, F >::fst_, TrivialFactorWeightFstImpl< A, F >::Element::state, and TrivialFactorWeightFstImpl< A, F >::Element::weight.

175  {
176  if (!HasFinal(s)) {
177  const Element &e = elements_[s];
178  Weight w;
179  if (e.state == kNoStateId) { // extra state inserted to represent final weights.
180  FactorIterator fit(e.weight);
181  if (fit.Done()) { // cannot be factored.
182  w = e.weight; // so it's final
183  } else {
184  w = Weight::Zero(); // need another transition.
185  }
186  } else {
187  if (e.weight != Weight::One()) { // Not a real state.
188  w = Weight::Zero();
189  } else { // corresponds to a "real" state.
190  w = fst_->Final(e.state);
191  FactorIterator fit(w);
192  if (!fit.Done()) // we would have intermediate states representing this final state.
193  w = Weight::Zero();
194  }
195  }
196  this->SetFinal(s, w);
197  return w;
198  } else {
199  return CacheImpl<A>::Final(s);
200  }
201  }
StateId FindState ( const Element e)
inline

Definition at line 230 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::element_map_, and TrivialFactorWeightFstImpl< A, F >::elements_.

Referenced by TrivialFactorWeightFstImpl< A, F >::Expand(), and TrivialFactorWeightFstImpl< A, F >::Start().

230  {
231  typename ElementMap::iterator eit = element_map_.find(e);
232  if (eit != element_map_.end()) {
233  return (*eit).second;
234  } else {
235  StateId s = elements_.size();
236  elements_.push_back(e);
237  element_map_.insert(pair<const Element, StateId>(e, s));
238  return s;
239  }
240  }
void InitArcIterator ( StateId  s,
ArcIteratorData< A > *  data 
)
inline

Definition at line 221 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::Expand().

Referenced by TrivialFactorWeightFst< A, F >::InitArcIterator().

221  {
222  if (!HasArcs(s))
223  Expand(s);
225  }
size_t NumArcs ( StateId  s)
inline

Definition at line 203 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::Expand(), and fst::NumArcs().

203  {
204  if (!HasArcs(s))
205  Expand(s);
206  return CacheImpl<A>::NumArcs(s);
207  }
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.
size_t NumInputEpsilons ( StateId  s)
inline

Definition at line 209 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::Expand().

209  {
210  if (!HasArcs(s))
211  Expand(s);
213  }
size_t NumOutputEpsilons ( StateId  s)
inline

Definition at line 215 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::Expand().

215  {
216  if (!HasArcs(s))
217  Expand(s);
219  }
void operator= ( const TrivialFactorWeightFstImpl< A, F > &  )
private
StateId Start ( )
inline

Definition at line 164 of file trivial-factor-weight.h.

References TrivialFactorWeightFstImpl< A, F >::FindState(), and TrivialFactorWeightFstImpl< A, F >::fst_.

164  {
165  if (!HasStart()) {
166  StateId s = fst_->Start();
167  if (s == kNoStateId)
168  return kNoStateId;
169  StateId start = this->FindState(Element(fst_->Start(), Weight::One()));
170  this->SetStart(start);
171  }
172  return CacheImpl<A>::Start();
173  }
StateId FindState(const Element &e)

Member Data Documentation

float delta_
private

Definition at line 311 of file trivial-factor-weight.h.

Referenced by TrivialFactorWeightFstImpl< A, F >::Expand().

ElementMap element_map_
private
Label extra_ilabel_
private

Definition at line 313 of file trivial-factor-weight.h.

Referenced by TrivialFactorWeightFstImpl< A, F >::Expand().

Label extra_olabel_
private

Definition at line 314 of file trivial-factor-weight.h.

Referenced by TrivialFactorWeightFstImpl< A, F >::Expand().

uint32 mode_
private

Definition at line 312 of file trivial-factor-weight.h.


The documentation for this class was generated from the following file: