Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SquareMatrix.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe8c2516d207e0908fb08824315a47155251fecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// $Id: SquareMatrix.cpp 1964 2008-12-20 20:22:35Z phkoehn $
// vim:tabstop=2

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

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.

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.

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
***********************************************************************/

#include <string>
#include <iostream>
#include "SquareMatrix.h"
#include "TypeDef.h"
#include "Util.h"

using namespace std;

namespace Moses
{

/**
 * Calculare future score estimate for a given coverage bitmap
 *
 * /param bitmap coverage bitmap
 */

float SquareMatrix::CalcFutureScore( WordsBitmap const &bitmap ) const
{
	const size_t notInGap= numeric_limits<size_t>::max();
	size_t startGap = notInGap;
	float futureScore = 0.0f;
	for(size_t currPos = 0 ; currPos < bitmap.GetSize() ; currPos++)
	{
		// start of a new gap?
		if(bitmap.GetValue(currPos) == false && startGap == notInGap)
		{
			startGap = currPos;
		}
		// end of a gap?
		else if(bitmap.GetValue(currPos) == true && startGap != notInGap)
		{
			futureScore += GetScore(startGap, currPos - 1);
			startGap = notInGap;
		}
	}
	// coverage ending with gap?
	if (startGap != notInGap)
	{
		futureScore += GetScore(startGap, bitmap.GetSize() - 1);
	}

	return futureScore;
}

/**
 * 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
 */

float SquareMatrix::CalcFutureScore( WordsBitmap const &bitmap, size_t startPos, size_t endPos ) const
{
	const size_t notInGap= numeric_limits<size_t>::max();
	float futureScore = 0.0f;
	size_t startGap = bitmap.GetFirstGapPos();
	if (startGap == NOT_FOUND) return futureScore; // everything filled

	// start loop at first gap
	size_t startLoop = startGap+1;
	if (startPos == startGap) // unless covered by phrase
	{
		startGap = notInGap;
		startLoop = endPos+1; // -> postpone start
	}

	size_t lastCovered = bitmap.GetLastPos();
	if (endPos > lastCovered || lastCovered == NOT_FOUND) lastCovered = endPos;

	for(size_t currPos = startLoop; currPos <= lastCovered ; currPos++)
	{
		// start of a new gap?
		if(startGap == notInGap && bitmap.GetValue(currPos) == false && (currPos < startPos || currPos > endPos))
		{
			startGap = currPos;
		}
		// end of a gap?
		else if(startGap != notInGap && (bitmap.GetValue(currPos) == true || (startPos <= currPos && currPos <= endPos)))
		{
			futureScore += GetScore(startGap, currPos - 1);
			startGap = notInGap;
		}
	}
	// coverage ending with gap?
	if (lastCovered != bitmap.GetSize() - 1)
	{
		futureScore += GetScore(lastCovered+1, bitmap.GetSize() - 1);
	}

	return futureScore;
}

TO_STRING_BODY(SquareMatrix);

}