summaryrefslogtreecommitdiff
path: root/gi/pf/base_distributions.h
blob: 41b513f89687c8e8b4e09818d10ef9eb2112a5f0 (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
230
231
232
233
234
235
236
237
238
#ifndef _BASE_MEASURES_H_
#define _BASE_MEASURES_H_

#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <iostream>
#include <cassert>

#include "unigrams.h"
#include "trule.h"
#include "prob.h"
#include "tdict.h"
#include "sampler.h"
#include "m.h"
#include "os_phrase.h"

struct Model1 {
  explicit Model1(const std::string& fname) :
      kNULL(TD::Convert("<eps>")),
      kZERO() {
    LoadModel1(fname);
  }

  void LoadModel1(const std::string& fname);

  // returns prob 0 if src or trg is not found
  const prob_t& operator()(WordID src, WordID trg) const {
    if (src == 0) src = kNULL;
    if (src < ttable.size()) {
      const std::map<WordID, prob_t>& cpd = ttable[src];
      const std::map<WordID, prob_t>::const_iterator it = cpd.find(trg);
      if (it != cpd.end())
        return it->second;
    }
    return kZERO;
  }

  const WordID kNULL;
  const prob_t kZERO;
  std::vector<std::map<WordID, prob_t> > ttable;
};

struct PoissonUniformUninformativeBase {
  explicit PoissonUniformUninformativeBase(const unsigned ves) : kUNIFORM(1.0 / ves) {}
  prob_t operator()(const TRule& r) const {
    prob_t p; p.logeq(Md::log_poisson(r.e_.size(), 1.0));
    prob_t q = kUNIFORM; q.poweq(r.e_.size());
    p *= q;
    return p;
  }
  void Summary() const {}
  void ResampleHyperparameters(MT19937*) {}
  void Increment(const TRule&) {}
  void Decrement(const TRule&) {}
  prob_t Likelihood() const { return prob_t::One(); }
  const prob_t kUNIFORM;
};

struct CompletelyUniformBase {
  explicit CompletelyUniformBase(const unsigned ves) : kUNIFORM(1.0 / ves) {}
  prob_t operator()(const TRule&) const {
    return kUNIFORM;
  }
  void Summary() const {}
  void ResampleHyperparameters(MT19937*) {}
  void Increment(const TRule&) {}
  void Decrement(const TRule&) {}
  prob_t Likelihood() const { return prob_t::One(); }
  const prob_t kUNIFORM;
};

struct UnigramWordBase {
  explicit UnigramWordBase(const std::string& fname) : un(fname) {}
  prob_t operator()(const TRule& r) const {
    return un(r.e_);
  }
  const UnigramWordModel un;
};

struct RuleHasher {
  size_t operator()(const TRule& r) const {
    return hash_value(r);
  }
};

struct TableLookupBase {
  TableLookupBase(const std::string& fname);

  prob_t operator()(const TRule& rule) const {
    const std::tr1::unordered_map<TRule,prob_t,RuleHasher>::const_iterator it = table.find(rule);
    if (it == table.end()) {
      std::cerr << rule << " not found\n";
      abort();
    }
    return it->second;
  }

  void ResampleHyperparameters(MT19937*) {}
  void Increment(const TRule&) {}
  void Decrement(const TRule&) {}
  prob_t Likelihood() const { return prob_t::One(); }
  void Summary() const {}

  std::tr1::unordered_map<TRule,prob_t,RuleHasher> table;
};

struct PhraseConditionalUninformativeBase {
  explicit PhraseConditionalUninformativeBase(const unsigned vocab_e_size) :
      kUNIFORM_TARGET(1.0 / vocab_e_size) {
    assert(vocab_e_size > 0);
  }

  // return p0 of rule.e_ | rule.f_
  prob_t operator()(const TRule& rule) const {
    return p0(rule.f_, rule.e_, 0, 0);
  }

  prob_t p0(const std::vector<WordID>& vsrc, const std::vector<WordID>& vtrg, int start_src, int start_trg) const;

  void Summary() const {}
  void ResampleHyperparameters(MT19937*) {}
  void Increment(const TRule&) {}
  void Decrement(const TRule&) {}
  prob_t Likelihood() const { return prob_t::One(); }
  const prob_t kUNIFORM_TARGET;
};

struct PhraseConditionalUninformativeUnigramBase {
  explicit PhraseConditionalUninformativeUnigramBase(const std::string& file, const unsigned vocab_e_size) : u(file, vocab_e_size) {}

  // return p0 of rule.e_ | rule.f_
  prob_t operator()(const TRule& rule) const {
    return p0(rule.f_, rule.e_, 0, 0);
  }

  prob_t p0(const std::vector<WordID>& vsrc, const std::vector<WordID>& vtrg, int start_src, int start_trg) const;

  const UnigramModel u;
};

struct PhraseConditionalBase {
  explicit PhraseConditionalBase(const Model1& m1, const double m1mixture, const unsigned vocab_e_size) :
      model1(m1),
      kM1MIXTURE(m1mixture),
      kUNIFORM_MIXTURE(1.0 - m1mixture),
      kUNIFORM_TARGET(1.0 / vocab_e_size) {
    assert(m1mixture >= 0.0 && m1mixture <= 1.0);
    assert(vocab_e_size > 0);
  }

  // return p0 of rule.e_ | rule.f_
  prob_t operator()(const TRule& rule) const {
    return p0(rule.f_, rule.e_, 0, 0);
  }

  prob_t p0(const std::vector<WordID>& vsrc, const std::vector<WordID>& vtrg, int start_src, int start_trg) const;

  const Model1& model1;
  const prob_t kM1MIXTURE;  // Model 1 mixture component
  const prob_t kUNIFORM_MIXTURE; // uniform mixture component
  const prob_t kUNIFORM_TARGET;
};

struct PhraseJointBase {
  explicit PhraseJointBase(const Model1& m1, const double m1mixture, const unsigned vocab_e_size, const unsigned vocab_f_size) :
      model1(m1),
      kM1MIXTURE(m1mixture),
      kUNIFORM_MIXTURE(1.0 - m1mixture),
      kUNIFORM_SOURCE(1.0 / vocab_f_size),
      kUNIFORM_TARGET(1.0 / vocab_e_size) {
    assert(m1mixture >= 0.0 && m1mixture <= 1.0);
    assert(vocab_e_size > 0);
  }

  // return p0 of rule.e_ , rule.f_
  prob_t operator()(const TRule& rule) const {
    return p0(rule.f_, rule.e_, 0, 0);
  }

  prob_t p0(const std::vector<WordID>& vsrc, const std::vector<WordID>& vtrg, int start_src, int start_trg) const;

  const Model1& model1;
  const prob_t kM1MIXTURE;  // Model 1 mixture component
  const prob_t kUNIFORM_MIXTURE; // uniform mixture component
  const prob_t kUNIFORM_SOURCE;
  const prob_t kUNIFORM_TARGET;
};

struct PhraseJointBase_BiDir {
  explicit PhraseJointBase_BiDir(const Model1& m1,
                                 const Model1& im1,
                                 const double m1mixture,
                                 const unsigned vocab_e_size,
                                 const unsigned vocab_f_size) :
      model1(m1),
      invmodel1(im1),
      kM1MIXTURE(m1mixture),
      kUNIFORM_MIXTURE(1.0 - m1mixture),
      kUNIFORM_SOURCE(1.0 / vocab_f_size),
      kUNIFORM_TARGET(1.0 / vocab_e_size) {
    assert(m1mixture >= 0.0 && m1mixture <= 1.0);
    assert(vocab_e_size > 0);
  }

  // return p0 of rule.e_ , rule.f_
  prob_t operator()(const TRule& rule) const {
    return p0(rule.f_, rule.e_, 0, 0);
  }

  prob_t p0(const std::vector<WordID>& vsrc, const std::vector<WordID>& vtrg, int start_src, int start_trg) const;

  const Model1& model1;
  const Model1& invmodel1;
  const prob_t kM1MIXTURE;  // Model 1 mixture component
  const prob_t kUNIFORM_MIXTURE; // uniform mixture component
  const prob_t kUNIFORM_SOURCE;
  const prob_t kUNIFORM_TARGET;
};

// base distribution for jump size multinomials
// basically p(0) = 0 and then, p(1) is max, and then
// you drop as you move to the max jump distance
struct JumpBase {
  JumpBase();

  const prob_t& operator()(int jump, unsigned src_len) const {
    assert(jump != 0);
    const std::map<int, prob_t>::const_iterator it = p[src_len].find(jump);
    assert(it != p[src_len].end());
    return it->second;
  }
  std::vector<std::map<int, prob_t> > p;
};


#endif