summaryrefslogtreecommitdiff
path: root/decoder/kbest.h
blob: 9a55f653108e0ea38090b0de05d2b78b2da8aed9 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef _HG_KBEST_H_
#define _HG_KBEST_H_

#include <vector>
#include <utility>
#include <tr1/unordered_set>

#include <boost/shared_ptr.hpp>

#include "wordid.h"
#include "hg.h"

namespace KBest {
  // default, don't filter any derivations from the k-best list
  template<typename Dummy>
  struct NoFilter {
    bool operator()(const Dummy&) {
      return false;
    }
  };

  // optional, filter unique yield strings
  struct FilterUnique {
    std::tr1::unordered_set<std::vector<WordID>, boost::hash<std::vector<WordID> > > unique;

    bool operator()(const std::vector<WordID>& yield) {
      return !unique.insert(yield).second;
    }
  };

  // utility class to lazily create the k-best derivations from a forest, uses
  // the lazy k-best algorithm (Algorithm 3) from Huang and Chiang (IWPT 2005)
  template<typename T,  // yield type (returned by Traversal)
           typename Traversal,
           typename DerivationFilter = NoFilter<T>,
           typename WeightType = prob_t,
           typename WeightFunction = EdgeProb>
  struct KBestDerivations {
    KBestDerivations(const Hypergraph& hg,
                     const size_t k,
                     const Traversal& tf = Traversal(),
                     const WeightFunction& wf = WeightFunction()) :
      traverse(tf), w(wf), g(hg), nds(g.nodes_.size()), k_prime(k) {}

    ~KBestDerivations() {
      for (unsigned i = 0; i < freelist.size(); ++i)
        delete freelist[i];
    }

    struct Derivation {
      Derivation(const HG::Edge& e,
                 const SmallVectorInt& jv,
                 const WeightType& w,
                 const SparseVector<double>& f) :
        edge(&e),
        j(jv),
        score(w),
        feature_values(f) {}

      // dummy constructor, just for query
      Derivation(const HG::Edge& e,
                 const SmallVectorInt& jv) : edge(&e), j(jv) {}

      T yield;
      const HG::Edge* const edge;
      const SmallVectorInt j;
      const WeightType score;
      const SparseVector<double> feature_values;
    };
    struct HeapCompare {
      bool operator()(const Derivation* a, const Derivation* b) const {
        return a->score < b->score;
      }
    };
    struct DerivationCompare {
      bool operator()(const Derivation* a, const Derivation* b) const {
        return a->score > b->score;
      }
    };

    struct EdgeHandle {
      Derivation const* d;
      explicit EdgeHandle(Derivation const* d) : d(d) {  }
//      operator bool() const { return d->edge; }
      operator HG::Edge const* () const { return d->edge; }
//      HG::Edge const * operator ->() const { return d->edge; }
    };

    EdgeHandle operator()(unsigned t,unsigned taili,EdgeHandle const& parent) const {
      return EdgeHandle(nds[t].D[parent.d->j[taili]]);
    }

    std::string derivation_tree(Derivation const& d,bool indent=true,int show_mask=Hypergraph::SPAN|Hypergraph::RULE,int maxdepth=0x7FFFFFFF,int depth=0) const {
      return d.edge->derivation_tree(*this,EdgeHandle(&d),indent,show_mask,maxdepth,depth);
    }

    struct DerivationUniquenessHash {
      size_t operator()(const Derivation* d) const {
        size_t x = 5381;
        x = ((x << 5) + x) ^ d->edge->id_;
        for (unsigned i = 0; i < d->j.size(); ++i)
          x = ((x << 5) + x) ^ d->j[i];
        return x;
      }
    };
    struct DerivationUniquenessEquals {
      bool operator()(const Derivation* a, const Derivation* b) const {
        return (a->edge == b->edge) && (a->j == b->j);
      }
    };
    typedef std::vector<Derivation*> CandidateHeap;
    typedef std::vector<Derivation*> DerivationList;
    typedef std::tr1::unordered_set<
       const Derivation*, DerivationUniquenessHash, DerivationUniquenessEquals> UniqueDerivationSet;

    struct NodeDerivationState {
      CandidateHeap cand;
      DerivationList D;
      DerivationFilter filter;
      UniqueDerivationSet ds;
      explicit NodeDerivationState(const DerivationFilter& f = DerivationFilter()) : filter(f) {}
    };

    Derivation* LazyKthBest(unsigned v, unsigned k) {
      NodeDerivationState& s = GetCandidates(v);
      CandidateHeap& cand = s.cand;
      DerivationList& D = s.D;
      DerivationFilter& filter = s.filter;
      bool add_next = true;
      while (D.size() <= k) {
        if (add_next && D.size() > 0) {
          const Derivation* d = D.back();
          LazyNext(d, &cand, &s.ds);
        }
        add_next = false;

        if (cand.size() > 0) {
          std::pop_heap(cand.begin(), cand.end(), HeapCompare());
          Derivation* d = cand.back();
          cand.pop_back();
          std::vector<const T*> ants(d->edge->Arity());
          for (unsigned j = 0; j < ants.size(); ++j)
            ants[j] = &LazyKthBest(d->edge->tail_nodes_[j], d->j[j])->yield;
          traverse(*d->edge, ants, &d->yield);
          if (!filter(d->yield)) {
            D.push_back(d);
            add_next = true;
          }
        } else {
          break;
        }
      }
      if (k < D.size()) return D[k]; else return NULL;
    }

  private:
    // creates a derivation object with all fields set but the yield
    // the yield is computed in LazyKthBest before the derivation is added to D
    // returns NULL if j refers to derivation numbers larger than the
    // antecedent structure define
    Derivation* CreateDerivation(const HG::Edge& e, const SmallVectorInt& j) {
      WeightType score = w(e);
      SparseVector<double> feats = e.feature_values_;
      for (int i = 0; i < e.Arity(); ++i) {
        const Derivation* ant = LazyKthBest(e.tail_nodes_[i], j[i]);
        if (!ant) { return NULL; }
        score *= ant->score;
        feats += ant->feature_values;
      }
      freelist.push_back(new Derivation(e, j, score, feats));
      return freelist.back();
    }

    NodeDerivationState& GetCandidates(unsigned v) {
      NodeDerivationState& s = nds[v];
      if (!s.D.empty() || !s.cand.empty()) return s;

      const Hypergraph::Node& node = g.nodes_[v];
      for (unsigned i = 0; i < node.in_edges_.size(); ++i) {
        const HG::Edge& edge = g.edges_[node.in_edges_[i]];
        SmallVectorInt jv(edge.Arity(), 0);
        Derivation* d = CreateDerivation(edge, jv);
        assert(d);
        s.cand.push_back(d);
      }

      const unsigned effective_k = std::min(k_prime, s.cand.size());
      const typename CandidateHeap::iterator kth = s.cand.begin() + effective_k;
      std::nth_element(s.cand.begin(), kth, s.cand.end(), DerivationCompare());
      s.cand.resize(effective_k);
      std::make_heap(s.cand.begin(), s.cand.end(), HeapCompare());

      return s;
    }

    void LazyNext(const Derivation* d, CandidateHeap* cand, UniqueDerivationSet* ds) {
      for (unsigned i = 0; i < d->j.size(); ++i) {
        SmallVectorInt j = d->j;
        ++j[i];
        const Derivation* ant = LazyKthBest(d->edge->tail_nodes_[i], j[i]);
        if (ant) {
          Derivation query_unique(*d->edge, j);
          if (ds->count(&query_unique) == 0) {
            Derivation* new_d = CreateDerivation(*d->edge, j);
            if (new_d) {
              cand->push_back(new_d);
              std::push_heap(cand->begin(), cand->end(), HeapCompare());
#ifdef NDEBUG
              ds->insert(new_d).second;  // insert into uniqueness set
#else
              bool inserted = ds->insert(new_d).second;  // insert into uniqueness set
              assert(inserted);
#endif
            }
          }
        }
      }
    }

    const Traversal traverse;
    const WeightFunction w;
    const Hypergraph& g;
    std::vector<NodeDerivationState> nds;
    std::vector<Derivation*> freelist;
    const size_t k_prime;
  };
}

#endif