00001 #include <sstream>
00002
00003 #include "FFState.h"
00004 #include "LexicalReordering.h"
00005 #include "LexicalReorderingState.h"
00006 #include "StaticData.h"
00007
00008 namespace Moses
00009 {
00010
00011 LexicalReordering::LexicalReordering(std::vector<FactorType>& f_factors,
00012 std::vector<FactorType>& e_factors,
00013 const std::string &modelType,
00014 const std::string &filePath,
00015 const std::vector<float>& weights)
00016 : m_configuration(this, modelType)
00017 {
00018 std::cerr << "Creating lexical reordering...\n";
00019 std::cerr << "weights: ";
00020 for(size_t w = 0; w < weights.size(); ++w) {
00021 std::cerr << weights[w] << " ";
00022 }
00023 std::cerr << "\n";
00024
00025 m_modelTypeString = modelType;
00026
00027 switch(m_configuration.GetCondition()) {
00028 case LexicalReorderingConfiguration::FE:
00029 case LexicalReorderingConfiguration::E:
00030 m_factorsE = e_factors;
00031 if(m_factorsE.empty()) {
00032 UserMessage::Add("TL factor mask for lexical reordering is unexpectedly empty");
00033 exit(1);
00034 }
00035 if(m_configuration.GetCondition() == LexicalReorderingConfiguration::E)
00036 break;
00037 case LexicalReorderingConfiguration::F:
00038 m_factorsF = f_factors;
00039 if(m_factorsF.empty()) {
00040 UserMessage::Add("SL factor mask for lexical reordering is unexpectedly empty");
00041 exit(1);
00042 }
00043 break;
00044 default:
00045 UserMessage::Add("Unknown conditioning option!");
00046 exit(1);
00047 }
00048
00049
00050 if(weights.size() != m_configuration.GetNumScoreComponents()) {
00051 std::ostringstream os;
00052 os << "Lexical reordering model (type " << modelType << "): expected " << m_numScoreComponents << " weights, got " << weights.size() << std::endl;
00053 UserMessage::Add(os.str());
00054 exit(1);
00055 }
00056
00057
00058 const_cast<ScoreIndexManager&>(StaticData::Instance().GetScoreIndexManager()).AddScoreProducer(this);
00059 const_cast<StaticData&>(StaticData::Instance()).SetWeightsForScoreProducer(this, weights);
00060
00061 m_table = LexicalReorderingTable::LoadAvailable(filePath, m_factorsF, m_factorsE, std::vector<FactorType>());
00062 }
00063
00064 LexicalReordering::~LexicalReordering()
00065 {
00066 if(m_table)
00067 delete m_table;
00068 }
00069
00070 Scores LexicalReordering::GetProb(const Phrase& f, const Phrase& e) const
00071 {
00072 return m_table->GetScore(f, e, Phrase(ARRAY_SIZE_INCR));
00073 }
00074
00075 FFState* LexicalReordering::Evaluate(const Hypothesis& hypo,
00076 const FFState* prev_state,
00077 ScoreComponentCollection* out) const
00078 {
00079 Scores score(GetNumScoreComponents(), 0);
00080 const LexicalReorderingState *prev = dynamic_cast<const LexicalReorderingState *>(prev_state);
00081 LexicalReorderingState *next_state = prev->Expand(hypo.GetTranslationOption(), score);
00082
00083 out->PlusEquals(this, score);
00084
00085 return next_state;
00086 }
00087
00088 const FFState* LexicalReordering::EmptyHypothesisState(const InputType &input) const
00089 {
00090 return m_configuration.CreateLexicalReorderingState(input);
00091 }
00092
00093 }
00094