LatticePhoneAligner::ComputationState Class Reference
Collaboration diagram for LatticePhoneAligner::ComputationState:

Public Member Functions

void Advance (const CompactLatticeArc &arc, const PhoneAlignLatticeOptions &opts, LatticeWeight *weight)
 The state of the computation in which,. More...
 
bool OutputPhoneArc (const TransitionModel &tmodel, const PhoneAlignLatticeOptions &opts, CompactLatticeArc *arc_out, bool *error)
 If it can output a whole phone, it will do so, will put it in arc_out, and return true; else it will return false. More...
 
bool OutputWordArc (const TransitionModel &tmodel, const PhoneAlignLatticeOptions &opts, CompactLatticeArc *arc_out, bool *error)
 This will succeed (and output the arc) if we have >1 word in words_; the arc won't have any transition-ids on it. More...
 
bool IsEmpty ()
 
LatticeWeight FinalWeight ()
 FinalWeight() will return "weight" if both transition_ids and word_labels are empty, otherwise it will return Weight::Zero(). More...
 
void OutputArcForce (const TransitionModel &tmodel, const PhoneAlignLatticeOptions &opts, CompactLatticeArc *arc_out, bool *error)
 This function may be called when you reach the end of the lattice and this structure hasn't voluntarily output words using "OutputArc". More...
 
size_t Hash () const
 
bool operator== (const ComputationState &other) const
 
 ComputationState ()
 
 ComputationState (const ComputationState &other)
 

Private Attributes

std::vector< int32transition_ids_
 
std::vector< int32word_labels_
 
LatticeWeight weight_
 

Detailed Description

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

Constructor & Destructor Documentation

◆ ComputationState() [1/2]

ComputationState ( )
inline

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

116 : weight_(LatticeWeight::One()) { } // initial state.
static const LatticeWeightTpl One()

◆ ComputationState() [2/2]

ComputationState ( const ComputationState other)
inline

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

117  :
118  transition_ids_(other.transition_ids_), word_labels_(other.word_labels_),
119  weight_(other.weight_) { }

Member Function Documentation

◆ Advance()

void Advance ( const CompactLatticeArc arc,
const PhoneAlignLatticeOptions opts,
LatticeWeight weight 
)
inline

The state of the computation in which,.

along a single path in the lattice, we work out the phone boundaries and output phone-aligned arcs. [These may or may not have words on them; the word symbols are not aligned with anything. Advance the computation state by adding the symbols and weights from this arc. Gets rid of the weight and puts it in "weight" which will be put on the output arc; this keeps the state-space small.

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

References LatticeWeightTpl< BaseFloat >::One(), LatticePhoneAligner::ComputationState::OutputPhoneArc(), LatticePhoneAligner::ComputationState::OutputWordArc(), PhoneAlignLatticeOptions::replace_output_symbols, fst::Times(), LatticePhoneAligner::ComputationState::transition_ids_, LatticePhoneAligner::ComputationState::weight_, and LatticePhoneAligner::ComputationState::word_labels_.

Referenced by LatticePhoneAligner::ProcessQueueElement().

43  {
44  const std::vector<int32> &string = arc.weight.String();
45  transition_ids_.insert(transition_ids_.end(),
46  string.begin(), string.end());
47  if (arc.ilabel != 0 && !opts.replace_output_symbols) // note: arc.ilabel==arc.olabel (acceptor)
48  word_labels_.push_back(arc.ilabel);
49  *weight = Times(weight_, arc.weight.Weight());
51  }
static const LatticeWeightTpl One()
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)

◆ FinalWeight()

◆ Hash()

size_t Hash ( ) const
inline

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

References LatticePhoneAligner::ComputationState::transition_ids_, and LatticePhoneAligner::ComputationState::word_labels_.

Referenced by LatticePhoneAligner::TupleHash::operator()().

99  {
100  VectorHasher<int32> vh;
101  return vh(transition_ids_) + 90647 * vh(word_labels_);
102  // 90647 is an arbitrary largish prime number.
103  // We don't bother including the weight in the hash--
104  // we don't really expect duplicates with the same vectors
105  // but different weights, and anyway, this is only an
106  // efficiency issue.
107  }

◆ IsEmpty()

◆ operator==()

bool operator== ( const ComputationState other) const
inline

◆ OutputArcForce()

void OutputArcForce ( const TransitionModel tmodel,
const PhoneAlignLatticeOptions opts,
CompactLatticeArc arc_out,
bool error 
)

This function may be called when you reach the end of the lattice and this structure hasn't voluntarily output words using "OutputArc".

If IsEmpty() == false, then you can call this function and it will output an arc. The only non-error state in which this happens, is when a word (or silence) has ended, but we don't know that it's ended because we haven't seen the first transition-id from the next word. Otherwise (error state), the output will consist of partial words, and this will only happen for lattices that were somehow broken, i.e. had not reached the final state.

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

References rnnlm::i, LatticePhoneAligner::ComputationState::IsEmpty(), TransitionModel::IsFinal(), KALDI_ASSERT, KALDI_WARN, LatticeWeightTpl< BaseFloat >::One(), PhoneAlignLatticeOptions::replace_output_symbols, LatticePhoneAligner::ComputationState::transition_ids_, TransitionModel::TransitionIdToPhone(), LatticePhoneAligner::ComputationState::weight_, and LatticePhoneAligner::ComputationState::word_labels_.

Referenced by LatticePhoneAligner::ComputationState::FinalWeight(), and LatticePhoneAligner::ProcessFinal().

368  {
369  KALDI_ASSERT(!IsEmpty());
370 
371  int32 phone = -1; // This value -1 will never be used,
372  // although it might not be obvious from superficially checking
373  // the code. IsEmpty() would be true if we had transition_ids_.empty()
374  // and opts.replace_output_symbols, so we would already die by assertion;
375  // in fact, this function would never be called.
376 
377  if (!transition_ids_.empty()) { // Do some checking here.
378  int32 tid = transition_ids_[0];
379  phone = tmodel.TransitionIdToPhone(tid);
380  int32 num_final = 0;
381  for (int32 i = 0; i < transition_ids_.size(); i++) { // A check.
382  int32 this_tid = transition_ids_[i];
383  int32 this_phone = tmodel.TransitionIdToPhone(this_tid);
384  bool is_final = tmodel.IsFinal(this_tid); // should be exactly one.
385  if (is_final) num_final++;
386  if (this_phone != phone && ! *error) {
387  KALDI_WARN << "Mismatch in phone: error in lattice or mismatched transition model?";
388  *error = true;
389  }
390  }
391  if (num_final != 1 && ! *error) {
392  KALDI_WARN << "Problem phone-aligning lattice: saw " << num_final
393  << " final-states in last phone in lattice (forced out?) "
394  << "Producing partial lattice.";
395  *error = true;
396  }
397  }
398 
399  Label output_label = 0;
400  if (!word_labels_.empty()) {
401  output_label = word_labels_[0];
402  word_labels_.erase(word_labels_.begin(), word_labels_.begin()+1);
403  }
404  if (opts.replace_output_symbols)
405  output_label = phone;
406  *arc_out = CompactLatticeArc(output_label, output_label,
408  fst::kNoStateId);
409  transition_ids_.clear();
410  weight_ = LatticeWeight::One(); // we just output the weight.
411 }
static const LatticeWeightTpl One()
fst::CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
Definition: kaldi-lattice.h:35
kaldi::int32 int32
#define KALDI_WARN
Definition: kaldi-error.h:150
CompactLatticeArc::Label Label
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
fst::ArcTpl< CompactLatticeWeight > CompactLatticeArc
Definition: kaldi-lattice.h:42

◆ OutputPhoneArc()

bool OutputPhoneArc ( const TransitionModel tmodel,
const PhoneAlignLatticeOptions opts,
CompactLatticeArc arc_out,
bool error 
)

If it can output a whole phone, it will do so, will put it in arc_out, and return true; else it will return false.

If it detects an error condition and *error = false, it will set *error to true and print a warning. In this case it will still output phone arcs, they will just be inaccurate. Of course once *error is set, something has gone wrong so don't trust the output too fully. Note: the "next_state" of the arc will not be set, you have to do that yourself.

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

References rnnlm::i, TransitionModel::IsFinal(), TransitionModel::IsSelfLoop(), KALDI_WARN, LatticeWeightTpl< BaseFloat >::One(), PhoneAlignLatticeOptions::reorder, PhoneAlignLatticeOptions::replace_output_symbols, LatticePhoneAligner::ComputationState::transition_ids_, TransitionModel::TransitionIdToPhone(), LatticePhoneAligner::ComputationState::weight_, and LatticePhoneAligner::ComputationState::word_labels_.

Referenced by LatticePhoneAligner::ComputationState::Advance().

299  {
300  if (transition_ids_.empty()) return false;
301  int32 phone = tmodel.TransitionIdToPhone(transition_ids_[0]);
302  // we assume the start of transition_ids_ is the start of the phone;
303  // this is a precondition.
304  size_t len = transition_ids_.size(), i;
305  // Keep going till we reach a "final" transition-id; note, if
306  // reorder==true, we have to go a bit further after this.
307  for (i = 0; i < len; i++) {
308  int32 tid = transition_ids_[i];
309  int32 this_phone = tmodel.TransitionIdToPhone(tid);
310  if (this_phone != phone && ! *error) { // error condition: should have
311  // reached final transition-id first.
312  *error = true;
313  KALDI_WARN << phone << " -> " << this_phone;
314  KALDI_WARN << "Phone changed before final transition-id found "
315  "[broken lattice or mismatched model or wrong --reorder option?]";
316  }
317  if (tmodel.IsFinal(tid))
318  break;
319  }
320  if (i == len) return false; // fell off loop.
321  i++; // go past the one for which IsFinal returned true.
322  if (opts.reorder) // we have to consume the following self-loop transition-ids.
323  while (i < len && tmodel.IsSelfLoop(transition_ids_[i])) i++;
324  if (i == len) return false; // we don't know if it ends here... so can't output arc.
325 
326  // interpret i as the number of transition-ids to consume.
327  std::vector<int32> tids_out(transition_ids_.begin(),
328  transition_ids_.begin()+i);
329 
330  Label output_label = 0;
331  if (!word_labels_.empty()) {
332  output_label = word_labels_[0];
333  word_labels_.erase(word_labels_.begin(), word_labels_.begin()+1);
334  }
335  if (opts.replace_output_symbols)
336  output_label = phone;
337  *arc_out = CompactLatticeArc(output_label, output_label,
338  CompactLatticeWeight(weight_, tids_out),
339  fst::kNoStateId);
340  transition_ids_.erase(transition_ids_.begin(), transition_ids_.begin()+i);
341  weight_ = LatticeWeight::One(); // we just output the weight.
342  return true;
343 }
static const LatticeWeightTpl One()
fst::CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
Definition: kaldi-lattice.h:35
kaldi::int32 int32
#define KALDI_WARN
Definition: kaldi-error.h:150
CompactLatticeArc::Label Label
fst::ArcTpl< CompactLatticeWeight > CompactLatticeArc
Definition: kaldi-lattice.h:42

◆ OutputWordArc()

bool OutputWordArc ( const TransitionModel tmodel,
const PhoneAlignLatticeOptions opts,
CompactLatticeArc arc_out,
bool error 
)

This will succeed (and output the arc) if we have >1 word in words_; the arc won't have any transition-ids on it.

This is intended to fix a particular pathology where too many words were pending and we had blowup.

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

References LatticeWeightTpl< BaseFloat >::One(), LatticePhoneAligner::ComputationState::weight_, and LatticePhoneAligner::ComputationState::word_labels_.

Referenced by LatticePhoneAligner::ComputationState::Advance().

349  {
350  // output a word but no phones.
351  if (word_labels_.size() < 2) return false;
352 
353  int32 output_label = word_labels_[0];
354  word_labels_.erase(word_labels_.begin(), word_labels_.begin()+1);
355 
356  *arc_out = CompactLatticeArc(output_label, output_label,
357  CompactLatticeWeight(weight_, std::vector<int32>()),
358  fst::kNoStateId);
359  weight_ = LatticeWeight::One(); // we just output the weight, so set it to one.
360  return true;
361 }
static const LatticeWeightTpl One()
fst::CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
Definition: kaldi-lattice.h:35
kaldi::int32 int32
fst::ArcTpl< CompactLatticeWeight > CompactLatticeArc
Definition: kaldi-lattice.h:42

Member Data Documentation

◆ transition_ids_

◆ weight_

◆ word_labels_


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