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 "RuleCubeItem.h"
00025
00026 #include <boost/functional/hash.hpp>
00027 #include <boost/unordered_set.hpp>
00028 #include <boost/version.hpp>
00029
00030 #include "util/exception.hh"
00031 #include <queue>
00032 #include <set>
00033 #include <vector>
00034
00035 namespace Moses
00036 {
00037
00038 class ChartCellCollection;
00039 class ChartManager;
00040 class ChartTranslationOptions;
00041
00045 class RuleCubeItemScoreOrderer
00046 {
00047 public:
00048 bool operator()(const RuleCubeItem *p, const RuleCubeItem *q) const {
00049 return p->GetScore() < q->GetScore();
00050 }
00051 };
00052
00055 class RuleCubeItemHasher
00056 {
00057 public:
00058 size_t operator()(const RuleCubeItem *p) const {
00059 size_t seed = 0;
00060 const std::vector<HypothesisDimension> &hypoDim = p->GetHypothesisDimensions();
00061 const ChartTranslationOption *transOpt = p->GetTranslationDimension().GetTranslationOption().get();
00062
00063 boost::hash_combine(seed, hypoDim);
00064 boost::hash_combine(seed, transOpt);
00065
00066 return seed;
00067 }
00068 };
00069
00072 class RuleCubeItemEqualityPred
00073 {
00074 public:
00075 bool operator()(const RuleCubeItem *p, const RuleCubeItem *q) const {
00076 bool ret = p->GetHypothesisDimensions() == q->GetHypothesisDimensions() &&
00077 p->GetTranslationDimension() == q->GetTranslationDimension();
00078 return ret;
00079 }
00080 };
00081
00084 class RuleCube
00085 {
00086 friend std::ostream& operator<<(std::ostream &out, const RuleCube &obj);
00087
00088 public:
00089 RuleCube(const ChartTranslationOptions &, const ChartCellCollection &,
00090 ChartManager &);
00091
00092 ~RuleCube();
00093
00094 float GetTopScore() const {
00095 UTIL_THROW_IF2(m_queue.empty(), "Empty queue, nothing to pop");
00096 RuleCubeItem *item = m_queue.top();
00097 return item->GetScore();
00098 }
00099
00100 RuleCubeItem *Pop(ChartManager &);
00101
00102 bool IsEmpty() const {
00103 return m_queue.empty();
00104 }
00105
00106 const ChartTranslationOptions &GetTranslationOption() const {
00107 return m_transOpt;
00108 }
00109
00110 size_t GetItemSetSize() const {
00111 return m_covered.size();
00112 }
00113
00114 private:
00115 typedef boost::unordered_set<RuleCubeItem*,
00116 RuleCubeItemHasher,
00117 RuleCubeItemEqualityPred
00118 > ItemSet;
00119
00120 typedef std::priority_queue<RuleCubeItem*,
00121 std::vector<RuleCubeItem*>,
00122 RuleCubeItemScoreOrderer
00123 > Queue;
00124
00125 RuleCube(const RuleCube &);
00126 RuleCube &operator=(const RuleCube &);
00127
00128 void CreateNeighbors(const RuleCubeItem &, ChartManager &);
00129 void CreateNeighbor(const RuleCubeItem &, int, ChartManager &);
00130
00131 const ChartTranslationOptions &m_transOpt;
00132 ItemSet m_covered;
00133 Queue m_queue;
00134 };
00135
00136 }