Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CorrectionPattern.cpp « FF « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9770f7d66989c27118dbfbfbfcadf6cf9c5e40b0 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <sstream>
#include "CorrectionPattern.h"
#include "moses/Phrase.h"
#include "moses/TargetPhrase.h"
#include "moses/InputPath.h"
#include "moses/Hypothesis.h"
#include "moses/ChartHypothesis.h"
#include "moses/ScoreComponentCollection.h"
#include "moses/TranslationOption.h"
#include "util/string_piece_hash.hh"
#include "util/exception.hh"

#include <functional>
#include <algorithm>

#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>

#include "Diffs.h"

namespace Moses
{

using namespace std;

std::string MakePair(const std::string &s1, const std::string &s2, bool general)
{
  std::vector<std::string> sourceList;
  std::vector<std::string> targetList;

  if(general) {
    Diffs diffs = CreateDiff(s1, s2);

    size_t i = 0, j = 0;
    char lastType = 'm';

    std::string source, target;
    std::string match;

    int count = 1;

    BOOST_FOREACH(Diff type, diffs) {
      if(type == 'm') {
        if(lastType != 'm') {
          sourceList.push_back(source);
          targetList.push_back(target);
        }
        source.clear();
        target.clear();

        if(s1[i] == '+') {
          if(match.size() >= 3) {
            sourceList.push_back("(\\w{3,})·");
            std::string temp = "1";
            sprintf((char*)temp.c_str(), "%d", count);
            targetList.push_back("\\" + temp + "·");
            count++;
          } else {
            sourceList.push_back(match + "·");
            targetList.push_back(match + "·");
          }
          match.clear();
        } else
          match.push_back(s1[i]);

        i++;
        j++;
      } else if(type == 'd') {
        if(s1[i] == '+')
          source += "·";
        else
          source.push_back(s1[i]);
        i++;
      } else if(type == 'i') {
        if(s2[j] == '+')
          target += "·";
        else
          target.push_back(s2[j]);
        j++;
      }
      if(type != 'm' && !match.empty()) {
        if(match.size() >= 3) {
          sourceList.push_back("(\\w{3,})");
          std::string temp = "1";
          sprintf((char*)temp.c_str(), "%d", count);
          targetList.push_back("\\" + temp);
          count++;
        } else {
          sourceList.push_back(match);
          targetList.push_back(match);
        }

        match.clear();
      }

      lastType = type;
    }
    if(lastType != 'm') {
      sourceList.push_back(source);
      targetList.push_back(target);
    }

    if(!match.empty()) {
      if(match.size() >= 3) {
        sourceList.push_back("(\\w{3,})");
        std::string temp = "1";
        sprintf((char*)temp.c_str(), "%d", count);
        targetList.push_back("\\"+ temp);
        count++;
      } else {
        sourceList.push_back(match);
        targetList.push_back(match);
      }
    }
    match.clear();
  } else {
    std::string cs1 = s1;
    std::string cs2 = s2;
    boost::replace_all(cs1, "+", "·");
    boost::replace_all(cs2, "+", "·");

    sourceList.push_back(cs1);
    targetList.push_back(cs2);
  }

  std::stringstream out;
  out << "sub(«";
  out << boost::join(sourceList, "");
  out << "»,«";
  out << boost::join(targetList, "");
  out << "»)";

  return out.str();
}

std::string CorrectionPattern::CreateSinglePattern(const Tokens &s1, const Tokens &s2) const
{
  std::stringstream out;
  if(s1.empty()) {
    out << "ins(«" << boost::join(s2, "·") << "»)";
    return out.str();
  } else if(s2.empty()) {
    out << "del(«" << boost::join(s1, "·") << "»)";
    return out.str();
  } else {
    Tokens::value_type v1 = boost::join(s1, "+");
    Tokens::value_type v2 = boost::join(s2, "+");
    out << MakePair(v1, v2, m_general);
    return out.str();
  }
}

std::vector<std::string> GetContext(size_t pos,
                                    size_t len,
                                    size_t window,
                                    const InputType &input,
                                    const InputPath &inputPath,
                                    const std::vector<FactorType>& factorTypes,
                                    bool isRight)
{

  const Sentence& sentence = static_cast<const Sentence&>(input);
  const Range& range = inputPath.GetWordsRange();

  int leftPos  = range.GetStartPos() + pos - len - 1;
  int rightPos = range.GetStartPos() + pos;

  std::vector<std::string> contexts;

  for(int length = 1; length <= (int)window; ++length) {
    std::vector<std::string> current;
    if(!isRight) {
      for(int i = 0; i < length; i++) {
        if(leftPos - i >= 0) {
          current.push_back(sentence.GetWord(leftPos - i).GetString(factorTypes, false));
        } else {
          current.push_back("<s>");
        }
      }

      if(current.back() == "<s>" && current.size() >= 2 && current[current.size()-2] == "<s>")
        continue;

      std::reverse(current.begin(), current.end());
      contexts.push_back("left(«" + boost::join(current, "·") + "»)_");
    }
    if(isRight) {
      for(int i = 0; i < length; i++) {
        if(rightPos + i < (int)sentence.GetSize()) {
          current.push_back(sentence.GetWord(rightPos + i).GetString(factorTypes, false));
        } else {
          current.push_back("</s>");
        }
      }

      if(current.back() == "</s>" && current.size() >= 2 && current[current.size()-2] == "</s>")
        continue;

      contexts.push_back("_right(«" + boost::join(current, "·") + "»)");
    }
  }
  return contexts;
}

std::vector<std::string>
CorrectionPattern::CreatePattern(const Tokens &s1,
                                 const Tokens &s2,
                                 const InputType &input,
                                 const InputPath &inputPath) const
{

  Diffs diffs = CreateDiff(s1, s2);
  size_t i = 0, j = 0;
  char lastType = 'm';
  std::vector<std::string> patternList;
  Tokens source, target;
  BOOST_FOREACH(Diff type, diffs) {
    if(type == 'm') {
      if(lastType != 'm') {
        std::string pattern = CreateSinglePattern(source, target);
        patternList.push_back(pattern);

        if(m_context > 0) {
          std::vector<std::string> leftContexts =  GetContext(i, source.size(), m_context, input, inputPath, m_contextFactors, false);
          std::vector<std::string> rightContexts = GetContext(i, source.size(), m_context, input, inputPath, m_contextFactors, true);

          BOOST_FOREACH(std::string left, leftContexts)
          patternList.push_back(left + pattern);

          BOOST_FOREACH(std::string right, rightContexts)
          patternList.push_back(pattern + right);

          BOOST_FOREACH(std::string left, leftContexts)
          BOOST_FOREACH(std::string right, rightContexts)
          patternList.push_back(left + pattern + right);
        }
      }
      source.clear();
      target.clear();
      if(s1[i] != s2[j]) {
        source.push_back(s1[i]);
        target.push_back(s2[j]);
      }
      i++;
      j++;
    } else if(type == 'd') {
      source.push_back(s1[i]);
      i++;
    } else if(type == 'i') {
      target.push_back(s2[j]);
      j++;
    }
    lastType = type;
  }
  if(lastType != 'm') {
    std::string pattern = CreateSinglePattern(source, target);
    patternList.push_back(pattern);

    if(m_context > 0) {
      std::vector<std::string> leftContexts =  GetContext(i, source.size(), m_context, input, inputPath, m_contextFactors, false);
      std::vector<std::string> rightContexts = GetContext(i, source.size(), m_context, input, inputPath, m_contextFactors, true);

      BOOST_FOREACH(std::string left, leftContexts)
      patternList.push_back(left + pattern);

      BOOST_FOREACH(std::string right, rightContexts)
      patternList.push_back(pattern + right);

      BOOST_FOREACH(std::string left, leftContexts)
      BOOST_FOREACH(std::string right, rightContexts)
      patternList.push_back(left + pattern + right);
    }
  }

  return patternList;
}

CorrectionPattern::CorrectionPattern(const std::string &line)
  : StatelessFeatureFunction(0, line), m_factors(1, 0), m_general(false),
    m_context(0), m_contextFactors(1, 0)
{
  std::cerr << "Initializing correction pattern feature.." << std::endl;
  ReadParameters();
}

void CorrectionPattern::SetParameter(const std::string& key, const std::string& value)
{
  if (key == "factor") {
    m_factors = std::vector<FactorType>(1, Scan<FactorType>(value));
  } else if (key == "context-factor") {
    m_contextFactors = std::vector<FactorType>(1, Scan<FactorType>(value));
  } else if (key == "general") {
    m_general = Scan<bool>(value);
  } else if (key == "context") {
    m_context = Scan<size_t>(value);
  } else {
    StatelessFeatureFunction::SetParameter(key, value);
  }
}

void CorrectionPattern::EvaluateWithSourceContext(const InputType &input
    , const InputPath &inputPath
    , const TargetPhrase &targetPhrase
    , const StackVec *stackVec
    , ScoreComponentCollection &scoreBreakdown
    , ScoreComponentCollection *estimatedFutureScore) const
{
  ComputeFeatures(input, inputPath, targetPhrase, &scoreBreakdown);
}

void CorrectionPattern::ComputeFeatures(
  const InputType &input,
  const InputPath &inputPath,
  const TargetPhrase& target,
  ScoreComponentCollection* accumulator) const
{
  const Phrase &source = inputPath.GetPhrase();

  std::vector<std::string> sourceTokens;
  for(size_t i = 0; i < source.GetSize(); ++i)
    sourceTokens.push_back(source.GetWord(i).GetString(m_factors, false));

  std::vector<std::string> targetTokens;
  for(size_t i = 0; i < target.GetSize(); ++i)
    targetTokens.push_back(target.GetWord(i).GetString(m_factors, false));

  std::vector<std::string> patternList = CreatePattern(sourceTokens, targetTokens, input, inputPath);
  for(size_t i = 0; i < patternList.size(); ++i)
    accumulator->PlusEquals(this, patternList[i], 1);

  /*
  BOOST_FOREACH(std::string w, sourceTokens)
    std::cerr << w << " ";
  std::cerr << std::endl;
  BOOST_FOREACH(std::string w, targetTokens)
    std::cerr << w << " ";
  std::cerr << std::endl;
  BOOST_FOREACH(std::string w, patternList)
    std::cerr << w << " ";
  std::cerr << std::endl << std::endl;
  */
}

bool CorrectionPattern::IsUseable(const FactorMask &mask) const
{
  bool ret = true;
  for(size_t i = 0; i < m_factors.size(); ++i)
    ret = ret && mask[m_factors[i]];
  for(size_t i = 0; i < m_contextFactors.size(); ++i)
    ret = ret && mask[m_contextFactors[i]];
  return ret;
}

}