summaryrefslogtreecommitdiff
path: root/decoder/ff_ruleshape.cc
blob: 35b41c4635efdbb7be863c82ca5ab363658099b2 (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
239
240
241
242
243
244
#include "ff_ruleshape.h"

#include "filelib.h"
#include "stringlib.h"
#include "verbose.h"
#include "trule.h"
#include "hg.h"
#include "fdict.h"
#include <sstream>

using namespace std;

inline bool IsBitSet(int i, int bit) {
  const int mask = 1 << bit;
  return (i & mask);
}

inline char BitAsChar(bool bit) {
  return (bit ? '1' : '0');
}

RuleShapeFeatures::RuleShapeFeatures(const string& /* param */) {
  bool first = true;
  for (int i = 0; i < 32; ++i) {
    for (int j = 0; j < 32; ++j) {
      ostringstream os;
      os << "Shape_S";
      Node* cur = &fidtree_;
      for (int k = 0; k < 5; ++k) {
        bool bit = IsBitSet(i,k);
        cur = &cur->next_[bit];
        os << BitAsChar(bit);
      }
      os << "_T";
      for (int k = 0; k < 5; ++k) {
        bool bit = IsBitSet(j,k);
        cur = &cur->next_[bit];
        os << BitAsChar(bit);
      }
      if (first) { first = false; cerr << "  Example feature: " << os.str() << endl; }
      cur->fid_ = FD::Convert(os.str());
    }
  }
}

void RuleShapeFeatures::TraversalFeaturesImpl(const SentenceMetadata& /* smeta */,
                                              const Hypergraph::Edge& edge,
                                              const vector<const void*>& /* ant_contexts */,
                                              SparseVector<double>* features,
                                              SparseVector<double>* /* estimated_features */,
                                              void* /* context */) const {
  const Node* cur = &fidtree_;
  TRule& rule = *edge.rule_;
  int pos = 0;  // feature position
  int i = 0;
  while(i < rule.f_.size()) {
    WordID sym = rule.f_[i];
    if (pos % 2 == 0) {
      if (sym > 0) {       // is terminal
        cur = Advance(cur, true);
        while (i < rule.f_.size() && rule.f_[i] > 0) ++i;  // consume lexical string
      } else {
        cur = Advance(cur, false);
      }
      ++pos;
    } else {  // expecting a NT
      if (sym < 1) {
        cur = Advance(cur, true);
        ++i;
        ++pos;
      } else {
        cerr << "BAD RULE: " << rule.AsString() << endl;
        exit(1);
      }
    }
  }
  for (; pos < 5; ++pos)
    cur = Advance(cur, false);
  assert(pos == 5);  // this will fail if you are using using > binary rules!

  i = 0;
  while(i < rule.e_.size()) {
    WordID sym = rule.e_[i];
    if (pos % 2 == 1) {
      if (sym > 0) {       // is terminal
        cur = Advance(cur, true);
        while (i < rule.e_.size() && rule.e_[i] > 0) ++i;  // consume lexical string
      } else {
        cur = Advance(cur, false);
      }
      ++pos;
    } else {  // expecting a NT
      if (sym < 1) {
        cur = Advance(cur, true);
        ++i;
        ++pos;
      } else {
        cerr << "BAD RULE: " << rule.AsString() << endl;
        exit(1);
      }
    }
  }
  for (;pos < 10; ++pos)
    cur = Advance(cur, false);
  assert(pos == 10);  // this will fail if you are using using > binary rules!

  features->set_value(cur->fid_, 1.0);
}

namespace {
void ParseRSArgs(string const& in, string* emapfile, string* fmapfile, unsigned *pfxsize) {
  vector<string> const& argv=SplitOnWhitespace(in);
  *emapfile = "";
  *fmapfile = "";
  *pfxsize = 0;
#define RSSPEC_NEXTARG if (i==argv.end()) {            \
    cerr << "Missing argument for "<<*last<<". "; goto usage; \
    } else { ++i; }

  for (vector<string>::const_iterator last,i=argv.begin(),e=argv.end();i!=e;++i) {
    string const& s=*i;
    if (s[0]=='-') {
      if (s.size()>2) goto fail;
      switch (s[1]) {
      case 'e':
        if (emapfile->size() > 0) { cerr << "Multiple -e specifications!\n"; abort(); }
        RSSPEC_NEXTARG; *emapfile=*i;
        break;
      case 'f':
        if (fmapfile->size() > 0) { cerr << "Multiple -f specifications!\n"; abort(); }
        RSSPEC_NEXTARG; *fmapfile=*i;
        break;
      case 'p':
        RSSPEC_NEXTARG; *pfxsize=atoi(i->c_str());
        break;
#undef RSSPEC_NEXTARG
      default:
      fail:
        cerr<<"Unknown RuleShape2 option "<<s<<" ; ";
        goto usage;
      }
    } else {
      cerr << "RuleShape2 bad argument!\n";
      abort();
    }
  }
  return;
usage:
  cerr << "Bad parameters for RuleShape2\n";
  abort();
}

inline void AddWordToClassMapping_(vector<WordID>* pv, unsigned f, unsigned t, unsigned pfx_size) {
  if (pfx_size) {
    const string& ts = TD::Convert(t);
    if (pfx_size < ts.size())
      t = TD::Convert(ts.substr(0, pfx_size));
  }
  if (f >= pv->size())
    pv->resize((f + 1) * 1.2);
  (*pv)[f] = t;
}
}

RuleShapeFeatures2::~RuleShapeFeatures2() {}

RuleShapeFeatures2::RuleShapeFeatures2(const string& param) : kNT(TD::Convert("NT")), kUNK(TD::Convert("<unk>")) {
  string emap;
  string fmap;
  unsigned pfxsize = 0;
  ParseRSArgs(param, &emap, &fmap, &pfxsize);
  has_src_ = fmap.size();
  has_trg_ = emap.size();
  if (has_trg_) LoadWordClasses(emap, pfxsize, &e2class_);
  if (has_src_) LoadWordClasses(fmap, pfxsize, &f2class_);
  if (!has_trg_ && !has_src_) {
    cerr << "RuleShapeFeatures2 requires [-e trg_map.gz] or [-f src_map.gz] or both, and optional [-p pfxsize]\n";
    abort();
  }
}

void RuleShapeFeatures2::LoadWordClasses(const string& file, const unsigned pfx_size, vector<WordID>* pv) {
  ReadFile rf(file);
  istream& in = *rf.stream();
  string line;
  vector<WordID> dummy;
  int lc = 0;
  if (!SILENT)
    cerr << "  Loading word classes from " << file << " ...\n";
  AddWordToClassMapping_(pv, TD::Convert("<s>"), TD::Convert("<s>"), 0);
  AddWordToClassMapping_(pv, TD::Convert("</s>"), TD::Convert("</s>"), 0);
  while(getline(in, line)) {
    dummy.clear();
    TD::ConvertSentence(line, &dummy);
    ++lc;
    if (dummy.size() != 2 && dummy.size() != 3) {
      cerr << "    Class map file expects: CLASS WORD [freq]\n";
      cerr << "    Format error in " << file << ", line " << lc << ": " << line << endl;
      abort();
    }
    AddWordToClassMapping_(pv, dummy[1], dummy[0], pfx_size);
  }
  if (!SILENT)
    cerr << "  Loaded word " << lc << " mapping rules.\n";
}

void RuleShapeFeatures2::TraversalFeaturesImpl(const SentenceMetadata& /* smeta */,
                                               const Hypergraph::Edge& edge,
                                               const vector<const void*>& /* ant_contexts */,
                                               SparseVector<double>* features,
                                               SparseVector<double>* /* estimated_features */,
                                               void* /* context */) const {
  const vector<int>& f = edge.rule_->f();
  const vector<int>& e = edge.rule_->e();
  Node* fid = &fidtree_;
  if (has_src_) {
    for (unsigned i = 0; i < f.size(); ++i)
      fid = &fid->next_[MapF(f[i])];
  }
  if (has_trg_) {
    for (unsigned i = 0; i < e.size(); ++i)
      fid = &fid->next_[MapE(e[i])];
  }
  if (!fid->fid_) {
    ostringstream os;
    os << "RS:";
    if (has_src_) {
      for (unsigned i = 0; i < f.size(); ++i) {
        if (i) os << '_';
        os << TD::Convert(MapF(f[i]));
      }
      if (has_trg_) os << "__";
    }
    if (has_trg_) {
      for (unsigned i = 0; i < e.size(); ++i) {
        if (i) os << '_';
        os << TD::Convert(MapE(e[i]));
      }
    }
    fid->fid_ = FD::Convert(os.str());
  }
  features->set_value(fid->fid_, 1);
}