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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// 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 <queue>
#include "moses/TranslationModel/PhraseDictionary.h"
#include "moses/StaticData.h"
#include "moses/InputType.h"
#include "moses/TranslationOption.h"
#include "moses/DecodeStep.h"
#include "moses/DecodeGraph.h"
#include "moses/InputPath.h"
#include "util/exception.hh"
using namespace std;
namespace Moses
{
std::vector<PhraseDictionary*> PhraseDictionary::s_staticColl;
// CacheColl::~CacheColl()
// {
// // not needed any more since the switch to shared pointers
// // for (iterator iter = begin(); iter != end(); ++iter) {
// // std::pair<TargetPhraseCollection::shared_ptr , clock_t> &key = iter->second;
// // TargetPhraseCollection::shared_ptr tps = key.first;
// // delete tps;
// // }
// }
PhraseDictionary::PhraseDictionary(const std::string &line, bool registerNow)
: DecodeFeature(line, registerNow)
, m_tableLimit(20) // default
, m_maxCacheSize(DEFAULT_MAX_TRANS_OPT_CACHE_SIZE)
{
m_id = s_staticColl.size();
s_staticColl.push_back(this);
}
bool
PhraseDictionary::
ProvidesPrefixCheck() const
{
return false;
}
TargetPhraseCollection::shared_ptr
PhraseDictionary::
GetTargetPhraseCollectionLEGACY(const Phrase& src) const
{
TargetPhraseCollection::shared_ptr ret;
typedef std::pair<TargetPhraseCollection::shared_ptr , clock_t> entry;
if (m_maxCacheSize) {
CacheColl &cache = GetCache();
size_t hash = hash_value(src);
CacheColl::iterator iter;
iter = cache.find(hash);
if (iter == cache.end()) {
// not in cache, need to look up from phrase table
ret = GetTargetPhraseCollectionNonCacheLEGACY(src);
if (ret) { // make a copy
ret.reset(new TargetPhraseCollection(*ret));
}
cache[hash] = entry(ret, clock());
} else {
// in cache. just use it
iter->second.second = clock();
ret = iter->second.first;
}
} else {
// don't use cache. look up from phrase table
ret = GetTargetPhraseCollectionNonCacheLEGACY(src);
}
return ret;
}
TargetPhraseCollection::shared_ptr
PhraseDictionary::
GetTargetPhraseCollectionNonCacheLEGACY(const Phrase& src) const
{
UTIL_THROW(util::Exception, "Legacy method not implemented");
}
TargetPhraseCollectionWithSourcePhrase::shared_ptr
PhraseDictionary::
GetTargetPhraseCollectionLEGACY(InputType const& src,WordsRange const& range) const
{
UTIL_THROW(util::Exception, "Legacy method not implemented");
}
void
PhraseDictionary::
SetParameter(const std::string& key, const std::string& value)
{
if (key == "cache-size") {
m_maxCacheSize = Scan<size_t>(value);
} else if (key == "path") {
m_filePath = value;
} else if (key == "table-limit") {
m_tableLimit = Scan<size_t>(value);
} else {
DecodeFeature::SetParameter(key, value);
}
}
void
PhraseDictionary::
SetFeaturesToApply()
{
// find out which feature function can be applied in this decode step
const std::vector<FeatureFunction*> &allFeatures = FeatureFunction::GetFeatureFunctions();
for (size_t i = 0; i < allFeatures.size(); ++i) {
FeatureFunction *feature = allFeatures[i];
if (feature->IsUseable(m_outputFactors)) {
m_featuresToApply.push_back(feature);
}
}
}
// // tell the Phrase Dictionary that the TargetPhraseCollection is not needed any more
// void
// PhraseDictionary::
// Release(ttasksptr const& ttask, TargetPhraseCollection const*& tpc) const
// {
// // do nothing by default
// return;
// }
bool
PhraseDictionary::
PrefixExists(ttasksptr const& ttask, Phrase const& phrase) const
{
return true;
}
void
PhraseDictionary::
GetTargetPhraseCollectionBatch(const InputPathList &inputPathQueue) const
{
InputPathList::const_iterator iter;
for (iter = inputPathQueue.begin(); iter != inputPathQueue.end(); ++iter) {
InputPath &inputPath = **iter;
// backoff
if (!SatisfyBackoff(inputPath)) {
continue;
}
const Phrase &phrase = inputPath.GetPhrase();
TargetPhraseCollection::shared_ptr targetPhrases = this->GetTargetPhraseCollectionLEGACY(phrase);
inputPath.SetTargetPhrases(*this, targetPhrases, NULL);
}
}
// persistent cache handling
// saving presistent cache to disk
//void PhraseDictionary::SaveCache() const
//{
// CacheColl &cache = GetCache();
// for( std::map<size_t, std::pair<TargetPhraseCollection::shared_ptr ,clock_t> >::iterator iter,
// iter != cache.end(),
// iter++ ) {
//
// }
//}
// loading persistent cache from disk
//void PhraseDictionary::LoadCache() const
//{
// CacheColl &cache = GetCache();
// std::map<size_t, std::pair<TargetPhraseCollection::shared_ptr ,clock_t> >::iterator iter;
// iter = cache.begin();
// while( iter != cache.end() ) {
// std::map<size_t, std::pair<TargetPhraseCollection::shared_ptr ,clock_t> >::iterator iterRemove = iter++;
// delete iterRemove->second.first;
// cache.erase(iterRemove);
// }
//}
// reduce presistent cache by half of maximum size
void PhraseDictionary::ReduceCache() const
{
Timer reduceCacheTime;
reduceCacheTime.start();
CacheColl &cache = GetCache();
if (cache.size() <= m_maxCacheSize) return; // not full
// find cutoff for last used time
priority_queue< clock_t > lastUsedTimes;
CacheColl::iterator iter;
iter = cache.begin();
while( iter != cache.end() ) {
lastUsedTimes.push( iter->second.second );
iter++;
}
for( size_t i=0; i < lastUsedTimes.size()-m_maxCacheSize/2; i++ )
lastUsedTimes.pop();
clock_t cutoffLastUsedTime = lastUsedTimes.top();
// remove all old entries
iter = cache.begin();
while( iter != cache.end() ) {
if (iter->second.second < cutoffLastUsedTime) {
CacheColl::iterator iterRemove = iter++;
// delete iterRemove->second.first;
cache.erase(iterRemove);
} else iter++;
}
VERBOSE(2,"Reduced persistent translation option cache in "
<< reduceCacheTime << " seconds." << std::endl);
}
CacheColl &PhraseDictionary::GetCache() const
{
CacheColl *cache;
cache = m_cache.get();
if (cache == NULL) {
cache = new CacheColl;
m_cache.reset(cache);
}
assert(cache);
return *cache;
}
bool PhraseDictionary::SatisfyBackoff(const InputPath &inputPath) const
{
const Phrase &sourcePhrase = inputPath.GetPhrase();
assert(m_container);
const DecodeGraph &decodeGraph = GetDecodeGraph();
size_t backoff = decodeGraph.GetBackoff();
if (backoff == 0) {
// ie. don't backoff. Collect ALL translations
return true;
}
if (sourcePhrase.GetSize() > backoff) {
// source phrase too big
return false;
}
// lookup translation only if no other translations
InputPath::TargetPhrases::const_iterator iter;
for (iter = inputPath.GetTargetPhrases().begin(); iter != inputPath.GetTargetPhrases().end(); ++iter) {
const std::pair<TargetPhraseCollection::shared_ptr , const void*> &temp = iter->second;
TargetPhraseCollection::shared_ptr tpCollPrev = temp.first;
if (tpCollPrev && tpCollPrev->GetSize()) {
// already have translation from another pt. Don't create translations
return false;
}
}
return true;
}
} // namespace
|