LatticePhoneAligner Class Reference
Collaboration diagram for LatticePhoneAligner:

Classes

class  ComputationState
 
struct  Tuple
 
struct  TupleEqual
 
struct  TupleHash
 

Public Types

typedef CompactLatticeArc::StateId StateId
 
typedef CompactLatticeArc::Label Label
 
typedef unordered_map< Tuple, StateId, TupleHash, TupleEqualMapType
 

Public Member Functions

StateId GetStateForTuple (const Tuple &tuple, bool add_to_queue)
 
void ProcessFinal (Tuple tuple, StateId output_state)
 
void ProcessQueueElement ()
 
 LatticePhoneAligner (const CompactLattice &lat, const TransitionModel &tmodel, const PhoneAlignLatticeOptions &opts, CompactLattice *lat_out)
 
void RemoveEpsilonsFromLattice ()
 
bool AlignLattice ()
 

Public Attributes

CompactLattice lat_
 
const TransitionModeltmodel_
 
const PhoneAlignLatticeOptionsopts_
 
CompactLatticelat_out_
 
std::vector< std::pair< Tuple, StateId > > queue_
 
MapType map_
 
bool error_
 

Detailed Description

Definition at line 28 of file phone-align-lattice.cc.

Member Typedef Documentation

◆ Label

typedef CompactLatticeArc::Label Label

Definition at line 31 of file phone-align-lattice.cc.

◆ MapType

typedef unordered_map<Tuple, StateId, TupleHash, TupleEqual> MapType

Definition at line 148 of file phone-align-lattice.cc.

◆ StateId

typedef CompactLatticeArc::StateId StateId

Definition at line 30 of file phone-align-lattice.cc.

Constructor & Destructor Documentation

◆ LatticePhoneAligner()

LatticePhoneAligner ( const CompactLattice lat,
const TransitionModel tmodel,
const PhoneAlignLatticeOptions opts,
CompactLattice lat_out 
)
inline

Definition at line 247 of file phone-align-lattice.cc.

References fst::CreateSuperFinal(), and LatticePhoneAligner::lat_.

250  :
251  lat_(lat), tmodel_(tmodel), opts_(opts), lat_out_(lat_out),
252  error_(false) {
253  fst::CreateSuperFinal(&lat_); // Creates a super-final state, so the
254  // only final-probs are One().
255  }
const PhoneAlignLatticeOptions & opts_
Arc::StateId CreateSuperFinal(MutableFst< Arc > *fst)
const TransitionModel & tmodel_

Member Function Documentation

◆ AlignLattice()

bool AlignLattice ( )
inline

Definition at line 265 of file phone-align-lattice.cc.

References LatticePhoneAligner::error_, LatticePhoneAligner::GetStateForTuple(), KALDI_WARN, LatticePhoneAligner::lat_, LatticePhoneAligner::lat_out_, LatticePhoneAligner::opts_, LatticePhoneAligner::ProcessQueueElement(), LatticePhoneAligner::queue_, PhoneAlignLatticeOptions::remove_epsilon, and LatticePhoneAligner::RemoveEpsilonsFromLattice().

Referenced by kaldi::PhoneAlignLattice().

265  {
266  lat_out_->DeleteStates();
267  if (lat_.Start() == fst::kNoStateId) {
268  KALDI_WARN << "Trying to word-align empty lattice.";
269  return false;
270  }
271  ComputationState initial_comp_state;
272  Tuple initial_tuple(lat_.Start(), initial_comp_state);
273  StateId start_state = GetStateForTuple(initial_tuple, true); // True = add this to queue.
274  lat_out_->SetStart(start_state);
275 
276  while (!queue_.empty())
278 
279  if (opts_.remove_epsilon)
281 
282  return !error_;
283  }
StateId GetStateForTuple(const Tuple &tuple, bool add_to_queue)
CompactLatticeArc::StateId StateId
const PhoneAlignLatticeOptions & opts_
#define KALDI_WARN
Definition: kaldi-error.h:150
std::vector< std::pair< Tuple, StateId > > queue_

◆ GetStateForTuple()

StateId GetStateForTuple ( const Tuple tuple,
bool  add_to_queue 
)
inline

Definition at line 150 of file phone-align-lattice.cc.

References LatticePhoneAligner::lat_out_, LatticePhoneAligner::map_, and LatticePhoneAligner::queue_.

Referenced by LatticePhoneAligner::AlignLattice(), LatticePhoneAligner::ProcessFinal(), and LatticePhoneAligner::ProcessQueueElement().

150  {
151  MapType::iterator iter = map_.find(tuple);
152  if (iter == map_.end()) { // not in map.
153  StateId output_state = lat_out_->AddState();
154  map_[tuple] = output_state;
155  if (add_to_queue)
156  queue_.push_back(std::make_pair(tuple, output_state));
157  return output_state;
158  } else {
159  return iter->second;
160  }
161  }
CompactLatticeArc::StateId StateId
std::vector< std::pair< Tuple, StateId > > queue_

◆ ProcessFinal()

void ProcessFinal ( Tuple  tuple,
StateId  output_state 
)
inline

Definition at line 163 of file phone-align-lattice.cc.

References LatticePhoneAligner::Tuple::comp_state, LatticePhoneAligner::error_, LatticePhoneAligner::ComputationState::FinalWeight(), LatticePhoneAligner::GetStateForTuple(), LatticePhoneAligner::ComputationState::IsEmpty(), KALDI_ASSERT, LatticePhoneAligner::lat_out_, LatticePhoneAligner::opts_, LatticePhoneAligner::ComputationState::OutputArcForce(), fst::Plus(), and LatticePhoneAligner::tmodel_.

Referenced by LatticePhoneAligner::ProcessQueueElement().

163  {
164  // ProcessFinal is only called if the input_state has
165  // final-prob of One(). [else it should be zero. This
166  // is because we called CreateSuperFinal().]
167 
168  if (tuple.comp_state.IsEmpty()) { // computation state doesn't have
169  // anything pending.
170  std::vector<int32> empty_vec;
171  CompactLatticeWeight cw(tuple.comp_state.FinalWeight(), empty_vec);
172  lat_out_->SetFinal(output_state, Plus(lat_out_->Final(output_state), cw));
173  } else {
174  // computation state has something pending, i.e. input or
175  // output symbols that need to be flushed out. Note: OutputArc() would
176  // have returned false or we wouldn't have been called, so we have to
177  // force it out.
178  CompactLatticeArc lat_arc;
179  // Note: the next call will change the computation-state of the tuple,
180  // so it becomes a different tuple.
181  tuple.comp_state.OutputArcForce(tmodel_, opts_, &lat_arc, &error_);
182  lat_arc.nextstate = GetStateForTuple(tuple, true); // true == add to queue.
183  // The final-prob stuff will get called again from ProcessQueueElement().
184  // Note: because we did CreateSuperFinal(), this final-state on the input
185  // lattice will have no output arcs (and unit final-prob), so there will be
186  // no complications with processing the arcs from this state (there won't
187  // be any).
188  KALDI_ASSERT(output_state != lat_arc.nextstate);
189  lat_out_->AddArc(output_state, lat_arc);
190  }
191  }
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
Definition: kaldi-lattice.h:35
StateId GetStateForTuple(const Tuple &tuple, bool add_to_queue)
const PhoneAlignLatticeOptions & opts_
const TransitionModel & tmodel_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
fst::ArcTpl< CompactLatticeWeight > CompactLatticeArc
Definition: kaldi-lattice.h:42

◆ ProcessQueueElement()

void ProcessQueueElement ( )
inline

Definition at line 194 of file phone-align-lattice.cc.

References LatticePhoneAligner::ComputationState::Advance(), LatticePhoneAligner::Tuple::comp_state, LatticePhoneAligner::error_, LatticePhoneAligner::GetStateForTuple(), LatticePhoneAligner::Tuple::input_state, KALDI_ASSERT, LatticePhoneAligner::lat_, LatticePhoneAligner::lat_out_, CompactLatticeWeightTpl< WeightType, IntType >::One(), LatticePhoneAligner::opts_, LatticePhoneAligner::ProcessFinal(), LatticePhoneAligner::queue_, LatticePhoneAligner::tmodel_, and CompactLatticeWeightTpl< WeightType, IntType >::Zero().

Referenced by LatticePhoneAligner::AlignLattice().

194  {
195  KALDI_ASSERT(!queue_.empty());
196  Tuple tuple = queue_.back().first;
197  StateId output_state = queue_.back().second;
198  queue_.pop_back();
199 
200  // First thing is-- we see whether the computation-state has something
201  // pending that it wants to output. In this case we don't do
202  // anything further. This is a chosen behavior similar to the
203  // epsilon-sequencing rules encoded by the filters in
204  // composition.
205  CompactLatticeArc lat_arc;
206  if (tuple.comp_state.OutputPhoneArc(tmodel_, opts_, &lat_arc, &error_) ||
207  tuple.comp_state.OutputWordArc(tmodel_, opts_, &lat_arc, &error_)) {
208  // note: the functions OutputPhoneArc() and OutputWordArc() change the
209  // tuple (when they return true).
210  lat_arc.nextstate = GetStateForTuple(tuple, true); // true == add to
211  // queue, if not
212  // already present.
213  KALDI_ASSERT(output_state != lat_arc.nextstate);
214  lat_out_->AddArc(output_state, lat_arc);
215  } else {
216  // when there's nothing to output, we'll process arcs from the input-state.
217  // note: it would in a sense be valid to do both (i.e. process the stuff
218  // above, and also these), but this is a bit like the epsilon-sequencing
219  // stuff in composition: we avoid duplicate arcs by doing it this way.
220 
221  if (lat_.Final(tuple.input_state) != CompactLatticeWeight::Zero()) {
222  KALDI_ASSERT(lat_.Final(tuple.input_state) == CompactLatticeWeight::One());
223  // ... since we did CreateSuperFinal.
224  ProcessFinal(tuple, output_state);
225  }
226  // Now process the arcs. Note: final-states shouldn't have any arcs.
227  for(fst::ArcIterator<CompactLattice> aiter(lat_, tuple.input_state);
228  !aiter.Done(); aiter.Next()) {
229  const CompactLatticeArc &arc = aiter.Value();
230  Tuple next_tuple(tuple);
231  LatticeWeight weight;
232  next_tuple.comp_state.Advance(arc, opts_, &weight);
233  next_tuple.input_state = arc.nextstate;
234  StateId next_output_state = GetStateForTuple(next_tuple, true); // true == add to queue,
235  // if not already present.
236  // We add an epsilon arc here (as the input and output happens
237  // separately)... the epsilons will get removed later.
238  KALDI_ASSERT(next_output_state != output_state);
239  lat_out_->AddArc(output_state,
240  CompactLatticeArc(0, 0,
241  CompactLatticeWeight(weight, std::vector<int32>()),
242  next_output_state));
243  }
244  }
245  }
fst::CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
Definition: kaldi-lattice.h:35
StateId GetStateForTuple(const Tuple &tuple, bool add_to_queue)
CompactLatticeArc::StateId StateId
const PhoneAlignLatticeOptions & opts_
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
Definition: kaldi-lattice.h:32
static const CompactLatticeWeightTpl< WeightType, IntType > One()
void ProcessFinal(Tuple tuple, StateId output_state)
const TransitionModel & tmodel_
std::vector< std::pair< Tuple, StateId > > queue_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
static const CompactLatticeWeightTpl< WeightType, IntType > Zero()
fst::ArcTpl< CompactLatticeWeight > CompactLatticeArc
Definition: kaldi-lattice.h:42

◆ RemoveEpsilonsFromLattice()

void RemoveEpsilonsFromLattice ( )
inline

Definition at line 261 of file phone-align-lattice.cc.

References LatticePhoneAligner::lat_out_.

Referenced by LatticePhoneAligner::AlignLattice().

261  {
262  RmEpsilon(lat_out_, true); // true = connect.
263  }

Member Data Documentation

◆ error_

◆ lat_

◆ lat_out_

◆ map_

MapType map_

Definition at line 291 of file phone-align-lattice.cc.

Referenced by LatticePhoneAligner::GetStateForTuple().

◆ opts_

◆ queue_

◆ tmodel_


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