diff --git a/openfst/lib/rmfinalepsilon.h b/openfst/lib/rmfinalepsilon.h index c828e13e..8e1fc981 100644 --- a/openfst/lib/rmfinalepsilon.h +++ b/openfst/lib/rmfinalepsilon.h @@ -71,15 +71,22 @@ void RmFinalEpsilon(MutableFst* fst) { const auto& arc = aiter.Value(); // Next state is in the list of finals. if (finals.find(arc.nextstate) != finals.end()) { - // Sums up all epsilon arcs if the semiring is left distributive. - // When left distributivity does not hold, at most one arc can be - // removed. - if (arc.ilabel == 0 && arc.olabel == 0 && - (weight == Weight::Zero() || - (Weight::Properties() & kLeftSemiring))) { - weight = Plus(Times(arc.weight, fst->Final(arc.nextstate)), weight); + // If the semiring is left-distributive we don't need to check if the + // weight is zero. + constexpr bool kIsLeftSemiring = + (Weight::Properties() & kLeftSemiring) != 0; + if constexpr (kIsLeftSemiring) { + if (arc.ilabel == 0 && arc.olabel == 0) { + weight = Plus(Times(arc.weight, fst->Final(arc.nextstate)), weight); + } else { + arcs.push_back(arc); + } } else { - arcs.push_back(arc); + if (arc.ilabel == 0 && arc.olabel == 0 && weight == Weight::Zero()) { + weight = Plus(Times(arc.weight, fst->Final(arc.nextstate)), weight); + } else { + arcs.push_back(arc); + } } } else { arcs.push_back(arc);