diff options
Diffstat (limited to 'dtrain/score.h')
-rw-r--r-- | dtrain/score.h | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/dtrain/score.h b/dtrain/score.h index eb8ad912..c5be2829 100644 --- a/dtrain/score.h +++ b/dtrain/score.h @@ -20,7 +20,7 @@ struct NgramCounts inline void operator+=(const NgramCounts& rhs) { - assert(N_ == rhs.N_); + if (rhs.N_ > N_) Resize(rhs.N_); for (unsigned i = 0; i < N_; i++) { this->clipped_[i] += rhs.clipped_.find(i)->second; this->sum_[i] += rhs.sum_.find(i)->second; @@ -59,14 +59,22 @@ struct NgramCounts inline void Zero() { - unsigned i; - for (i = 0; i < N_; i++) { + for (unsigned i = 0; i < N_; i++) { clipped_[i] = 0.; sum_[i] = 0.; } } inline void + One() + { + for (unsigned i = 0; i < N_; i++) { + clipped_[i] = 1.; + sum_[i] = 1.; + } + } + + inline void Print() { for (unsigned i = 0; i < N_; i++) { @@ -74,6 +82,23 @@ struct NgramCounts cout << i+1 << "grams:\t\t\t" << sum_[i] << endl; } } + + inline void Resize(unsigned N) + { + if (N == N_) return; + else if (N > N_) { + for (unsigned i = N_; i < N; i++) { + clipped_[i] = 0.; + sum_[i] = 0.; + } + } else { // N < N_ + for (unsigned i = N_-1; i > N-1; i--) { + clipped_.erase(i); + sum_.erase(i); + } + } + N_ = N; + } }; typedef map<vector<WordID>, unsigned> Ngrams; @@ -128,6 +153,11 @@ struct SmoothBleuScorer : public LocalScorer score_t Score(vector<WordID>& hyp, vector<WordID>& ref, const unsigned /*rank*/, const unsigned /*src_len*/); }; +struct SmoothSingleBleuScorer : public LocalScorer +{ + score_t Score(vector<WordID>& hyp, vector<WordID>& ref, const unsigned /*rank*/, const unsigned /*src_len*/); +}; + struct ApproxBleuScorer : public BleuScorer { NgramCounts glob_onebest_counts_; @@ -147,6 +177,24 @@ struct ApproxBleuScorer : public BleuScorer score_t Score(vector<WordID>& hyp, vector<WordID>& ref, const unsigned rank, const unsigned src_len); }; +struct LinearBleuScorer : public BleuScorer +{ + unsigned onebest_len_; + NgramCounts onebest_counts_; + + LinearBleuScorer(unsigned N) : onebest_len_(1), onebest_counts_(N) + { + onebest_counts_.One(); + } + + score_t Score(vector<WordID>& hyp, vector<WordID>& ref, const unsigned rank, const unsigned /*src_len*/); + + inline void Reset() { + onebest_len_ = 1; + onebest_counts_.One(); + } +}; + } // namespace |