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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
#include "PrefixTreeMap.h"
#include "TypeDef.h"
#ifdef WITH_THREADS
#include <boost/thread.hpp>
#endif
using namespace std;
namespace Moses
{
void GenericCandidate::readBin(FILE* f)
{
m_PhraseList.clear();
m_ScoreList.clear();
UINT32 num_phrases; // on older compilers, <stdint.h> may need to be included
fRead(f, num_phrases);
for(unsigned int i = 0; i < num_phrases; ++i) {
IPhrase phrase;
fReadVector(f, phrase);
m_PhraseList.push_back(phrase);
};
UINT32 num_scores;
fRead(f, num_scores);
for(unsigned int j = 0; j < num_scores; ++j) {
std::vector<float> score;
fReadVector(f, score);
m_ScoreList.push_back(score);
};
};
void GenericCandidate::writeBin(FILE* f) const
{
// cast is necessary to ensure compatibility between 32- and 64-bit platforms
fWrite(f, static_cast<UINT32>(m_PhraseList.size()));
for(size_t i = 0; i < m_PhraseList.size(); ++i) {
fWriteVector(f, m_PhraseList[i]);
}
fWrite(f, static_cast<UINT32>(m_ScoreList.size()));
for(size_t j = 0; j < m_ScoreList.size(); ++j) {
fWriteVector(f, m_ScoreList[j]);
}
};
void Candidates::writeBin(FILE* f) const
{
UINT32 s = this->size();
fWrite(f,s);
for(size_t i = 0; i < s; ++i) {
MyBase::operator[](i).writeBin(f);
}
}
void Candidates::readBin(FILE* f)
{
UINT32 s;
fRead(f,s);
this->resize(s);
for(size_t i = 0; i<s; ++i) {
MyBase::operator[](i).readBin(f);
}
}
const LabelId PrefixTreeMap::MagicWord = std::numeric_limits<LabelId>::max() - 1;
//////////////////////////////////////////////////////////////////
PrefixTreeMap::~PrefixTreeMap()
{
if(m_FileSrc) {
fClose(m_FileSrc);
}
if(m_FileTgt) {
fClose(m_FileTgt);
}
FreeMemory();
}
void PrefixTreeMap::FreeMemory()
{
for(Data::iterator i = m_Data.begin(); i != m_Data.end(); ++i) {
i->free();
}
/*for(size_t i = 0; i < m_Voc.size(); ++i){
delete m_Voc[i];
m_Voc[i] = 0;
}*/
m_PtrPool.reset();
}
WordVoc &ReadVoc(std::map<std::string,WordVoc> &vocs, const std::string& filename)
{
#ifdef WITH_THREADS
boost::mutex mutex;
boost::mutex::scoped_lock lock(mutex);
#endif
std::map<std::string,WordVoc>::iterator vi = vocs.find(filename);
if (vi == vocs.end()) {
WordVoc &voc = vocs[filename];
voc.Read(filename);
return voc;
} else {
return vi->second;
}
}
int PrefixTreeMap::Read(const std::string& fileNameStem, int numVocs)
{
std::string ifs(fileNameStem + ".srctree"),
ift(fileNameStem + ".tgtdata"),
ifi(fileNameStem + ".idx"),
ifv(fileNameStem + ".voc");
std::vector<OFF_T> srcOffsets;
FILE *ii=fOpen(ifi.c_str(),"rb");
fReadVector(ii,srcOffsets);
fClose(ii);
if (m_FileSrc) {
fClose(m_FileSrc);
}
m_FileSrc = fOpen(ifs.c_str(),"rb");
if (m_FileTgt) {
fClose(m_FileTgt);
}
m_FileTgt = fOpen(ift.c_str(),"rb");
m_Data.resize(srcOffsets.size());
for(size_t i = 0; i < m_Data.size(); ++i) {
m_Data[i] = CPT(m_FileSrc, srcOffsets[i]);
}
if(-1 == numVocs) {
char num[5];
numVocs = 0;
sprintf(num, "%d", numVocs);
while(FileExists(ifv + num)) {
++numVocs;
sprintf(num, "%d", numVocs);
}
}
char num[5];
m_Voc.resize(numVocs);
for(int i = 0; i < numVocs; ++i) {
sprintf(num, "%d", i);
//m_Voc[i] = new WordVoc();
//m_Voc[i]->Read(ifv + num);
m_Voc[i] = &ReadVoc(m_vocs, ifv + num);
}
TRACE_ERR("binary file loaded, default OFF_T: "<< PTF::getDefault()<<"\n");
return 1;
};
void PrefixTreeMap::GetCandidates(const IPhrase& key, Candidates* cands)
{
//check if key is valid
if(key.empty() || key[0] >= m_Data.size() || !m_Data[key[0]]) {
return;
}
UTIL_THROW_IF2(m_Data[key[0]]->findKey(key[0]) >= m_Data[key[0]]->size(),
"Key not found: " << key[0]);
OFF_T candOffset = m_Data[key[0]]->find(key);
if(candOffset == InvalidOffT) {
return;
}
fSeek(m_FileTgt,candOffset);
cands->readBin(m_FileTgt);
}
void PrefixTreeMap::GetCandidates(const PPimp& p, Candidates* cands)
{
UTIL_THROW_IF2(!p.isValid(), "Not a valid PPimp...");
if(p.isRoot()) {
return;
};
OFF_T candOffset = p.ptr()->getData(p.idx);
if(candOffset == InvalidOffT) {
return;
}
fSeek(m_FileTgt,candOffset);
cands->readBin(m_FileTgt);
}
std::vector< std::string const * > PrefixTreeMap::ConvertPhrase(const IPhrase& p, unsigned int voc) const
{
UTIL_THROW_IF2(voc >= m_Voc.size() || m_Voc[voc] == 0,
"Invalid vocab id: " << voc);
std::vector< std::string const * > result;
result.reserve(p.size());
for(IPhrase::const_iterator i = p.begin(); i != p.end(); ++i) {
result.push_back(&(m_Voc[voc]->symbol(*i)));
}
return result;
}
IPhrase PrefixTreeMap::ConvertPhrase(const std::vector< std::string >& p, unsigned int voc) const
{
UTIL_THROW_IF2(voc >= m_Voc.size() || m_Voc[voc] == 0,
"Invalid vocab id: " << voc);
IPhrase result;
result.reserve(p.size());
for(size_t i = 0; i < p.size(); ++i) {
result.push_back(m_Voc[voc]->index(p[i]));
}
return result;
}
LabelId PrefixTreeMap::ConvertWord(const std::string& w, unsigned int voc) const
{
UTIL_THROW_IF2(voc >= m_Voc.size() || m_Voc[voc] == 0,
"Invalid vocab id: " << voc);
return m_Voc[voc]->index(w);
}
std::string PrefixTreeMap::ConvertWord(LabelId w, unsigned int voc) const
{
UTIL_THROW_IF2(voc >= m_Voc.size() || m_Voc[voc] == 0,
"Invalid vocab id: " << voc);
if(w == PrefixTreeMap::MagicWord) {
return "|||";
} else if (w == InvalidLabelId) {
return "<invalid>";
} else {
return m_Voc[voc]->symbol(w);
}
}
PPimp* PrefixTreeMap::GetRoot()
{
return m_PtrPool.get(PPimp(0,0,1));
}
PPimp* PrefixTreeMap::Extend(PPimp* p, LabelId wi)
{
UTIL_THROW_IF2(!p->isValid(), "Not a valid PPimp...");
if(wi == InvalidLabelId) {
return 0; // unknown word, return invalid pointer
} else if(p->isRoot()) {
if(wi < m_Data.size() && m_Data[wi]) {
const void* ptr = m_Data[wi]->findKeyPtr(wi);
UTIL_THROW_IF2(ptr == NULL, "Null pointer");
return m_PtrPool.get(PPimp(m_Data[wi],m_Data[wi]->findKey(wi),0));
}
} else if(PTF const* nextP = p->ptr()->getPtr(p->idx)) {
return m_PtrPool.get(PPimp(nextP, nextP->findKey(wi),0));
}
return 0; // should never get here, return invalid pointer
}
}
|