00001
00002
00003 #pragma once
00004
00005 #include <vector>
00006 #include <map>
00007 #include <memory>
00008 #include <string>
00009 #include <iostream>
00010
00011 #ifdef WITH_THREADS
00012 #include <boost/thread/tss.hpp>
00013 #endif
00014
00015 #include "moses/TypeDef.h"
00016 #include "moses/Phrase.h"
00017 #include "moses/InputType.h"
00018 #include "moses/ConfusionNet.h"
00019 #include "moses/Sentence.h"
00020 #include "moses/PrefixTreeMap.h"
00021
00022 namespace Moses
00023 {
00024
00025 class Phrase;
00026 class InputType;
00027 class ConfusionNet;
00028
00030 class LexicalReorderingTable
00031 {
00032 public:
00033 LexicalReorderingTable(const FactorList& f_factors,
00034 const FactorList& e_factors,
00035 const FactorList& c_factors)
00036 : m_FactorsF(f_factors)
00037 , m_FactorsE(e_factors)
00038 , m_FactorsC(c_factors) { }
00039
00040 virtual
00041 ~LexicalReorderingTable() { }
00042
00043 public:
00044 static
00045 LexicalReorderingTable*
00046 LoadAvailable(const std::string& filePath,
00047 const FactorList& f_factors,
00048 const FactorList& e_factors,
00049 const FactorList& c_factors);
00050
00051 virtual
00052 Scores
00053 GetScore(const Phrase& f, const Phrase& e, const Phrase& c) = 0;
00054
00055 virtual
00056 void
00057 InitializeForInput(ttasksptr const& ttask) {
00058
00059 };
00060
00061 virtual
00062 void
00063 InitializeForInputPhrase(const Phrase&) { }
00064
00065
00066 const FactorList& GetFFactorMask() const {
00067 return m_FactorsF;
00068 }
00069 const FactorList& GetEFactorMask() const {
00070 return m_FactorsE;
00071 }
00072 const FactorList& GetCFactorMask() const {
00073 return m_FactorsC;
00074 }
00075
00076 virtual
00077 void
00078 DbgDump(std::ostream* out) const {
00079 *out << "Overwrite in subclass...\n";
00080 };
00081
00082
00083 protected:
00084 FactorList m_FactorsF;
00085 FactorList m_FactorsE;
00086 FactorList m_FactorsC;
00087 };
00088
00090 class LexicalReorderingTableMemory
00091 : public LexicalReorderingTable
00092 {
00093 typedef std::map< std::string, std::vector<float> > TableType;
00094 TableType m_Table;
00095
00096
00097
00098 public:
00099 LexicalReorderingTableMemory(const std::string& filePath,
00100 const std::vector<FactorType>& f_factors,
00101 const std::vector<FactorType>& e_factors,
00102 const std::vector<FactorType>& c_factors);
00103
00104 virtual
00105 ~LexicalReorderingTableMemory();
00106
00107 public:
00108 virtual
00109 std::vector<float>
00110 GetScore(const Phrase& f, const Phrase& e, const Phrase& c);
00111
00112 void
00113 DbgDump(std::ostream* out) const;
00114
00115 private:
00116
00117 std::string
00118 MakeKey(const Phrase& f, const Phrase& e, const Phrase& c) const;
00119
00120 std::string
00121 MakeKey(const std::string& f, const std::string& e, const std::string& c) const;
00122
00123 void
00124 LoadFromFile(const std::string& filePath);
00125 };
00126
00127 class LexicalReorderingTableTree
00128 : public LexicalReorderingTable
00129 {
00130
00131
00132 typedef std::map< std::string, Candidates > CacheType;
00133
00134 #ifdef WITH_THREADS
00135 typedef boost::thread_specific_ptr<PrefixTreeMap> TableType;
00136 #else
00137 typedef std::auto_ptr<PrefixTreeMap> TableType;
00138 #endif
00139
00140 static const int SourceVocId = 0;
00141 static const int TargetVocId = 1;
00142
00143 bool m_UseCache;
00144 std::string m_FilePath;
00145 CacheType m_Cache;
00146 TableType m_Table;
00147
00148 public:
00149
00150 static
00151 bool
00152 Create(std::istream& inFile, const std::string& outFileName);
00153
00154 LexicalReorderingTableTree(const std::string& filePath,
00155 const std::vector<FactorType>& f_factors,
00156 const std::vector<FactorType>& e_factors,
00157 const std::vector<FactorType>& c_factors);
00158
00159 ~LexicalReorderingTableTree();
00160
00161 bool IsCacheEnabled() const {
00162 return m_UseCache;
00163 };
00164 void EnableCache() {
00165 m_UseCache = true;
00166 };
00167 void DisableCache() {
00168 m_UseCache = false;
00169 };
00170 void ClearCache() {
00171 if (m_UseCache) m_Cache.clear();
00172 };
00173
00174 virtual
00175 std::vector<float>
00176 GetScore(const Phrase& f, const Phrase& e, const Phrase& c);
00177
00178 virtual
00179 void
00180 InitializeForInput(ttasksptr const& ttask);
00181
00182 virtual
00183 void
00184 InitializeForInputPhrase(const Phrase& f) {
00185 ClearCache();
00186 auxCacheForSrcPhrase(f);
00187 }
00188
00189
00190 private:
00191 std::string
00192 MakeCacheKey(const Phrase& f, const Phrase& e) const;
00193
00194 IPhrase
00195 MakeTableKey(const Phrase& f, const Phrase& e) const;
00196
00197 void
00198 Cache(const ConfusionNet& input);
00199
00200 void
00201 Cache(const Sentence& input);
00202
00203 void
00204 auxCacheForSrcPhrase(const Phrase& f);
00205
00206 Scores
00207 auxFindScoreForContext(const Candidates& cands, const Phrase& contex);
00208
00209 };
00210
00211 }
00212