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
00033 #include <boost/functional/hash.hpp>
00034 #include <boost/unordered_map.hpp>
00035 #include <boost/version.hpp>
00036
00037 namespace Moses
00038 {
00039
00040 class PhraseDictionarySCFG;
00041 class PhraseDictionaryFuzzyMatch;
00042
00044 class NonTerminalMapKeyHasher
00045 {
00046 public:
00047 size_t operator()(const std::pair<Word, Word> & k) const {
00048
00049 const Word & w1 = k.first;
00050 const Word & w2 = k.second;
00051 const Factor * f1 = w1[0];
00052 const Factor * f2 = w2[0];
00053 size_t seed = 0;
00054 boost::hash_combine(seed, *f1);
00055 boost::hash_combine(seed, *f2);
00056 return seed;
00057 }
00058 };
00059
00061 class NonTerminalMapKeyEqualityPred
00062 {
00063 public:
00064 bool operator()(const std::pair<Word, Word> & k1,
00065 const std::pair<Word, Word> & k2) const {
00066
00067
00068 {
00069 const Word & w1 = k1.first;
00070 const Word & w2 = k2.first;
00071 const Factor * f1 = w1[0];
00072 const Factor * f2 = w2[0];
00073 if (f1->Compare(*f2)) {
00074 return false;
00075 }
00076 }
00077
00078 {
00079 const Word & w1 = k1.second;
00080 const Word & w2 = k2.second;
00081 const Factor * f1 = w1[0];
00082 const Factor * f2 = w2[0];
00083 if (f1->Compare(*f2)) {
00084 return false;
00085 }
00086 }
00087 return true;
00088 }
00089 };
00090
00093 class PhraseDictionaryNodeSCFG
00094 {
00095 public:
00096 typedef std::pair<Word, Word> NonTerminalMapKey;
00097
00098 #if defined(BOOST_VERSION) && (BOOST_VERSION >= 104200)
00099 typedef boost::unordered_map<Word,
00100 PhraseDictionaryNodeSCFG*,
00101 TerminalHasher,
00102 TerminalEqualityPred> TerminalMap;
00103
00104 typedef boost::unordered_map<NonTerminalMapKey,
00105 PhraseDictionaryNodeSCFG*,
00106 NonTerminalMapKeyHasher,
00107 NonTerminalMapKeyEqualityPred> NonTerminalMap;
00108 #else
00109 typedef std::map<Word, PhraseDictionaryNodeSCFG*> TerminalMap;
00110 typedef std::map<NonTerminalMapKey, PhraseDictionaryNodeSCFG*> NonTerminalMap;
00111 #endif
00112
00113 private:
00114 friend std::ostream& operator<<(std::ostream&, const PhraseDictionarySCFG&);
00115 friend std::ostream& operator<<(std::ostream&, const PhraseDictionaryFuzzyMatch&);
00116
00117
00118 friend class PhraseDictionarySCFG;
00119 friend class PhraseDictionaryFuzzyMatch;
00120 friend class std::map<Word, PhraseDictionaryNodeSCFG>;
00121 friend class std::map<long, PhraseDictionaryNodeSCFG>;
00122
00123 protected:
00124 TerminalMap m_sourceTermMap;
00125 NonTerminalMap m_nonTermMap;
00126 TargetPhraseCollection *m_targetPhraseCollection;
00127
00128 PhraseDictionaryNodeSCFG()
00129 :m_targetPhraseCollection(NULL)
00130 {}
00131 public:
00132 virtual ~PhraseDictionaryNodeSCFG();
00133
00134 bool IsLeaf() const {
00135 return m_sourceTermMap.empty() && m_nonTermMap.empty();
00136 }
00137
00138 void Prune(size_t tableLimit);
00139 void Sort(size_t tableLimit);
00140 PhraseDictionaryNodeSCFG *GetOrCreateChild(const Word &sourceTerm);
00141 PhraseDictionaryNodeSCFG *GetOrCreateChild(const Word &sourceNonTerm, const Word &targetNonTerm);
00142 const PhraseDictionaryNodeSCFG *GetChild(const Word &sourceTerm) const;
00143 const PhraseDictionaryNodeSCFG *GetChild(const Word &sourceNonTerm, const Word &targetNonTerm) const;
00144
00145 const TargetPhraseCollection *GetTargetPhraseCollection() const {
00146 return m_targetPhraseCollection;
00147 }
00148 TargetPhraseCollection &GetOrCreateTargetPhraseCollection();
00149
00150 const NonTerminalMap & GetNonTerminalMap() const {
00151 return m_nonTermMap;
00152 }
00153
00154 void Clear();
00155
00156 TO_STRING();
00157 };
00158
00159 std::ostream& operator<<(std::ostream&, const PhraseDictionaryNodeSCFG&);
00160
00161 }