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

Sentence.cpp « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 98bfb9e0a75de996337df743905639f3cb3f5e5a (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// $Id$
// vim:tabstop=2

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

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

#include "Sentence.h"
#include "TranslationOptionCollectionText.h"
#include "StaticData.h"
#include "moses/FF/DynamicCacheBasedLanguageModel.h"
#include "moses/TranslationModel/PhraseDictionaryDynamicCacheBased.h"
#include "ChartTranslationOptions.h"
#include "Util.h"
#include "XmlOption.h"
#include "FactorCollection.h"
#include "TranslationTask.h"

using namespace std;

namespace Moses
{

Sentence::
Sentence(AllOptions::ptr const& opts) : Phrase(0) , InputType(opts)
{
  if (is_syntax(opts->search.algo))
    m_defaultLabelSet.insert(opts->syntax.input_default_non_terminal);
}

Sentence::
~Sentence()
{
  RemoveAllInColl(m_xmlOptions);
}

void
Sentence::
aux_init_partial_translation(string& line)
{
  string sourceCompletedStr;
  int loc1 = line.find( "|||", 0 );
  int loc2 = line.find( "|||", loc1 + 3 );
  if (loc1 > -1 && loc2 > -1) {
    m_initialTargetPhrase = Trim(line.substr(0, loc1));
    string scov = Trim(line.substr(loc1 + 3, loc2 - loc1 - 3));
    line = line.substr(loc2 + 3);

    m_sourceCompleted.resize(scov.size());
    int contiguous = 1;
    for (size_t i = 0; i < scov.size(); ++i) {
      if (sourceCompletedStr.at(i) == '1') {
        m_sourceCompleted[i] = true;
        if (contiguous) m_frontSpanCoveredLength++;
      } else {
        m_sourceCompleted[i] = false;
        contiguous = 0;
      }
    }
  }
}

void
Sentence::
aux_interpret_sgml_markup(string& line)
{
  // if sentences is specified as "<seg id=1> ... </seg>", extract id
  typedef std::map<std::string, std::string> metamap;
  metamap meta = ProcessAndStripSGML(line);
  metamap::const_iterator i;
  if ((i = meta.find("id")) != meta.end())
    this->SetTranslationId(atol(i->second.c_str()));
  if ((i = meta.find("docid")) != meta.end()) {
    this->SetDocumentId(atol(i->second.c_str()));
    this->SetUseTopicId(false);
    this->SetUseTopicIdAndProb(false);
  }
  if ((i = meta.find("topic")) != meta.end()) {
    vector<string> topic_params;
    boost::split(topic_params, i->second, boost::is_any_of("\t "));
    if (topic_params.size() == 1) {
      this->SetTopicId(atol(topic_params[0].c_str()));
      this->SetUseTopicId(true);
      this->SetUseTopicIdAndProb(false);
    } else {
      this->SetTopicIdAndProb(topic_params);
      this->SetUseTopicId(false);
      this->SetUseTopicIdAndProb(true);
    }
  }
  if ((i = meta.find("weight-setting")) != meta.end()) {
    this->SetWeightSetting(i->second);
    this->SetSpecifiesWeightSetting(true);
    StaticData::Instance().SetWeightSetting(i->second);
    // oh this is so horrible! Why does this have to be propagated globally?
    // --- UG
  } else this->SetSpecifiesWeightSetting(false);
}

void
Sentence::
aux_interpret_dlt(string& line) // whatever DLT means ... --- UG
{
  using namespace std;
  typedef map<string, string> str2str_map;
  m_dlt_meta = ProcessAndStripDLT(line);
  // what's happening below is most likely not thread-safe! UG
  BOOST_FOREACH(str2str_map const& M, m_dlt_meta) {
    str2str_map::const_iterator i,j;
    if ((i = M.find("type")) != M.end()) {
      j = M.find("id");
      string id = j == M.end() ? "default" : j->second;
      if (i->second == "cbtm") {
        PhraseDictionaryDynamicCacheBased* cbtm;
        cbtm = PhraseDictionaryDynamicCacheBased::InstanceNonConst(id);
        if (cbtm) cbtm->ExecuteDlt(M);
      }
      if (i->second == "cblm") {
        DynamicCacheBasedLanguageModel* cblm;
        cblm = DynamicCacheBasedLanguageModel::InstanceNonConst(id);
        if (cblm) cblm->ExecuteDlt(M);
      }
    }
  }
}

void
Sentence::
aux_interpret_xml(std::string& line, std::vector<size_t> & xmlWalls,
                  std::vector<std::pair<size_t, std::string> >& placeholders)
{
  // parse XML markup in translation line
  using namespace std;
  if (m_options->input.xml_policy != XmlPassThrough) {
    bool OK = ProcessAndStripXMLTags(*m_options, line,
                                     m_xmlOptions,
                                     m_reorderingConstraint,
                                     xmlWalls, placeholders);
    if (!OK) {
      TRACE_ERR("Unable to parse XML in line: " << line);
    }
  }
}

void
Sentence::
init(string line)
{
  using namespace std;

  m_frontSpanCoveredLength = 0;
  m_sourceCompleted.resize(0);

  if (m_options->input.continue_partial_translation)
    aux_init_partial_translation(line);

  line = Trim(line);
  aux_interpret_sgml_markup(line); // for "<seg id=..." markup
  aux_interpret_dlt(line); // some poorly documented cache-based stuff

  // if sentences is specified as "<passthrough tag1=""/>"
  if (m_options->output.PrintPassThrough ||m_options->nbest.include_passthrough) {
    string pthru = PassthroughSGML(line,"passthrough");
    this->SetPassthroughInformation(pthru);
  }

  vector<size_t> xmlWalls;
  vector<pair<size_t, string> >placeholders;
  aux_interpret_xml(line, xmlWalls, placeholders);

  Phrase::CreateFromString(Input, m_options->input.factor_order, line, NULL);

  ProcessPlaceholders(placeholders);

  if (is_syntax(m_options->search.algo))
    InitStartEndWord();

  // now that we have final word positions in phrase (from
  // CreateFromString), we can make input phrase objects to go with
  // our XmlOptions and create TranslationOptions

  // only fill the vector if we are parsing XML
  if (m_options->input.xml_policy != XmlPassThrough) {
    m_xmlCoverageMap.assign(GetSize(), false);
    BOOST_FOREACH(XmlOption const* o, m_xmlOptions) {
      Range const& r = o->range;
      for(size_t j = r.GetStartPos(); j <= r.GetEndPos(); ++j)
        m_xmlCoverageMap[j]=true;
    }
  }

  // reordering walls and zones
  m_reorderingConstraint.InitializeWalls(GetSize());

  // set reordering walls, if "-monotone-at-punction" is set
  if (m_options->reordering.monotone_at_punct && GetSize()) {
    Range r(0, GetSize()-1);
    m_reorderingConstraint.SetMonotoneAtPunctuation(GetSubString(r));
  }

  // set walls obtained from xml
  for(size_t i=0; i<xmlWalls.size(); i++)
    if(xmlWalls[i] < GetSize()) // no buggy walls, please
      m_reorderingConstraint.SetWall(xmlWalls[i], true);
  m_reorderingConstraint.FinalizeWalls();

}

int
Sentence::
Read(std::istream& in)
{
  std::string line;
  if (getline(in, line, '\n').eof())
    return 0;
  init(line);
  return 1;
}

void
Sentence::
ProcessPlaceholders(const std::vector< std::pair<size_t, std::string> > &placeholders)
{
  FactorType placeholderFactor = m_options->input.placeholder_factor;
  if (placeholderFactor == NOT_FOUND) {
    return;
  }

  for (size_t i = 0; i < placeholders.size(); ++i) {
    size_t pos = placeholders[i].first;
    const string &str = placeholders[i].second;
    const Factor *factor = FactorCollection::Instance().AddFactor(str);
    Word &word = Phrase::GetWord(pos);
    word[placeholderFactor] = factor;
  }
}

TranslationOptionCollection*
Sentence::
CreateTranslationOptionCollection(ttasksptr const& ttask) const
{
  TranslationOptionCollection *rv
  = new TranslationOptionCollectionText(ttask, *this);
  assert(rv);
  return rv;
}
void Sentence::Print(std::ostream& out) const
{
  out<<*static_cast<Phrase const*>(this);
}


bool Sentence::XmlOverlap(size_t startPos, size_t endPos) const
{
  for (size_t pos = startPos; pos <=  endPos ; pos++) {
    if (pos < m_xmlCoverageMap.size() && m_xmlCoverageMap[pos]) {
      return true;
    }
  }
  return false;
}

void Sentence::GetXmlTranslationOptions(std::vector <TranslationOption*> &list) const
{
  for (std::vector<XmlOption const*>::const_iterator iterXMLOpts = m_xmlOptions.begin();
       iterXMLOpts != m_xmlOptions.end(); ++iterXMLOpts) {
    const XmlOption &xmlOption = **iterXMLOpts;
    const Range &range = xmlOption.range;
    const TargetPhrase &targetPhrase = xmlOption.targetPhrase;
    TranslationOption *transOpt = new TranslationOption(range, targetPhrase);
    list.push_back(transOpt);
  }
}

void Sentence::GetXmlTranslationOptions(std::vector <TranslationOption*> &list, size_t startPos, size_t endPos) const
{
  //iterate over XmlOptions list, find exact source/target matches

  for (std::vector<XmlOption const*>::const_iterator iterXMLOpts = m_xmlOptions.begin();
       iterXMLOpts != m_xmlOptions.end(); ++iterXMLOpts) {
    const XmlOption &xmlOption = **iterXMLOpts;
    const Range &range = xmlOption.range;

    if (startPos == range.GetStartPos()
        && endPos == range.GetEndPos()) {
      const TargetPhrase &targetPhrase = xmlOption.targetPhrase;

      TranslationOption *transOpt = new TranslationOption(range, targetPhrase);
      list.push_back(transOpt);
    }
  }
}

std::vector <ChartTranslationOptions*>
Sentence::
GetXmlChartTranslationOptions() const
{
  std::vector <ChartTranslationOptions*> ret;

  // XML Options
  // this code is a copy of the 1 in Sentence.

  //only fill the vector if we are parsing XML
  if (m_options->input.xml_policy != XmlPassThrough ) {
    //TODO: needed to handle exclusive
    //for (size_t i=0; i<GetSize(); i++) {
    //  m_xmlCoverageMap.push_back(false);
    //}

    //iterXMLOpts will be empty for XmlIgnore
    //look at each column
    for(std::vector<XmlOption const*>::const_iterator iterXmlOpts = m_xmlOptions.begin();
        iterXmlOpts != m_xmlOptions.end(); iterXmlOpts++) {

      const XmlOption &xmlOption = **iterXmlOpts;
      TargetPhrase *targetPhrase = new TargetPhrase(xmlOption.targetPhrase);

      Range *range = new Range(xmlOption.range);
      StackVec emptyStackVec; // hmmm... maybe dangerous, but it is never consulted

      TargetPhraseCollection *tpc = new TargetPhraseCollection;
      tpc->Add(targetPhrase);

      ChartTranslationOptions *transOpt = new ChartTranslationOptions(*tpc, emptyStackVec, *range, 0.0f);
      ret.push_back(transOpt);

      //TODO: needed to handle exclusive
      //for(size_t j=transOpt->GetSourceWordsRange().GetStartPos(); j<=transOpt->GetSourceWordsRange().GetEndPos(); j++) {
      //  m_xmlCoverageMap[j]=true;
      //}
    }
  }

  return ret;
}

void
Sentence::
CreateFromString(vector<FactorType> const& FOrder, string const& phraseString)
{
  Phrase::CreateFromString(Input, FOrder, phraseString, NULL);
}

Sentence::
Sentence(AllOptions::ptr const& opts, size_t const transId, string stext)
  : InputType(opts, transId)
{
  init(stext);
}

}