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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
|
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
#include "mmsapt.h"
#include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/tokenizer.hpp>
#include <boost/thread/locks.hpp>
#include <algorithm>
#include "util/exception.hh"
#include <set>
#include "util/usage.hh"
namespace Moses
{
using namespace sapt;
using namespace std;
using namespace boost;
void
fillIdSeq(Phrase const& mophrase, std::vector<FactorType> const& ifactors,
TokenIndex const& V, vector<id_type>& dest)
{
dest.resize(mophrase.GetSize());
for (size_t i = 0; i < mophrase.GetSize(); ++i)
{
// Factor const* f = mophrase.GetFactor(i,ifactor);
dest[i] = V[mophrase.GetWord(i).GetString(ifactors, false)]; // f->ToString()];
}
}
void
parseLine(string const& line, map<string,string> & param)
{
char_separator<char> sep("; ");
tokenizer<char_separator<char> > tokens(line,sep);
BOOST_FOREACH(string const& t,tokens)
{
size_t i = t.find_first_not_of(" =");
size_t j = t.find_first_of(" =",i+1);
size_t k = t.find_first_not_of(" =",j+1);
UTIL_THROW_IF2(i == string::npos || k == string::npos,
"[" << HERE << "] "
<< "Parameter specification error near '"
<< t << "' in moses ini line\n"
<< line);
assert(i != string::npos);
assert(k != string::npos);
param[t.substr(i,j)] = t.substr(k);
}
}
vector<string> const&
Mmsapt::
GetFeatureNames() const
{
return m_feature_names;
}
Mmsapt::
Mmsapt(string const& line)
: PhraseDictionary(line, false)
, btfix(new mmbitext)
, m_bias_log(NULL)
, m_bias_loglevel(0)
#ifndef NO_MOSES
, m_lr_func(NULL)
#endif
, m_sampling_method(random_sampling)
, bias_key(((char*)this)+3)
, cache_key(((char*)this)+2)
, context_key(((char*)this)+1)
// , m_tpc_ctr(0)
// , m_ifactor(1,0)
// , m_ofactor(1,0)
{
init(line);
setup_local_feature_functions();
// Set features used for scoring extracted phrases:
// * Use all features that can operate on input factors and this model's
// output factor
// * Don't use features that depend on generation steps that won't be run
// yet at extract time
SetFeaturesToApply();
// Register();
}
void
Mmsapt::
read_config_file(string fname, map<string,string>& param)
{
string line;
ifstream config(fname.c_str());
while (getline(config,line))
{
if (line[0] == '#') continue;
char_separator<char> sep(" \t");
tokenizer<char_separator<char> > tokens(line,sep);
tokenizer<char_separator<char> >::const_iterator t = tokens.begin();
if (t == tokens.end()) continue;
string& foo = param[*t++];
if (t == tokens.end() || foo.size()) continue;
// second condition: do not overwrite settings from the line in moses.ini
UTIL_THROW_IF2(*t++ != "=" || t == tokens.end(),
"Syntax error in Mmsapt config file '" << fname << "'.");
for (foo = *t++; t != tokens.end(); foo += " " + *t++);
}
}
void
Mmsapt::
register_ff(SPTR<pscorer> const& ff, vector<SPTR<pscorer> > & registry)
{
registry.push_back(ff);
ff->setIndex(m_feature_names.size());
for (int i = 0; i < ff->fcnt(); ++i)
{
m_feature_names.push_back(ff->fname(i));
m_is_logval.push_back(ff->isLogVal(i));
m_is_integer.push_back(ff->isIntegerValued(i));
}
}
bool Mmsapt::isLogVal(int i) const { return m_is_logval.at(i); }
bool Mmsapt::isInteger(int i) const { return m_is_integer.at(i); }
void
Mmsapt::
parse_factor_spec(std::vector<FactorType>& flist, std::string const key)
{
pair<string,string> dflt(key, "0");
string factors = this->param.insert(dflt).first->second;
size_t p = 0, q = factors.find(',');
for (; q < factors.size(); q = factors.find(',', p=q+1))
flist.push_back(atoi(factors.substr(p, q-p).c_str()));
flist.push_back(atoi(factors.substr(p).c_str()));
}
void Mmsapt::init(string const& line)
{
map<string,string>::const_iterator m;
parseLine(line,this->param);
this->m_numScoreComponents = atoi(param["num-features"].c_str());
m = param.find("config");
if (m != param.end())
read_config_file(m->second,param);
m = param.find("base");
if (m != param.end())
{
m_bname = m->second;
m = param.find("path");
UTIL_THROW_IF2((m != param.end() && m->second != m_bname),
"Conflicting aliases for path:\n"
<< "path=" << string(m->second) << "\n"
<< "base=" << m_bname.c_str() );
}
else m_bname = param["path"];
L1 = param["L1"];
L2 = param["L2"];
UTIL_THROW_IF2(m_bname.size() == 0, "Missing corpus base name at " << HERE);
UTIL_THROW_IF2(L1.size() == 0, "Missing L1 tag at " << HERE);
UTIL_THROW_IF2(L2.size() == 0, "Missing L2 tag at " << HERE);
// set defaults for all parameters if not specified so far
parse_factor_spec(m_ifactor,"input-factor");
parse_factor_spec(m_ofactor,"output-factor");
// Masks for available factors that inform SetFeaturesToApply
m_inputFactors = FactorMask(m_ifactor);
m_outputFactors = FactorMask(m_ofactor);
pair<string,string> dflt = pair<string,string> ("smooth",".01");
m_lbop_conf = atof(param.insert(dflt).first->second.c_str());
dflt = pair<string,string> ("lexalpha","0");
m_lex_alpha = atof(param.insert(dflt).first->second.c_str());
dflt = pair<string,string> ("sample","1000");
m_default_sample_size = atoi(param.insert(dflt).first->second.c_str());
dflt = pair<string,string> ("min-sample","0");
m_min_sample_size = atoi(param.insert(dflt).first->second.c_str());
dflt = pair<string,string>("workers","0");
m_workers = atoi(param.insert(dflt).first->second.c_str());
if (m_workers == 0) m_workers = StaticData::Instance().ThreadCount();
else m_workers = min(m_workers,size_t(boost::thread::hardware_concurrency()));
dflt = pair<string,string>("bias-loglevel","0");
m_bias_loglevel = atoi(param.insert(dflt).first->second.c_str());
dflt = pair<string,string>("table-limit","20");
m_tableLimit = atoi(param.insert(dflt).first->second.c_str());
dflt = pair<string,string>("cache","100000");
m_cache_size = max(10000,atoi(param.insert(dflt).first->second.c_str()));
m_cache.reset(new TPCollCache(m_cache_size));
// m_history.reserve(hsize);
// in plain language: cache size is at least 1000, and 10,000 by default
// this cache keeps track of the most frequently used target
// phrase collections even when not actively in use
// Feature functions are initialized in function Load();
param.insert(pair<string,string>("pfwd", "g"));
param.insert(pair<string,string>("pbwd", "g"));
param.insert(pair<string,string>("lenrat", "1"));
param.insert(pair<string,string>("rare", "1"));
param.insert(pair<string,string>("logcnt", "0"));
param.insert(pair<string,string>("coh", "0"));
param.insert(pair<string,string>("prov", "0"));
param.insert(pair<string,string>("cumb", "0"));
poolCounts = true;
// this is for pre-comuted sentence-level bias; DEPRECATED!
if ((m = param.find("bias")) != param.end())
m_bias_file = m->second;
if ((m = param.find("bias-server")) != param.end())
m_bias_server = m->second;
if (m_bias_loglevel)
{
dflt = pair<string,string>("bias-logfile","/dev/stderr");
param.insert(dflt);
}
if ((m = param.find("bias-logfile")) != param.end())
{
m_bias_logfile = m->second;
if (m_bias_logfile == "/dev/stderr")
m_bias_log = &std::cerr;
else if (m_bias_logfile == "/dev/stdout")
m_bias_log = &std::cout;
else
{
m_bias_logger.reset(new std::ofstream(m_bias_logfile.c_str()));
m_bias_log = m_bias_logger.get();
}
}
if ((m = param.find("lr-func")) != param.end())
m_lr_func_name = m->second;
// accommodate typo in Germann, 2015: Sampling Phrase Tables for
// the Moses SMT System (PBML):
if ((m = param.find("lrfunc")) != param.end())
m_lr_func_name = m->second;
if ((m = param.find("extra")) != param.end())
m_extra_data = m->second;
if ((m = param.find("method")) != param.end())
{
if (m->second == "random")
m_sampling_method = random_sampling;
else if (m->second == "ranked")
m_sampling_method = ranked_sampling;
else if (m->second == "ranked2")
m_sampling_method = ranked_sampling2;
else if (m->second == "full")
m_sampling_method = full_coverage;
else UTIL_THROW2("unrecognized specification 'method='" << m->second
<< "' in line:\n" << line);
}
dflt = pair<string,string>("tuneable","true");
m_tuneable = Scan<bool>(param.insert(dflt).first->second.c_str());
dflt = pair<string,string>("feature-sets","standard");
m_feature_set_names = Tokenize(param.insert(dflt).first->second.c_str(), ",");
m = param.find("name");
if (m != param.end()) m_name = m->second;
// check for unknown parameters
vector<string> known_parameters; known_parameters.reserve(50);
known_parameters.push_back("L1");
known_parameters.push_back("L2");
known_parameters.push_back("Mmsapt");
known_parameters.push_back("PhraseDictionaryBitextSampling");
// alias for Mmsapt
known_parameters.push_back("base"); // alias for path
known_parameters.push_back("bias");
known_parameters.push_back("bias-server");
known_parameters.push_back("bias-logfile");
known_parameters.push_back("bias-loglevel");
known_parameters.push_back("cache");
known_parameters.push_back("coh");
known_parameters.push_back("config");
known_parameters.push_back("cumb");
known_parameters.push_back("extra");
known_parameters.push_back("feature-sets");
known_parameters.push_back("input-factor");
known_parameters.push_back("lenrat");
known_parameters.push_back("lexalpha");
// known_parameters.push_back("limit"); // replaced by "table-limit"
known_parameters.push_back("logcnt");
known_parameters.push_back("lr-func"); // associated lexical reordering function
known_parameters.push_back("lrfunc"); // associated lexical reordering function
known_parameters.push_back("method");
known_parameters.push_back("name");
known_parameters.push_back("num-features");
known_parameters.push_back("output-factor");
known_parameters.push_back("path");
known_parameters.push_back("pbwd");
known_parameters.push_back("pfwd");
known_parameters.push_back("prov");
known_parameters.push_back("rare");
known_parameters.push_back("sample");
known_parameters.push_back("min-sample");
known_parameters.push_back("smooth");
known_parameters.push_back("table-limit");
known_parameters.push_back("tuneable");
known_parameters.push_back("unal");
known_parameters.push_back("workers");
sort(known_parameters.begin(),known_parameters.end());
for (map<string,string>::iterator m = param.begin(); m != param.end(); ++m)
{
UTIL_THROW_IF2(!binary_search(known_parameters.begin(),
known_parameters.end(), m->first),
HERE << ": Unknown parameter specification for Mmsapt: "
<< m->first);
}
}
void
Mmsapt::
load_bias(string const fname)
{
m_bias = btfix->loadSentenceBias(fname);
}
void
Mmsapt::
load_extra_data(string bname, bool locking = true)
{
using namespace boost;
using namespace ugdiss;
// TO DO: ADD CHECKS FOR ROBUSTNESS
// - file existence?
// - same number of lines?
// - sane word alignment?
vector<string> text1,text2,symal;
string line;
boost::iostreams::filtering_istream in1,in2,ina;
open_input_stream(bname+L1+".txt.gz",in1);
open_input_stream(bname+L2+".txt.gz",in2);
open_input_stream(bname+L1+"-"+L2+".symal.gz",ina);
while(getline(in1,line)) text1.push_back(line);
while(getline(in2,line)) text2.push_back(line);
while(getline(ina,line)) symal.push_back(line);
scoped_ptr<boost::unique_lock<shared_mutex> > guard;
if (locking) guard.reset(new boost::unique_lock<shared_mutex>(m_lock));
btdyn = btdyn->add(text1,text2,symal);
assert(btdyn);
cerr << "Loaded " << btdyn->T1->size() << " sentence pairs" << endl;
}
template<typename fftype>
void
Mmsapt::
check_ff(string const ffname, vector<SPTR<pscorer> >* registry)
{
string const& spec = param[ffname];
if (spec == "" || spec == "0") return;
if (registry)
{
SPTR<fftype> ff(new fftype(spec));
register_ff(ff, *registry);
}
else if (spec[spec.size()-1] == '+') // corpus specific
{
SPTR<fftype> ff(new fftype(spec));
register_ff(ff, m_active_ff_fix);
ff.reset(new fftype(spec));
register_ff(ff, m_active_ff_dyn);
}
else
{
SPTR<fftype> ff(new fftype(spec));
register_ff(ff, m_active_ff_common);
}
}
template<typename fftype>
void
Mmsapt::
check_ff(string const ffname, float const xtra,
vector<SPTR<pscorer> >* registry)
{
string const& spec = param[ffname];
if (spec == "" || spec == "0") return;
if (registry)
{
SPTR<fftype> ff(new fftype(xtra,spec));
register_ff(ff, *registry);
}
else if (spec[spec.size()-1] == '+') // corpus specific
{
SPTR<fftype> ff(new fftype(xtra,spec));
register_ff(ff, m_active_ff_fix);
ff.reset(new fftype(xtra,spec));
register_ff(ff, m_active_ff_dyn);
}
else
{
SPTR<fftype> ff(new fftype(xtra,spec));
register_ff(ff, m_active_ff_common);
}
}
void
Mmsapt::
Load(AllOptions::ptr const& opts)
{
Load(opts, true);
}
void
Mmsapt
::setup_local_feature_functions()
{
boost::unique_lock<boost::shared_mutex> lock(m_lock);
// load feature sets
BOOST_FOREACH(string const& fsname, m_feature_set_names)
{
// standard (default) feature set
if (fsname == "standard")
{
// lexical scores
string lexfile = m_bname + L1 + "-" + L2 + ".lex";
SPTR<PScoreLex1<Token> >
ff(new PScoreLex1<Token>(param["lex_alpha"],lexfile));
register_ff(ff,m_active_ff_common);
// these are always computed on pooled data
check_ff<PScoreRareness<Token> > ("rare", &m_active_ff_common);
check_ff<PScoreUnaligned<Token> >("unal", &m_active_ff_common);
check_ff<PScoreCoherence<Token> >("coh", &m_active_ff_common);
check_ff<PScoreCumBias<Token> >("cumb", &m_active_ff_common);
check_ff<PScoreLengthRatio<Token> > ("lenrat", &m_active_ff_common);
// for these ones either way is possible (specification ends with '+'
// if corpus-specific
check_ff<PScorePfwd<Token> >("pfwd", m_lbop_conf);
check_ff<PScorePbwd<Token> >("pbwd", m_lbop_conf);
check_ff<PScoreLogCnt<Token> >("logcnt");
// These are always corpus-specific
check_ff<PScoreProvenance<Token> >("prov", &m_active_ff_fix);
check_ff<PScoreProvenance<Token> >("prov", &m_active_ff_dyn);
}
// data source features (copies of phrase and word count specific to
// this translation model)
else if (fsname == "datasource")
{
SPTR<PScorePC<Token> > ffpcnt(new PScorePC<Token>("pcnt"));
register_ff(ffpcnt,m_active_ff_common);
SPTR<PScoreWC<Token> > ffwcnt(new PScoreWC<Token>("wcnt"));
register_ff(ffwcnt,m_active_ff_common);
}
}
// cerr << "Features: " << Join("|",m_feature_names) << endl;
this->m_numScoreComponents = this->m_feature_names.size();
this->m_numTuneableComponents = this->m_numScoreComponents;
}
void
Mmsapt::
Load(AllOptions::ptr const& opts, bool with_checks)
{
m_options = opts;
boost::unique_lock<boost::shared_mutex> lock(m_lock);
// load feature functions (i.e., load underlying data bases, if any)
BOOST_FOREACH(SPTR<pscorer>& ff, m_active_ff_fix) ff->load();
BOOST_FOREACH(SPTR<pscorer>& ff, m_active_ff_dyn) ff->load();
BOOST_FOREACH(SPTR<pscorer>& ff, m_active_ff_common) ff->load();
#if 0
if (with_checks)
{
UTIL_THROW_IF2(this->m_feature_names.size() != this->m_numScoreComponents,
"At " << HERE << ": number of feature values provided by "
<< "Phrase table (" << this->m_feature_names.size()
<< ") does not match number specified in Moses config file ("
<< this->m_numScoreComponents << ")!\n";);
}
#endif
m_thread_pool.reset(new ug::ThreadPool(max(m_workers,size_t(1))));
// Load corpora. For the time being, we can have one memory-mapped static
// corpus and one in-memory dynamic corpus
btfix->m_num_workers = this->m_workers;
btfix->open(m_bname, L1, L2);
btfix->setDefaultSampleSize(m_default_sample_size);
btdyn.reset(new imbitext(btfix->V1, btfix->V2, m_default_sample_size, m_workers));
if (m_bias_file.size())
load_bias(m_bias_file);
if (m_extra_data.size())
load_extra_data(m_extra_data, false);
#if 0
// currently not used
LexicalPhraseScorer2<Token>::table_t & COOC = calc_lex.scorer.COOC;
typedef LexicalPhraseScorer2<Token>::table_t::Cell cell_t;
wlex21.resize(COOC.numCols);
for (size_t r = 0; r < COOC.numRows; ++r)
for (cell_t const* c = COOC[r].start; c < COOC[r].stop; ++c)
wlex21[c->id].push_back(r);
COOCraw.open(m_bname + L1 + "-" + L2 + ".coc");
#endif
assert(btdyn);
// cerr << "LOADED " << HERE << endl;
}
void
Mmsapt::
add(string const& s1, string const& s2, string const& a)
{
vector<string> S1(1,s1);
vector<string> S2(1,s2);
vector<string> ALN(1,a);
boost::unique_lock<boost::shared_mutex> guard(m_lock);
btdyn = btdyn->add(S1,S2,ALN);
}
TargetPhrase*
Mmsapt::
mkTPhrase(ttasksptr const& ttask,
Phrase const& src,
PhrasePair<Token>* fix,
PhrasePair<Token>* dyn,
SPTR<Bitext<Token> > const& dynbt) const
{
UTIL_THROW_IF2(!fix && !dyn, HERE <<
": Can't create target phrase from nothing.");
vector<float> fvals(this->m_numScoreComponents);
PhrasePair<Token> pool = fix ? *fix : *dyn;
if (fix)
{
BOOST_FOREACH(SPTR<pscorer> const& ff, m_active_ff_fix)
(*ff)(*btfix, *fix, &fvals);
}
if (dyn)
{
BOOST_FOREACH(SPTR<pscorer> const& ff, m_active_ff_dyn)
(*ff)(*dynbt, *dyn, &fvals);
}
if (fix && dyn) { pool += *dyn; }
else if (fix)
{
PhrasePair<Token> zilch; zilch.init();
TSA<Token>::tree_iterator m(dynbt->I2.get(), fix->start2, fix->len2);
if (m.size() == fix->len2)
zilch.raw2 = m.approxOccurrenceCount();
pool += zilch;
BOOST_FOREACH(SPTR<pscorer> const& ff, m_active_ff_dyn)
(*ff)(*dynbt, ff->allowPooling() ? pool : zilch, &fvals);
}
else if (dyn)
{
PhrasePair<Token> zilch; zilch.init();
TSA<Token>::tree_iterator m(btfix->I2.get(), dyn->start2, dyn->len2);
if (m.size() == dyn->len2)
zilch.raw2 = m.approxOccurrenceCount();
pool += zilch;
BOOST_FOREACH(SPTR<pscorer> const& ff, m_active_ff_fix)
(*ff)(*dynbt, ff->allowPooling() ? pool : zilch, &fvals);
}
if (fix)
{
BOOST_FOREACH(SPTR<pscorer> const& ff, m_active_ff_common)
(*ff)(*btfix, pool, &fvals);
}
else
{
BOOST_FOREACH(SPTR<pscorer> const& ff, m_active_ff_common)
(*ff)(*dynbt, pool, &fvals);
}
TargetPhrase* tp = new TargetPhrase(const_cast<ttasksptr&>(ttask), this);
Token const* x = fix ? fix->start2 : dyn->start2;
uint32_t len = fix ? fix->len2 : dyn->len2;
for (uint32_t k = 0; k < len; ++k, x = x->next())
{
StringPiece wrd = (*(btfix->V2))[x->id()];
Word w;
w.CreateFromString(Output, m_ofactor, wrd, false);
tp->AddWord(w);
}
tp->SetAlignTerm(pool.aln);
tp->GetScoreBreakdown().Assign(this, fvals);
// Evaluate with all features that can be computed using available factors
tp->EvaluateInIsolation(src, m_featuresToApply);
#ifndef NO_MOSES
if (m_lr_func)
{
LRModel::ModelType mdl = m_lr_func->GetModel().GetModelType();
LRModel::Direction dir = m_lr_func->GetModel().GetDirection();
SPTR<Scores> scores(new Scores());
pool.fill_lr_vec(dir, mdl, *scores);
tp->SetExtraScores(m_lr_func, scores);
}
#endif
return tp;
}
void
Mmsapt::
GetTargetPhraseCollectionBatch(ttasksptr const& ttask,
const InputPathList &inputPathQueue) const
{
InputPathList::const_iterator iter;
for (iter = inputPathQueue.begin(); iter != inputPathQueue.end(); ++iter)
{
InputPath &inputPath = **iter;
const Phrase &phrase = inputPath.GetPhrase();
PrefixExists(ttask, phrase); // launches parallel lookup
}
for (iter = inputPathQueue.begin(); iter != inputPathQueue.end(); ++iter)
{
InputPath &inputPath = **iter;
const Phrase &phrase = inputPath.GetPhrase();
TargetPhraseCollection::shared_ptr targetPhrases
= this->GetTargetPhraseCollectionLEGACY(ttask,phrase);
inputPath.SetTargetPhrases(*this, targetPhrases, NULL);
}
}
// TargetPhraseCollection::shared_ptr
// Mmsapt::
// GetTargetPhraseCollectionLEGACY(const Phrase& src) const
// {
// UTIL_THROW2("Don't call me without the translation task.");
// }
// This is not the most efficient way of phrase lookup!
TargetPhraseCollection::shared_ptr
Mmsapt::
GetTargetPhraseCollectionLEGACY(ttasksptr const& ttask, const Phrase& src) const
{
SPTR<TPCollWrapper> ret;
// boost::unique_lock<boost::shared_mutex> xlock(m_lock);
// map from Moses Phrase to internal id sequence
vector<id_type> sphrase;
fillIdSeq(src, m_ifactor, *(btfix->V1), sphrase);
if (sphrase.size() == 0) return ret;
// Reserve a local copy of the dynamic bitext in its current form. /btdyn/
// is set to a new copy of the dynamic bitext every time a sentence pair
// is added. /dyn/ keeps the old bitext around as long as we need it.
SPTR<imBitext<Token> > dyn;
{ // braces are needed for scoping mutex lock guard!
boost::unique_lock<boost::shared_mutex> guard(m_lock);
assert(btdyn);
dyn = btdyn;
}
assert(dyn);
// lookup phrases in both bitexts
TSA<Token>::tree_iterator mfix(btfix->I1.get(), &sphrase[0], sphrase.size());
TSA<Token>::tree_iterator mdyn(dyn->I1.get());
if (dyn->I1.get()) // we have a dynamic bitext
for (size_t i = 0; mdyn.size() == i && i < sphrase.size(); ++i)
mdyn.extend(sphrase[i]);
if (mdyn.size() != sphrase.size() && mfix.size() != sphrase.size())
return ret; // phrase not found in either bitext
// do we have cached results for this phrase?
uint64_t phrasekey = (mfix.size() == sphrase.size()
? (mfix.getPid()<<1)
: (mdyn.getPid()<<1)+1);
// get context-specific cache of items previously looked up
SPTR<ContextScope> const& scope = ttask->GetScope();
SPTR<TPCollCache> cache = scope->get<TPCollCache>(cache_key);
if (!cache) cache = m_cache; // no context-specific cache, use global one
ret = cache->get(phrasekey, dyn->revision());
// TO DO: we should revise the revision mechanism: we take the
// length of the dynamic bitext (in sentences) at the time the PT
// entry was stored as the time stamp. For each word in the
// vocabulary, we also store its most recent occurrence in the
// bitext. Only if the timestamp of each word in the phrase is
// newer than the timestamp of the phrase itself we must update
// the entry.
// std::cerr << "Phrasekey is " << ret->key << " at " << HERE << std::endl;
// std::cerr << ret << " with " << ret->refCount << " references at "
// << HERE << std::endl;
boost::upgrade_lock<boost::shared_mutex> rlock(ret->lock);
if (ret->GetSize()) return ret;
// new TPC (not found or old one was not up to date)
boost::upgrade_to_unique_lock<boost::shared_mutex> wlock(rlock);
// maybe another thread did the work while we waited for the lock ?
if (ret->GetSize()) return ret;
// OK: pt entry NOT found or NOT up to date
// lookup and expansion could be done in parallel threads,
// but ppdyn is probably small anyway
// TO DO: have Bitexts return lists of PhrasePairs instead of pstats
// no need to expand pstats at every single lookup again, especially
// for btfix.
SPTR<pstats> sfix,sdyn;
if (mfix.size() == sphrase.size())
{
SPTR<ContextForQuery> context = scope->get<ContextForQuery>(btfix.get());
SPTR<pstats> const* foo = context->cache1->get(mfix.getPid());
if (foo) { sfix = *foo; sfix->wait(); }
else
{
BitextSampler<Token> s(btfix, mfix, context->bias,
m_min_sample_size,
m_default_sample_size,
m_sampling_method);
s();
sfix = s.stats();
}
}
if (mdyn.size() == sphrase.size())
sdyn = dyn->lookup(ttask, mdyn);
vector<PhrasePair<Token> > ppfix,ppdyn;
PhrasePair<Token>::SortByTargetIdSeq sort_by_tgt_id;
if (sfix)
{
expand(mfix, *btfix, *sfix, ppfix, m_bias_log);
sort(ppfix.begin(), ppfix.end(),sort_by_tgt_id);
}
if (sdyn)
{
expand(mdyn, *dyn, *sdyn, ppdyn, m_bias_log);
sort(ppdyn.begin(), ppdyn.end(),sort_by_tgt_id);
}
// now we have two lists of Phrase Pairs, let's merge them
PhrasePair<Token>::SortByTargetIdSeq sorter;
size_t i = 0; size_t k = 0;
while (i < ppfix.size() && k < ppdyn.size())
{
int cmp = sorter.cmp(ppfix[i], ppdyn[k]);
if (cmp < 0) ret->Add(mkTPhrase(ttask,src,&ppfix[i++],NULL,dyn));
else if (cmp == 0) ret->Add(mkTPhrase(ttask,src,&ppfix[i++],&ppdyn[k++],dyn));
else ret->Add(mkTPhrase(ttask,src,NULL,&ppdyn[k++],dyn));
}
while (i < ppfix.size()) ret->Add(mkTPhrase(ttask,src,&ppfix[i++],NULL,dyn));
while (k < ppdyn.size()) ret->Add(mkTPhrase(ttask,src,NULL,&ppdyn[k++],dyn));
// Pruning should not be done here but outside!
if (m_tableLimit) ret->Prune(true, m_tableLimit);
else ret->Prune(true,ret->GetSize());
#if 1
if (m_bias_log && m_lr_func && m_bias_loglevel > 3)
{
PhrasePair<Token>::SortDescendingByJointCount sorter;
sort(ppfix.begin(), ppfix.end(),sorter);
BOOST_FOREACH(PhrasePair<Token> const& pp, ppfix)
{
// if (&pp != &ppfix.front() && pp.joint <= 1) break;
pp.print(*m_bias_log,*btfix->V1, *btfix->V2, m_lr_func->GetModel());
}
}
#endif
return ret;
}
size_t
Mmsapt::
SetTableLimit(size_t limit)
{
std::swap(m_tableLimit,limit);
return limit;
}
void
Mmsapt::
CleanUpAfterSentenceProcessing(ttasksptr const& ttask)
{ }
ChartRuleLookupManager*
Mmsapt::
CreateRuleLookupManager(const ChartParser &, const ChartCellCollectionBase &)
{
throw "CreateRuleLookupManager is currently not supported in Mmsapt!";
}
ChartRuleLookupManager*
Mmsapt::
CreateRuleLookupManager(const ChartParser &, const ChartCellCollectionBase &,
size_t )
{
throw "CreateRuleLookupManager is currently not supported in Mmsapt!";
}
void
Mmsapt::
setup_bias(ttasksptr const& ttask)
{
SPTR<ContextScope> const& scope = ttask->GetScope();
SPTR<ContextForQuery> context;
context = scope->get<ContextForQuery>(btfix.get(), true);
if (context->bias) return;
// bias weights specified with the session?
SPTR<std::map<std::string, float> const> w;
w = ttask->GetScope()->GetContextWeights();
if (w && !w->empty())
{
if (m_bias_log)
*m_bias_log << "BIAS WEIGHTS GIVEN WITH INPUT at " << HERE << endl;
context->bias = btfix->SetupDocumentBias(*w, m_bias_log);
}
else if (m_bias_server.size() && ttask->GetContextWindow())
{
// std::cerr << "via server at " << HERE << std::endl;
string context_words;
BOOST_FOREACH(string const& line, *ttask->GetContextWindow())
{
if (context_words.size()) context_words += " ";
context_words += line;
}
if (context_words.size())
{
if (m_bias_log)
*m_bias_log << "GETTING BIAS FROM SERVER at " << HERE << endl
<< "BIAS LOOKUP CONTEXT: " << context_words << endl;
context->bias
= btfix->SetupDocumentBias(m_bias_server,context_words,m_bias_log);
//Reset the bias in the ttaskptr so that other functions
//so that other functions can utilize the biases;
ttask->GetScope()->SetContextWeights(context->bias->getBiasMap());
}
}
if (context->bias)
{
context->bias_log = m_bias_log;
context->bias->loglevel = m_bias_loglevel;
}
}
void
Mmsapt::
InitializeForInput(ttasksptr const& ttask)
{
boost::unique_lock<boost::shared_mutex> mylock(m_lock);
SPTR<ContextScope> const& scope = ttask->GetScope();
SPTR<TPCollCache> localcache = scope->get<TPCollCache>(cache_key);
SPTR<ContextForQuery> context = scope->get<ContextForQuery>(btfix.get(), true);
boost::unique_lock<boost::shared_mutex> ctxlock(context->lock);
// if (localcache) std::cerr << "have local cache " << std::endl;
// std::cerr << "BOO at " << HERE << std::endl;
if (!localcache)
{
// std::cerr << "no local cache at " << HERE << std::endl;
setup_bias(ttask);
if (context->bias)
{
localcache.reset(new TPCollCache(m_cache_size));
}
else localcache = m_cache;
scope->set<TPCollCache>(cache_key, localcache);
}
if (!context->cache1) context->cache1.reset(new pstats::cache_t);
if (!context->cache2) context->cache2.reset(new pstats::cache_t);
#ifndef NO_MOSES
if (m_lr_func_name.size() && m_lr_func == NULL)
{
FeatureFunction* lr = &FeatureFunction::FindFeatureFunction(m_lr_func_name);
m_lr_func = dynamic_cast<LexicalReordering*>(lr);
UTIL_THROW_IF2(lr == NULL, "FF " << m_lr_func_name
<< " does not seem to be a lexical reordering function!");
// todo: verify that lr_func implements a hierarchical reordering model
}
#endif
}
bool
Mmsapt::
PrefixExists(ttasksptr const& ttask, Moses::Phrase const& phrase) const
{
if (phrase.GetSize() == 0) return false;
SPTR<ContextScope> const& scope = ttask->GetScope();
vector<id_type> myphrase;
fillIdSeq(phrase, m_ifactor, *btfix->V1, myphrase);
TSA<Token>::tree_iterator mfix(btfix->I1.get(),&myphrase[0],myphrase.size());
if (mfix.size() == myphrase.size())
{
SPTR<ContextForQuery> context = scope->get<ContextForQuery>(btfix.get(), true);
uint64_t pid = mfix.getPid();
if (!context->cache1->get(pid))
{
BitextSampler<Token> s(btfix, mfix, context->bias,
m_min_sample_size, m_default_sample_size,
m_sampling_method);
if (*context->cache1->get(pid, s.stats()) == s.stats())
m_thread_pool->add(s);
}
// btfix->prep(ttask, mfix);
// cerr << phrase << " " << mfix.approxOccurrenceCount() << endl;
return true;
}
SPTR<imBitext<Token> > dyn;
{ // braces are needed for scoping lock!
boost::unique_lock<boost::shared_mutex> guard(m_lock);
dyn = btdyn;
}
assert(dyn);
TSA<Token>::tree_iterator mdyn(dyn->I1.get());
if (dyn->I1.get())
{
for (size_t i = 0; mdyn.size() == i && i < myphrase.size(); ++i)
mdyn.extend(myphrase[i]);
// let's assume a uniform bias over the foreground corpus
if (mdyn.size() == myphrase.size()) dyn->prep(ttask, mdyn);
}
return mdyn.size() == myphrase.size();
}
#if 0
void
Mmsapt
::Release(ttasksptr const& ttask, TargetPhraseCollection::shared_ptr*& tpc) const
{
if (!tpc)
{
// std::cerr << "NULL pointer at " << HERE << std::endl;
return;
}
SPTR<TPCollCache> cache = ttask->GetScope()->get<TPCollCache>(cache_key);
TPCollWrapper const* foo = static_cast<TPCollWrapper const*>(tpc);
// std::cerr << "\nReleasing " << foo->key << "\n" << std::endl;
if (cache) cache->release(static_cast<TPCollWrapper const*>(tpc));
tpc = NULL;
}
#endif
bool Mmsapt
::ProvidesPrefixCheck() const { return true; }
string const& Mmsapt
::GetName() const { return m_name; }
// SPTR<DocumentBias>
// Mmsapt
// ::setupDocumentBias(map<string,float> const& bias) const
// {
// return btfix->SetupDocumentBias(bias);
// }
vector<float>
Mmsapt
::DefaultWeights() const
{ return vector<float>(this->GetNumScoreComponents(), 1.); }
}
|