create deviant paths

This commit is contained in:
Hieu Hoang 2016-03-19 10:53:31 +00:00
parent ee8171a937
commit fa4b673211
3 changed files with 54 additions and 15 deletions

View File

@ -191,21 +191,26 @@ void Manager::OutputNBest()
arcLists.Sort();
TrellisPaths contenders;
cerr << "START AddInitialTrellisPaths" << endl;
//cerr << "START AddInitialTrellisPaths" << endl;
m_search->AddInitialTrellisPaths(contenders);
cerr << "END AddInitialTrellisPaths" << endl;
//cerr << "END AddInitialTrellisPaths" << endl;
long transId = m_input->GetTranslationId();
// MAIN LOOP
stringstream out;
size_t bestInd = 0;
while (bestInd < system.nbestSize && contenders.GetSize()) {
cerr << "bestInd=" << bestInd << endl;
//cerr << "bestInd=" << bestInd << endl;
TrellisPath *path = contenders.pop();
out << transId << " ||| ";
path->OutputToStream(out, system);
out << "\n";
// create next paths
path->CreateDeviantPaths(contenders);
++bestInd;
}
system.GetNBestCollector().Write(transId, out.str());

View File

@ -6,6 +6,7 @@
*/
#include <cassert>
#include "TrellisPath.h"
#include "TrellisPaths.h"
#include "Hypothesis.h"
using namespace std;
@ -18,17 +19,19 @@ std::ostream& operator<<(std::ostream &out, const TrellishNode &node)
return out;
}
TrellisPath::TrellisPath() {
// TODO Auto-generated constructor stub
}
/////////////////////////////////////////////////////////////////////////////////
TrellisPath::TrellisPath(const Hypothesis *hypo, const ArcLists &arcLists)
:prevEdgeChanged(-1)
{
AddNodes(hypo, arcLists);
m_scores = &hypo->GetScores();
}
TrellisPath::TrellisPath(const TrellisPath &origPath, size_t edgeIndex, const Hypothesis *arc)
{
}
TrellisPath::~TrellisPath() {
// TODO Auto-generated destructor stub
}
@ -41,10 +44,6 @@ SCORE TrellisPath::GetFutureScore() const
void TrellisPath::AddNodes(const Hypothesis *hypo, const ArcLists &arcLists)
{
if (hypo) {
// add prev hypos
const Hypothesis *prev = hypo->GetPrevHypo();
AddNodes(prev, arcLists);
// add this hypo
//cerr << "hypo=" << hypo << " " << flush;
//cerr << *hypo << endl;
@ -52,13 +51,17 @@ void TrellisPath::AddNodes(const Hypothesis *hypo, const ArcLists &arcLists)
assert(list);
TrellishNode *node = new TrellishNode(*list, 0);
nodes.push_back(node);
// add prev hypos
const Hypothesis *prev = hypo->GetPrevHypo();
AddNodes(prev, arcLists);
}
}
void TrellisPath::OutputToStream(std::ostream &out, const System &system) const
{
cerr << "path=" << this << " " << nodes.size() << endl;
for (size_t i = 0; i < nodes.size(); ++i) {
//cerr << "path=" << this << " " << nodes.size() << endl;
for (int i = nodes.size() - 1; i >= 0; --i) {
const TrellishNode *node = nodes[i];
const Hypothesis *hypo = static_cast<const Hypothesis*>(node->arcList[node->ind]);
//cerr << "hypo=" << hypo << " " << *hypo << endl;
@ -70,4 +73,22 @@ void TrellisPath::OutputToStream(std::ostream &out, const System &system) const
GetScores().OutputToStream(out, system);
}
void TrellisPath::CreateDeviantPaths(TrellisPaths &paths) const
{
const size_t sizePath = nodes.size();
for (size_t currEdge = prevEdgeChanged + 1 ; currEdge < sizePath ; currEdge++) {
const TrellishNode &node = *nodes[currEdge];
assert(node.ind == 0);
const ArcList &arcList = node.arcList;
for (size_t i = 1; i < arcList.size(); ++i) {
const Hypothesis *arcReplace = static_cast<const Hypothesis *>(arcList[i]);
TrellisPath *deviantPath = new TrellisPath(*this, currEdge, arcReplace);
paths.Add(deviantPath);
}
}
}
} /* namespace Moses2 */

View File

@ -14,6 +14,7 @@ namespace Moses2 {
class Scores;
class Hypothesis;
class System;
class TrellisPaths;
class TrellishNode
{
@ -32,9 +33,18 @@ public:
class TrellisPath {
public:
std::vector<const TrellishNode *> nodes;
int prevEdgeChanged;
/**< the last node that was wiggled to create this path
, or NOT_FOUND if this path is the best trans so consist of only hypos
*/
TrellisPath();
TrellisPath(const Hypothesis *hypo, const ArcLists &arcLists);
/** create path from another path, deviate at edgeIndex by using arc instead,
* which may change other hypo back from there
*/
TrellisPath(const TrellisPath &origPath, size_t edgeIndex, const Hypothesis *arc);
virtual ~TrellisPath();
const Scores &GetScores() const
@ -43,6 +53,9 @@ public:
void OutputToStream(std::ostream &out, const System &system) const;
//! create a set of next best paths by wiggling 1 of the node at a time.
void CreateDeviantPaths(TrellisPaths &paths) const;
protected:
const Scores *m_scores;