mosesdecoder/moses2/EstimatedScores.cpp

118 lines
3.7 KiB
C++
Raw Permalink Normal View History

2016-02-09 18:36:53 +03:00
// $Id$
// vim:tabstop=2
/***********************************************************************
2016-03-31 23:00:16 +03:00
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh
2016-02-09 18:36:53 +03:00
2016-03-31 23:00:16 +03:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
2016-02-09 18:36:53 +03:00
2016-03-31 23:00:16 +03:00
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2016-02-09 18:36:53 +03:00
2016-03-31 23:00:16 +03:00
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
2016-02-09 18:36:53 +03:00
#include <string>
#include <iostream>
#include "EstimatedScores.h"
using namespace std;
namespace Moses2
{
/**
* Calculate future score estimate for a given coverage bitmap
*
* /param bitmap coverage bitmap
*/
2016-03-31 23:00:16 +03:00
float EstimatedScores::CalcEstimatedScore(Bitmap const &bitmap) const
2016-02-09 18:36:53 +03:00
{
2016-03-31 23:00:16 +03:00
const size_t notInGap = numeric_limits<size_t>::max();
2016-02-09 18:36:53 +03:00
size_t startGap = notInGap;
float estimatedScore = 0.0f;
2016-03-31 23:00:16 +03:00
for (size_t currPos = 0; currPos < bitmap.GetSize(); currPos++) {
2016-02-09 18:36:53 +03:00
// start of a new gap?
2016-03-31 23:00:16 +03:00
if (bitmap.GetValue(currPos) == false && startGap == notInGap) {
2016-02-09 18:36:53 +03:00
startGap = currPos;
}
// end of a gap?
2016-03-31 23:00:16 +03:00
else if (bitmap.GetValue(currPos) == true && startGap != notInGap) {
2016-02-09 18:36:53 +03:00
estimatedScore += GetValue(startGap, currPos - 1);
startGap = notInGap;
}
}
// coverage ending with gap?
if (startGap != notInGap) {
estimatedScore += GetValue(startGap, bitmap.GetSize() - 1);
}
return estimatedScore;
}
/**
* Calculare future score estimate for a given coverage bitmap
* and an additional span that is also covered. This function is used
* to compute future score estimates for hypotheses that we may want
* build, but first want to check.
*
* Note: this function is implemented a bit more complex than
* the basic one (w/o additional phrase) for speed reasons,
* which is probably overkill.
*
* /param bitmap coverage bitmap
* /param startPos start of the span that is added to the coverage
* /param endPos end of the span that is added to the coverage
*/
2016-03-31 23:00:16 +03:00
float EstimatedScores::CalcEstimatedScore(Bitmap const &bitmap, size_t startPos,
size_t endPos) const
2016-02-09 18:36:53 +03:00
{
2016-03-31 23:00:16 +03:00
const size_t notInGap = numeric_limits<size_t>::max();
2016-02-09 18:36:53 +03:00
float estimatedScore = 0.0f;
size_t startGap = bitmap.GetFirstGapPos();
if (startGap == NOT_FOUND) return estimatedScore; // everything filled
// start loop at first gap
2016-03-31 23:00:16 +03:00
size_t startLoop = startGap + 1;
2016-02-09 18:36:53 +03:00
if (startPos == startGap) { // unless covered by phrase
startGap = notInGap;
2016-03-31 23:00:16 +03:00
startLoop = endPos + 1; // -> postpone start
2016-02-09 18:36:53 +03:00
}
size_t lastCovered = bitmap.GetLastPos();
if (endPos > lastCovered || lastCovered == NOT_FOUND) lastCovered = endPos;
2016-03-31 23:00:16 +03:00
for (size_t currPos = startLoop; currPos <= lastCovered; currPos++) {
2016-02-09 18:36:53 +03:00
// start of a new gap?
2016-03-31 23:00:16 +03:00
if (startGap == notInGap && bitmap.GetValue(currPos) == false
&& (currPos < startPos || currPos > endPos)) {
2016-02-09 18:36:53 +03:00
startGap = currPos;
}
// end of a gap?
2016-03-31 23:00:16 +03:00
else if (startGap != notInGap
2017-02-01 03:10:16 +03:00
&& (bitmap.GetValue(currPos) == true
|| (startPos <= currPos && currPos <= endPos))) {
2016-02-09 18:36:53 +03:00
estimatedScore += GetValue(startGap, currPos - 1);
startGap = notInGap;
}
}
// coverage ending with gap?
if (lastCovered != bitmap.GetSize() - 1) {
2016-03-31 23:00:16 +03:00
estimatedScore += GetValue(lastCovered + 1, bitmap.GetSize() - 1);
2016-02-09 18:36:53 +03:00
}
return estimatedScore;
}
}