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/RuleTable.h" 00011 #include "moses/Syntax/SymbolEqualityPred.h" 00012 #include "moses/Syntax/SymbolHasher.h" 00013 #include "moses/TargetPhrase.h" 00014 #include "moses/TargetPhraseCollection.h" 00015 #include "moses/Terminal.h" 00016 #include "moses/Util.h" 00017 #include "moses/Word.h" 00018 00019 namespace Moses 00020 { 00021 namespace Syntax 00022 { 00023 namespace T2S 00024 { 00025 00026 class RuleTrie: public RuleTable 00027 { 00028 public: 00029 class Node 00030 { 00031 public: 00032 typedef boost::unordered_map<Word, Node, SymbolHasher, 00033 SymbolEqualityPred> SymbolMap; 00034 00035 typedef boost::unordered_map<Word, TargetPhraseCollection::shared_ptr, 00036 SymbolHasher, SymbolEqualityPred> TPCMap; 00037 00038 bool IsLeaf() const { 00039 return m_sourceTermMap.empty() && m_nonTermMap.empty(); 00040 } 00041 00042 bool HasRules() const { 00043 return !m_targetPhraseCollections.empty(); 00044 } 00045 00046 void Prune(std::size_t tableLimit); 00047 void Sort(std::size_t tableLimit); 00048 00049 Node *GetOrCreateChild(const Word &sourceTerm); 00050 Node *GetOrCreateNonTerminalChild(const Word &targetNonTerm); 00051 TargetPhraseCollection::shared_ptr GetOrCreateTargetPhraseCollection(const Word &); 00052 00053 const Node *GetChild(const Word &sourceTerm) const; 00054 const Node *GetNonTerminalChild(const Word &targetNonTerm) const; 00055 00056 TargetPhraseCollection::shared_ptr 00057 GetTargetPhraseCollection(const Word &sourceLHS) const { 00058 TPCMap::const_iterator p = m_targetPhraseCollections.find(sourceLHS); 00059 if (p != m_targetPhraseCollections.end()) 00060 return p->second; 00061 else 00062 return TargetPhraseCollection::shared_ptr(); 00063 } 00064 00065 // FIXME IS there any reason to distinguish these two for T2S? 00066 const SymbolMap &GetTerminalMap() const { 00067 return m_sourceTermMap; 00068 } 00069 00070 const SymbolMap &GetNonTerminalMap() const { 00071 return m_nonTermMap; 00072 } 00073 00074 private: 00075 SymbolMap m_sourceTermMap; 00076 SymbolMap m_nonTermMap; 00077 TPCMap m_targetPhraseCollections; 00078 }; 00079 00080 RuleTrie(const RuleTableFF *ff) : RuleTable(ff) {} 00081 00082 const Node &GetRootNode() const { 00083 return m_root; 00084 } 00085 00086 private: 00087 friend class RuleTrieCreator; 00088 00089 TargetPhraseCollection::shared_ptr 00090 GetOrCreateTargetPhraseCollection 00091 (const Word &sourceLHS, const Phrase &sourceRHS); 00092 00093 Node &GetOrCreateNode(const Phrase &sourceRHS); 00094 00095 void SortAndPrune(std::size_t); 00096 00097 Node m_root; 00098 }; 00099 00100 } // namespace T2S 00101 } // namespace Syntax 00102 } // namespace Moses