This commit is contained in:
Hieu Hoang 2016-03-25 17:19:06 +00:00
parent 3d8231bbe6
commit dc5201ee6e
8 changed files with 77 additions and 21 deletions

View File

@ -6,13 +6,17 @@
*/ */
#include <boost/functional/hash_fwd.hpp> #include <boost/functional/hash_fwd.hpp>
#include "BidirectionalReorderingState.h" #include "BidirectionalReorderingState.h"
#include "../../legacy/Util2.h"
using namespace std;
namespace Moses2 { namespace Moses2 {
BidirectionalReorderingState::BidirectionalReorderingState( BidirectionalReorderingState::BidirectionalReorderingState(
const LRModel &config, const LRModel &config,
const LRState *bw, LRState *bw,
const LRState *fw, size_t offset) LRState *fw,
size_t offset)
: LRState(config, : LRState(config,
LRModel::Bidirectional, LRModel::Bidirectional,
offset) offset)
@ -24,10 +28,31 @@ BidirectionalReorderingState::~BidirectionalReorderingState() {
// TODO Auto-generated destructor stub // TODO Auto-generated destructor stub
} }
void BidirectionalReorderingState::Init(const LRState *prev,
const TargetPhrase &topt,
const InputPathBase &path,
bool first)
{
if (m_backward) {
m_backward->Init(prev, topt, path, first);
}
}
std::string BidirectionalReorderingState::ToString() const
{ return "BidirectionalReorderingState "
+ SPrint(this) + " "
+ SPrint(m_backward) + " "
+ SPrint(m_forward);
}
size_t BidirectionalReorderingState::hash() const size_t BidirectionalReorderingState::hash() const
{ {
cerr << "hashing " << *this << endl;
cerr << "BEFORE hash " << m_backward->ToString() << endl;
size_t ret = m_backward->hash(); size_t ret = m_backward->hash();
cerr << "HH1" << endl;
boost::hash_combine(ret, m_forward->hash()); boost::hash_combine(ret, m_forward->hash());
cerr << "ret=" << ret << endl;
return ret; return ret;
} }
@ -49,7 +74,7 @@ void BidirectionalReorderingState::Expand(const System &system,
Scores &scores, Scores &scores,
FFState &state) const FFState &state) const
{ {
cerr << "BidirectionalReorderingState::Expand" << endl;
} }
} /* namespace Moses2 */ } /* namespace Moses2 */

View File

@ -5,24 +5,30 @@
* Author: hieu * Author: hieu
*/ */
#pragma once #pragma once
#include "PhraseBasedReorderingState.h" #include "LRState.h"
namespace Moses2 { namespace Moses2 {
class BidirectionalReorderingState: public LRState class BidirectionalReorderingState: public LRState
{ {
public: public:
BidirectionalReorderingState(const LRModel &config, BidirectionalReorderingState(
const LRState *bw, const LRModel &config,
const LRState *fw, size_t offset); LRState *bw,
LRState *fw,
size_t offset);
virtual ~BidirectionalReorderingState(); virtual ~BidirectionalReorderingState();
void Init(const LRState *prev,
const TargetPhrase &topt,
const InputPathBase &path,
bool first);
size_t hash() const; size_t hash() const;
virtual bool operator==(const FFState& other) const; virtual bool operator==(const FFState& other) const;
virtual std::string ToString() const virtual std::string ToString() const;
{ return ""; }
void Expand(const System &system, void Expand(const System &system,
const LexicalReordering &ff, const LexicalReordering &ff,
@ -32,8 +38,8 @@ public:
FFState &state) const; FFState &state) const;
protected: protected:
const LRState *m_backward; LRState *m_backward;
const LRState *m_forward; LRState *m_forward;
}; };

View File

@ -12,6 +12,8 @@
#include "PhraseBasedReorderingState.h" #include "PhraseBasedReorderingState.h"
#include "BidirectionalReorderingState.h" #include "BidirectionalReorderingState.h"
using namespace std;
namespace Moses2 { namespace Moses2 {
LRModel::LRModel(const std::string &modelType, LexicalReordering &ff) LRModel::LRModel(const std::string &modelType, LexicalReordering &ff)
@ -132,6 +134,7 @@ CreateLRState() const
case Bidirectional: case Bidirectional:
if (m_phraseBased) { if (m_phraseBased) {
bwd = new PhraseBasedReorderingState(*this, Backward, offset); bwd = new PhraseBasedReorderingState(*this, Backward, offset);
cerr << "bwd=" << bwd << bwd->ToString() << endl;
} }
else { else {
//bwd = new HReorderingBackwardState(*this, offset); //bwd = new HReorderingBackwardState(*this, offset);
@ -141,6 +144,7 @@ CreateLRState() const
case Forward: case Forward:
if (m_phraseBased) { if (m_phraseBased) {
fwd = new PhraseBasedReorderingState(*this, Forward, offset); fwd = new PhraseBasedReorderingState(*this, Forward, offset);
cerr << "fwd=" << fwd << fwd->ToString() << endl;
} }
else { else {
//fwd = new HReorderingForwardState(*this, input.GetSize(), offset); //fwd = new HReorderingForwardState(*this, input.GetSize(), offset);
@ -148,8 +152,10 @@ CreateLRState() const
offset += m_collapseScores ? 1 : GetNumberOfTypes(); offset += m_collapseScores ? 1 : GetNumberOfTypes();
if (m_direction == Forward) return fwd; if (m_direction == Forward) return fwd;
} }
return new BidirectionalReorderingState(*this, bwd, fwd, 0);
BidirectionalReorderingState *ret = new BidirectionalReorderingState(*this, bwd, fwd, 0);
cerr << "ret=" << ret->ToString() << endl;
return ret;
} }
} /* namespace Moses2 */ } /* namespace Moses2 */

View File

@ -10,6 +10,7 @@ class System;
class Scores; class Scores;
class TargetPhrase; class TargetPhrase;
class InputType; class InputType;
class InputPathBase;
class LRState : public FFState class LRState : public FFState
{ {
@ -21,6 +22,11 @@ public:
LRModel::Direction dir, LRModel::Direction dir,
size_t offset); size_t offset);
virtual void Init(const LRState *prev,
const TargetPhrase &topt,
const InputPathBase &path,
bool first) = 0;
virtual void Expand(const System &system, virtual void Expand(const System &system,
const LexicalReordering &ff, const LexicalReordering &ff,
const Hypothesis &hypo, const Hypothesis &hypo,

View File

@ -117,7 +117,9 @@ void LexicalReordering::SetParameter(const std::string& key, const std::string&
FFState* LexicalReordering::BlankState(MemPool &pool) const FFState* LexicalReordering::BlankState(MemPool &pool) const
{ {
cerr << "BEFORE CreateLRState" << endl;
FFState *ret = m_configuration->CreateLRState(); FFState *ret = m_configuration->CreateLRState();
cerr << "AFTER CreateLRState" << endl;
return ret; return ret;
} }
@ -126,13 +128,16 @@ void LexicalReordering::EmptyHypothesisState(FFState &state,
const InputType &input, const InputType &input,
const Hypothesis &hypo) const const Hypothesis &hypo) const
{ {
cerr << "START EmptyHypothesisState" << endl;
if (m_configuration->IsPhraseBased()) { if (m_configuration->IsPhraseBased()) {
PhraseBasedReorderingState &stateCast = static_cast<PhraseBasedReorderingState&>(state); BidirectionalReorderingState &stateCast = static_cast<BidirectionalReorderingState&>(state);
cerr << "BEFORE Init" << endl;
stateCast.Init(NULL, hypo.GetTargetPhrase(), hypo.GetInputPath(), true); stateCast.Init(NULL, hypo.GetTargetPhrase(), hypo.GetInputPath(), true);
cerr << "AFTER Init" << endl;
} }
else { else {
cerr << "BEFORE BidirectionalReorderingState" << endl;
BidirectionalReorderingState &stateCast = static_cast<BidirectionalReorderingState&>(state); BidirectionalReorderingState &stateCast = static_cast<BidirectionalReorderingState&>(state);
} }
} }
@ -202,7 +207,9 @@ void LexicalReordering::EvaluateWhenApplied(const ManagerBase &mgr,
FFState &state) const FFState &state) const
{ {
const LRState &prevStateCast = static_cast<const LRState&>(prevState); const LRState &prevStateCast = static_cast<const LRState&>(prevState);
cerr << "BEFORE Expand" << endl;
prevStateCast.Expand(mgr.system, *this, hypo, m_PhraseTableInd, scores, state); prevStateCast.Expand(mgr.system, *this, hypo, m_PhraseTableInd, scores, state);
cerr << "AFTER Expand" << endl;
} }
const LexicalReordering::Values *LexicalReordering::GetValues(const Phrase &source, const Phrase &target) const const LexicalReordering::Values *LexicalReordering::GetValues(const Phrase &source, const Phrase &target) const

View File

@ -9,6 +9,8 @@
#include "LexicalReordering.h" #include "LexicalReordering.h"
#include "../../PhraseBased/Hypothesis.h" #include "../../PhraseBased/Hypothesis.h"
using namespace std;
namespace Moses2 { namespace Moses2 {
PhraseBasedReorderingState::PhraseBasedReorderingState( PhraseBasedReorderingState::PhraseBasedReorderingState(
@ -22,7 +24,7 @@ PhraseBasedReorderingState::PhraseBasedReorderingState(
void PhraseBasedReorderingState::Init( void PhraseBasedReorderingState::Init(
const PhraseBasedReorderingState *prev, const LRState *prev,
const TargetPhrase &topt, const TargetPhrase &topt,
const InputPathBase &path, const InputPathBase &path,
bool first) bool first)
@ -33,11 +35,12 @@ void PhraseBasedReorderingState::Init(
} }
size_t PhraseBasedReorderingState::hash() const { size_t PhraseBasedReorderingState::hash() const {
size_t ret; cerr << "prevPath=" << prevPath << endl;
ret = hash_value(prevPath->range); size_t ret;
boost::hash_combine(ret, m_direction); ret = hash_value(prevPath->range);
boost::hash_combine(ret, m_direction);
return ret; return ret;
} }
bool PhraseBasedReorderingState::operator==(const FFState& o) const { bool PhraseBasedReorderingState::operator==(const FFState& o) const {

View File

@ -21,7 +21,7 @@ public:
LRModel::Direction dir, LRModel::Direction dir,
size_t offset); size_t offset);
void Init(const PhraseBasedReorderingState *prev, void Init(const LRState *prev,
const TargetPhrase &topt, const TargetPhrase &topt,
const InputPathBase &path, const InputPathBase &path,
bool first); bool first);
@ -31,7 +31,7 @@ public:
virtual std::string ToString() const virtual std::string ToString() const
{ {
return ""; return "PhraseBasedReorderingState";
} }
void Expand(const System &system, void Expand(const System &system,

View File

@ -58,8 +58,11 @@ void Search::Decode()
Hypothesis *initHypo = Hypothesis::Create(mgr.GetSystemPool(), mgr); Hypothesis *initHypo = Hypothesis::Create(mgr.GetSystemPool(), mgr);
initHypo->Init(mgr, mgr.GetInputPaths().GetBlank(), mgr.GetInitPhrase(), initBitmap); initHypo->Init(mgr, mgr.GetInputPaths().GetBlank(), mgr.GetInitPhrase(), initBitmap);
initHypo->EmptyHypothesisState(mgr.GetInput()); initHypo->EmptyHypothesisState(mgr.GetInput());
cerr << "initHypo=" << *initHypo << endl;
cerr << "BEFORE Add" << endl;
m_stack.Add(initHypo, mgr.GetHypoRecycle(), mgr.arcLists); m_stack.Add(initHypo, mgr.GetHypoRecycle(), mgr.arcLists);
cerr << "AFTER Add" << endl;
PostDecode(0); PostDecode(0);
for (size_t stackInd = 1; stackInd < mgr.GetInput().GetSize() + 1; ++stackInd) { for (size_t stackInd = 1; stackInd < mgr.GetInput().GetSize() + 1; ++stackInd) {