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

HypothesisCollection.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b7561eb934157420c68d51be050cb1b1fa7fbf9a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// $Id$

/***********************************************************************
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 <algorithm>
#include <set>
#include <queue>
#include "HypothesisCollection.h"
#include "TypeDef.h"
#include "Util.h"
#include "StaticData.h"

using namespace std;

size_t HypothesisRecombinationOrderer::s_ngramMaxOrder[NUM_FACTORS] = {0,0,0,0};
	// need to change if we add more factors, or use a macro

void HypothesisCollection::RemoveAll()
{
	while (m_hypos.begin() != m_hypos.end())
	{
		Remove(m_hypos.begin());
	}
}
 
void HypothesisCollection::Add(Hypothesis *hypo)
{

	AddNoPrune(hypo);

	if (hypo->GetTotalScore() > m_bestScore)
	{
		m_bestScore = hypo->GetTotalScore();
        if ( m_bestScore + m_beamThreshold > m_worstScore )
          m_worstScore = m_bestScore + m_beamThreshold;
	}

    // Prune only of stack is twice as big as needed
	if (m_hypos.size() > 2*m_maxHypoStackSize-10)
	{
		PruneToSize(m_maxHypoStackSize);
	}
}

void HypothesisCollection::AddPrune(Hypothesis *hypo)
{ // if returns false, hypothesis not used
	// caller must take care to delete unused hypo to avoid leak

	if (hypo->GetTotalScore() < m_worstScore)
	{ // really bad score. don't bother adding hypo into collection
		ObjectPool<Hypothesis> &pool = Hypothesis::GetObjectPool();
		pool.freeObject(hypo);		
		return;
	}

	// over threshold		
	// recombine if ngram-equivalent to another hypo
	iterator iter = m_hypos.find(hypo);
	if (iter == m_hypos.end())
	{ // nothing found. add to collection
		Add(hypo);
		return;
  }

	StaticData::Instance()->GetSentenceStats().numRecombinations++;
	
	// found existing hypo with same target ending.
	// keep the best 1
	Hypothesis *hypoExisting = *iter;
	if (hypo->GetTotalScore() > hypoExisting->GetTotalScore())
	{ // incoming hypo is better than the 1 we have
		#ifdef N_BEST
			hypo->AddArc(hypoExisting);
			Detach(iter);
		#else
			Remove(iter);
		#endif
		Add(hypo);		
		return;
	}
	else
	{ // already storing the best hypo. discard current hypo 
		#ifdef N_BEST
			(*iter)->AddArc(hypo);
		#else
			ObjectPool<Hypothesis> &pool = Hypothesis::GetObjectPool();
			pool.freeObject(hypo);				
		#endif
		return;
	}
}

void HypothesisCollection::PruneToSize(size_t newSize)
{
	if (m_hypos.size() > newSize)
	{
        // Pruning alg: find a threshold and delete all hypothesis below it
        //   the threshold is chosen so that exactly newSize top items remain on the stack
        //   in fact, in situations where some of the hypothesis fell below m_beamThreshold,
        //   the stack will contain less items

		priority_queue<float> bestScores;

        // cerr << "About to prune from " << size() << " to " << newSize << endl;
        // push all scores to a heap
        //   (but never push scores below m_bestScore+m_beamThreshold)
		iterator iter = m_hypos.begin();
        float score = 0;
		while (iter != m_hypos.end())
		{
			Hypothesis *hypo = *iter;
			score = hypo->GetTotalScore();
            // cerr << "H score: " << score << ", mbestscore: " << m_bestScore << " + m_beamThreshold "<< m_beamThreshold << " = " << m_bestScore+m_beamThreshold;
            if (score > m_bestScore+m_beamThreshold) {
			  bestScores.push(score);
              // cerr << " pushed.";
            }
            // cerr << endl;
            ++iter;
        }
        // cerr << "Heap contains " << bestScores.size() << " items" << endl;
        
        // pop the top newSize scores (and ignore them, these are the scores of hyps that will remain)
        //  ensure to never pop beyond heap size
        size_t minNewSizeHeapSize = newSize > bestScores.size() ? bestScores.size() : newSize;
		for (size_t i = 1 ; i < minNewSizeHeapSize ; i++)
          bestScores.pop();

        // cerr << "Popped "<< newSize << ", heap now contains " << bestScores.size() << " items" << endl;

        // and remember the threshold
        float scoreThreshold = bestScores.top();
        // cerr << "threshold: " << scoreThreshold << endl;

		// delete all hypos under score threshold
		iter = m_hypos.begin();
		while (iter != m_hypos.end())
		{
			Hypothesis *hypo = *iter;
			float score = hypo->GetTotalScore();
			if (score < scoreThreshold)
			{
				iterator iterRemove = iter++;
				Remove(iterRemove);
				StaticData::Instance()->GetSentenceStats().numPruned++;
			}
			else
			{
				++iter;
			}
		}
        // cerr << "Stack size after pruning: " << size() << endl;

        // set the worstScore, so that newly generated hypotheses will not be added if worse than the worst in the stack
        m_worstScore = scoreThreshold;
	}
}

const Hypothesis *HypothesisCollection::GetBestHypothesis() const
{
	if (!m_hypos.empty())
	{
		const_iterator iter = m_hypos.begin();
		Hypothesis *bestHypo = *iter;
		while (++iter != m_hypos.end())
		{
			Hypothesis *hypo = *iter;
			if (hypo->GetTotalScore() > bestHypo->GetTotalScore())
				bestHypo = hypo;
		}
		return bestHypo;
	}
	return NULL;
}

// sorting helper
struct HypothesisSortDescending
{
	const bool operator()(const Hypothesis* hypo1, const Hypothesis* hypo2) const
	{
		return hypo1->GetTotalScore() > hypo2->GetTotalScore();
	}
};

vector<const Hypothesis*> HypothesisCollection::GetSortedList() const
{
	vector<const Hypothesis*> ret; ret.reserve(m_hypos.size());
	std::copy(m_hypos.begin(), m_hypos.end(), std::inserter(ret, ret.end()));
	sort(ret.begin(), ret.end(), HypothesisSortDescending());

	return ret;
}


void HypothesisCollection::InitializeArcs()
{
#ifdef N_BEST
	iterator iter;
	for (iter = m_hypos.begin() ; iter != m_hypos.end() ; ++iter)
	{
		Hypothesis *mainHypo = *iter;
		mainHypo->InitializeArcs();
	}
#endif
}

TO_STRING_BODY(HypothesisCollection);


// friend
std::ostream& operator<<(std::ostream& out, const HypothesisCollection& hypoColl)
{
	HypothesisCollection::const_iterator iter;
	
	for (iter = hypoColl.begin() ; iter != hypoColl.end() ; ++iter)
	{
		const Hypothesis &hypo = **iter;
		out << hypo << endl;
		
	}
	return out;
}