1 #ifndef LMERT_LMERT_HPP 2 #define LMERT_LMERT_HPP 14 #include <unordered_map> 28 MertLine() :
x ( -std::numeric_limits<double>::infinity() ),
y ( 0.0 ),
32 x ( -std::numeric_limits<double>::infinity() ), y ( y ), m ( m ) {}
46 std::vector<MertLine<Arc> >
lines;
50 return line1.
m < line2.
m;
65 for (
typename std::vector<
MertLine<Arc> >::size_type i = 0; i < lines.size(); i++ ) {
67 l.
x = -std::numeric_limits<double>::infinity();
69 if ( lines[j - 1].
m == l.
m ) {
70 if ( l.
y <= lines[j - 1].y )
75 l.
x = ( l.
y - lines[j - 1].y ) / ( lines[j - 1].
m - l.
m );
77 if ( lines[j - 1].
x < l.
x )
83 l.
x = -std::numeric_limits<double>::infinity();
94 std::string
ToString (
bool show_hypothesis =
false ) {
95 std::ostringstream oss;
97 for (
typename std::vector<
MertLine<Arc> >::size_type i = 0; i < lines.size();
99 oss <<
"line i=[" << std::right << std::setw ( 4 ) << i <<
"]" << std::fixed
100 << std::setprecision ( 6 ) <<
" x=[" << std::right << std::setw (
102 <<
"]" <<
" y=[" << std::right << std::setw ( 12 ) << lines[i].y <<
"]" 103 <<
" m=[" << std::right << std::setw ( 12 ) << lines[i].m <<
"]";
105 if ( show_hypothesis ) {
106 oss <<
" t=[" << lines[i].t <<
"]";
109 oss <<
" w=[" << lines[i].weight <<
"]";
122 std::vector<ucam::fsttools::BleuStats>
prev;
124 MertLattice (fst::VectorFst<Arc>*
fst,
const PARAMS32& lambda,
const PARAMS32& direction ) :
125 fst_ ( fst ), lambda_ ( lambda ), direction_ ( direction ) {
126 finalEnvelope.
lines.clear();
129 envelopes_.resize ( fst_->NumStates() + 1 );
131 envelopes_[fst->Start()].lines.push_back (
MertLine<Arc>() );
133 for ( fst::StateIterator < fst::VectorFst<Arc> > si ( *fst ); !si.Done();
135 const typename Arc::StateId& s = si.Value();
136 envelopes_[s].SweepLine();
137 for ( fst::ArcIterator < fst::VectorFst<Arc> > ai ( *fst, si.Value() );
138 !ai.Done(); ai.Next() ) {
139 const Arc& a = ai.Value();
140 PropagateEnvelope ( s, a.nextstate, a.weight, a.ilabel );
142 if ( fst->Final ( s ) != Arc::Weight::Zero() ) {
143 PropagateEnvelope ( s, fst->NumStates(), fst->Final ( s ) );
145 envelopes_[s].lines.clear();
148 envelopes_[fst->NumStates()].SweepLine();
150 finalEnvelope = envelopes_[fst->NumStates()];
151 envelopes_[fst->NumStates()].
lines.clear();
155 const typename Arc::StateId& trg,
const typename Arc::Weight&
weight,
157 for (
unsigned int i = 0; i < envelopes_[src].lines.size(); ++i ) {
159 line.
y += fst::DotProduct<float> (
weight, lambda_ ) * -1;
160 line.
m += fst::DotProduct<float> (
weight, direction_ ) * -1;
163 line.
t.push_back ( w );
165 envelopes_[trg].lines.push_back ( line );
170 fst::VectorFst<Arc>* fst_;
171 std::vector<MertEnvelope<Arc> > envelopes_;
182 sid_ ( sidx ), fst_ ( fst ), lambda_ ( lambda ), direction_ ( direction ),
194 vector< MertEnvelope<Arc> >&
env_;
MertLattice(fst::VectorFst< Arc > *fst, const PARAMS32 &lambda, const PARAMS32 &direction)
MertEnvelope< Arc > finalEnvelope
std::string ToString(bool show_hypothesis=false)
MertLatticeWrap(Sid sidx, fst::VectorFst< Arc > *fst, const PARAMS32 &lambda, const PARAMS32 &direction, vector< MertEnvelope< Arc > > &env)
bool GradientSortPredicate(const MertLine &line1, const MertLine &line2)
MertLine(double y, double m, Wid word)
void PropagateEnvelope(const typename Arc::StateId &src, const typename Arc::StateId &trg, const typename Arc::Weight &weight, const Wid &w=0)
vector< MertEnvelope< Arc > > & env_
fst::VectorFst< Arc > * fst_
static bool GradientSortPredicate(const MertLine< Arc > &line1, const MertLine< Arc > &line2)
ucam::fsttools::SentenceIdx SentenceIdx
ucam::fsttools::PARAMS32 PARAMS32
std::vector< ucam::fsttools::BleuStats > prev
std::vector< MertLine< Arc > > lines