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
|
#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 std::string &modelType,
const std::string &filePath,
const std::vector<float>& weights)
: m_configuration(this, modelType)
{
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 = modelType;
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);
}
if(weights.size() != m_configuration.GetNumScoreComponents()) {
std::ostringstream os;
os << "Lexical reordering model (type " << modelType << "): expected " << m_numScoreComponents << " weights, got " << weights.size() << std::endl;
UserMessage::Add(os.str());
exit(1);
}
// add ScoreProducer - don't do this before our object is set up
const_cast<ScoreIndexManager&>(StaticData::Instance().GetScoreIndexManager()).AddScoreProducer(this);
const_cast<StaticData&>(StaticData::Instance()).SetWeightsForScoreProducer(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(Output, 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);
}
}
|