00001 #include <sstream>
00002 #include <boost/algorithm/string/predicate.hpp>
00003 #include <boost/foreach.hpp>
00004
00005 #include "moses/FF/FFState.h"
00006 #include "moses/TranslationOptionList.h"
00007 #include "LexicalReordering.h"
00008 #include "LRState.h"
00009 #include "moses/StaticData.h"
00010 #include "moses/Util.h"
00011 #include "moses/InputPath.h"
00012
00013 using namespace std;
00014 using namespace boost::algorithm;
00015
00016 namespace Moses
00017 {
00018 LexicalReordering::
00019 LexicalReordering(const std::string &line)
00020 : StatefulFeatureFunction(line,false)
00021 {
00022 VERBOSE(1, "Initializing Lexical Reordering Feature.." << std::endl);
00023
00024 map<string,string> sparseArgs;
00025 m_haveDefaultScores = false;
00026 for (size_t i = 0; i < m_args.size(); ++i) {
00027 const vector<string> &args = m_args[i];
00028
00029 if (args[0] == "type") {
00030 m_configuration.reset(new LRModel(args[1]));
00031 m_configuration->SetScoreProducer(this);
00032 m_modelTypeString = m_configuration->GetModelString();
00033 } else if (args[0] == "input-factor")
00034 m_factorsF =Tokenize<FactorType>(args[1]);
00035 else if (args[0] == "output-factor")
00036 m_factorsE =Tokenize<FactorType>(args[1]);
00037 else if (args[0] == "path")
00038 m_filePath = args[1];
00039 else if (starts_with(args[0], "sparse-"))
00040 sparseArgs[args[0].substr(7)] = args[1];
00041 else if (args[0] == "default-scores") {
00042 vector<string> tokens = Tokenize(args[1],",");
00043 for(size_t i=0; i<tokens.size(); i++)
00044 m_defaultScores.push_back( TransformScore( Scan<float>(tokens[i])));
00045 m_haveDefaultScores = true;
00046 } else UTIL_THROW2("Unknown argument " + args[0]);
00047 }
00048
00049 switch(m_configuration->GetCondition()) {
00050 case LRModel::FE:
00051 case LRModel::E:
00052 UTIL_THROW_IF2(m_factorsE.empty(),
00053 "TL factor mask for lexical reordering is "
00054 << "unexpectedly empty");
00055
00056 if(m_configuration->GetCondition() == LRModel::E)
00057 break;
00058 case LRModel::F:
00059 UTIL_THROW_IF2(m_factorsF.empty(),
00060 "SL factor mask for lexical reordering is "
00061 << "unexpectedly empty");
00062 break;
00063 default:
00064 UTIL_THROW2("Unknown conditioning option!");
00065 }
00066
00067
00068 size_t numScores
00069 = m_numScoreComponents
00070 = m_numTuneableComponents
00071 = m_configuration->GetNumScoreComponents();
00072 UTIL_THROW_IF2(m_haveDefaultScores && m_defaultScores.size() != numScores,
00073 "wrong number of default scores (" << m_defaultScores.size()
00074 << ") for lexicalized reordering model (expected "
00075 << m_configuration->GetNumScoreComponents() << ")");
00076
00077 m_configuration->ConfigureSparse(sparseArgs, this);
00078
00079 }
00080
00081 LexicalReordering::
00082 ~LexicalReordering()
00083 { }
00084
00085 void
00086 LexicalReordering::
00087 Load(AllOptions::ptr const& opts)
00088 {
00089 m_options = opts;
00090 typedef LexicalReorderingTable LRTable;
00091 if (m_filePath.size())
00092 m_table.reset(LRTable::LoadAvailable(m_filePath, m_factorsF,
00093 m_factorsE, std::vector<FactorType>()));
00094 }
00095
00096 Scores
00097 LexicalReordering::
00098 GetProb(const Phrase& f, const Phrase& e) const
00099 {
00100 return m_table->GetScore(f, e, Phrase(ARRAY_SIZE_INCR));
00101 }
00102
00103 FFState*
00104 LexicalReordering::
00105 EvaluateWhenApplied(const Hypothesis& hypo,
00106 const FFState* prev_state,
00107 ScoreComponentCollection* out) const
00108 {
00109 VERBOSE(3,"LexicalReordering::Evaluate(const Hypothesis& hypo,...) START" << std::endl);
00110 const LRState *prev = static_cast<const LRState *>(prev_state);
00111 LRState *next_state = prev->Expand(hypo.GetTranslationOption(), hypo.GetInput(), out);
00112
00113 VERBOSE(3,"LexicalReordering::Evaluate(const Hypothesis& hypo,...) END" << std::endl);
00114
00115 return next_state;
00116 }
00117
00118 FFState const*
00119 LexicalReordering::EmptyHypothesisState(const InputType &input) const
00120 {
00121 return m_configuration->CreateLRState(input);
00122 }
00123
00124 bool
00125 LexicalReordering::
00126 IsUseable(const FactorMask &mask) const
00127 {
00128 BOOST_FOREACH(FactorType const& f, m_factorsE) {
00129 if (!mask[f]) return false;
00130 }
00131 return true;
00132 }
00133
00134
00135 void
00136 LexicalReordering::
00137 SetCache(TranslationOption& to) const
00138 {
00139 if (to.GetLexReorderingScores(this)) return;
00140
00141
00142 if (m_table) {
00143 Phrase const& sphrase = to.GetInputPath().GetPhrase();
00144 Phrase const& tphrase = to.GetTargetPhrase();
00145 to.CacheLexReorderingScores(*this, this->GetProb(sphrase,tphrase));
00146 } else {
00147
00148
00149 }
00150 }
00151
00152 LRModel const&
00153 LexicalReordering
00154 ::GetModel() const
00155 {
00156 return *m_configuration;
00157 }
00158
00159
00160 void
00161 LexicalReordering::
00162 SetCache(TranslationOptionList& tol) const
00163 {
00164 BOOST_FOREACH(TranslationOption* to, tol)
00165 this->SetCache(*to);
00166 }
00167
00168
00169 }
00170