00001 #pragma once 00002 00003 #include <map> 00004 #include <vector> 00005 00006 #include <boost/unordered_map.hpp> 00007 00008 #include "moses/Syntax/RuleTable.h" 00009 #include "moses/TargetPhraseCollection.h" 00010 00011 #include "HyperPath.h" 00012 00013 namespace Moses 00014 { 00015 namespace Syntax 00016 { 00017 namespace F2S 00018 { 00019 00020 // A HyperTree for representing a tree-to-string rule table. See this paper: 00021 // 00022 // Hui Zhang, Min Zhang, Haizhou Li, and Chew Lim Tan 00023 // "Fast Translation Rule Matching for Syntax-based Statistical Machine 00024 // Translation" 00025 // In proceedings of EMNLP 2009 00026 // 00027 class HyperTree : public RuleTable 00028 { 00029 public: 00030 class Node 00031 { 00032 public: 00033 typedef boost::unordered_map<HyperPath::NodeSeq, Node> Map; 00034 00035 bool IsLeaf() const { 00036 return m_map.empty(); 00037 } 00038 00039 bool HasRules() const { 00040 return !m_targetPhraseCollection->IsEmpty(); 00041 } 00042 00043 void Prune(std::size_t tableLimit); 00044 void Sort(std::size_t tableLimit); 00045 00046 Node *GetOrCreateChild(const HyperPath::NodeSeq &); 00047 00048 const Node *GetChild(const HyperPath::NodeSeq &) const; 00049 00050 TargetPhraseCollection::shared_ptr 00051 GetTargetPhraseCollection() const { 00052 return m_targetPhraseCollection; 00053 } 00054 00055 TargetPhraseCollection::shared_ptr 00056 GetTargetPhraseCollection() { 00057 return m_targetPhraseCollection; 00058 } 00059 00060 const Map &GetMap() const { 00061 return m_map; 00062 } 00063 00064 Node() : m_targetPhraseCollection(new TargetPhraseCollection) { } 00065 00066 private: 00067 Map m_map; 00068 TargetPhraseCollection::shared_ptr m_targetPhraseCollection; 00069 }; 00070 00071 HyperTree(const RuleTableFF *ff) : RuleTable(ff) { } 00072 00073 const Node &GetRootNode() const { 00074 return m_root; 00075 } 00076 00077 private: 00078 friend class HyperTreeCreator; 00079 00080 TargetPhraseCollection::shared_ptr 00081 GetOrCreateTargetPhraseCollection(const HyperPath &); 00082 00083 Node &GetOrCreateNode(const HyperPath &); 00084 00085 void SortAndPrune(std::size_t); 00086 00087 Node m_root; 00088 }; 00089 00090 } // namespace F2S 00091 } // namespace Syntax 00092 } // namespace Moses