summaryrefslogtreecommitdiff
path: root/dtrain/ksampler.h
blob: 767dc42e2a3f1334283b7e2d80afc3fd326dd709 (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
#ifndef _DTRAIN_KSAMPLER_H_
#define _DTRAIN_KSAMPLER_H_

#include "kbestget.h"
#include "hgsampler.h"
#include <vector>
#include <string>

using namespace std;

#include "kbest.h" // cdec
#include "sampler.h"

namespace dtrain
{


struct KSampler : public HypSampler
{
  const unsigned k_;
  vector<ScoredHyp> s_;
  MT19937* prng_;
  score_t (*scorer)(NgramCounts&, const unsigned, const unsigned, unsigned, vector<score_t>);

  explicit KSampler(const unsigned k, MT19937* prng) :
    k_(k), prng_(prng) {}

  virtual void
  NotifyTranslationForest(const SentenceMetadata& smeta, Hypergraph* hg)
  {
    Sample(*hg);
  }

  vector<ScoredHyp>* GetSamples() { return &s_; }

  void Sample(const Hypergraph& forest) {
    s_.clear();
    std::vector<HypergraphSampler::Hypothesis> samples;
    HypergraphSampler::sample_hypotheses(forest, k_, prng_, &samples);
    for (unsigned i = 0; i < k_; ++i) {
      ScoredHyp h;
      h.w = samples[i].words;
      h.f = samples[i].fmap;
      h.model = log(samples[i].model_score); 
      h.rank = i;
      s_.push_back(h);
    }
  }
};


} // namespace

#endif