00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #pragma once
00023
00024 #include <map>
00025 #include <vector>
00026 #include <iterator>
00027 #include <utility>
00028 #include <ostream>
00029 #include "moses/Word.h"
00030 #include "moses/TargetPhraseCollection.h"
00031 #include "moses/Terminal.h"
00032 #include "moses/NonTerminal.h"
00033
00034 #include <boost/functional/hash.hpp>
00035 #include <boost/unordered_map.hpp>
00036 #include <boost/version.hpp>
00037
00038 namespace Moses
00039 {
00040
00041 class PhraseDictionaryMemory;
00042 class PhraseDictionaryScope3;
00043 class PhraseDictionaryFuzzyMatch;
00044
00046 class NonTerminalMapKeyHasher
00047 {
00048 public:
00049 size_t operator()(const std::pair<Word, Word> & k) const {
00050
00051 const Word & w1 = k.first;
00052 const Word & w2 = k.second;
00053 const Factor * f1 = w1[0];
00054 const Factor * f2 = w2[0];
00055 size_t seed = 0;
00056 boost::hash_combine(seed, *f1);
00057 boost::hash_combine(seed, *f2);
00058 return seed;
00059 }
00060 };
00061
00063 class NonTerminalMapKeyEqualityPred
00064 {
00065 public:
00066 bool operator()(const std::pair<Word, Word> & k1,
00067 const std::pair<Word, Word> & k2) const {
00068
00069
00070 {
00071 const Word & w1 = k1.first;
00072 const Word & w2 = k2.first;
00073 const Factor * f1 = w1[0];
00074 const Factor * f2 = w2[0];
00075 if (f1->Compare(*f2)) {
00076 return false;
00077 }
00078 }
00079
00080 {
00081 const Word & w1 = k1.second;
00082 const Word & w2 = k2.second;
00083 const Factor * f1 = w1[0];
00084 const Factor * f2 = w2[0];
00085 if (f1->Compare(*f2)) {
00086 return false;
00087 }
00088 }
00089 return true;
00090 }
00091 };
00092
00095 class PhraseDictionaryNodeMemory
00096 {
00097 public:
00098 typedef std::pair<Word, Word> NonTerminalMapKey;
00099
00100 #if defined(BOOST_VERSION) && (BOOST_VERSION >= 104200)
00101 typedef boost::unordered_map<Word,
00102 PhraseDictionaryNodeMemory,
00103 TerminalHasher,
00104 TerminalEqualityPred> TerminalMap;
00105
00106 #if defined(UNLABELLED_SOURCE)
00107 typedef boost::unordered_map<Word,
00108 PhraseDictionaryNodeMemory,
00109 NonTerminalHasher,
00110 NonTerminalEqualityPred> NonTerminalMap;
00111 #else
00112 typedef boost::unordered_map<NonTerminalMapKey,
00113 PhraseDictionaryNodeMemory,
00114 NonTerminalMapKeyHasher,
00115 NonTerminalMapKeyEqualityPred> NonTerminalMap;
00116 #endif
00117 #else
00118 typedef std::map<Word, PhraseDictionaryNodeMemory> TerminalMap;
00119 #if defined(UNLABELLED_SOURCE)
00120 typedef std::map<Word, PhraseDictionaryNodeMemory> NonTerminalMap;
00121 #else
00122 typedef std::map<NonTerminalMapKey, PhraseDictionaryNodeMemory> NonTerminalMap;
00123 #endif
00124 #endif
00125
00126 private:
00127 friend std::ostream& operator<<(std::ostream&, const PhraseDictionaryMemory&);
00128 friend std::ostream& operator<<(std::ostream&, const PhraseDictionaryScope3&);
00129 friend std::ostream& operator<<(std::ostream&, const PhraseDictionaryFuzzyMatch&);
00130
00131 TerminalMap m_sourceTermMap;
00132 NonTerminalMap m_nonTermMap;
00133 TargetPhraseCollection::shared_ptr m_targetPhraseCollection;
00134
00135
00136 public:
00137 PhraseDictionaryNodeMemory()
00138 : m_targetPhraseCollection(new TargetPhraseCollection) { }
00139
00140 bool IsLeaf() const {
00141 return m_sourceTermMap.empty() && m_nonTermMap.empty();
00142 }
00143
00144 void Prune(size_t tableLimit);
00145 void Sort(size_t tableLimit);
00146 PhraseDictionaryNodeMemory *GetOrCreateChild(const Word &sourceTerm);
00147 const PhraseDictionaryNodeMemory *GetChild(const Word &sourceTerm) const;
00148 #if defined(UNLABELLED_SOURCE)
00149 PhraseDictionaryNodeMemory *GetOrCreateNonTerminalChild(const Word &targetNonTerm);
00150 const PhraseDictionaryNodeMemory *GetNonTerminalChild(const Word &targetNonTerm) const;
00151 #else
00152 PhraseDictionaryNodeMemory *GetOrCreateChild(const Word &sourceNonTerm, const Word &targetNonTerm);
00153 const PhraseDictionaryNodeMemory *GetChild(const Word &sourceNonTerm, const Word &targetNonTerm) const;
00154 #endif
00155
00156 TargetPhraseCollection::shared_ptr
00157 GetTargetPhraseCollection() const {
00158 return m_targetPhraseCollection;
00159 }
00160 TargetPhraseCollection::shared_ptr
00161 GetTargetPhraseCollection() {
00162 return m_targetPhraseCollection;
00163 }
00164
00165 const TerminalMap & GetTerminalMap() const {
00166 return m_sourceTermMap;
00167 }
00168
00169 const NonTerminalMap & GetNonTerminalMap() const {
00170 return m_nonTermMap;
00171 }
00172
00173 void Remove();
00174
00175 TO_STRING();
00176 };
00177
00178 std::ostream& operator<<(std::ostream&, const PhraseDictionaryNodeMemory&);
00179
00180 }