00001 
00002 #include <vector>
00003 #include <string>
00004 
00005 #include "LRState.h"
00006 #include "moses/FF/FFState.h"
00007 #include "moses/Hypothesis.h"
00008 #include "moses/Range.h"
00009 #include "moses/TranslationOption.h"
00010 #include "moses/Util.h"
00011 
00012 #include "LexicalReordering.h"
00013 
00014 namespace Moses
00015 {
00016 
00017 void
00018 LRState::
00019 CopyScores(ScoreComponentCollection*  accum,
00020            const TranslationOption &topt,
00021            const InputType& input,
00022            ReorderingType reoType) const
00023 {
00024   
00025   UTIL_THROW_IF2(m_direction != LRModel::Backward &&
00026                  m_direction != LRModel::Forward,
00027                  "Unknown direction: " << m_direction);
00028 
00029   TranslationOption const* relevantOpt = ((m_direction == LRModel::Backward)
00030                                           ? &topt : m_prevOption);
00031 
00032   LexicalReordering* producer = m_configuration.GetScoreProducer();
00033   Scores const* cached = relevantOpt->GetLexReorderingScores(producer);
00034 
00035   
00036   
00037   size_t off_remote = m_offset + reoType;
00038   size_t off_local  = m_configuration.CollapseScores() ? m_offset : off_remote;
00039 
00040   UTIL_THROW_IF2(off_local >= producer->GetNumScoreComponents(),
00041                  "offset out of vector bounds!");
00042 
00043   
00044   if(cached) {
00045     UTIL_THROW_IF2(off_remote >= cached->size(), "offset out of vector bounds!");
00046     Scores scores(producer->GetNumScoreComponents(),0);
00047     scores[off_local ] = (*cached)[off_remote];
00048     accum->PlusEquals(producer, scores);
00049   }
00050 
00051   
00052   else if (producer->GetHaveDefaultScores()) {
00053     Scores scores(producer->GetNumScoreComponents(),0);
00054     scores[off_local] = producer->GetDefaultScore(off_remote);
00055     accum->PlusEquals(m_configuration.GetScoreProducer(), scores);
00056   }
00057   
00058 
00059   const SparseReordering* sparse = m_configuration.GetSparseReordering();
00060   if (sparse) sparse->CopyScores(*relevantOpt, m_prevOption, input, reoType,
00061                                    m_direction, accum);
00062 }
00063 
00064 
00065 int
00066 LRState::
00067 ComparePrevScores(const TranslationOption *other) const
00068 {
00069   LexicalReordering* producer = m_configuration.GetScoreProducer();
00070   const Scores* myScores = m_prevOption->GetLexReorderingScores(producer);
00071   const Scores* yrScores = other->GetLexReorderingScores(producer);
00072 
00073   if(myScores == yrScores) return 0;
00074 
00075   
00076   if(yrScores == NULL) return -1;
00077   if(myScores == NULL) return  1;
00078 
00079   size_t stop = m_offset + m_configuration.GetNumberOfTypes();
00080   for(size_t i = m_offset; i < stop; i++) {
00081     if((*myScores)[i] < (*yrScores)[i]) return -1;
00082     if((*myScores)[i] > (*yrScores)[i]) return  1;
00083   }
00084   return 0;
00085 }
00086 
00087 }
00088