00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <boost/test/unit_test.hpp>
00021 #include "MockHypothesis.h"
00022 #include "TranslationOption.h"
00023 #include "TranslationTask.h"
00024 #include "Bitmaps.h"
00025
00026 using namespace Moses;
00027 using namespace std;
00028
00029 namespace MosesTest
00030 {
00031
00032 MockHypothesisGuard
00033 ::MockHypothesisGuard
00034 ( const string& sourceSentence,
00035 const vector<Alignment>& alignments,
00036 const vector<string>& targetSegments)
00037 : m_initialTransOpt(), m_wp("WordPenalty"),
00038 m_uwp("UnknownWordPenalty"), m_dist("Distortion")
00039 {
00040 BOOST_CHECK_EQUAL(alignments.size(), targetSegments.size());
00041 AllOptions::ptr opts(new AllOptions(*StaticData::Instance().options()));
00042 m_sentence.reset(new Sentence(opts, 0, sourceSentence));
00043 m_ttask = TranslationTask::create(m_sentence);
00044 m_manager.reset(new Manager(m_ttask));
00045
00046
00047 Bitmaps bitmaps(m_sentence.get()->GetSize(),
00048 m_sentence.get()->m_sourceCompleted);
00049 m_manager->ResetSentenceStats(*m_sentence);
00050
00051 const Bitmap &initBitmap = bitmaps.GetInitialBitmap();
00052 m_hypothesis = new Hypothesis(*m_manager, *m_sentence, m_initialTransOpt,
00053 initBitmap, m_manager->GetNextHypoId());
00054
00055
00056 vector<Alignment>::const_iterator ai = alignments.begin();
00057 vector<string>::const_iterator ti = targetSegments.begin();
00058 for (; ti != targetSegments.end() && ai != alignments.end(); ++ti,++ai) {
00059 Hypothesis* prevHypo = m_hypothesis;
00060 Range range(ai->first,ai->second);
00061 const Bitmap &newBitmap = bitmaps.GetBitmap(prevHypo->GetWordsBitmap(), range);
00062 m_targetPhrases.push_back(TargetPhrase(NULL));
00063 vector<FactorType> const& factors = opts->output.factor_order;
00064 m_targetPhrases.back().CreateFromString(Input, factors, *ti, NULL);
00065 m_toptions.push_back(new TranslationOption
00066 (range,m_targetPhrases.back()));
00067 m_hypothesis = new Hypothesis(*prevHypo, *m_toptions.back(), newBitmap,
00068 m_manager->GetNextHypoId());
00069 }
00070
00071
00072 }
00073
00074 MockHypothesisGuard::~MockHypothesisGuard()
00075 {
00076 RemoveAllInColl(m_toptions);
00077 while (m_hypothesis) {
00078 Hypothesis* prevHypo = const_cast<Hypothesis*>(m_hypothesis->GetPrevHypo());
00079 delete m_hypothesis;
00080 m_hypothesis = prevHypo;
00081 }
00082 }
00083
00084 HypothesisFixture::HypothesisFixture()
00085 {
00086 string source = "je ne sais pas . ";
00087 vector<string> target;
00088 vector<Alignment> alignments;
00089 m_empty.reset(new MockHypothesisGuard(source,alignments,target));
00090 target.push_back("i");
00091 target.push_back("do not");
00092 alignments.push_back(Alignment(0,0));
00093 alignments.push_back(Alignment(3,3));
00094 m_partial.reset(new MockHypothesisGuard(source,alignments,target));
00095 target.push_back("know");
00096 target.push_back(".");
00097 alignments.push_back(Alignment(1,2));
00098 alignments.push_back(Alignment(4,4));
00099 m_full.reset(new MockHypothesisGuard(source,alignments,target));
00100 }
00101
00102 }
00103