00001 #pragma once 00002 00003 #include <map> 00004 #include <vector> 00005 00006 #include <boost/functional/hash.hpp> 00007 #include <boost/unordered_map.hpp> 00008 #include <boost/version.hpp> 00009 00010 #include "moses/Syntax/SymbolEqualityPred.h" 00011 #include "moses/Syntax/SymbolHasher.h" 00012 #include "moses/TargetPhrase.h" 00013 #include "moses/TargetPhraseCollection.h" 00014 #include "moses/Util.h" 00015 #include "moses/Word.h" 00016 00017 #include "RuleTrie.h" 00018 00019 namespace Moses 00020 { 00021 namespace Syntax 00022 { 00023 namespace S2T 00024 { 00025 00026 class RuleTrieScope3 : public RuleTrie 00027 { 00028 public: 00029 class Node 00030 { 00031 public: 00032 typedef std::vector<std::vector<Word> > LabelTable; 00033 00034 typedef boost::unordered_map<Word, Node, SymbolHasher, 00035 SymbolEqualityPred> TerminalMap; 00036 00037 typedef boost::unordered_map<std::vector<int>, 00038 TargetPhraseCollection::shared_ptr> LabelMap; 00039 00040 ~Node() { 00041 delete m_gapNode; 00042 } 00043 00044 const LabelTable &GetLabelTable() const { 00045 return m_labelTable; 00046 } 00047 00048 const LabelMap &GetLabelMap() const { 00049 return m_labelMap; 00050 } 00051 00052 const TerminalMap &GetTerminalMap() const { 00053 return m_terminalMap; 00054 } 00055 00056 const Node *GetNonTerminalChild() const { 00057 return m_gapNode; 00058 } 00059 00060 Node *GetOrCreateTerminalChild(const Word &sourceTerm); 00061 00062 Node *GetOrCreateNonTerminalChild(const Word &targetNonTerm); 00063 00064 TargetPhraseCollection::shared_ptr 00065 GetOrCreateTargetPhraseCollection(const TargetPhrase &); 00066 00067 bool IsLeaf() const { 00068 return m_terminalMap.empty() && m_gapNode == NULL; 00069 } 00070 00071 bool HasRules() const { 00072 return !m_labelMap.empty(); 00073 } 00074 00075 void Prune(std::size_t tableLimit); 00076 void Sort(std::size_t tableLimit); 00077 00078 private: 00079 friend class RuleTrieScope3; 00080 00081 Node() : m_gapNode(NULL) {} 00082 00083 int InsertLabel(int i, const Word &w) { 00084 std::vector<Word> &inner = m_labelTable[i]; 00085 for (std::size_t j = 0; j < inner.size(); ++j) { 00086 if (inner[j] == w) { 00087 return j; 00088 } 00089 } 00090 inner.push_back(w); 00091 return inner.size()-1; 00092 } 00093 00094 LabelTable m_labelTable; 00095 LabelMap m_labelMap; 00096 TerminalMap m_terminalMap; 00097 Node *m_gapNode; 00098 }; 00099 00100 RuleTrieScope3(const RuleTableFF *ff) : RuleTrie(ff) {} 00101 00102 const Node &GetRootNode() const { 00103 return m_root; 00104 } 00105 00106 bool HasPreterminalRule(const Word &) const; 00107 00108 private: 00109 TargetPhraseCollection::shared_ptr 00110 GetOrCreateTargetPhraseCollection(const Phrase &source, 00111 const TargetPhrase &target, 00112 const Word *sourceLHS); 00113 00114 Node &GetOrCreateNode(const Phrase &source, const TargetPhrase &target, 00115 const Word *sourceLHS); 00116 00117 void SortAndPrune(std::size_t); 00118 00119 Node m_root; 00120 }; 00121 00122 } // namespace S2T 00123 } // namespace Syntax 00124 } // namespace Moses