summaryrefslogtreecommitdiff
path: root/dtrain/kbestget.h
blob: 792011829c292f1363a119571720016da0fdaae9 (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
#ifndef _DTRAIN_KBESTGET_H_
#define _DTRAIN_KBESTGET_H_

#include "kbest.h"

namespace dtrain
{


struct Samples
{
  vector<SparseVector<double> > feats;
  vector<vector<WordID> > sents;
  vector<double> model_scores;
  vector<double> scores;
  size_t GetSize() { return sents.size(); }
};

struct HypoSampler : public DecoderObserver
{
  virtual Samples* GetSamples() {}
};

struct KBestGetter : public HypoSampler
{
  const size_t k_;
  const string filter_type;
  Samples s;

  KBestGetter(const size_t k, const string filter_type) :
    k_(k), filter_type(filter_type) {}

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

  Samples* GetSamples() { return &s; }

  void
  KBest(const Hypergraph& forest)
  {
    if (filter_type == "unique") {
      KBestUnique(forest);
    } else if (filter_type == "no") {
      KBestNoFilter(forest);
    }
  }

  void
  KBestUnique(const Hypergraph& forest)
  {
    s.sents.clear();
    s.feats.clear();
    s.model_scores.clear();
    s.scores.clear();
    KBest::KBestDerivations<vector<WordID>, ESentenceTraversal, KBest::FilterUnique, prob_t, EdgeProb> kbest(forest, k_);
    for (size_t i = 0; i < k_; ++i) {
      const KBest::KBestDerivations<vector<WordID>, ESentenceTraversal, KBest::FilterUnique, prob_t, EdgeProb>::Derivation* d =
            kbest.LazyKthBest(forest.nodes_.size() - 1, i);
      if (!d) break;
      s.sents.push_back(d->yield);
      s.feats.push_back(d->feature_values);
      s.model_scores.push_back(log(d->score));
    }
  }

  void
  KBestNoFilter(const Hypergraph& forest)
  {
    s.sents.clear();
    s.feats.clear();
    s.model_scores.clear();
    s.scores.clear();
    KBest::KBestDerivations<vector<WordID>, ESentenceTraversal> kbest(forest, k_);
    for (size_t i = 0; i < k_; ++i) {
      const KBest::KBestDerivations<vector<WordID>, ESentenceTraversal>::Derivation* d =
            kbest.LazyKthBest(forest.nodes_.size() - 1, i);
      if (!d) break;
      s.sents.push_back(d->yield);
      s.feats.push_back(d->feature_values);
      s.model_scores.push_back(log(d->score));
    }
  }
};


} // namespace

#endif