18 #ifndef NLP_GRM2_SFST_RMPHI_H_ 19 #define NLP_GRM2_SFST_RMPHI_H_ 21 #include <sys/types.h> 28 #include <fst/arc-map.h> 29 #include <fst/arcsort.h> 31 #include <fst/matcher.h> 32 #include <fst/properties.h> 33 #include <fst/state-map.h> 34 #include <fst/weight.h> 63 typedef typename Arc::Label
Label;
66 fst::MatcherRewriteMode rewrite_mode =
67 fst::MATCHER_REWRITE_AUTO)
70 match_input_(match_input),
73 final_(Weight::Zero()),
74 matcher_(*fst_, match_input_ ?
75 fst::MATCH_INPUT : fst::MATCH_OUTPUT),
76 failpath_(*fst_, phi, match_input) {
78 if (rewrite_mode == f::MATCHER_REWRITE_AUTO) {
79 rewrite_both_ = fst.Properties(f::kAcceptor,
true);
80 }
else if (rewrite_mode == f::MATCHER_REWRITE_ALWAYS) {
83 rewrite_both_ =
false;
89 const fst::Fst<Arc> *
fst =
nullptr)
90 : fst_(
fst ?
fst->Copy() : mapper.fst_->Copy()),
92 match_input_(mapper.match_input_),
93 rewrite_both_(mapper.rewrite_both_),
96 final_(Weight::Zero()),
98 match_input_ ?
fst::MATCH_INPUT :
fst::MATCH_OUTPUT),
99 failpath_(*fst_, phi_, match_input_) {}
101 StateId
Start() {
return fst_->Start(); }
102 Weight
Final(StateId s)
const;
106 bool Done()
const {
return i_ >= arcs_.size(); }
107 const Arc &
Value()
const {
return arcs_[i_]; }
110 fst::MapSymbolsAction
113 fst::MapSymbolsAction
118 uint64_t oprops = iprops & f::kAddArcProperties & f::kSetFinalProperties &
119 f::kWeightInvariantProperties;
120 if (phi_ != 0 || !rewrite_both_) oprops &= f::kSetArcProperties;
126 std::unique_ptr<fst::Fst<Arc>> fst_;
134 std::vector<Arc> arcs_;
138 mutable fst::Matcher< fst::Fst<Arc> > matcher_;
145 Weight sfinal = fst_->Final(s);
146 if (sfinal == Weight::Zero())
149 failpath_.SetState(s);
150 Weight fail_weight = Weight::One();
151 for (
size_t i = 0; i < failpath_.Length(); ++i) {
152 fail_weight =
Times(fail_weight, failpath_.GetWeight(i));
153 Weight dfinal = fst_->Final(failpath_.GetNextState(i));
154 if (dfinal != Weight::Zero()) {
156 sfinal =
Minus(sfinal,
Times(fail_weight, dfinal));
168 failpath_.SetState(s);
172 arcs_.reserve(fst_->NumArcs(s));
174 Label prev_label = f::kNoLabel;
175 for (f::ArcIterator<f::Fst<Arc>> aiter(*fst_, s);
176 !aiter.Done(); aiter.Next()) {
177 Arc arc = aiter.Value();
178 Label label = match_input_ ? arc.ilabel : arc.olabel;
180 if (arc.ilabel == phi_ && (rewrite_both_ || match_input_))
182 if (arc.olabel == phi_ && (rewrite_both_ || !match_input_))
184 }
else if (label != 0) {
185 Weight fail_weight = Weight::One();
187 bool matched = label == prev_label;
188 for (
size_t i = 0; i < failpath_.Length() && !matched; ++i) {
189 fail_weight =
f::Times(fail_weight, failpath_.GetWeight(i));
190 matcher_.SetState(failpath_.GetNextState(i));
191 for (matcher_.Find(label); !matcher_.Done(); matcher_.Next()) {
192 Arc failarc = matcher_.Value();
193 Label faillabel = match_input_ ? failarc.ilabel : failarc.olabel;
195 Weight comb_weight =
Minus(arc.weight, corr_weight);
196 if (faillabel == f::kNoLabel) {
199 }
else if (failarc.nextstate == arc.nextstate &&
200 failarc.ilabel == arc.ilabel &&
201 failarc.olabel == arc.olabel &&
203 comb_weight != Weight::Zero()) {
208 arc.weight = comb_weight;
212 failarc.weight =
Minus(Weight::Zero(), corr_weight);
213 arcs_.push_back(failarc);
219 arcs_.push_back(arc);
231 template <
class IArc,
class OArc,
232 class WC = fst::WeightConvert<
typename IArc::Weight,
233 typename OArc::Weight>>
234 void RmPhi(
const fst::Fst<IArc>& ifst,
235 fst::MutableFst<OArc> *ofst,
236 typename IArc::Label phi_label = fst::kNoLabel,
237 fst::MatcherRewriteMode rewrite_mode =
238 fst::MATCHER_REWRITE_AUTO,
239 const WC &weight_convert = WC()) {
241 using WCM = f::WeightConvertMapper<IArc, OArc, WC>;
243 WCM to_signed_mapper(weight_convert);
244 if (phi_label != f::kNoLabel) {
245 f::ArcMapFst tfst(ifst, to_signed_mapper);
247 tfst, phi_label,
true, rewrite_mode);
248 f::StateMap(tfst, ofst, rm_phi_mapper);
249 f::ArcSort(ofst, f::ILabelCompare<OArc>());
251 f::ArcMap(ifst, ofst, to_signed_mapper);
259 #endif // NLP_GRM2_SFST_RMPHI_H_
Entropy64Weight Minus(Entropy64Weight w1, Entropy64Weight w2)
bool IsNegative(fst::SignedLog64Weight w)
uint64_t Properties(uint64_t iprops) const
void RmPhi(const fst::Fst< IArc > &ifst, fst::MutableFst< OArc > *ofst, typename IArc::Label phi_label=fst::kNoLabel, fst::MatcherRewriteMode rewrite_mode=fst::MATCHER_REWRITE_AUTO, const WC &weight_convert=WC())
fst::MapSymbolsAction InputSymbolsAction() const
RmPhiMapper(const fst::Fst< Arc > &fst, Label phi, bool match_input=true, fst::MatcherRewriteMode rewrite_mode=fst::MATCHER_REWRITE_AUTO)
fst::MapSymbolsAction OutputSymbolsAction() const
const Arc & Value() const
RmPhiMapper(const RmPhiMapper< Arc > &mapper, const fst::Fst< Arc > *fst=nullptr)
Weight Final(StateId s) const
Real64Weight Times(SignedLog64Weight w1, Real64Weight w2)