summaryrefslogtreecommitdiff
path: root/dtrain/score.cc
blob: 9b22508b489d66677d178a48297233e4a266dd6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "score.h"

namespace dtrain
{


/*
 * bleu
 *
 * as in "BLEU: a Method for Automatic Evaluation
 *        of Machine Translation"
 * (Papineni et al. '02)
 *
 * NOTE: 0 if one n in {1..N} has 0 count
 */
score_t
BleuScorer::Bleu(NgramCounts& counts, const unsigned hyp_len, const unsigned ref_len)
{
  if (hyp_len == 0 || ref_len == 0) return 0;
  unsigned M = N_;
  if (ref_len < N_) M = ref_len;
  score_t sum = 0;
  for (unsigned i = 0; i < M; i++) {
    if (counts.clipped[i] == 0 || counts.sum[i] == 0) return 0;
    sum += w_[i] * log((score_t)counts.clipped[i] / counts.sum[i]);
  }
  return brevity_penaly(hyp_len, ref_len) * exp(sum);
}

score_t
BleuScorer::Score(ScoredHyp& hyp, vector<WordID>& ref_ids, unsigned id)
{
  unsigned hyp_len = hyp.w.size(), ref_len = ref_ids.size();
  if (hyp_len == 0 || ref_len == 0) return 0;
  NgramCounts counts = make_ngram_counts(hyp.w, ref_ids, N_);
  return Bleu(counts, hyp_len, ref_len);
}

/*
 * 'stupid' bleu
 *
 * as in "ORANGE: a Method for Evaluating
 *        Automatic Evaluation Metrics
 *        for Machine Translation"
 * (Lin & Och '04)
 *
 * NOTE: 0 iff no 1gram match
 */
score_t
StupidBleuScorer::Score(ScoredHyp& hyp, vector<WordID>& ref_ids, unsigned id)
{
  unsigned hyp_len = hyp.w.size(), ref_len = ref_ids.size();
  if (hyp_len == 0 || ref_len == 0) return 0;
  NgramCounts counts = make_ngram_counts(hyp.w, ref_ids, N_);
  unsigned M = N_;
  if (ref_len < N_) M = ref_len;
  score_t sum = 0, add = 0;
  for (unsigned i = 0; i < M; i++) {
    if (i == 1) add = 1;
    //cout << ((score_t)counts.clipped[i] + add) << "/" << counts.sum[i] +add << "." << endl;
    //cout << "w_[i] " << w_[i] << endl;
    sum += w_[i] * log(((score_t)counts.clipped[i] + add) / ((counts.sum[i] + add)));
    //cout << "sum += "<< w_[i] * log(((score_t)counts.clipped[i] + add) / ((counts.sum[i] + add))) << endl;
  }
  /*cout << ref_ids << endl;
  cout << hyp.w << endl;
  cout << "ref_len " << ref_len << endl;
  cout << "hyp_len " << hyp_len << endl;
  cout << "bp " << brevity_penaly(hyp_len, ref_len) << endl;
  cout << "exp(sum) " << exp(sum) << endl;
  counts.Print();
  cout << brevity_penaly(hyp_len, ref_len) * exp(sum) << endl;
  cout << "---" << endl;*/
  return  brevity_penaly(hyp_len, ref_len) * exp(sum);
}

/*
 * smooth bleu
 *
 * as in "An End-to-End Discriminative Approach
 *        to Machine Translation"
 * (Liang et al. '06)
 *
 * NOTE: max is 0.9375
 */
score_t
SmoothBleuScorer::Score(ScoredHyp& hyp, vector<WordID>& ref_ids, unsigned id)
{
  unsigned hyp_len = hyp.w.size(), ref_len = ref_ids.size();
  if (hyp_len == 0 || ref_len == 0) return 0;
  NgramCounts counts = make_ngram_counts(hyp.w, ref_ids, N_);
  score_t sum = 0;
  unsigned j = 1;
  for (unsigned i = 0; i < N_; i++) {
    if (counts.clipped[i] == 0 || counts.sum[i] == 0) continue;
    sum += exp((w_[i] * log((score_t)counts.clipped[i]/counts.sum[i]))) / pow(2, N_-j+1);
    j++;
  }
  return brevity_penaly(hyp_len, ref_len) * sum;
}

/*
 * approx. bleu
 *
 * as in "Online Large-Margin Training of Syntactic
 *        and Structural Translation Features"
 * (Chiang et al. '08)
 */
/*void
ApproxBleuScorer::Prep(NgramCounts& counts, const unsigned hyp_len, const unsigned ref_len)
{
  glob_onebest_counts += counts;
  glob_hyp_len += hyp_len;
  glob_ref_len += ref_len;
}

void
ApproxBleuScorer::Reset()
{
  glob_onebest_counts.Zero();
  glob_hyp_len = 0;
  glob_ref_len = 0;
}

score_t
ApproxBleuScorer::Score(ScoredHyp& hyp, vector<WordID>& ref_ids, unsigned id)
{
  NgramCounts counts = make_ngram_counts(hyp.w, ref_ids, N_);
  if (id == 0) reset();
  unsigned hyp_len = 0, ref_len = 0;
  if (hyp.rank == 0) { // 'context of 1best translations'
    scorer->prep(counts, hyp.w.size(), ref_ids.size()); 
    counts.reset();
  } else {
    hyp_len = hyp.w.size();
    ref_len = ref_ids.size();
  }
  return 0.9 * BleuScorer::Bleu(glob_onebest_counts + counts,
                                glob_hyp_len + hyp_len, glob_ref_len + ref_len);
}*/


} // namespace