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

NBest.cpp « nbest « SCFG « moses2 « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8515d49b62cc3d40fb30a746d6c51d69ff79ce6 (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
/*
 * NBest.cpp
 *
 *  Created on: 24 Aug 2016
 *      Author: hieu
 */
#include <sstream>
#include <boost/foreach.hpp>
#include "util/exception.hh"
#include "NBest.h"
#include "NBests.h"
#include "NBestColl.h"
#include "../Manager.h"
#include "../TargetPhraseImpl.h"
#include "../../System.h"

using namespace std;

namespace Moses2
{
namespace SCFG
{

NBest::NBest(
		const SCFG::Manager &mgr,
		const ArcList &varcList,
		size_t vind,
		NBestColl &nbestColl)
:arcList(&varcList)
,arcInd(vind)
{
	const SCFG::Hypothesis &hypo = GetHypo();

	// copy scores from best hypo
	MemPool &pool = mgr.GetPool();
	m_scores = new (pool.Allocate<Scores>())
				Scores(mgr.system,  pool, mgr.system.featureFunctions.GetNumScores(), hypo.GetScores());

	// children
	const ArcLists &arcLists = mgr.arcLists;
	//const SCFG::TargetPhraseImpl &tp = hypo.GetTargetPhrase();

	const Vector<const Hypothesis*> &prevHypos = hypo.GetPrevHypos();
	for (size_t i = 0; i < prevHypos.size(); ++i) {
		const SCFG::Hypothesis *prevHypo = prevHypos[i];
		const ArcList &childArc = arcLists.GetArcList(prevHypo);
		NBests &childNBests = nbestColl.GetOrCreateNBests(mgr, childArc);
		Child child(&childNBests, 0);
		children.push_back(child);
	}

	stringstream strm;
	OutputToStream(mgr, strm, nbestColl);
	m_str = strm.str();
}

NBest::NBest(const SCFG::Manager &mgr,
		const NBest &orig,
		size_t childInd,
		NBestColl &nbestColl)
:arcList(orig.arcList)
,arcInd(orig.arcInd)
,children(orig.children)
{
	Child &child = children[childInd];
	size_t &ind = child.second;
	++ind;
	UTIL_THROW_IF2(ind >= child.first->GetSize(),
			"out of bound:" << ind << ">=" << child.first->GetSize());

	// scores
	MemPool &pool = mgr.GetPool();
	m_scores = new (pool.Allocate<Scores>())
				Scores(mgr.system,
						pool,
						mgr.system.featureFunctions.GetNumScores(),
						orig.GetScores());

	const Scores &origScores = orig.GetChild(childInd).GetScores();
	const Scores &newScores = GetChild(childInd).GetScores();

	m_scores->MinusEquals(mgr.system, origScores);
	m_scores->PlusEquals(mgr.system, newScores);

	stringstream strm;
	OutputToStream(mgr, strm, nbestColl);
	m_str = strm.str();
}

const SCFG::Hypothesis &NBest::GetHypo() const
{
	const HypothesisBase *hypoBase = (*arcList)[arcInd];
	const SCFG::Hypothesis &hypo = *static_cast<const SCFG::Hypothesis*>(hypoBase);
	return hypo;
}

const NBest &NBest::GetChild(size_t ind) const
{
	const Child &child = children[ind];
	const NBests &nbests = *child.first;
	const NBest &nbest = nbests.Get(child.second);
	return nbest;
}


void NBest::CreateDeviants(
		const SCFG::Manager &mgr,
		NBestColl &nbestColl,
		Contenders &contenders) const
{
	if (arcInd + 1 < arcList->size()) {
		// to use next arclist, all children must be 1st. Not sure if this is correct
		bool ok = true;
		BOOST_FOREACH(const Child &child, children) {
			if (child.second) {
				ok = false;
				break;
			}
		}

		if (ok) {
			NBest *next = new NBest(mgr, *arcList, arcInd + 1, nbestColl);
			contenders.push(next);
		}
	}

	for (size_t childInd = 0; childInd < children.size(); ++childInd) {
		const Child &child = children[childInd];
		NBests &childNBests = *child.first;
		bool extended = childNBests.Extend(mgr, nbestColl, child.second + 1);
		if (extended) {
			//cerr << "HH1 " << childInd << endl;
			NBest *next = new NBest(mgr, *this, childInd, nbestColl);

			//cerr << "HH2 " << childInd << endl;
			contenders.push(next);
			//cerr << "HH3 " << childInd << endl;
		}
	}
}

void NBest::OutputToStream(
		const SCFG::Manager &mgr,
		std::stringstream &strm,
		const NBestColl &nbestColl) const
{
  const SCFG::Hypothesis &hypo = GetHypo();
  //strm << &hypo << " ";

  const SCFG::TargetPhraseImpl &tp = hypo.GetTargetPhrase();

  for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
	const SCFG::Word &word = tp[targetPos];
	//cerr << "word " << pos << "=" << word << endl;
	if (word.isNonTerminal) {
	  //cerr << "is nt" << endl;
	  // non-term. fill out with prev hypo
	  size_t nonTermInd = tp.GetAlignNonTerm().GetNonTermIndexMap()[targetPos];

	  UTIL_THROW_IF2(nonTermInd >= children.size(), "Out of bounds:" << nonTermInd << ">=" << children.size());

	  const NBest &nbest = GetChild(nonTermInd);
	  strm << nbest.GetString();
	}
	else {
	  //cerr << "not nt" << endl;
      word.OutputToStream(hypo.GetManager(), targetPos, hypo, strm);

	  strm << " ";
	}
  }
}

std::string NBest::Debug(const System &system) const
{
	stringstream strm;
	strm << GetScores().GetTotalScore() << " "
			<< arcList << "("
			<< arcList->size() << ")["
			<< arcInd << "] ";
	for (size_t i = 0; i < children.size(); ++i) {
		const Child &child = children[i];
		const NBest &childNBest = child.first->Get(child.second);

		strm << child.first << "("
				<< child.first->GetSize() << ")["
				<< child.second << "]";
		strm << childNBest.GetScores().GetTotalScore() << " ";
	}
	return strm.str();
}

}
}