WIP. Fix for incorrect alignment from moses_chart

This commit is contained in:
Hieu Hoang 2013-02-26 18:27:55 +00:00
parent 1bba58b134
commit 1ee365e0d7

View File

@ -620,9 +620,15 @@ void IOWrapper::FixPrecision(std::ostream &stream, size_t size)
template <class T>
void ShiftOffsets(vector<T> &offsets, T shift)
{
T currPos = shift;
for (size_t i = 0; i < offsets.size(); ++i) {
shift += offsets[i];
offsets[i] += shift;
if (offsets[i] == 0) {
offsets[i] = currPos;
++currPos;
}
else {
currPos += offsets[i];
}
}
}
@ -716,6 +722,17 @@ void IOWrapper::OutputAlignment(size_t translationId , const Moses::ChartHypothe
m_alignmentInfoCollector->Write(translationId, out.str());
}
size_t CalcSourceSize(const Moses::ChartHypothesis *hypo)
{
size_t ret = hypo->GetCurrSourceRange().GetNumWordsCovered();
const std::vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
for (size_t i = 0; i < prevHypos.size(); ++i) {
size_t childSize = prevHypos[i]->GetCurrSourceRange().GetNumWordsCovered();
ret -= (childSize - 1);
}
return ret;
}
size_t IOWrapper::OutputAlignment(Alignments &retAlign, const Moses::ChartHypothesis *hypo, size_t startTarget)
{
size_t totalTargetSize = 0;
@ -723,7 +740,14 @@ size_t IOWrapper::OutputAlignment(Alignments &retAlign, const Moses::ChartHypoth
const TargetPhrase &tp = hypo->GetCurrTargetPhrase();
vector<size_t> sourceOffsets(hypo->GetCurrSourceRange().GetNumWordsCovered(), 0);
if (hypo->GetCurrSourceRange().GetStartPos() == 12
&& hypo->GetCurrSourceRange().GetEndPos() == 18)
{
cerr << "stop" << endl;
}
size_t thisSourceSize = CalcSourceSize(hypo);
vector<size_t> sourceOffsets(thisSourceSize, 0);
vector<size_t> targetOffsets(tp.GetSize(), 0);
const vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
@ -765,22 +789,21 @@ size_t IOWrapper::OutputAlignment(Alignments &retAlign, const Moses::ChartHypoth
ShiftOffsets(targetOffsets, startTarget);
// get alignments from this hypo
vector< set<size_t> > retAlignmentsS2T(hypo->GetCurrSourceRange().GetNumWordsCovered());
const AlignmentInfo &aiTerm = hypo->GetCurrTargetPhrase().GetAlignTerm();
OutputAlignment(retAlignmentsS2T, aiTerm);
// add to output arg, offsetting by source & target
for (size_t source = 0; source < retAlignmentsS2T.size(); ++source) {
const set<size_t> &targets = retAlignmentsS2T[source];
set<size_t>::const_iterator iter;
for (iter = targets.begin(); iter != targets.end(); ++iter) {
size_t target = *iter;
pair<size_t, size_t> alignPoint(source + sourceOffsets[source]
,target + targetOffsets[target]);
pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint);
CHECK(ret.second);
AlignmentInfo::const_iterator iter;
for (iter = aiTerm.begin(); iter != aiTerm.end(); ++iter) {
const std::pair<size_t,size_t> &align = *iter;
size_t relSource = align.first;
size_t relTarget = align.second;
size_t absSource = sourceOffsets[relSource];
size_t absTarget = targetOffsets[relTarget];
pair<size_t, size_t> alignPoint(absSource, absTarget);
pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint);
CHECK(ret.second);
}
}
return totalTargetSize;