summaryrefslogtreecommitdiff
path: root/training/dtrain/sample.h
blob: 64d93cb0bac5cc06825244cb41a770f5cf600bc5 (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
#ifndef _DTRAIN_SAMPLE_H_
#define _DTRAIN_SAMPLE_H_

#include "kbest.h"

namespace dtrain
{


struct ScoredKbest : public DecoderObserver
{
  const unsigned k_;
  vector<ScoredHyp> s_;
  unsigned src_len_;
  PerSentenceBleuScorer* scorer_;
  vector<vector<WordID> >* refs_;
  unsigned f_count_, sz_;

  ScoredKbest(const unsigned k, PerSentenceBleuScorer* scorer) :
    k_(k), scorer_(scorer) {}

  virtual void
  NotifyTranslationForest(const SentenceMetadata& smeta, Hypergraph* hg)
  {
    src_len_ = smeta.GetSourceLength();
    s_.clear(); sz_ = f_count_ = 0;
    KBest::KBestDerivations<vector<WordID>, ESentenceTraversal,
      KBest::FilterUnique, prob_t, EdgeProb> kbest(*hg, k_);
    for (unsigned i = 0; i < k_; ++i) {
      const KBest::KBestDerivations<vector<WordID>, ESentenceTraversal, KBest::FilterUnique,
              prob_t, EdgeProb>::Derivation* d =
            kbest.LazyKthBest(hg->nodes_.size() - 1, i);
      if (!d) break;
      ScoredHyp h;
      h.w = d->yield;
      h.f = d->feature_values;
      h.model = log(d->score);
      h.rank = i;
      h.score = scorer_->Score(h.w, *refs_);
      s_.push_back(h);
      sz_++;
      f_count_ += h.f.size();
    }
  }

  vector<ScoredHyp>* GetSamples() { return &s_; }
  inline void SetReference(vector<vector<WordID> >& refs) { refs_ = &refs; }
  inline unsigned GetFeatureCount() { return f_count_; }
  inline unsigned GetSize() { return sz_; }
};


} // namespace

#endif