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

ConfusionNet.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: af4a49a7c9292b188f110e7dd2d53e3d00c5fc9d (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
// $Id$

#include "ConfusionNet.h"
#include <sstream>

#include "FactorCollection.h"
#include "Util.h"
#include "PhraseDictionaryTreeAdaptor.h"
#include "TranslationOptionCollectionConfusionNet.h"
#include "StaticData.h"
#include "Sentence.h"

struct CNStats {
	unsigned created,destr,read,colls,words;

	CNStats() : created(0),destr(0),read(0),colls(0),words(0) {}
	~CNStats() {print(std::cerr);}

	void createOne() {++created;}
	void destroyOne() {++destr;}

	void collect(const ConfusionNet& cn)
	{
		++read;
		colls+=cn.GetSize();
		for(size_t i=0;i<cn.GetSize();++i)
			words+=cn[i].size();
	}
	void print(std::ostream& out) const
	{
		if(created>0)
			{
				out<<"confusion net statistics:\n"
					" created:\t"<<created<<"\n"
					" destroyed:\t"<<destr<<"\n"
					" succ. read:\t"<<read<<"\n"
					" columns:\t"<<colls<<"\n"
					" words:\t"<<words<<"\n"
					" avg. word/column:\t"<<words/(1.0*colls)<<"\n"
					" avg. cols/sent:\t"<<colls/(1.0*read)<<"\n"
					"\n\n";
			}
	}

};

CNStats stats;


ConfusionNet::ConfusionNet(FactorCollection* p) 
	: InputType(),m_factorCollection(p) {stats.createOne();}
ConfusionNet::~ConfusionNet() {stats.destroyOne();}

ConfusionNet::ConfusionNet(Sentence const& s)
{
	data.resize(s.GetSize());
	for(size_t i=0;i<s.GetSize();++i)
		data[i].push_back(std::make_pair(Word(s.GetFactorArray(i)),0.0));
}


void ConfusionNet::SetFactorCollection(FactorCollection *p) 
{
	m_factorCollection=p;
}
bool ConfusionNet::ReadF(std::istream& in,
												 const std::vector<FactorType>& factorOrder,
												 int format) 
{
	TRACE_ERR("read confusion net with format "<<format<<"\n");
	switch(format) 
		{
		case 0: return ReadFormat0(in,factorOrder);
		case 1: return ReadFormat1(in,factorOrder);
		default: 
			std::cerr<<"ERROR: unknown format '"<<format
							 <<"' in ConfusionNet::Read\n";
		}
	return 0;
}

int ConfusionNet::Read(std::istream& in,
											 const std::vector<FactorType>& factorOrder, 
											 FactorCollection &factorCollection) 
{
	SetFactorCollection(&factorCollection);
	int rv=ReadF(in,factorOrder,0);
	if(rv) stats.collect(*this);
	return rv;
}


void ConfusionNet::String2Word(const std::string& s,Word& w,
															 const std::vector<FactorType>& factorOrder) 
{
	std::vector<std::string> factorStrVector = Tokenize(s, "|");
	for(size_t i=0;i<factorOrder.size();++i) 
		w.SetFactor(factorOrder[i],
								m_factorCollection->AddFactor(Input,factorOrder[i],
																							factorStrVector[i]));
}

bool ConfusionNet::ReadFormat0(std::istream& in,
															 const std::vector<FactorType>& factorOrder) 
{
	assert(m_factorCollection);
	Clear();
	std::string line;
	while(getline(in,line)) {
		std::istringstream is(line);
		std::string word;double prob;
		Column col;
		while(is>>word>>prob) {
			Word w;
			String2Word(word,w,factorOrder);
			if(prob<0.0) 
				{
					std::cerr<<"WARN: negative prob: "<<prob<<" ->set to 0.0\n";
					prob=0.0;
				}
			else if (prob>1.0)
				{
					std::cerr<<"WARN: prob > 1.0 : "<<prob<<" -> set to 1.0\n";
					prob=1.0;
				}
			col.push_back(std::make_pair(w,std::max(static_cast<float>(log(prob)),
																							LOWEST_SCORE)));
		}
		if(col.size()) {
			data.push_back(col);
			ShrinkToFit(data.back());
		}
		else break;
	}
	return !data.empty();
}
bool ConfusionNet::ReadFormat1(std::istream& in,
															 const std::vector<FactorType>& factorOrder) 
{
	assert(m_factorCollection);
	Clear();
	std::string line;
	if(!getline(in,line)) return 0;
	size_t s;
	if(getline(in,line)) s=atoi(line.c_str()); else return 0;
	data.resize(s);
	for(size_t i=0;i<data.size();++i) {
		if(!getline(in,line)) return 0;
		std::istringstream is(line);
		if(!(is>>s)) return 0;
		std::string word;double prob;
		data[i].resize(s);
		for(size_t j=0;j<s;++j)
			if(is>>word>>prob) {
				data[i][j].second=log(prob); 
				if(data[i][j].second<0) {
					std::cerr<<"WARN: neg costs: "<<data[i][j].second<<" -> set to 0\n";
					data[i][j].second=0.0;}
				String2Word(word,data[i][j].first,factorOrder);
			} else return 0;
	}
	return !data.empty();
}

void ConfusionNet::Print(std::ostream& out) const {
	out<<"conf net: "<<data.size()<<"\n";
	for(size_t i=0;i<data.size();++i) {
		out<<i<<" -- ";
		for(size_t j=0;j<data[i].size();++j)
			out<<"("<<data[i][j].first.ToString()<<", "<<data[i][j].second<<") ";
		out<<"\n";
	}
	out<<"\n\n";
}

Phrase ConfusionNet::GetSubString(const WordsRange&) const {
	std::cerr<<"ERROR: call to ConfusionNet::GetSubString\n";
	abort();
	return Phrase(Input);
}

std::string ConfusionNet::GetStringRep(const WordsRange&) const{ //not well defined yet
	std::cerr<<"ERROR: call to ConfusionNet::GeStringRep\n";
	abort();
	return "";
}
#pragma warning(disable:4716)
const FactorArray& ConfusionNet::GetFactorArray(size_t) const {
	std::cerr<<"ERROR: call to ConfusionNet::GetFactorArray\n";
	abort();
}
#pragma warning(default:4716)

std::ostream& operator<<(std::ostream& out,const ConfusionNet& cn) 
{
	cn.Print(out);return out;
}

TargetPhraseCollection const* ConfusionNet::
CreateTargetPhraseCollection(PhraseDictionaryBase const& d,
														 const WordsRange& r) const 
{
	if(PhraseDictionaryTreeAdaptor const* pdict=
		 dynamic_cast<PhraseDictionaryTreeAdaptor const*>(&d))
		return pdict->GetTargetPhraseCollection(*this,r);

	std::cerr<<"ERROR: wrong phrase dictionary type for confusion net decoding!"
		"  Has to be PhraseDictionaryTreeAdaptor\n";
	abort();
}

TranslationOptionCollection* 
ConfusionNet::CreateTranslationOptionCollection() const 
{
	size_t maxNoTransOptPerCoverage = StaticData::Instance()->GetMaxNoTransOptPerCoverage();
	TranslationOptionCollection *rv= new TranslationOptionCollectionConfusionNet(*this, maxNoTransOptPerCoverage);
	assert(rv);
	return rv;
}