00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00022 #ifndef moses_PhraseDictionaryDynamicCacheBased_H
00023 #define moses_PhraseDictionaryDynamicCacheBased_H
00024
00025 #include "moses/TypeDef.h"
00026 #include "moses/TranslationModel/PhraseDictionary.h"
00027
00028 #ifdef WITH_THREADS
00029 #include <boost/thread/shared_mutex.hpp>
00030 #include <boost/thread/locks.hpp>
00031 #endif
00032
00033 #define CBTM_SCORE_TYPE_UNDEFINED (-1)
00034 #define CBTM_SCORE_TYPE_HYPERBOLA 0
00035 #define CBTM_SCORE_TYPE_POWER 1
00036 #define CBTM_SCORE_TYPE_EXPONENTIAL 2
00037 #define CBTM_SCORE_TYPE_COSINE 3
00038 #define CBTM_SCORE_TYPE_HYPERBOLA_REWARD 10
00039 #define CBTM_SCORE_TYPE_POWER_REWARD 11
00040 #define CBTM_SCORE_TYPE_EXPONENTIAL_REWARD 12
00041 #define PI 3.14159265
00042
00043
00044 namespace Moses
00045 {
00046 class ChartParser;
00047 class ChartCellCollectionBase;
00048 class ChartRuleLookupManager;
00049
00052 class PhraseDictionaryDynamicCacheBased : public PhraseDictionary
00053 {
00054
00055 typedef std::vector<unsigned int> AgeCollection;
00056 typedef std::pair<TargetPhraseCollection::shared_ptr , AgeCollection*> TargetCollectionAgePair;
00057 typedef std::map<Phrase, TargetCollectionAgePair> cacheMap;
00058
00059
00060 cacheMap m_cacheTM;
00061 std::vector<Scores> precomputedScores;
00062 unsigned int m_maxAge;
00063 size_t m_score_type;
00064 size_t m_entries;
00065 float m_lower_score;
00066 bool m_constant;
00067 std::string m_initfiles;
00068 std::string m_name;
00069
00070 #ifdef WITH_THREADS
00071
00072 mutable boost::shared_mutex m_cacheLock;
00073 #endif
00074
00075 friend std::ostream& operator<<(std::ostream&, const PhraseDictionaryDynamicCacheBased&);
00076
00077 public:
00078 PhraseDictionaryDynamicCacheBased(const std::string &line);
00079 ~PhraseDictionaryDynamicCacheBased();
00080
00081 inline const std::string GetName() {
00082 return m_name;
00083 };
00084 inline void SetName(const std::string name) {
00085 m_name = name;
00086 }
00087
00088 static const PhraseDictionaryDynamicCacheBased* Instance(const std::string& name) {
00089 if (s_instance_map.find(name) == s_instance_map.end()) {
00090 return NULL;
00091 }
00092 return s_instance_map[name];
00093 }
00094
00095 static PhraseDictionaryDynamicCacheBased* InstanceNonConst(const std::string& name) {
00096 if (s_instance_map.find(name) == s_instance_map.end()) {
00097 return NULL;
00098 }
00099 return s_instance_map[name];
00100 }
00101
00102
00103 static const PhraseDictionaryDynamicCacheBased& Instance() {
00104 return *s_instance;
00105 }
00106
00107 static PhraseDictionaryDynamicCacheBased& InstanceNonConst() {
00108 return *s_instance;
00109 }
00110
00111 void Load(AllOptions::ptr const& opts);
00112 void Load(const std::string files);
00113
00114 TargetPhraseCollection::shared_ptr
00115 GetTargetPhraseCollection(const Phrase &src) const;
00116
00117 TargetPhraseCollection::shared_ptr
00118 GetTargetPhraseCollectionLEGACY(Phrase const &src) const;
00119
00120 TargetPhraseCollection::shared_ptr
00121 GetTargetPhraseCollectionNonCacheLEGACY(Phrase const &src) const;
00122
00123
00124
00125
00126
00127 ChartRuleLookupManager* CreateRuleLookupManager(const ChartParser&, const ChartCellCollectionBase&, std::size_t);
00128
00129 void SetParameter(const std::string& key, const std::string& value);
00130
00131 void InitializeForInput(ttasksptr const& ttask);
00132
00133
00134
00135
00136
00137 void Print() const;
00138 void Clear();
00139
00140 void ClearEntries(std::string &entries);
00141 void ClearSource(std::string &entries);
00142 void Insert(std::string &entries);
00143 void Execute(std::string command);
00144 void ExecuteDlt(std::map<std::string, std::string> dlt_meta);
00145
00146 void SetScoreType(size_t type);
00147 void SetMaxAge(unsigned int age);
00148
00149 protected:
00150 static PhraseDictionaryDynamicCacheBased *s_instance;
00151 static std::map< const std::string, PhraseDictionaryDynamicCacheBased * > s_instance_map;
00152
00153 float decaying_score(const int age);
00154 void Insert(std::vector<std::string> entries);
00155
00156 void Decay();
00157 void Decay(Phrase p);
00158 void Update(std::vector<std::string> entries, std::string ageString);
00159 void Update(std::string sourceString, std::string targetString, std::string ageString, std::string waString="");
00160 void Update(Phrase p, TargetPhrase tp, int age, std::string waString="");
00161
00162 void ClearEntries(std::vector<std::string> entries);
00163 void ClearEntries(std::string sourceString, std::string targetString);
00164 void ClearEntries(Phrase p, Phrase tp);
00165
00166 void ClearSource(std::vector<std::string> entries);
00167 void ClearSource(Phrase sp);
00168
00169 void Execute(std::vector<std::string> commands);
00170 void Execute_Single_Command(std::string command);
00171
00172
00173 void SetPreComputedScores(const unsigned int numScoreComponent);
00174 Scores GetPreComputedScores(const unsigned int age);
00175
00176 void Load_Multiple_Files(std::vector<std::string> files);
00177 void Load_Single_File(const std::string file);
00178
00179 TargetPhrase *CreateTargetPhrase(const Phrase &sourcePhrase) const;
00180 };
00181
00182 }
00183
00184 #endif