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