word-align-lattice.cc 38.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
// lat/word-align-lattice.cc

// Copyright 2011-2012  Microsoft Corporation  Johns Hopkins University (Author: Daniel Povey)

// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.


#include "lat/word-align-lattice.h"
#include "hmm/transition-model.h"
#include "util/stl-utils.h"

namespace kaldi {

class LatticeWordAligner {
 public:
  typedef CompactLatticeArc::StateId StateId;
  typedef CompactLatticeArc::Label Label;

  class ComputationState { /// The state of the computation in which,
    /// along a single path in the lattice, we work out the word
    /// boundaries and output aligned arcs.
   public:

    /// Advance the computation state by adding the symbols and weights
    /// from this arc.  We'll put the weight on the output arc; this helps
    /// keep the state-space smaller.
    void Advance(const CompactLatticeArc &arc, LatticeWeight *weight) {
      const std::vector<int32> &string = arc.weight.String();
      transition_ids_.insert(transition_ids_.end(),
                             string.begin(), string.end());
      if (arc.ilabel != 0) // note: arc.ilabel==arc.olabel (acceptor)
        word_labels_.push_back(arc.ilabel);
      *weight = Times(weight_, arc.weight.Weight());
      weight_ = LatticeWeight::One();
    }

    /// If it can output a whole word, 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 may or may not [output an arc and return true],
    /// depending on what we think is most likely the right thing to do.  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.
    bool OutputArc(const WordBoundaryInfo &info,
                   const TransitionModel &tmodel,
                   CompactLatticeArc *arc_out,
                   bool *error) {
      // order of this ||-expression doesn't matter for
      // function behavior, only for efficiency, since the
      // cases are disjoint.
      return OutputNormalWordArc(info, tmodel, arc_out, error) ||
          OutputSilenceArc(info, tmodel, arc_out, error) ||
          OutputOnePhoneWordArc(info, tmodel, arc_out, error);
    }

    bool OutputSilenceArc(const WordBoundaryInfo &info,
                          const TransitionModel &tmodel,
                          CompactLatticeArc *arc_out,
                          bool *error);
    bool OutputOnePhoneWordArc(const WordBoundaryInfo &info,
                               const TransitionModel &tmodel,
                               CompactLatticeArc *arc_out,
                               bool *error);
    bool OutputNormalWordArc(const WordBoundaryInfo &info,
                             const TransitionModel &tmodel,
                             CompactLatticeArc *arc_out,
                             bool *error);

    bool IsEmpty() { return (transition_ids_.empty() && word_labels_.empty()); }

    /// FinalWeight() will return "weight" if both transition_ids
    /// and word_labels are empty, otherwise it will return
    /// Weight::Zero().
    LatticeWeight FinalWeight() { return (IsEmpty() ? weight_ : LatticeWeight::Zero()); }

    /// 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.
    void OutputArcForce(const WordBoundaryInfo &info,
                        const TransitionModel &tmodel,
                        CompactLatticeArc *arc_out,
                        bool *error);

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

    // Just need an arbitrary complete order.
    bool operator == (const ComputationState &other) const {
      return (transition_ids_ == other.transition_ids_
              && word_labels_ == other.word_labels_
              && weight_ == other.weight_);
    }

    ComputationState(): weight_(LatticeWeight::One()) { } // initial state.
    ComputationState(const ComputationState &other):
        transition_ids_(other.transition_ids_), word_labels_(other.word_labels_),
        weight_(other.weight_) { }
   private:
    std::vector<int32> transition_ids_;
    std::vector<int32> word_labels_;
    LatticeWeight weight_; // contains two floats.
  };


  struct Tuple {
    Tuple(StateId input_state, ComputationState comp_state):
        input_state(input_state), comp_state(comp_state) {}
    StateId input_state;
    ComputationState comp_state;
  };

  struct TupleHash {
    size_t operator() (const Tuple &state) const {
      return state.input_state + 102763 * state.comp_state.Hash();
      // 102763 is just an arbitrary prime number
    }
  };
  struct TupleEqual {
    bool operator () (const Tuple &state1, const Tuple &state2) const {
      // treat this like operator ==
      return (state1.input_state == state2.input_state
              && state1.comp_state == state2.comp_state);
    }
  };

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

  StateId GetStateForTuple(const Tuple &tuple, bool add_to_queue) {
    MapType::iterator iter = map_.find(tuple);
    if (iter == map_.end()) { // not in map.
      StateId output_state = lat_out_->AddState();
      map_[tuple] = output_state;
      if (add_to_queue)
        queue_.push_back(std::make_pair(tuple, output_state));
      return output_state;
    } else {
      return iter->second;
    }
  }

  void ProcessFinal(Tuple tuple, StateId output_state) {
    // ProcessFinal is only called if the input_state has
    // final-prob of One().  [else it should be zero.  This
    // is because we called CreateSuperFinal().]

    if (tuple.comp_state.IsEmpty()) { // computation state doesn't have
      // anything pending.
      std::vector<int32> empty_vec;
      CompactLatticeWeight cw(tuple.comp_state.FinalWeight(), empty_vec);
      lat_out_->SetFinal(output_state, Plus(lat_out_->Final(output_state), cw));
    } else {
      // computation state has something pending, i.e. input or
      // output symbols that need to be flushed out.  Note: OutputArc() would
      // have returned false or we wouldn't have been called, so we have to
      // force it out.
      CompactLatticeArc lat_arc;
      tuple.comp_state.OutputArcForce(info_, tmodel_, &lat_arc, &error_);
      // True in the next line means add it to the queue.
      lat_arc.nextstate = GetStateForTuple(tuple, true);
      // The final-prob stuff will get called again from ProcessQueueElement().
      // Note: because we did CreateSuperFinal(), this final-state on the input
      // lattice will have no output arcs (and unit final-prob), so there will be
      // no complications with processing the arcs from this state (there won't
      // be any).
      KALDI_ASSERT(output_state != lat_arc.nextstate);
      lat_out_->AddArc(output_state, lat_arc);
    }
  }


  void ProcessQueueElement() {
    KALDI_ASSERT(!queue_.empty());
    Tuple tuple = queue_.back().first;
    StateId output_state = queue_.back().second;
    queue_.pop_back();

    // First thing is-- we see whether the computation-state has something
    // pending that it wants to output.  In this case we don't do
    // anything further.  This is a chosen behavior similar to the
    // epsilon-sequencing rules encoded by the filters in
    // composition.
    CompactLatticeArc lat_arc;
    if (tuple.comp_state.OutputArc(info_, tmodel_, &lat_arc, &error_)) {
      // note: this function changes the tuple (when it returns true).
      lat_arc.nextstate = GetStateForTuple(tuple, true); // true == add to queue,
      // if not already present.
      KALDI_ASSERT(output_state != lat_arc.nextstate);
      lat_out_->AddArc(output_state, lat_arc);
    } else {
      // when there's nothing to output, we'll process arcs from the input-state.
      // note: it would in a sense be valid to do both (i.e. process the stuff
      // above, and also these), but this is a bit like the epsilon-sequencing
      // stuff in composition: we avoid duplicate arcs by doing it this way.

      if (lat_.Final(tuple.input_state) != CompactLatticeWeight::Zero()) {
        KALDI_ASSERT(lat_.Final(tuple.input_state) == CompactLatticeWeight::One());
        // ... since we did CreateSuperFinal.
        ProcessFinal(tuple, output_state);
      }
      // Now process the arcs.  Note: final-state shouldn't have any arcs.
      for (fst::ArcIterator<CompactLattice> aiter(lat_, tuple.input_state);
          !aiter.Done(); aiter.Next()) {
        const CompactLatticeArc &arc = aiter.Value();
        Tuple next_tuple(tuple);
        LatticeWeight weight;
        next_tuple.comp_state.Advance(arc, &weight);
        next_tuple.input_state = arc.nextstate;
        StateId next_output_state = GetStateForTuple(next_tuple, true); // true == add to queue,
        // if not already present.
        // We add an epsilon arc here (as the input and output happens
        // separately)... the epsilons will get removed later.
        KALDI_ASSERT(next_output_state != output_state);
        lat_out_->AddArc(output_state,
                         CompactLatticeArc(0, 0,
                             CompactLatticeWeight(weight, std::vector<int32>()),
                             next_output_state));
      }
    }
  }

  LatticeWordAligner(const CompactLattice &lat,
                     const TransitionModel &tmodel,
                     const WordBoundaryInfo &info,
                     int32 max_states,
                     CompactLattice *lat_out):
      lat_(lat), tmodel_(tmodel), info_in_(info), info_(info),
      max_states_(max_states), lat_out_(lat_out),
      error_(false) {
    bool test = true;
    uint64 props = lat_.Properties(fst::kIDeterministic|fst::kIEpsilons, test);
    if (props != fst::kIDeterministic) {
      KALDI_WARN << "[Lattice has input epsilons and/or is not input-deterministic "
                 << "(in Mohri sense)]-- i.e. lattice is not deterministic.  "
                 << "Word-alignment may be slow and-or blow up in memory.";
    }
    fst::CreateSuperFinal(&lat_); // Creates a super-final state, so the
    // only final-probs are One().

    // Inside this class, we don't want to use zero for the silence
    // or partial-word labels, as this will interfere with the RmEpsilon
    // stage, where we don't want the arcs corresponding to silence or
    // partial words to be removed-- only the arcs with nothing at all
    // on them.
    if (info_.partial_word_label == 0 || info_.silence_label == 0) {
      int32 unused_label = 1 + HighestNumberedOutputSymbol(lat);
      if (info_.partial_word_label >= unused_label)
        unused_label = info_.partial_word_label + 1;
      if (info_.silence_label >= unused_label)
        unused_label = info_.silence_label + 1;
      KALDI_ASSERT(unused_label > 0);
      if (info_.partial_word_label == 0)
        info_.partial_word_label = unused_label++;
      if (info_.silence_label == 0)
        info_.silence_label = unused_label;
    }
  }

  // Removes epsilons; also removes unreachable states...
  // not sure if these would exist if original was connected.
  // This also replaces the temporary symbols for the silence
  // and partial-words, with epsilons, if we wanted epsilons.
  void RemoveEpsilonsFromLattice() {
    // Remove epsilon arcs from output lattice.
    RmEpsilon(lat_out_, true); // true = connect.
    std::vector<int32> syms_to_remove;
    if (info_in_.partial_word_label == 0)
      syms_to_remove.push_back(info_.partial_word_label);
    if (info_in_.silence_label == 0)
      syms_to_remove.push_back(info_.silence_label);
    if (!syms_to_remove.empty()) {
      RemoveSomeInputSymbols(syms_to_remove, lat_out_);
      Project(lat_out_, fst::PROJECT_INPUT);
    }
  }

  bool AlignLattice() {
    lat_out_->DeleteStates();
    if (lat_.Start() == fst::kNoStateId) {
      KALDI_WARN << "Trying to word-align empty lattice.";
      return false;
    }
    ComputationState initial_comp_state;
    Tuple initial_tuple(lat_.Start(), initial_comp_state);
    StateId start_state = GetStateForTuple(initial_tuple, true); // True = add this to queue.
    lat_out_->SetStart(start_state);

    while (!queue_.empty()) {
      if (max_states_ > 0 && lat_out_->NumStates() > max_states_) {
        KALDI_WARN << "Number of states in lattice exceeded max-states of "
                   << max_states_ << ", original lattice had "
                   << lat_.NumStates() << " states.  Returning what we have.";
        RemoveEpsilonsFromLattice();
        return false;
      }
      ProcessQueueElement();
    }

    RemoveEpsilonsFromLattice();

    return !error_;
  }

  CompactLattice lat_;
  const TransitionModel &tmodel_;
  const WordBoundaryInfo &info_in_;
  WordBoundaryInfo info_;
  int32 max_states_;
  CompactLattice *lat_out_;

  std::vector<std::pair<Tuple, StateId> > queue_;



  MapType map_; // map from tuples to StateId.
  bool error_;

};

bool LatticeWordAligner::ComputationState::OutputSilenceArc(
    const WordBoundaryInfo &info, const TransitionModel &tmodel,
    CompactLatticeArc *arc_out,  bool *error) {
  if (transition_ids_.empty()) return false;
  int32 phone = tmodel.TransitionIdToPhone(transition_ids_[0]);
  if (info.TypeOfPhone(phone) != WordBoundaryInfo::kNonWordPhone) return false;

  // we assume the start of transition_ids_ is the start of the phone [silence];
  // this is a precondition.
  size_t len = transition_ids_.size(), i;
  // Keep going till we reach a "final" transition-id; note, if
  // reorder==true, we have to go a bit further after this.
  for (i = 0; i < len; i++) {
    int32 tid = transition_ids_[i];
    int32 this_phone = tmodel.TransitionIdToPhone(tid);
    if (this_phone != phone && ! *error) { // error condition: should have reached final transition-id first.
      *error = true;
      KALDI_WARN << "Phone changed before final transition-id found "
          "[broken lattice or mismatched model or wrong --reorder option?]";
    }
    if (tmodel.IsFinal(tid))
      break;
  }
  if (i == len) return false; // fell off loop.
  i++; // go past the one for which IsFinal returned true.
  if (info.reorder) // we have to consume the following self-loop transition-ids.
    while (i < len && tmodel.IsSelfLoop(transition_ids_[i])) i++;
  if (i == len) return false; // we don't know if it ends here... so can't output arc.

  if (tmodel.TransitionIdToPhone(transition_ids_[i-1]) != phone
      && ! *error) { // another check.
    KALDI_WARN << "Phone changed unexpectedly in lattice "
        "[broken lattice or mismatched model?]";
  }
  // interpret i as the number of transition-ids to consume.
  std::vector<int32> tids_out(transition_ids_.begin(), transition_ids_.begin()+i);

  // consumed transition ids from our internal state.
  *arc_out = CompactLatticeArc(info.silence_label, info.silence_label,
                               CompactLatticeWeight(weight_, tids_out), fst::kNoStateId);
  transition_ids_.erase(transition_ids_.begin(), transition_ids_.begin()+i); // delete these
  weight_ = LatticeWeight::One(); // we just output the weight.
  return true;
}


bool LatticeWordAligner::ComputationState::OutputOnePhoneWordArc(
    const WordBoundaryInfo &info, const TransitionModel &tmodel,
    CompactLatticeArc *arc_out,  bool *error) {
  if (transition_ids_.empty()) return false;
  if (word_labels_.empty()) return false;
  int32 phone = tmodel.TransitionIdToPhone(transition_ids_[0]);
  if (info.TypeOfPhone(phone) != WordBoundaryInfo::kWordBeginAndEndPhone)
    return false;
  // we assume the start of transition_ids_ is the start of the phone.
  // this is a precondition.
  size_t len = transition_ids_.size(), i;
  for (i = 0; i < len; i++) {
    int32 tid = transition_ids_[i];
    int32 this_phone = tmodel.TransitionIdToPhone(tid);
    if (this_phone != phone && ! *error) { // error condition: should have reached final transition-id first.
      KALDI_WARN << "Phone changed before final transition-id found "
          "[broken lattice or mismatched model or wrong --reorder option?]";
      // just continue, ignoring this-- we'll probably output something...
    }
    if (tmodel.IsFinal(tid))
      break;
  }
  if (i == len) return false; // fell off loop.
  i++; // go past the one for which IsFinal returned true.
  if (info.reorder) // we have to consume the following self-loop transition-ids.
    while (i < len && tmodel.IsSelfLoop(transition_ids_[i])) i++;
  if (i == len) return false; // we don't know if it ends here... so can't output arc.

  if (tmodel.TransitionIdToPhone(transition_ids_[i-1]) != phone
      && ! *error) { // another check.
    KALDI_WARN << "Phone changed unexpectedly in lattice "
        "[broken lattice or mismatched model?]";
    *error = true;
  }

  // interpret i as the number of transition-ids to consume.
  std::vector<int32> tids_out(transition_ids_.begin(),
                              transition_ids_.begin() + i);

  // consumed transition ids from our internal state.
  int32 word = word_labels_[0];
  *arc_out = CompactLatticeArc(word, word,
                               CompactLatticeWeight(weight_, tids_out), fst::kNoStateId);
  transition_ids_.erase(transition_ids_.begin(),
                        transition_ids_.begin() + i); // delete these
  // Remove the word that we just output.
  word_labels_.erase(word_labels_.begin(), word_labels_.begin() + 1);
  weight_ = LatticeWeight::One(); // we just output the weight.
  return true;
}


/// This function tries to see if it can output a normal word arc--
/// one with at least two phones in it.
bool LatticeWordAligner::ComputationState::OutputNormalWordArc(
    const WordBoundaryInfo &info, const TransitionModel &tmodel,
    CompactLatticeArc *arc_out,  bool *error) {
  if (transition_ids_.empty()) return false;
  if (word_labels_.empty()) return false;
  int32 begin_phone = tmodel.TransitionIdToPhone(transition_ids_[0]);
  if (info.TypeOfPhone(begin_phone) != WordBoundaryInfo::kWordBeginPhone)
    return false;
  // we assume the start of transition_ids_ is the start of the phone.
  // this is a precondition.
  size_t len = transition_ids_.size(), i;

  // Eat up the transition-ids of this word-begin phone until we get to the
  // "final" transition-id.  [there may be self-loops following this though,
  // if reorder==true]
  for (i = 0; i < len && !tmodel.IsFinal(transition_ids_[i]); i++);
  if (i == len) return false;
  i++; // Skip over this final-transition.
  if (info.reorder) // Skip over any reordered self-loops for this final-transition
    for (; i < len && tmodel.IsSelfLoop(transition_ids_[i]); i++);
  if (i == len) return false;
  if (tmodel.TransitionIdToPhone(transition_ids_[i-1]) != begin_phone
      && ! *error) { // another check.
    KALDI_WARN << "Phone changed unexpectedly in lattice "
        "[broken lattice or mismatched model?]";
    *error = true;
  }
  // Now keep going till we hit a word-ending phone.
  // Note: we don't expect anything except word-internal phones
  // here, but we'll just print a warning if we get something
  // else.
  for (; i < len; i++) {
    int32 this_phone = tmodel.TransitionIdToPhone(transition_ids_[i]);
    if (info.TypeOfPhone(this_phone) == WordBoundaryInfo::kWordEndPhone)
      break;
    if (info.TypeOfPhone(this_phone) != WordBoundaryInfo::kWordInternalPhone
        && !*error) {
      KALDI_WARN << "Unexpected phone " << this_phone
                 << " found inside a word.";
      *error = true;
    }
  }
  if (i == len) return false;

  // OK, we hit a word-ending phone.  Continue till we get to
  // a "final-transition".

  // this variable just used for checks.
  int32 final_phone = tmodel.TransitionIdToPhone(transition_ids_[i]);
  for (; i < len; i++) {
    int32 this_phone = tmodel.TransitionIdToPhone(transition_ids_[i]);
    if (this_phone != final_phone && ! *error) {
      *error = true;
      KALDI_WARN << "Phone changed before final transition-id found "
          "[broken lattice or mismatched model or wrong --reorder option?]";
    }
    if (tmodel.IsFinal(transition_ids_[i])) break;
  }
  if (i == len) return false;
  i++;
  // We got to the final-transition of the final phone;
  // if reorder==true, continue eating up the self-loop.
  if (info.reorder == true)
    while (i < len && tmodel.IsSelfLoop(transition_ids_[i])) i++;
  if (i == len) return false;
  if (tmodel.TransitionIdToPhone(transition_ids_[i-1]) != final_phone
      && ! *error) {
    *error = true;
    KALDI_WARN << "Phone changed while following final self-loop "
        "[broken lattice or mismatched model or wrong --reorder option?]";
  }

  // OK, we're ready to output the word.
  // Interpret i as the number of transition-ids to consume.
  std::vector<int32> tids_out(transition_ids_.begin(),
                              transition_ids_.begin() + i);

  // consumed transition ids from our internal state.
  int32 word = word_labels_[0];
  *arc_out = CompactLatticeArc(word, word,
                               CompactLatticeWeight(weight_, tids_out),
                               fst::kNoStateId);
  transition_ids_.erase(transition_ids_.begin(),
                        transition_ids_.begin() + i); // delete these
  // Remove the word that we just output.
  word_labels_.erase(word_labels_.begin(),
                     word_labels_.begin() + 1);
  weight_ = LatticeWeight::One(); // we just output the weight.
  return true;
}

// Returns true if this vector of transition-ids could be a valid
// word.  Note: the checks are not 100% exhaustive.
static bool IsPlausibleWord(const WordBoundaryInfo &info,
                            const TransitionModel &tmodel,
                            const std::vector<int32> &transition_ids) {
  if (transition_ids.empty()) return false;
  int32 first_phone = tmodel.TransitionIdToPhone(transition_ids.front()),
      last_phone = tmodel.TransitionIdToPhone(transition_ids.back());
  if ( (info.TypeOfPhone(first_phone) == WordBoundaryInfo::kWordBeginAndEndPhone
        && first_phone == last_phone)
       ||
       (info.TypeOfPhone(first_phone) == WordBoundaryInfo::kWordBeginPhone &&
        info.TypeOfPhone(last_phone) == WordBoundaryInfo::kWordEndPhone) ) {
    if (! info.reorder) {
      return (tmodel.IsFinal(transition_ids.back()));
    } else {
      int32 i = transition_ids.size() - 1;
      while (i > 0 && tmodel.IsSelfLoop(transition_ids[i])) i--;
      return tmodel.IsFinal(transition_ids[i]);
    }
  } else return false;
}


void LatticeWordAligner::ComputationState::OutputArcForce(
    const WordBoundaryInfo &info, const TransitionModel &tmodel,
    CompactLatticeArc *arc_out,  bool *error) {

  KALDI_ASSERT(!IsEmpty());
  if (!word_labels_.empty()
      && !transition_ids_.empty()) { // We have at least one word to
    // output, and some transition-ids.  We assume that the normal OutputArc was called
    // and failed, so this means we didn't see the end of that
    // word.
    int32 word = word_labels_[0];
    if (! *error && !IsPlausibleWord(info, tmodel, transition_ids_)) {
      *error = true;
      KALDI_WARN << "Invalid word at end of lattice [partial lattice, forced out?]";
    }
    CompactLatticeWeight cw(weight_, transition_ids_);
    *arc_out = CompactLatticeArc(word, word, cw, fst::kNoStateId);
    weight_ = LatticeWeight::One();
    transition_ids_.clear();
    word_labels_.erase(word_labels_.begin(), word_labels_.begin()+1);
  } else if (!word_labels_.empty() && transition_ids_.empty()) {
    // We won't create arcs with these word labels on, as most likely
    // this will cause errors down the road.  This is an error
    // condition anyway, in some sense.
    if (! *error) {
      *error = true;
      KALDI_WARN << "Discarding word-ids at the end of a sentence, "
          "that don't have alignments.";
    }
    CompactLatticeWeight cw(weight_, transition_ids_);
    // This creates an epsilon arc with a weight on it, but
    // no transition-ids since the vector is empty.
    // The word labels are discarded.
    *arc_out = CompactLatticeArc(0, 0, cw, fst::kNoStateId);
    weight_ = LatticeWeight::One();
    word_labels_.clear();
  } else if (!transition_ids_.empty() && word_labels_.empty()) {
    // Transition-ids but no word label-- either silence or partial word.
    int32 first_phone = tmodel.TransitionIdToPhone(transition_ids_[0]);
    if (info.TypeOfPhone(first_phone) == WordBoundaryInfo::kNonWordPhone) {
      // first phone is silence...
      if (first_phone != tmodel.TransitionIdToPhone(transition_ids_.back())
          && ! *error) {
        *error = true;
        // Phone changed-- this is a code error, because the regular OutputArc
        // should have output an arc (a silence arc) if that phone finished.
        // So we make it fatal.
        KALDI_ERR << "Broken silence arc at end of utterance (the phone "
            "changed); code error";
      }
      if (!*error) { // Check that it ends at the end state of silence; error otherwise.
        int32 i = transition_ids_.size() - 1;
        if (info.reorder)
          while (tmodel.IsSelfLoop(transition_ids_[i]) && i > 0)
            i--;
        if (!tmodel.IsFinal(transition_ids_[i])) {
          *error = true;
          KALDI_WARN << "Broken silence arc at end of utterance (does not "
              "reach end of silence)";
        }
      }
      CompactLatticeWeight cw(weight_, transition_ids_);
      *arc_out = CompactLatticeArc(info.silence_label, info.silence_label,
                                   cw, fst::kNoStateId);
    } else {
      // Not silence phone -- treat as partial word (with no word label).
      // This is in itself an error condition, i.e. the lattice was maybe
      // forced out.
      if (! *error) {
        *error = true;
        KALDI_WARN << "Partial word detected at end of utterance";
      }
      CompactLatticeWeight cw(weight_, transition_ids_);
      *arc_out = CompactLatticeArc(info.partial_word_label, info.partial_word_label,
                                   cw, fst::kNoStateId);
    }
    transition_ids_.clear();
    weight_ = LatticeWeight::One();
  } else {
    KALDI_ERR << "Code error, word-aligning lattice"; // this shouldn't
    // be able to happen; we don't call this function of they're both empty.
  }
}

// This code will eventually be removed.
void WordBoundaryInfo::SetOptions(const std::string int_list, PhoneType phone_type) {
  KALDI_ASSERT(!int_list.empty() && phone_type != kNoPhone);
  std::vector<int32> phone_list;
  if (!kaldi::SplitStringToIntegers(int_list, ":",
                                    false,
                                    &phone_list)
      || phone_list.empty())
    KALDI_ERR << "Invalid argument to --*-phones option: " << int_list;
  for (size_t i= 0; i < phone_list.size(); i++) {
    if (phone_to_type.size() <= phone_list[i])
      phone_to_type.resize(phone_list[i]+1, kNoPhone);
    if (phone_to_type[phone_list[i]] != kNoPhone)
      KALDI_ERR << "Phone " << phone_list[i] << "was given two incompatible "
          "assignments.";
    phone_to_type[phone_list[i]] = phone_type;
  }
}

// This initializer will be deleted eventually.
WordBoundaryInfo::WordBoundaryInfo(const WordBoundaryInfoOpts &opts) {
  SetOptions(opts.wbegin_phones, kWordBeginPhone);
  SetOptions(opts.wend_phones, kWordEndPhone);
  SetOptions(opts.wbegin_and_end_phones, kWordBeginAndEndPhone);
  SetOptions(opts.winternal_phones, kWordInternalPhone);
  SetOptions(opts.silence_phones, (opts.silence_has_olabels ?
                                   kWordBeginAndEndPhone : kNonWordPhone));
  reorder = opts.reorder;
  silence_label = opts.silence_label;
  partial_word_label = opts.partial_word_label;
}

WordBoundaryInfo::WordBoundaryInfo(const WordBoundaryInfoNewOpts &opts) {
  reorder = opts.reorder;
  silence_label = opts.silence_label;
  partial_word_label = opts.partial_word_label;
}

WordBoundaryInfo::WordBoundaryInfo(const WordBoundaryInfoNewOpts &opts,
                                   std::string word_boundary_file) {
  reorder = opts.reorder;
  silence_label = opts.silence_label;
  partial_word_label = opts.partial_word_label;
  bool binary_in;
  Input ki(word_boundary_file, &binary_in);
  KALDI_ASSERT(!binary_in && "Not expecting binary word-boundary file.");
  Init(ki.Stream());
}

void WordBoundaryInfo::Init(std::istream &stream) {
  std::string line;
  while (std::getline(stream, line)) {
    std::vector<std::string> split_line;
    SplitStringToVector(line, " \t\r", true, &split_line);// split the line by space or tab
    int32 p = 0;
    if (split_line.size() != 2 ||
        !ConvertStringToInteger(split_line[0], &p))
      KALDI_ERR << "Invalid line in word-boundary file: " << line;
    KALDI_ASSERT(p > 0);
    if (phone_to_type.size() <= static_cast<size_t>(p))
      phone_to_type.resize(p+1, kNoPhone);
    std::string t = split_line[1];
    if (t == "nonword") phone_to_type[p] = kNonWordPhone;
    else if (t == "begin") phone_to_type[p] = kWordBeginPhone;
    else if (t == "singleton") phone_to_type[p] = kWordBeginAndEndPhone;
    else if (t == "end") phone_to_type[p] = kWordEndPhone;
    else if (t == "internal") phone_to_type[p] = kWordInternalPhone;
    else
      KALDI_ERR << "Invalid line in word-boundary file: " << line;
  }
  if (phone_to_type.empty())
    KALDI_ERR << "Empty word-boundary file";
}

bool WordAlignLattice(const CompactLattice &lat,
                      const TransitionModel &tmodel,
                      const WordBoundaryInfo &info,
                      int32 max_states,
                      CompactLattice *lat_out) {
  LatticeWordAligner aligner(lat, tmodel, info, max_states, lat_out);
  return aligner.AlignLattice();
}



class WordAlignedLatticeTester {
 public:
  WordAlignedLatticeTester(const CompactLattice &lat,
                           const TransitionModel &tmodel,
                           const WordBoundaryInfo &info,
                           const CompactLattice &aligned_lat):
      lat_(lat), tmodel_(tmodel), info_(info), aligned_lat_(aligned_lat) { }

  void Test() {
    // First test that each aligned arc is valid.
    typedef CompactLattice::StateId StateId ;
    for (StateId s = 0; s < aligned_lat_.NumStates(); s++) {
      for (fst::ArcIterator<CompactLattice> iter(aligned_lat_, s);
           !iter.Done();
           iter.Next()) {
        TestArc(iter.Value());
      }
      if (aligned_lat_.Final(s) != CompactLatticeWeight::Zero()) {
        TestFinal(aligned_lat_.Final(s));
      }
    }
    TestEquivalent();
  }
 private:
  void TestArc(const CompactLatticeArc &arc) {
    if (! (TestArcSilence(arc) || TestArcNormalWord(arc) || TestArcOnePhoneWord(arc)
           || TestArcEmpty(arc)))
      KALDI_ERR << "Invalid arc in aligned CompactLattice: "
                << arc.ilabel << " " << arc.olabel << " " << arc.nextstate
                << " " << arc.weight;
  }
  bool TestArcEmpty(const CompactLatticeArc &arc) {
    if (arc.ilabel != 0) return false; // Check there is no label.  Note, ilabel==olabel.
    const std::vector<int32> &tids = arc.weight.String();
    return tids.empty();
  }
  bool TestArcSilence(const CompactLatticeArc &arc) {
    // This only applies when silence doesn't have word labels.
    if (arc.ilabel !=  info_.silence_label) return false; // Check the label is
    // the silence label. Note, ilabel==olabel.
    const std::vector<int32> &tids = arc.weight.String();
    if (tids.empty()) return false;
    int32 first_phone = tmodel_.TransitionIdToPhone(tids.front());
    if (info_.TypeOfPhone(first_phone) != WordBoundaryInfo::kNonWordPhone)
      return false;
    for (size_t i = 0; i < tids.size(); i++)
      if (tmodel_.TransitionIdToPhone(tids[i]) != first_phone) return false;

    if (!info_.reorder) return tmodel_.IsFinal(tids.back());
    else {
      for (size_t i = 0; i < tids.size(); i++) {
        if (tmodel_.IsFinal(tids[i])) { // got the "final" transition, which is
          // reordered to actually not be final.  Make sure that all the
          // rest of the transition ids are the self-loop of that same
          // transition-state.
          for (size_t j = i+1; j < tids.size(); j++) {
            if (!(tmodel_.TransitionIdToTransitionState(tids[j])
                  == tmodel_.TransitionIdToTransitionState(tids[i]))) return false;
          }
          return true;
        }
      }
      return false; // fell off loop.  No final-state present.
    }
  }

  bool TestArcOnePhoneWord(const CompactLatticeArc &arc) {
    if (arc.ilabel == 0) return false; // Check there's a label.  Note, ilabel==olabel.
    const std::vector<int32> &tids = arc.weight.String();
    if (tids.empty()) return false;
    int32 first_phone = tmodel_.TransitionIdToPhone(tids.front());
    if (info_.TypeOfPhone(first_phone) !=
        WordBoundaryInfo::kWordBeginAndEndPhone) return false;
    for (size_t i = 0; i < tids.size(); i++)
      if (tmodel_.TransitionIdToPhone(tids[i]) != first_phone) return false;

    if (!info_.reorder) return tmodel_.IsFinal(tids.back());
    else {
      for (size_t i = 0; i < tids.size(); i++) {
        if (tmodel_.IsFinal(tids[i])) { // got the "final" transition, which is
          // reordered to actually not be final.  Make sure that all the
          // rest of the transition ids are the self-loop of that same
          // transition-state.
          for (size_t j = i+1; j < tids.size(); j++) {
            if (tmodel_.TransitionIdToTransitionState(tids[j])
                != tmodel_.TransitionIdToTransitionState(tids[i])) return false;
          }
          return true;
        }
      }
      return false; // fell off loop.  No final-state present.
    }
  }

  bool TestArcNormalWord(const CompactLatticeArc &arc) {
    if (arc.ilabel == 0) return false; // Check there's a label.  Note, ilabel==olabel.
    const std::vector<int32> &tids = arc.weight.String();
    if (tids.empty()) return false;
    int32 first_phone = tmodel_.TransitionIdToPhone(tids.front());
    if (info_.TypeOfPhone(first_phone) != WordBoundaryInfo::kWordBeginPhone)
      return false;
    size_t i;
    { // first phone.
      int num_final = 0;
      for (i = 0; i < tids.size(); i++) {
        if (tmodel_.TransitionIdToPhone(tids[i]) != first_phone) break;
        if (tmodel_.IsFinal(tids[i])) num_final++;
      }
      if (num_final != 1)
        return false; // Something went wrong-- perhaps we
      // got two beginning phones in a row.
    }
    { // middle phones.  Skip over them.
      while (i < tids.size() &&
             info_.TypeOfPhone(tmodel_.TransitionIdToPhone(tids[i]))
             == WordBoundaryInfo::kWordInternalPhone)
        i++;
    }
    if (i == tids.size()) return false;
    int32 final_phone = tmodel_.TransitionIdToPhone(tids[i]);
    if (info_.TypeOfPhone(final_phone) != WordBoundaryInfo::kWordEndPhone)
      return false; // not word-ending.
    for (size_t j = i; j < tids.size(); j++) // make sure only this final phone till end.
      if (tmodel_.TransitionIdToPhone(tids[j]) != final_phone)
        return false; // Other phones after final phone.

    for (size_t j = i; j < tids.size(); j++) {
      if (tmodel_.IsFinal(tids[j])) { // Found "final transition"..   Note:
        // may be "reordered" with its self loops.
        if (!info_.reorder) return (j+1 == tids.size());
        else {
          // Make sure the only thing that follows this is self-loops
          // of the final transition-state.
          for (size_t k = j + 1; k < tids.size(); k++)
            if (tmodel_.TransitionIdToTransitionState(tids[k])
                != tmodel_.TransitionIdToTransitionState(tids[j])
                || !tmodel_.IsSelfLoop(tids[k]))
              return false;
          return true;
        }
      }
    }
    return false; // Found no final state.
  }

  bool TestArcPartialWord(const CompactLatticeArc &arc) {
    if (arc.ilabel != info_.partial_word_label) return false; // label should
    // be the partial-word label.
    const std::vector<int32> &tids = arc.weight.String();
    if (tids.empty()) return false;
    return true; // We're pretty liberal when it comes to partial words here.
  }

  void TestFinal(const CompactLatticeWeight &w) {
    if (!w.String().empty())
      KALDI_ERR << "Expect to have no strings on final-weights of lattices.";
  }
  void TestEquivalent() {
    CompactLattice aligned_lat(aligned_lat_);
    if (info_.silence_label != 0) { // remove silence labels.
      std::vector<int32> to_remove;
      to_remove.push_back(info_.silence_label);
      RemoveSomeInputSymbols(to_remove, &aligned_lat);
      Project(&aligned_lat, fst::PROJECT_INPUT);
    }

    if (!RandEquivalent(lat_, aligned_lat, 5/*paths*/, 1.0e+10/*delta*/, Rand()/*seed*/,
                        200/*path length (max?)*/))
      KALDI_ERR << "Equivalence test failed (testing word-alignment of lattices.) "
                << "Make sure your model and lattices match!";
  }

  const CompactLattice &lat_;
  const TransitionModel &tmodel_;
  const WordBoundaryInfo &info_;
  const CompactLattice &aligned_lat_;
};




/// You should only test a lattice if WordAlignLattice returned true (i.e. it
/// succeeded and it wasn't a forced-out lattice); otherwise the test will most
/// likely fail.
void TestWordAlignedLattice(const CompactLattice &lat,
                            const TransitionModel &tmodel,
                            const WordBoundaryInfo &info,
                            const CompactLattice &aligned_lat) {
  WordAlignedLatticeTester t(lat, tmodel, info, aligned_lat);
  t.Test();
}






}  // namespace kaldi