16 #include <sys/types.h> 23 #include <fst/float-weight.h> 25 #include <fst/matcher.h> 26 #include <fst/signed-log-weight.h> 28 #ifndef NLP_GRM2_SFST_SFST_H_ 29 #define NLP_GRM2_SFST_SFST_H_ 38 inline bool Less(fst::LogWeightTpl<T> weight1,
39 fst::LogWeightTpl<T> weight2) {
40 return weight1.Value() > weight2.Value();
45 inline bool Less(fst::TropicalWeightTpl<T> weight1,
46 fst::TropicalWeightTpl<T> weight2) {
47 return weight1.Value() > weight2.Value();
52 inline bool Less(fst::RealWeightTpl<T> weight1,
53 fst::RealWeightTpl<T> weight2) {
54 return weight1.Value() < weight2.Value();
58 inline bool Less(fst::SignedLogWeightTpl<T> weight1,
59 fst::SignedLogWeightTpl<T> weight2) {
60 bool s1 = weight1.Value1().Value() > 0.0;
61 bool s2 = weight2.Value1().Value() > 0.0;
65 }
else if (s1 && !s2) {
67 }
else if (s1 && s2) {
68 return Less(weight1.Value2(), weight2.Value2());
70 return Less(weight2.Value2(), weight1.Value2());
74 template <
class Weight>
76 return Less(w1, w2) || w1 == w2;
80 using SLWeight = fst::SignedLog64Weight;
81 return Less(w, SLWeight::Zero());
85 fst::Log64Weight weight,
86 fst::Log64Weight approx_zero = kApproxZeroWeight) {
91 fst::SignedLog64Weight weight,
92 fst::Log64Weight pos_approx_zero = kApproxZeroWeight,
93 fst::Log64Weight neg_approx_zero = 10.0) {
94 if (weight.Value1().Value() > 0.0) {
95 return LessOrEqual(weight.Value2(), pos_approx_zero);
97 return LessOrEqual(weight.Value2(), neg_approx_zero);
104 template <
class Weight>
110 double sgn1 = w1.Value1().Value();
111 double sgn2 = w2.Value1().Value();
112 double val1 = w1.Value2().Value();
113 double val2 = w2.Value2().Value();
114 double exp1 = sgn1 * std::exp(-val1);
115 double exp2 = sgn2 * std::exp(-val2);
117 return std::abs(exp1 - exp2) < delta_;
134 typedef fst::ExplicitMatcher<fst::SortedMatcher<fst::Fst<Arc>>>
139 phi_label_(phi_label),
141 match_input ? fst::MATCH_INPUT : fst::MATCH_OUTPUT),
142 s_(fst::kNoStateId) {}
145 void SetState(StateId s);
149 size_t Length()
const {
return faildest_.size(); }
154 Weight
GetWeight(
size_t i)
const {
return failweight_[i]; }
162 ssize_t GetFailureArc(StateId s, Arc *arc);
164 const fst::Fst<Arc> &fst_;
170 std::vector<StateId> faildest_;
171 std::vector<Weight> failweight_;
172 std::vector<size_t> failpos_;
189 for (
StateId r = s;; r = failarc.nextstate) {
190 ssize_t pos = GetFailureArc(r, &failarc);
191 if (failarc.nextstate == f::kNoStateId)
break;
192 faildest_.push_back(failarc.nextstate);
193 failweight_.push_back(failarc.weight);
194 failpos_.push_back(pos);
201 *failarc = Arc(f::kNoLabel, f::kNoLabel, Weight::Zero(), f::kNoStateId);
203 if (phi_label_ == f::kNoLabel)
return pos;
205 matcher_.SetState(s);
206 if (matcher_.Find(phi_label_)) {
207 *failarc = matcher_.Value();
208 pos = matcher_.GetMatcher()->Position();
215 #endif // NLP_GRM2_SFST_SFST_H_ bool Less(fst::LogWeightTpl< T > weight1, fst::LogWeightTpl< T > weight2)
Weight GetWeight(size_t i) const
fst::ExplicitMatcher< fst::SortedMatcher< fst::Fst< Arc > > > Matr
bool IsNegative(fst::SignedLog64Weight w)
bool ApproxZero(fst::Log64Weight weight, fst::Log64Weight approx_zero=kApproxZeroWeight)
bool LessOrEqual(Weight w1, Weight w2)
FailurePath(const fst::Fst< Arc > &fst, Label phi_label, bool match_input)
StateId GetNextState(size_t i) const
size_t GetPosition(size_t i) const
const fst::Log64Weight kApproxZeroWeight
bool operator()(const Weight &w1, const Weight &w2) const
SignedLogWeightApproxEqual(float delta)