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

#include "kbest.h"
#include "hgsampler.h"
#include "sampler.h"

namespace dtrain
{

/*
 * KSampler
 *
 */
struct KSampler : public DecoderObserver
{
  const size_t k_;
  KBestList kb;
  MT19937* rng;

  explicit KSampler( const size_t k, MT19937* prng ) :
    k_(k), rng(prng) {}

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

  KBestList* GetKBest() { return &kb; }

  void Sample( const Hypergraph& forest ) {
    kb.sents.clear();
    kb.feats.clear();
    kb.model_scores.clear();
    kb.scores.clear();
    std::vector<HypergraphSampler::Hypothesis> samples;
    HypergraphSampler::sample_hypotheses(forest, k_, rng, &samples);
    for ( size_t i = 0; i < k_; ++i ) {
      kb.sents.push_back( samples[i].words );
      kb.feats.push_back( samples[i].fmap );
      kb.model_scores.push_back( log(samples[i].model_score) );
    }
  }
};


} // namespace


#endif