21 #ifndef KALDI_FSTEXT_FSTEXT_UTILS_INL_H_ 22 #define KALDI_FSTEXT_FSTEXT_UTILS_INL_H_ 43 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
45 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
46 const Arc &arc = aiter.Value();
47 ans = std::max(ans, arc.olabel);
56 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
58 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
59 const Arc &arc = aiter.Value();
60 ans = std::max(ans, arc.ilabel);
70 for (StateId s = 0; s < fst.NumStates(); s++)
71 num_arcs += fst.NumArcs(s);
75 template<
class Arc,
class I>
78 std::vector<I> *symbols) {
81 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
83 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
84 const Arc &arc = aiter.Value();
85 all_syms.insert(arc.olabel);
90 if (!include_eps && !all_syms.empty() && *all_syms.begin() == 0)
96 template<
class Arc,
class I>
99 std::vector<I> *symbols) {
101 unordered_set<I> all_syms;
102 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
104 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
105 const Arc &arc = aiter.Value();
106 all_syms.insert(arc.ilabel);
110 if (!include_eps && all_syms.count(0) != 0)
114 std::sort(symbols->begin(), symbols->end());
118 template<
class Arc,
class I>
120 MutableFst<Arc> *
fst) {
126 template<
class Arc,
class I>
131 if (ans.ilabel > 0 &&
132 ans.ilabel < static_cast<typename Arc::Label>((*symbol_mapping_).size()))
133 ans.ilabel = (*symbol_mapping_)[ans.ilabel];
140 bool remove_epsilons = (
symbol_mapping_->size() > 0 && (*symbol_mapping_)[0] != 0);
145 uint64 props_to_remove = kAcceptor|kNotAcceptor|kIDeterministic|kNonIDeterministic|
146 kILabelSorted|kNotILabelSorted;
147 if (remove_epsilons) props_to_remove |= kEpsilons|kIEpsilons;
148 if (add_epsilons) props_to_remove |= kNoEpsilons|kNoIEpsilons;
149 uint64 props_to_add = 0;
150 if (remove_epsilons && !add_epsilons) props_to_add |= kNoEpsilons|kNoIEpsilons;
151 return (props & ~props_to_remove) | props_to_add;
167 template<
class Arc,
class I>
169 MutableFst<Arc> *
fst) {
177 template<
class Arc,
class I>
179 std::vector<I> *isymbols_out,
180 std::vector<I> *osymbols_out,
185 Weight tot_weight = Weight::One();
186 std::vector<I> ilabel_seq;
187 std::vector<I> olabel_seq;
189 StateId cur_state = fst.Start();
190 if (cur_state == kNoStateId) {
191 if (isymbols_out != NULL) isymbols_out->clear();
192 if (osymbols_out != NULL) osymbols_out->clear();
193 if (tot_weight_out != NULL) *tot_weight_out = Weight::Zero();
197 Weight w = fst.Final(cur_state);
198 if (w != Weight::Zero()) {
199 tot_weight =
Times(w, tot_weight);
200 if (fst.NumArcs(cur_state) != 0)
return false;
201 if (isymbols_out != NULL) *isymbols_out = ilabel_seq;
202 if (osymbols_out != NULL) *osymbols_out = olabel_seq;
203 if (tot_weight_out != NULL) *tot_weight_out = tot_weight;
206 if (fst.NumArcs(cur_state) != 1)
return false;
208 ArcIterator<Fst<Arc> > iter(fst, cur_state);
209 const Arc &arc = iter.Value();
210 tot_weight =
Times(arc.weight, tot_weight);
211 if (arc.ilabel != 0) ilabel_seq.push_back(arc.ilabel);
212 if (arc.olabel != 0) olabel_seq.push_back(arc.olabel);
213 cur_state = arc.nextstate;
222 std::vector<VectorFst<Arc> > *fsts_out) {
226 StateId start_state = fst.Start();
227 if (start_state == kNoStateId)
return;
228 size_t n_arcs = fst.NumArcs(start_state);
229 bool start_is_final = (fst.Final(start_state) != Weight::Zero());
230 fsts_out->reserve(n_arcs + (start_is_final ? 1 : 0));
232 if (start_is_final) {
233 fsts_out->resize(fsts_out->size() + 1);
234 StateId start_state_out = fsts_out->back().AddState();
235 fsts_out->back().SetFinal(start_state_out, fst.Final(start_state));
238 for (ArcIterator<Fst<Arc> > start_aiter(fst, start_state);
240 start_aiter.Next()) {
241 fsts_out->resize(fsts_out->size() + 1);
242 VectorFst<Arc> &ofst = fsts_out->back();
243 const Arc &first_arc = start_aiter.Value();
244 StateId cur_state = start_state,
245 cur_ostate = ofst.AddState();
246 ofst.SetStart(cur_ostate);
247 StateId next_ostate = ofst.AddState();
248 ofst.AddArc(cur_ostate,
Arc(first_arc.ilabel, first_arc.olabel,
249 first_arc.weight, next_ostate));
250 cur_state = first_arc.nextstate;
251 cur_ostate = next_ostate;
253 size_t this_n_arcs = fst.NumArcs(cur_state);
256 if (this_n_arcs == 1) {
259 ArcIterator<Fst<Arc> > aiter(fst, cur_state);
260 const Arc &arc = aiter.Value();
261 next_ostate = ofst.AddState();
262 ofst.AddArc(cur_ostate,
Arc(arc.ilabel, arc.olabel,
263 arc.weight, next_ostate));
264 cur_state = arc.nextstate;
265 cur_ostate = next_ostate;
269 ofst.SetFinal(cur_ostate, fst.Final(cur_state));
281 std::vector<VectorFst<Arc> > *fsts_out) {
284 VectorFst<Arc> nbest_fst;
285 ShortestPath(fst, &nbest_fst, n);
289 template<
class Arc,
class I>
291 MutableFst<Arc> *ofst) {
295 ofst->DeleteStates();
296 StateId cur_state = ofst->AddState();
297 ofst->SetStart(cur_state);
298 for (
size_t i = 0;
i < labels.size();
i++) {
300 StateId next_state = ofst->AddState();
301 for (
size_t j = 0;
j < labels[
i].size();
j++) {
302 Arc arc(labels[
i][
j], labels[
i][j], Weight::One(), next_state);
303 ofst->AddArc(cur_state, arc);
305 cur_state = next_state;
307 ofst->SetFinal(cur_state, Weight::One());
310 template<
class Arc,
class I>
315 ofst->DeleteStates();
316 StateId cur_state = ofst->AddState();
317 ofst->SetStart(cur_state);
318 for (
size_t i = 0;
i < labels.size();
i++) {
319 StateId next_state = ofst->AddState();
320 Arc arc(labels[
i], labels[i], Weight::One(), next_state);
321 ofst->AddArc(cur_state, arc);
322 cur_state = next_state;
324 ofst->SetFinal(cur_state, Weight::One());
331 std::vector<I> *syms_out) {
334 for (SymbolTableIterator iter(symtab);
337 if (include_eps || iter.Value() != 0) {
338 syms_out->push_back(iter.Value());
347 std::vector<typename Arc::Label> extra_syms;
359 std::vector<typename Arc::Label> extra_syms;
375 ArcSort(fst, ILabelCompare<StdArc>());
376 VectorFst<LogArc> *fst_log =
new VectorFst<LogArc>;
378 VectorFst<StdArc> tmp;
380 VectorFst<LogArc> *fst_det_log =
new VectorFst<LogArc>;
382 Cast(*fst_det_log, fst);
391 ArcSort(fst, ILabelCompare<StdArc>());
392 VectorFst<LogArc> *fst_log =
new VectorFst<LogArc>;
394 VectorFst<StdArc> tmp;
396 VectorFst<LogArc> *fst_det_log =
new VectorFst<LogArc>;
397 Determinize(*fst_log, fst_det_log);
398 Cast(*fst_det_log, fst);
409 VectorFst<LogArc> *ifst_log =
new VectorFst<LogArc>;
410 Cast(*ifst, ifst_log);
411 VectorFst<LogArc> *ofst_log =
new VectorFst<LogArc>;
413 Cast(*ofst_log, ofst);
422 VectorFst<LogArc> *ifst_log =
new VectorFst<LogArc>;
423 Cast(*ifst, ifst_log);
424 VectorFst<LogArc> *ofst_log =
new VectorFst<LogArc>;
426 Cast(*ofst_log, ofst);
438 for (StateIterator<MutableFst<Arc> > siter(*ifst); !siter.Done(); siter.Next()) {
439 StateId s = siter.Value();
440 for (MutableArcIterator<MutableFst<Arc> > aiter(ifst, s); !aiter.Done(); aiter.Next()) {
441 Arc arc(aiter.Value());
442 arc.weight = Weight::One();
445 if (ifst->Final(s) != Weight::Zero())
446 ifst->SetFinal(s, Weight::One());
448 ifst->SetProperties(kUnweighted, kUnweighted);
465 template<
class Arc,
class F>
467 typedef typename F::Result ClassType;
469 std::vector<ClassType> classes;
470 ClassType noClass = f(kNoLabel);
472 if (start_is_epsilon) {
473 StateId start_state = fst.Start();
474 if (start_state < 0 || start_state == kNoStateId)
476 classes.resize(start_state+1, noClass);
477 classes[start_state] = 0;
480 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
481 StateId s = siter.Value();
482 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
483 const Arc &arc = aiter.Value();
484 if (classes.size() <= arc.nextstate)
485 classes.resize(arc.nextstate+1, noClass);
486 if (classes[arc.nextstate] == noClass)
487 classes[arc.nextstate] = f(arc.ilabel);
489 if (classes[arc.nextstate] != f(arc.ilabel))
503 template<
class Arc,
class F>
507 typedef typename F::Result ClassType;
508 const ClassType noClass = f(kNoLabel), epsClass = f(0);
509 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
510 StateId s = siter.Value();
511 ClassType c = noClass;
512 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
513 const Arc &arc = aiter.Value();
517 if (c != f(arc.ilabel))
520 if (end_is_epsilon && c != noClass &&
521 c != epsClass && fst.Final(s) != Weight::Zero())
533 template<
class Arc,
class F>
535 typedef typename F::Result ClassType;
538 std::vector<ClassType> classes;
539 ClassType noClass = f(kNoLabel);
540 ClassType epsClass = f(0);
541 if (start_is_epsilon) {
542 StateId start_state = fst->Start();
543 if (start_state < 0 || start_state == kNoStateId)
545 classes.resize(start_state+1, noClass);
546 classes[start_state] = epsClass;
550 std::set<StateId> bad_states;
551 for (StateIterator<Fst<Arc> > siter(*fst); !siter.Done(); siter.Next()) {
552 StateId s = siter.Value();
553 for (ArcIterator<Fst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
554 const Arc &arc = aiter.Value();
555 if (classes.size() <=
static_cast<size_t>(arc.nextstate))
556 classes.resize(arc.nextstate+1, noClass);
557 if (classes[arc.nextstate] == noClass)
558 classes[arc.nextstate] = f(arc.ilabel);
560 if (classes[arc.nextstate] != f(arc.ilabel))
561 bad_states.insert(arc.nextstate);
564 if (bad_states.empty())
return;
570 std::vector<std::pair<StateId, size_t> > arcs_to_change;
571 for (StateIterator<Fst<Arc> > siter(*fst); !siter.Done(); siter.Next()) {
572 StateId s = siter.Value();
573 for (ArcIterator<Fst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
574 const Arc &arc = aiter.Value();
575 if (arc.ilabel != 0 &&
576 bad_states_ciset.
count(arc.nextstate) != 0)
577 arcs_to_change.push_back(std::make_pair(s, aiter.Position()));
582 std::map<std::pair<StateId, ClassType>, StateId> state_map;
585 for (
size_t i = 0;
i < arcs_to_change.size();
i++) {
586 StateId s = arcs_to_change[
i].first;
587 ArcIterator<MutableFst<Arc> > aiter(*fst, s);
588 aiter.Seek(arcs_to_change[
i].second);
589 Arc arc = aiter.Value();
593 std::pair<StateId, ClassType> p(arc.nextstate, f(arc.ilabel));
594 if (state_map.count(p) == 0) {
595 StateId newstate = state_map[p] = fst->AddState();
596 fst->AddArc(newstate,
Arc(0, 0, Weight::One(), arc.nextstate));
598 StateId dst_state = state_map[p];
599 arc.nextstate = dst_state;
603 MutableArcIterator<MutableFst<Arc> > maiter(fst, s);
604 maiter.Seek(arcs_to_change[
i].second);
605 maiter.SetValue(arc);
615 template<
class Arc,
class F>
619 typedef typename F::Result ClassType;
620 std::vector<StateId> bad_states;
621 ClassType noClass = f(kNoLabel);
622 ClassType epsClass = f(0);
623 for (StateIterator<Fst<Arc> > siter(*fst); !siter.Done(); siter.Next()) {
624 StateId s = siter.Value();
625 ClassType c = noClass;
627 for (ArcIterator<Fst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
628 const Arc &arc = aiter.Value();
632 if (c != f(arc.ilabel)) {
637 if (end_is_epsilon && c != noClass &&
638 c != epsClass && fst->Final(s) != Weight::Zero())
641 bad_states.push_back(s);
643 std::vector<Arc> my_arcs;
644 for (
size_t i = 0;
i < bad_states.size();
i++) {
645 StateId s = bad_states[
i];
647 for (ArcIterator<MutableFst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next())
648 my_arcs.push_back(aiter.Value());
650 for (
size_t j = 0;
j < my_arcs.size();
j++) {
651 Arc &arc = my_arcs[
j];
652 if (arc.ilabel != 0) {
653 StateId newstate = fst->AddState();
658 fst->AddArc(newstate,
Arc(arc.ilabel, 0, Weight::One(), arc.nextstate));
659 MutableArcIterator<MutableFst<Arc> > maiter(fst, s);
661 maiter.SetValue(
Arc(0, arc.olabel, arc.weight, newstate));
669 VectorFst<Arc>*
MakeLoopFst(
const std::vector<
const ExpandedFst<Arc> *> &fsts) {
674 VectorFst<Arc> *ans =
new VectorFst<Arc>;
675 StateId loop_state = ans->AddState();
676 ans->SetStart(loop_state);
677 ans->SetFinal(loop_state, Weight::One());
681 unordered_map<const ExpandedFst<Arc> *,
Arc> cache;
683 for (Label
i = 0; i < static_cast<Label>(fsts.size());
i++) {
684 const ExpandedFst<Arc> *
fst = fsts[
i];
685 if (fst == NULL)
continue;
688 typename unordered_map<const ExpandedFst<Arc> *,
Arc>::iterator
689 iter = cache.find(fst);
690 if (iter != cache.end()) {
691 Arc arc = iter->second;
698 KALDI_ASSERT(fst->Properties(kAcceptor,
true) == kAcceptor);
700 StateId fst_num_states = fst->NumStates();
701 StateId fst_start_state = fst->Start();
703 if (fst_start_state == kNoStateId)
706 bool share_start_state =
707 fst->Properties(kInitialAcyclic,
true) == kInitialAcyclic
708 && fst->NumArcs(fst_start_state) == 1
709 && fst->Final(fst_start_state) == Weight::Zero();
711 std::vector<StateId> state_map(fst_num_states);
712 for (StateId s = 0; s < fst_num_states; s++) {
713 if (s == fst_start_state && share_start_state) state_map[s] = loop_state;
714 else state_map[s] = ans->AddState();
716 if (!share_start_state) {
717 Arc arc(0,
i, Weight::One(), state_map[fst_start_state]);
721 for (StateId s = 0; s < fst_num_states; s++) {
723 for (ArcIterator<ExpandedFst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
724 const Arc &arc = aiter.Value();
725 Label olabel = (s == fst_start_state && share_start_state ?
i : 0);
726 Arc newarc(arc.ilabel, olabel, arc.weight, state_map[arc.nextstate]);
727 ans->AddArc(state_map[s], newarc);
728 if (s == fst_start_state && share_start_state)
731 if (fst->Final(s) != Weight::Zero()) {
732 KALDI_ASSERT(!(s == fst_start_state && share_start_state));
733 ans->AddArc(state_map[s],
Arc(0, 0, fst->Final(s), loop_state));
744 MutableFst<Arc> *
fst) {
745 for (StateIterator<MutableFst<Arc> > siter(*fst);
749 for (MutableArcIterator<MutableFst<Arc> > aiter(fst, s);
752 Arc arc = aiter.Value();
754 if (clear_input && arc.ilabel != 0) {
758 if (clear_output && arc.olabel != 0) {
774 for (StateIterator<MutableFst<Arc> > siter(*fst);
777 StateId s = siter.Value();
778 for (MutableArcIterator<MutableFst<Arc> > aiter(fst, s);
781 Arc arc = aiter.Value();
782 arc.weight =
Weight(arc.weight.Value() * scale);
785 if (fst->Final(s) != Weight::Zero())
786 fst->SetFinal(s, Weight(fst->Final(s).Value() * scale));
795 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next())
796 if (aiter.Value().nextstate == s
797 && aiter.Value().ilabel != 0)
return static_cast<ssize_t>(aiter.Position());
798 return static_cast<ssize_t
>(-1);
806 MutableFst<Arc> *ofst,
812 KALDI_ASSERT(ifst.Properties(kCoAccessible,
true) == kCoAccessible);
817 if (ifst.Start() == kNoStateId) {
822 std::vector<StateId> path;
823 std::vector<size_t> arc_offsets;
824 std::vector<int> nof_ilabels;
826 StateId num_ilabels = 0;
838 path.push_back(ifst.Start());
842 StateId s = path.back();
843 size_t num_arcs = ifst.NumArcs(s);
844 size_t num_arcs_tot = num_arcs;
845 if (ifst.Final(s) != Weight::Zero()) num_arcs_tot++;
849 size_t arc_offset =
static_cast<size_t>(
kaldi::RandInt(0, num_arcs_tot-1));
851 if (arc_offset < num_arcs) {
852 ArcIterator<Fst<Arc> > aiter(ifst, s);
853 aiter.Seek(arc_offset);
854 const Arc &arc = aiter.Value();
855 if (arc.nextstate == s) {
858 arc_offsets.push_back(arc_offset);
859 path.push_back(arc.nextstate);
860 if (arc.ilabel != 0) num_ilabels++;
867 nof_ilabels.push_back(num_ilabels);
868 }
while (( ++retry_no < num_retries) && (num_ilabels > length));
870 if (num_ilabels > length) {
871 std::stringstream ilabel_vec;
872 std::copy(nof_ilabels.begin(), nof_ilabels.end(),
873 std::ostream_iterator<int>(ilabel_vec,
","));
874 std::string s = ilabel_vec.str();
875 s.erase(s.end() - 1);
876 KALDI_WARN <<
"EqualAlign: the randomly constructed paths lengths: " << s;
877 KALDI_WARN <<
"EqualAlign: utterance has too few frames " << length
882 StateId num_self_loops = 0;
883 std::vector<ssize_t> self_loop_offsets(path.size());
884 for (
size_t i = 0;
i < path.size();
i++)
886 !=
static_cast<ssize_t
>(-1) )
889 if (num_self_loops == 0
890 && num_ilabels < length) {
891 KALDI_WARN <<
"No self-loops on chosen path; cannot match length.";
895 StateId num_extra = length - num_ilabels;
897 StateId min_num_loops = 0;
898 if (num_extra != 0) min_num_loops = num_extra / num_self_loops;
899 StateId num_with_one_more_loop = num_extra - (min_num_loops*num_self_loops);
900 KALDI_ASSERT(num_with_one_more_loop < num_self_loops || num_self_loops == 0);
904 StateId cur_state = 0;
906 for (
size_t i = 0; i < path.size(); i++) {
908 StateId num_loops = 0;
909 if (self_loop_offsets[i] != static_cast<ssize_t>(-1)) {
910 num_loops = min_num_loops + (counter < num_with_one_more_loop ? 1 : 0);
913 for (StateId
j = 0;
j < num_loops;
j++) {
914 ArcIterator<Fst<Arc> > aiter(ifst, path[i]);
915 aiter.Seek(self_loop_offsets[i]);
916 Arc arc = aiter.Value();
919 StateId next_state = ofst->AddState();
920 ofst->AddArc(cur_state,
Arc(arc.ilabel, arc.olabel, arc.weight, next_state));
921 cur_state = next_state;
923 if (i+1 < path.size()) {
924 ArcIterator<Fst<Arc> > aiter(ifst, path[i]);
925 aiter.Seek(arc_offsets[i]);
926 Arc arc = aiter.Value();
928 StateId next_state = ofst->AddState();
929 ofst->AddArc(cur_state,
Arc(arc.ilabel, arc.olabel, arc.weight, next_state));
930 cur_state = next_state;
932 Weight weight = ifst.Final(path[i]);
934 ofst->SetFinal(cur_state, weight);
954 NaturalLess<Weight> nl;
955 StateId non_coacc_state = kNoStateId;
956 size_t num_arcs_removed = 0, tot_arcs = 0;
957 for (StateIterator<MutableFst<Arc> > siter(*fst);
960 std::vector<size_t> arcs_to_delete;
961 std::vector<Arc> arcs;
963 std::map<std::pair<Label, StateId>, std::vector<size_t> > pair2arclist;
964 StateId state = siter.Value();
965 for (ArcIterator<MutableFst<Arc> > aiter(*fst, state);
968 size_t pos = arcs.size();
969 const Arc &arc = aiter.Value();
971 pair2arclist[std::make_pair(arc.ilabel, arc.nextstate)].push_back(pos);
973 typename std::map<std::pair<Label, StateId>, std::vector<size_t> >::iterator
974 iter = pair2arclist.begin(), end = pair2arclist.end();
975 for (; iter!= end; ++iter) {
976 const std::vector<size_t> &poslist = iter->second;
977 if (poslist.size() > 1) {
978 size_t best_pos = poslist[0];
979 Weight best_weight = arcs[best_pos].weight;
980 for (
size_t j = 1;
j < poslist.size();
j++) {
981 size_t pos = poslist[
j];
982 Weight this_weight = arcs[pos].weight;
983 if (nl(this_weight, best_weight)) {
985 best_weight = this_weight;
989 for (
size_t j = 0;
j < poslist.size();
j++)
990 if (poslist[
j] != best_pos)
991 arcs_to_delete.push_back(poslist[
j]);
994 size_t pos = poslist[0];
995 Arc &arc = arcs[pos];
996 if (arc.ilabel == 0 && arc.nextstate == state)
997 arcs_to_delete.push_back(pos);
1000 tot_arcs += arcs.size();
1001 if (arcs_to_delete.size() != 0) {
1002 num_arcs_removed += arcs_to_delete.size();
1003 if (non_coacc_state == kNoStateId)
1004 non_coacc_state = fst->AddState();
1005 MutableArcIterator<MutableFst<Arc> > maiter(fst, state);
1006 for (
size_t j = 0;
j < arcs_to_delete.size();
j++) {
1007 size_t pos = arcs_to_delete[
j];
1009 arcs[pos].nextstate = non_coacc_state;
1010 maiter.SetValue(arcs[pos]);
1014 if (non_coacc_state != kNoStateId)
1016 KALDI_VLOG(1) <<
"removed " << num_arcs_removed <<
" of " << tot_arcs
1022 const Fst<Arc> &fst2,
1024 MutableFst<Arc> *ofst) {
1027 typedef PhiMatcher<SortedMatcher<F> > PM;
1029 base_opts.gc_limit = 0;
1032 ComposeFstImplOptions<SortedMatcher<F>, PM> impl_opts(base_opts);
1039 new PM(fst2, MATCH_INPUT, phi_label,
false);
1040 SortedMatcher<F> *sorted_matcher =
1041 new SortedMatcher<F>(fst1, MATCH_NONE);
1044 impl_opts.matcher1 = sorted_matcher;
1045 impl_opts.matcher2 = phi_matcher;
1046 *ofst = ComposeFst<Arc>(fst1, fst2, impl_opts);
1054 MutableFst<Arc> *
fst) {
1056 if (fst->Final(s) == Weight::Zero()) {
1061 for (ArcIterator<Fst<Arc> > aiter(*fst, s);
1062 !aiter.Done(); aiter.Next()) {
1063 const Arc &arc = aiter.Value();
1064 if (arc.ilabel == phi_label) {
1066 if (arc.nextstate == s)
continue;
1075 if (fst->Final(arc.nextstate) != Weight::Zero())
1076 fst->SetFinal(s,
Times(fst->Final(arc.nextstate), arc.weight));
1078 KALDI_ASSERT(num_phis <= 1 &&
"Phi nondeterminism found");
1085 MutableFst<Arc> *
fst) {
1087 if (fst->Properties(kIEpsilons,
true))
1088 KALDI_WARN <<
"PropagateFinal: this may not work as desired " 1089 "since your FST has input epsilons.";
1090 StateId num_states = fst->NumStates();
1091 for (StateId s = 0; s < num_states; s++)
1097 const Fst<Arc> &fst2,
1099 MutableFst<Arc> *ofst) {
1102 typedef RhoMatcher<SortedMatcher<F> > RM;
1104 base_opts.gc_limit = 0;
1107 ComposeFstImplOptions<SortedMatcher<F>, RM> impl_opts(base_opts);
1114 new RM(fst2, MATCH_INPUT, rho_label);
1115 SortedMatcher<F> *sorted_matcher =
1116 new SortedMatcher<F>(fst1, MATCH_NONE);
1119 impl_opts.matcher1 = sorted_matcher;
1120 impl_opts.matcher2 = rho_matcher;
1121 *ofst = ComposeFst<Arc>(fst1, fst2, impl_opts);
1141 NaturalLess<Weight> nl;
1142 bool first_time =
true;
1144 if (min_sum) *min_sum = Arc::Weight::One();
1145 if (max_sum) *max_sum = Arc::Weight::One();
1146 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
1147 StateId s = siter.Value();
1148 Weight sum = fst.Final(s);
1149 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
1150 const Arc &arc = aiter.Value();
1151 sum =
Plus(sum, arc.weight);
1153 if (!
ApproxEqual(Weight::One(), sum, delta)) ans =
false;
1156 if (max_sum) *max_sum = sum;
1157 if (min_sum) *min_sum = sum;
1159 if (max_sum && nl(*max_sum, sum)) *max_sum = sum;
1160 if (min_sum && nl(sum, *min_sum)) *min_sum = sum;
1164 if (max_sum) *max_sum = Weight::One();
1165 if (min_sum) *min_sum = Weight::One();
1180 bool first_time =
true;
1182 if (min_sum) *min_sum = LogArc::Weight::One();
1183 if (max_sum) *max_sum = LogArc::Weight::One();
1184 for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
1185 StateId s = siter.Value();
1186 Weight sum = fst.Final(s);
1187 for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
1188 const Arc &arc = aiter.Value();
1189 sum =
Plus(sum, arc.weight);
1191 if (!
ApproxEqual(Weight::One(), sum, delta)) ans =
false;
1194 if (max_sum) *max_sum = sum;
1195 if (min_sum) *min_sum = sum;
1199 if (max_sum && sum.Value() < max_sum->Value()) *max_sum = sum;
1200 if (min_sum && sum.Value() > min_sum->Value()) *min_sum = sum;
1204 if (max_sum) *max_sum = Weight::One();
1205 if (min_sum) *min_sum = Weight::One();
1221 log_max = LogArc::Weight::Zero();
1222 if (fst.Type() ==
"const") {
1223 ConstFst<LogArc> logfst;
1224 Cast(
dynamic_cast<const ConstFst<StdArc>&
>(fst), &logfst);
1226 }
else if (fst.Type() ==
"vector") {
1227 VectorFst<LogArc> logfst;
1228 Cast(
dynamic_cast<const VectorFst<StdArc>&
>(fst), &logfst);
1231 KALDI_ERR <<
"This version currently supports ConstFst<StdArc> " 1232 <<
"or VectorFst<StdArc>";
fst::StdArc::StateId StateId
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
void GetSymbols(const SymbolTable &symtab, bool include_eps, std::vector< I > *syms_out)
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
ssize_t FindSelfLoopWithILabel(const Fst< Arc > &fst, typename Arc::StateId s)
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
void MakePrecedingInputSymbolsSame(bool start_is_epsilon, MutableFst< Arc > *fst)
MakePrecedingInputSymbolsSame ensures that all arcs entering any given fst state have the same input ...
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
void PropagateFinal(typename Arc::Label phi_label, MutableFst< Arc > *fst)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
void PhiCompose(const Fst< Arc > &fst1, const Fst< Arc > &fst2, typename Arc::Label phi_label, MutableFst< Arc > *ofst)
void DeterminizeInLog(VectorFst< StdArc > *fst)
void MinimizeEncoded(VectorFst< Arc > *fst, float delta=kDelta)
bool ApproxEqual(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, float delta=kDelta)
void ClearSymbols(bool clear_input, bool clear_output, MutableFst< Arc > *fst)
ClearSymbols sets all the symbols on the input and/or output side of the FST to zero, as specified.
void SafeDeterminizeWrapperInLog(VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, float delta)
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
void RemoveUselessArcs(MutableFst< Arc > *fst)
void MakeLinearAcceptorWithAlternatives(const std::vector< std::vector< I > > &labels, MutableFst< Arc > *ofst)
Creates an unweighted acceptor with a linear structure, with alternatives at each position...
void DeterminizeStarInLog(VectorFst< StdArc > *fst, float delta, bool *debug_ptr, int max_states)
void MakeLinearAcceptor(const std::vector< I > &labels, MutableFst< Arc > *ofst)
Creates unweighted linear acceptor from symbol sequence.
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
bool EqualAlign(const Fst< Arc > &ifst, typename Arc::StateId length, int rand_seed, MutableFst< Arc > *ofst, int num_retries)
EqualAlign is similar to RandGen, but it generates a sequence with exactly "length" input symbols...
void ConvertNbestToVector(const Fst< Arc > &fst, std::vector< VectorFst< Arc > > *fsts_out)
This function converts an FST with a special structure, which is output by the OpenFst functions Shor...
void NbestAsFsts(const Fst< Arc > &fst, size_t n, std::vector< VectorFst< Arc > > *fsts_out)
Takes the n-shortest-paths (using ShortestPath), but outputs the result as a vector of up to n fsts...
Arc::Label HighestNumberedOutputSymbol(const Fst< Arc > &fst)
Returns the highest numbered output symbol id of the FST (or zero for an empty FST.
bool IsStochasticFstInLog(const Fst< StdArc > &fst, float delta, StdArc::Weight *min_sum, StdArc::Weight *max_sum)
VectorFst< Arc > * MakeLoopFst(const std::vector< const ExpandedFst< Arc > *> &fsts)
MakeLoopFst creates an FST that has a state that is both initial and final (weight == Weight::One())...
bool PrecedingInputSymbolsAreSame(bool start_is_epsilon, const Fst< Arc > &fst)
Returns true if and only if the FST is such that the input symbols on arcs entering any given state a...
bool IsStochasticFst(const Fst< LogArc > &fst, float delta, LogArc::Weight *min_sum, LogArc::Weight *max_sum)
void ApplyProbabilityScale(float scale, MutableFst< Arc > *fst)
ApplyProbabilityScale is applicable to FSTs in the log or tropical semiring.
bool PrecedingInputSymbolsAreSameClass(bool start_is_epsilon, const Fst< Arc > &fst, const F &f)
This is as PrecedingInputSymbolsAreSame, but with a functor f that maps labels to classes...
void MakePrecedingInputSymbolsSameClass(bool start_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakePrecedingInputSymbolsSame, but takes a functor object that maps labels to classes.
fst::StdArc::Weight Weight
bool FollowingInputSymbolsAreSame(bool end_is_epsilon, const Fst< Arc > &fst)
Returns true if and only if the FST is such that the input symbols on arcs exiting any given state al...
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.
#define KALDI_ASSERT(cond)
void MapInputSymbols(const std::vector< I > &symbol_mapping, MutableFst< Arc > *fst)
void RemoveWeights(MutableFst< Arc > *ifst)
void SafeDeterminizeWrapper(MutableFst< Arc > *ifst, MutableFst< Arc > *ofst, float delta)
Does PreDeterminize and DeterminizeStar and then removes the disambiguation symbols.
void PropagateFinalInternal(typename Arc::Label phi_label, typename Arc::StateId s, MutableFst< Arc > *fst)
void SafeDeterminizeMinimizeWrapperInLog(VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, float delta)
SafeDeterminizeMinimizeWapperInLog is as SafeDeterminizeMinimizeWrapper except it first casts tothe l...
void SafeDeterminizeMinimizeWrapper(MutableFst< Arc > *ifst, VectorFst< Arc > *ofst, float delta)
SafeDeterminizeMinimizeWapper is as SafeDeterminizeWrapper except that it also minimizes (encoded min...
void RhoCompose(const Fst< Arc > &fst1, const Fst< Arc > &fst2, typename Arc::Label rho_label, MutableFst< Arc > *ofst)
bool FollowingInputSymbolsAreSameClass(bool end_is_epsilon, const Fst< Arc > &fst, const F &f)
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.
void MakeFollowingInputSymbolsSameClass(bool end_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakeFollowingInputSymbolsSame, but takes a functor object that maps labels to classes.
void MakeFollowingInputSymbolsSame(bool end_is_epsilon, MutableFst< Arc > *fst)
MakeFollowingInputSymbolsSame ensures that all arcs exiting any given fst state have the same input s...
void GetOutputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetOutputSymbols gets the list of symbols on the output of fst (including epsilon, if include_eps == true)
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
void RemoveSomeInputSymbols(const std::vector< I > &to_remove, MutableFst< Arc > *fst)
RemoveSomeInputSymbols removes any symbol that appears in "to_remove", from the input side of the FST...