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

LexicalReordering.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b32b96b0d4b1d4963f8ad04c053e911c8e922fc5 (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
#include <sstream>

#include "FFState.h"
#include "LexicalReordering.h"
#include "LexicalReorderingState.h"
#include "StaticData.h"

namespace Moses
{

LexicalReordering::LexicalReordering(std::vector<FactorType>& f_factors,
                                     std::vector<FactorType>& e_factors,
                                     const LexicalReorderingConfiguration& configuration,
                                     const std::string &filePath,
                                     const std::vector<float>& weights)
  : StatefulFeatureFunction("LexicalReordering_" + configuration.GetModelString(),
                            configuration.GetNumScoreComponents()),
    m_configuration(configuration)
{
  m_configuration.SetScoreProducer(this);
  std::cerr << "Creating lexical reordering...\n";
  std::cerr << "weights: ";
  for(size_t w = 0; w < weights.size(); ++w) {
    std::cerr << weights[w] << " ";
  }
  std::cerr << "\n";

  m_modelTypeString = m_configuration.GetModelString();

  switch(m_configuration.GetCondition()) {
  case LexicalReorderingConfiguration::FE:
  case LexicalReorderingConfiguration::E:
    m_factorsE = e_factors;
    if(m_factorsE.empty()) {
      UserMessage::Add("TL factor mask for lexical reordering is unexpectedly empty");
      exit(1);
    }
    if(m_configuration.GetCondition() == LexicalReorderingConfiguration::E)
      break; // else fall through
  case LexicalReorderingConfiguration::F:
    m_factorsF = f_factors;
    if(m_factorsF.empty()) {
      UserMessage::Add("SL factor mask for lexical reordering is unexpectedly empty");
      exit(1);
    }
    break;
  default:
    UserMessage::Add("Unknown conditioning option!");
    exit(1);
  }

  size_t numberOfScoreComponents = m_configuration.GetNumScoreComponents();
  if (weights.size() > numberOfScoreComponents) {
    m_configuration.SetAdditionalScoreComponents(weights.size() - numberOfScoreComponents);
  } else if(weights.size() < numberOfScoreComponents) {
    std::ostringstream os;
    os << "Lexical reordering model (type " << m_modelTypeString << "): expected " << numberOfScoreComponents << " weights, got " << weights.size() << std::endl;
    UserMessage::Add(os.str());
    exit(1);
  }

  const_cast<StaticData&>(StaticData::Instance()).SetWeights(this, weights);

  m_table = LexicalReorderingTable::LoadAvailable(filePath, m_factorsF, m_factorsE, std::vector<FactorType>());
}

LexicalReordering::~LexicalReordering()
{
  if(m_table)
    delete m_table;
}

Scores LexicalReordering::GetProb(const Phrase& f, const Phrase& e) const
{
  return m_table->GetScore(f, e, Phrase(ARRAY_SIZE_INCR));
}

FFState* LexicalReordering::Evaluate(const Hypothesis& hypo,
                                     const FFState* prev_state,
                                     ScoreComponentCollection* out) const
{
  Scores score(GetNumScoreComponents(), 0);
  const LexicalReorderingState *prev = dynamic_cast<const LexicalReorderingState *>(prev_state);
  LexicalReorderingState *next_state = prev->Expand(hypo.GetTranslationOption(), score);

  out->PlusEquals(this, score);

  return next_state;
}

const FFState* LexicalReordering::EmptyHypothesisState(const InputType &input) const
{
  return m_configuration.CreateLexicalReorderingState(input);
}

}