00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "ChartCell.h"
00021 #include "ChartCellCollection.h"
00022 #include "ChartTranslationOptions.h"
00023 #include "ChartManager.h"
00024 #include "RuleCubeItem.h"
00025 #include "RuleCubeQueue.h"
00026 #include "Range.h"
00027 #include "Util.h"
00028 #include "util/exception.hh"
00029
00030 #include <boost/functional/hash.hpp>
00031
00032 namespace Moses
00033 {
00034
00035 std::size_t hash_value(const HypothesisDimension &dimension)
00036 {
00037 boost::hash<const ChartHypothesis*> hasher;
00038 return hasher(dimension.GetHypothesis());
00039 }
00040
00041 RuleCubeItem::RuleCubeItem(const ChartTranslationOptions &transOpt,
00042 const ChartCellCollection &)
00043 : m_translationDimension(0, transOpt.GetTargetPhrases())
00044 , m_hypothesis(0)
00045 {
00046 CreateHypothesisDimensions(transOpt.GetStackVec());
00047 }
00048
00049
00050 RuleCubeItem::RuleCubeItem(const RuleCubeItem ©, int hypoDimensionIncr)
00051 : m_translationDimension(copy.m_translationDimension)
00052 , m_hypothesisDimensions(copy.m_hypothesisDimensions)
00053 , m_hypothesis(0)
00054 {
00055 if (hypoDimensionIncr == -1) {
00056 m_translationDimension.IncrementPos();
00057 } else {
00058 HypothesisDimension &dimension = m_hypothesisDimensions[hypoDimensionIncr];
00059 dimension.IncrementPos();
00060 }
00061 }
00062
00063 RuleCubeItem::~RuleCubeItem()
00064 {
00065 delete m_hypothesis;
00066 }
00067
00068 void RuleCubeItem::EstimateScore()
00069 {
00070 m_score = m_translationDimension.GetTranslationOption()->GetPhrase().GetFutureScore();
00071 std::vector<HypothesisDimension>::const_iterator p;
00072 for (p = m_hypothesisDimensions.begin();
00073 p != m_hypothesisDimensions.end(); ++p) {
00074 m_score += p->GetHypothesis()->GetFutureScore();
00075 }
00076 }
00077
00078 void RuleCubeItem::CreateHypothesis(const ChartTranslationOptions &transOpt,
00079 ChartManager &manager)
00080 {
00081 m_hypothesis = new ChartHypothesis(transOpt, *this, manager);
00082 m_hypothesis->EvaluateWhenApplied();
00083 m_score = m_hypothesis->GetFutureScore();
00084 }
00085
00086 ChartHypothesis *RuleCubeItem::ReleaseHypothesis()
00087 {
00088 UTIL_THROW_IF2(m_hypothesis == NULL, "Hypothesis is NULL");
00089 ChartHypothesis *hypo = m_hypothesis;
00090 m_hypothesis = NULL;
00091 return hypo;
00092 }
00093
00094
00095
00096 void RuleCubeItem::CreateHypothesisDimensions(const StackVec &stackVec)
00097 {
00098 for (StackVec::const_iterator p = stackVec.begin(); p != stackVec.end();
00099 ++p) {
00100 const HypoList *stack = (*p)->GetStack().cube;
00101 assert(stack);
00102
00103
00104
00105 assert(!stack->empty());
00106
00107
00108 HypothesisDimension dimension(0, *stack);
00109
00110 m_hypothesisDimensions.push_back(dimension);
00111 }
00112 }
00113
00114 bool RuleCubeItem::operator<(const RuleCubeItem &compare) const
00115 {
00116 if (m_translationDimension == compare.m_translationDimension) {
00117 return m_hypothesisDimensions < compare.m_hypothesisDimensions;
00118 }
00119 return m_translationDimension < compare.m_translationDimension;
00120 }
00121
00122 }