00001 #ifndef moses_LexicalReorderingTable_h
00002 #define moses_LexicalReorderingTable_h
00003
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
00016 #include "TypeDef.h"
00017 #include "Phrase.h"
00018 #include "InputType.h"
00019 #include "ConfusionNet.h"
00020 #include "Sentence.h"
00021 #include "PrefixTreeMap.h"
00022
00023 namespace Moses
00024 {
00025
00026 class Phrase;
00027 class InputType;
00028 class ConfusionNet;
00029
00030
00031
00032 class LexicalReorderingTable
00033 {
00034 public:
00035 LexicalReorderingTable(const FactorList& f_factors, const FactorList& e_factors, const FactorList& c_factors)
00036 : m_FactorsF(f_factors), m_FactorsE(e_factors), m_FactorsC(c_factors) {
00037 }
00038 virtual ~LexicalReorderingTable() {
00039 }
00040 public:
00041 static LexicalReorderingTable* LoadAvailable(const std::string& filePath, const FactorList& f_factors, const FactorList& e_factors, const FactorList& c_factors);
00042 public:
00043 virtual Scores GetScore(const Phrase& f, const Phrase& e, const Phrase& c) = 0;
00044 virtual void InitializeForInput(const InputType&) {
00045
00046 };
00047 virtual void InitializeForInputPhrase(const Phrase&) {
00048 };
00049
00050
00051
00052
00053
00054 const FactorList& GetFFactorMask() const {
00055 return m_FactorsF;
00056 }
00057 const FactorList& GetEFactorMask() const {
00058 return m_FactorsE;
00059 }
00060 const FactorList& GetCFactorMask() const {
00061 return m_FactorsC;
00062 }
00063 virtual void DbgDump(std::ostream* out) const {
00064 *out << "Overwrite in subclass...\n";
00065 };
00066 protected:
00067 FactorList m_FactorsF;
00068 FactorList m_FactorsE;
00069 FactorList m_FactorsC;
00070 };
00071
00072 class LexicalReorderingTableMemory : public LexicalReorderingTable
00073 {
00074
00075
00076 public:
00077 LexicalReorderingTableMemory( const std::string& filePath,
00078 const std::vector<FactorType>& f_factors,
00079 const std::vector<FactorType>& e_factors,
00080 const std::vector<FactorType>& c_factors);
00081 virtual ~LexicalReorderingTableMemory();
00082 public:
00083 virtual std::vector<float> GetScore(const Phrase& f, const Phrase& e, const Phrase& c);
00084 void DbgDump(std::ostream* out) const;
00085 private:
00086 std::string MakeKey(const Phrase& f, const Phrase& e, const Phrase& c) const;
00087 std::string MakeKey(const std::string& f, const std::string& e, const std::string& c) const;
00088
00089 void LoadFromFile(const std::string& filePath);
00090 private:
00091 typedef std::map< std::string, std::vector<float> > TableType;
00092 TableType m_Table;
00093 };
00094
00095 class LexicalReorderingTableTree : public LexicalReorderingTable
00096 {
00097
00098 public:
00099 LexicalReorderingTableTree(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 ~LexicalReorderingTableTree();
00104 public:
00105 bool IsCacheEnabled() const {
00106 return m_UseCache;
00107 };
00108 void EnableCache() {
00109 m_UseCache = true;
00110 };
00111 void DisableCache() {
00112 m_UseCache = false;
00113 };
00114 void ClearCache() {
00115 if (m_UseCache) {
00116 m_Cache.clear();
00117 }
00118 };
00119
00120 virtual std::vector<float> GetScore(const Phrase& f, const Phrase& e, const Phrase& c);
00121
00122 virtual void InitializeForInput(const InputType& input);
00123 virtual void InitializeForInputPhrase(const Phrase& f) {
00124 ClearCache();
00125 auxCacheForSrcPhrase(f);
00126 }
00127 public:
00128 static bool Create(std::istream& inFile, const std::string& outFileName);
00129 private:
00130 std::string MakeCacheKey(const Phrase& f, const Phrase& e) const;
00131 IPhrase MakeTableKey(const Phrase& f, const Phrase& e) const;
00132
00133 void Cache(const ConfusionNet& input);
00134 void Cache(const Sentence& input);
00135
00136 void auxCacheForSrcPhrase(const Phrase& f);
00137 Scores auxFindScoreForContext(const Candidates& cands, const Phrase& contex);
00138 private:
00139
00140 typedef std::map< std::string, Candidates > CacheType;
00141 #ifdef WITH_THREADS
00142 typedef boost::thread_specific_ptr<PrefixTreeMap> TableType;
00143 #else
00144 typedef std::auto_ptr<PrefixTreeMap> TableType;
00145 #endif
00146
00147 static const int SourceVocId = 0;
00148 static const int TargetVocId = 1;
00149
00150 bool m_UseCache;
00151 std::string m_FilePath;
00152 CacheType m_Cache;
00153 TableType m_Table;
00154 };
00155
00156 }
00157
00158 #endif