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/check.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
00057 class RuleCubeItemPositionOrderer
00058 {
00059 public:
00060 bool operator()(const RuleCubeItem *p, const RuleCubeItem *q) const {
00061 return *p < *q;
00062 }
00063 };
00064
00067 class RuleCubeItemHasher
00068 {
00069 public:
00070 size_t operator()(const RuleCubeItem *p) const {
00071 size_t seed = 0;
00072 boost::hash_combine(seed, p->GetHypothesisDimensions());
00073 boost::hash_combine(seed, p->GetTranslationDimension().GetTargetPhrase());
00074 return seed;
00075 }
00076 };
00077
00080 class RuleCubeItemEqualityPred
00081 {
00082 public:
00083 bool operator()(const RuleCubeItem *p, const RuleCubeItem *q) const {
00084 return p->GetHypothesisDimensions() == q->GetHypothesisDimensions() &&
00085 p->GetTranslationDimension() == q->GetTranslationDimension();
00086 }
00087 };
00088
00091 class RuleCube
00092 {
00093 public:
00094 RuleCube(const ChartTranslationOptions &, const ChartCellCollection &,
00095 ChartManager &);
00096
00097 ~RuleCube();
00098
00099 float GetTopScore() const {
00100 CHECK(!m_queue.empty());
00101 RuleCubeItem *item = m_queue.top();
00102 return item->GetScore();
00103 }
00104
00105 RuleCubeItem *Pop(ChartManager &);
00106
00107 bool IsEmpty() const {
00108 return m_queue.empty();
00109 }
00110
00111 const ChartTranslationOptions &GetTranslationOption() const {
00112 return m_transOpt;
00113 }
00114
00115 private:
00116 #if defined(BOOST_VERSION) && (BOOST_VERSION >= 104200)
00117 typedef boost::unordered_set<RuleCubeItem*,
00118 RuleCubeItemHasher,
00119 RuleCubeItemEqualityPred
00120 > ItemSet;
00121 #else
00122 typedef std::set<RuleCubeItem*, RuleCubeItemPositionOrderer> ItemSet;
00123 #endif
00124
00125 typedef std::priority_queue<RuleCubeItem*,
00126 std::vector<RuleCubeItem*>,
00127 RuleCubeItemScoreOrderer
00128 > Queue;
00129
00130 RuleCube(const RuleCube &);
00131 RuleCube &operator=(const RuleCube &);
00132
00133 void CreateNeighbors(const RuleCubeItem &, ChartManager &);
00134 void CreateNeighbor(const RuleCubeItem &, int, ChartManager &);
00135
00136 const ChartTranslationOptions &m_transOpt;
00137 ItemSet m_covered;
00138 Queue m_queue;
00139 };
00140
00141 }