summaryrefslogtreecommitdiff
path: root/training/dtrain/sample_net_interface.h
blob: 6d00e5d563e43637ac222416084e99dbb640b2b6 (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
#ifndef _DTRAIN_SAMPLE_NET_H_
#define _DTRAIN_SAMPLE_NET_H_

#include "kbest.h"

#include "score_net_interface.h"

namespace dtrain
{

struct ScoredKbest : public DecoderObserver
{
  const size_t k_;
  size_t feature_count_, effective_sz_;
  vector<ScoredHyp> samples_;
  PerSentenceBleuScorer* scorer_;
  vector<Ngrams>* ref_ngs_;
  vector<size_t>* ref_ls_;
  bool dont_score;
  string viterbiTreeStr_, viterbiRules_;

  ScoredKbest(const size_t k, PerSentenceBleuScorer* scorer) :
    k_(k), scorer_(scorer), dont_score(false) {}

  virtual void
  NotifyTranslationForest(const SentenceMetadata& /*smeta*/, Hypergraph* hg)
  {
    samples_.clear(); effective_sz_ = feature_count_ = 0;
    KBest::KBestDerivations<vector<WordID>, ESentenceTraversal,
      KBest::FilterUnique, prob_t, EdgeProb> kbest(*hg, k_);
    for (size_t 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;
      if (!dont_score)
        h.gold = scorer_->Score(h.w, *ref_ngs_, *ref_ls_);
      samples_.push_back(h);
      effective_sz_++;
      feature_count_ += h.f.size();
      viterbiTreeStr_ = hg->show_viterbi_tree(false);
      ostringstream ss;
      ViterbiRules(*hg, &ss);
      viterbiRules_ = ss.str();
    }
  }

  vector<ScoredHyp>* GetSamples() { return &samples_; }
  inline void SetReference(vector<Ngrams>& ngs, vector<size_t>& ls)
  {
    ref_ngs_ = &ngs;
    ref_ls_ = &ls;
  }
  inline size_t GetFeatureCount() { return feature_count_; }
  inline size_t GetSize() { return effective_sz_; }
  inline string GetViterbiTreeStr() { return viterbiTreeStr_; }
  inline string GetViterbiRules() { return viterbiRules_; }
};

} // namespace

#endif