-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.cpp
1792 lines (1711 loc) · 63.9 KB
/
state.cpp
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "state.h"
state::state (unsigned int & last_state_id, state * _father, const vector<pair<wlink,bool> > & lnks, const wlinkSequence & possLinks, const wlinkCoverage & posiOrderLinkCoverage, param & pars, const sentPair & sp, ostringstream & oss, int level, vector<pair<int,int> > & srcChFiltLimits, vector<pair<int,int> > & trgChFiltLimits, const futureCostEstimation & fcEstim, string direction){
struct tms *time_buffer = new tms;
float clocks_per_sec = sysconf (_SC_CLK_TCK);
pars.nNewStates+=1.0;
++last_state_id;
nhist=0;
nPtr=0;
id=last_state_id;
father=_father;
// (void) times (time_buffer);
// double tt1 = time_buffer->tms_utime / clocks_per_sec;
// stcov.assign(posiOrderLinkCoverage.size(),false);
// (void) times (time_buffer);
// double tt2 = time_buffer->tms_utime / clocks_per_sec;
// pars.timeControl[5]+=tt2-tt1;
(void) times (time_buffer);
double t1 = time_buffer->tms_utime / clocks_per_sec;
stcov=father->stcov;
(void) times (time_buffer);
double t2 = time_buffer->tms_utime / clocks_per_sec;
pars.timeControl[0]+=t2-t1;
(void) times (time_buffer);
t1 = time_buffer->tms_utime / clocks_per_sec;
nstcov=father->nstcov;
if (pars.backTrack){lcov=father->lcov;}
for (vector<pair<wlink,bool> >::const_iterator vpit=lnks.begin();vpit!=lnks.end();++vpit){
lnkids.push_back(make_pair(vpit->first.id,vpit->second));
if (pars.backTrack){
// update link coverage vector
if (vpit->second){lcov.at(vpit->first.id)=true;}
else {lcov.at(vpit->first.id)=false;}
}
}
int newLinkedSrc=0;
int newLinkedTrg=0;
scov=father->scov;
nscov=father->nscov;
internalCrossNumber=father->internalCrossNumber; // compensation for internal crossings in phrase-based links involving more than one src and trg words
internalCrossLength=father->internalCrossLength;
nInternalZigzags=father->nInternalZigzags;
tcov=father->tcov;
ntcov=father->ntcov;
(void) times (time_buffer);
t2 = time_buffer->tms_utime / clocks_per_sec;
pars.timeControl[1]+=t2-t1;
cost sfertCost=0;
cost tfertCost=0;
cost sumCost=0;
cost tumCost=0;
set<int> visitedSposi,visitedTposi;
int formerFirst=father->first;
int formerLast=father->last;
int formerNstcov=father->nstcov;
bool updateFirst=false;
bool updateLast=false;
vector<int> histKey; // state history vector for hypothesis recombination
for (vector<pair<wlink,bool> >::const_iterator lit=lnks.begin();lit!=lnks.end();++lit){
bool addLink=lit->second;
// update weighted nstcov
int nsrc=lit->first.send()-lit->first.sbeg()+1;
int ntrg=lit->first.tend()-lit->first.tbeg()+1;
if (nsrc>1 && ntrg>1){
if (addLink) nInternalZigzags+=nsrc*ntrg;
else nInternalZigzags-=nsrc*ntrg;
}
// update coverage vectors
for (int sposi=lit->first.sbeg();sposi<=lit->first.send();++sposi){
for (int tposi=lit->first.tbeg();tposi<=lit->first.tend();++tposi){
visitedSposi.insert(sposi);
visitedTposi.insert(tposi);
linkIndex pairId=posiOrderLinkCoverage.getID(sposi,tposi);
if (addLink){
// update link coverage vector
stcov[pairId]=true;
++nstcov;
// update word coverage vectors
++scov[sposi];
++tcov[tposi];
}else{
// update link coverage vector
stcov[pairId]=false;
--nstcov;
// update word coverage vectors
--scov[sposi];
--tcov[tposi];
}
}
}
// update internalCrossNumber, internalCrossLength
if (lit->first.send()>lit->first.sbeg() && lit->first.tend()>lit->first.tbeg()){
int deltacn=lit->first.send()-lit->first.sbeg();
int deltacl=deltacn*(lit->first.tend()-lit->first.tbeg());
if (addLink){
internalCrossNumber+=deltacn;
internalCrossLength+=deltacl;
}else{
internalCrossNumber-=deltacn;
internalCrossLength-=deltacl;
}
// cout<<"[internal] "<<lit->first.print()<<"delta cn:"<<deltacn<<" internal cn:"<<internalCrossNumber<<" delta cl:"<<deltacl<<" internal cl:"<<internalCrossLength<<endl;
}
// update (first,last) info
linkIndex firstCovLinkId=posiOrderLinkCoverage.getID(lit->first.sbeg(),lit->first.tbeg());
linkIndex lastCovLinkId=posiOrderLinkCoverage.getID(lit->first.send(),lit->first.tend());
//cout<<"firstCovLinkId:"<<firstCovLinkId<<" lastCovLinkId:"<<lastCovLinkId<<endl;
//cout<<"[before] former first:"<<formerFirst<<" former last:"<<formerLast<<" first:"<<first<<" last:"<<last<<endl;
if (addLink && formerNstcov==0){
first=firstCovLinkId;
last=lastCovLinkId;
}else if (!addLink && formerNstcov==1){
first=-1;
last=-1;
}else {
if (addLink){
// first
if (firstCovLinkId < formerFirst){
first=firstCovLinkId;
}else{
first=formerFirst;
}
// last
if (lastCovLinkId > formerLast){
last=lastCovLinkId;
}else{
last=formerLast;
}
}else{
// first
if (firstCovLinkId == formerFirst){ // we remove the first link; we know the new first link is greater, but we need to search for its exact position
first=firstCovLinkId;
updateFirst=true;
}else{
first=formerFirst;
}
// last
if (lastCovLinkId == formerLast){ // we remove the last link; we know the new last link is smaller, but we need to search for its exact position
last=lastCovLinkId;
updateLast=true;
}else{
last=formerLast;
}
}
}
//cout<<"[after] former first:"<<formerFirst<<" former last:"<<formerLast<<" first:"<<first<<" last:"<<last<<endl;
formerNstcov=nstcov;
formerFirst=first;
formerLast=last;
//cout<<"[after 2] former first:"<<formerFirst<<" former last:"<<formerLast<<" first:"<<first<<" last:"<<last<<endl;
// update history for hypothesis recombination
if (pars.hr>0){
if (pars.exp=="word" && (direction=="as" || direction =="st")){histKey.push_back(lit->first.tbeg());}
else if (pars.exp=="word" && (direction=="at" || direction =="ts")){histKey.push_back(lit->first.sbeg());}
else {histKey.push_back(lit->first.id);}
}
} // for (vector<pair<wlink,bool> >::const_iterator lit=lnks.begin();lit!=lnks.end();++lit){
// update history from father
if (pars.hr>0){
list<vector<int> >::reverse_iterator h=father->hist.rbegin();
int nh=0;
while (h != father->hist.rend() && nh< pars.hr-1){
hist.push_front(*h);++nhist;
++nh;
++h;
}
hist.push_back(histKey);
++nhist;
}
(void) times (time_buffer);
t1 = time_buffer->tms_utime / clocks_per_sec;
for (set<int>::const_iterator posit=visitedSposi.begin();posit!=visitedSposi.end();++posit){
if (scov[*posit]>0 && father->scov[*posit]==0){
++newLinkedSrc;
++nscov;
if (pars.wum!=0 || pars.wums!=0) sumCost+=sp.src().at(*posit).getNullCst();
}else if (scov[*posit]==0 && father->scov[*posit]>0 ){
--newLinkedSrc;
--nscov;
if (pars.wum!=0 || pars.wums!=0) sumCost-=sp.src().at(*posit).getNullCst();
}
if (pars.wf>0 || pars.wfs>0) sfertCost+=sp.src().at(*posit).getFertCst(scov[*posit])-sp.src().at(*posit).getFertCst(father->scov[*posit]);
}
for (set<int>::const_iterator posit=visitedTposi.begin();posit!=visitedTposi.end();++posit){
if (tcov[*posit]>0 && father->tcov[*posit]==0){
++newLinkedTrg;
++ntcov;
if (pars.wum!=0 || pars.wumt!=0) tumCost+=sp.trg().at(*posit).getNullCst();
}else if (tcov[*posit]==0 && father->tcov[*posit]>0 ){
--newLinkedTrg;
--ntcov;
if (pars.wum!=0 || pars.wumt!=0) tumCost-=sp.trg().at(*posit).getNullCst();
}
if (pars.wf>0 || pars.wft>0) tfertCost+=sp.trg().at(*posit).getFertCst(tcov[*posit])-sp.trg().at(*posit).getFertCst(father->tcov[*posit]);
}
int ncross=0;
int lcross=0;
int nSrcGaps=0;
int nTrgGaps=0;
int nzigzags=0;
int n1to1=0;
int n1toN=0;
int nNto1=0;
int nNtoM=0;
//int nSameSrc=0; // number of links with same source as lnk
vector<int> lastSrc(tcov.size(),-1); // Last source position seen for this target position
(void) times (time_buffer);
t2 = time_buffer->tms_utime / clocks_per_sec;
pars.timeControl[2]+=t2-t1;
// UPDATE COVERAGE AND OTHER COUNTS
int curTrg=-1;
int curSrc=-1;
int firstFound=-1;
int lastFound=-1;
(void) times (time_buffer);
t1 = time_buffer->tms_utime / clocks_per_sec;
// for (linkIndex v=first;v<=last;++v){
for (linkIndex v=0;v<stcov.size();++v){
if (stcov[v]){
if (updateFirst && firstFound<0){firstFound=v;}
if (updateLast){lastFound=v;}
int sposi=posiOrderLinkCoverage.src(v);
int tposi=posiOrderLinkCoverage.trg(v);
//cerr<<"("<<sposi<<","<<tposi<<")"<<endl;
// count crossings
if (tposi-curTrg<0){
++ncross;
lcross+=-tposi+curTrg;
//cerr<<"\tcrossing\n";
}
// gap calculations
if (lastSrc[tposi]>-1 && sposi-lastSrc[tposi]>1){
nSrcGaps+=sposi-lastSrc[tposi]-1;
}
lastSrc[tposi]=sposi;
if (sposi>curSrc){
curSrc=sposi;
}else{
if (tposi-curTrg>1){
nTrgGaps+=tposi-curTrg-1;
}
}
curTrg=tposi;
// zigzag calculations (links between positions which both have at least another link)
if (scov.at(sposi)>1 && tcov.at(tposi)>1){++nzigzags;}
// link type counts
if (scov.at(sposi)==1){
if (tcov.at(tposi)==1){++n1to1;}
else if (tcov.at(tposi)>1){++nNto1;}
}else if (scov.at(sposi)>1){
if (tcov.at(tposi)==1){++n1toN;}
else if (tcov.at(tposi)>1){++nNtoM;}
}
}
} //for (linkIndex v=first;v<=last;++v){
// For many-to-many links, we count only nsrc*ntrg*linkWeight zigzags instead of nsrc*ntrg
nzigzags-=nInternalZigzags;
(void) times (time_buffer);
t2 = time_buffer->tms_utime / clocks_per_sec;
pars.timeControl[3]+=t2-t1;
(void) times (time_buffer);
t1 = time_buffer->tms_utime / clocks_per_sec;
if (updateFirst){first=firstFound;}
if (updateLast){last=lastFound;}
if (pars.verbose>2){
oss<<"nc:"<<ncross<<" lc:"<<lcross<<"\n";
oss<<"src gaps:"<<nSrcGaps<<"\n";
oss<<"trg gaps:"<<nTrgGaps<<"\n";
}
// Chunk Filter
int nChFiltSrc=0;
int nChFiltTrg=0;
// WARNING: NOT COMPATIBLE WITH LEVEL>0 SO FAR
/*
if ( (pars.wchfilt>0 ||pars.wchfilts>0) && !srcChFiltLimits.empty()){
int curCh=sp.srcat(lnk.sbeg()).getPos(1);
if (lnk.tbeg()<srcChFiltLimits[curCh].first || lnk.tbeg()>srcChFiltLimits[curCh].second){
if (addLink) ++nChFiltSrc;
else --nChFiltSrc;
//oss<<"chfiltsrc:"<<lnk.tbeg()<<" outside ["<<srcChFiltLimits[curCh].first<<","<<srcChFiltLimits[curCh].second<<"]\n";
}
}
if ( (pars.wchfilt>0 ||pars.wchfiltt>0) && !trgChFiltLimits.empty()){
int curCh=sp.trgat(lnk.tbeg()).getPos(1);
if (lnk.sbeg()<trgChFiltLimits[curCh].first || lnk.sbeg()>trgChFiltLimits[curCh].second){
if (addLink) ++nChFiltTrg;
else --nChFiltTrg;
//oss<<"chfilttrg:"<<lnk.sbeg()<<" outside ["<<trgChFiltLimits[curCh].first<<","<<trgChFiltLimits[curCh].second<<"]\n";
}
}
*/
// COST CALCULATIONS
//********************
int lastModel=0;
cost currentCost;
transmissibleCst=father->transmissibleCst;
cost wwda1Cst=0; cost wwda2Cst=0; cost wwda3Cst=0; cost wwda4Cst=0; cost wwda5Cst=0; cost wwda6Cst=0; cost wtgaCst=0;
cost wrkCst=0; cost wrksCst=0; cost wrktCst=0; cost wrkbCst=0; cost wrkwCst=0;
cost wmatchbCst=0; cost wstembCst=0; cost wsynbCst=0;
cost wlbCst=0; cost wppCst=0; cost wupCst=0; cost wusCst=0; cost wutCst=0; cost wumCst=0; cost wumsCst=0; cost wumtCst=0;
cost wfCst=0; cost wfsCst=0; cost wftCst=0;
for (vector<pair<wlink,bool> >::const_iterator lit=lnks.begin();lit!=lnks.end();++lit){
bool addLink=lit->second;
// word association scores
if (pars.wwda1[level]!=0){
if (addLink) currentCost=pars.wwda1[level] * lit->first.costs[0];
else currentCost=-pars.wwda1[level] * lit->first.costs[0];
wwda1Cst+=currentCost;
}
if (pars.wwda2!=0 && level==0){
if (addLink) currentCost=pars.wwda2*lit->first.costs[1];
else currentCost=-pars.wwda2*lit->first.costs[1];
wwda2Cst+=currentCost;
}
if (pars.wwda3!=0 && level==0){
if (addLink) currentCost=pars.wwda3*lit->first.costs[2];
else currentCost=-pars.wwda3*lit->first.costs[2];
wwda3Cst+=currentCost;
}
if (pars.wwda4!=0 && level==0){
if (addLink) currentCost=pars.wwda4*lit->first.costs[3];
else currentCost=-pars.wwda4*lit->first.costs[3];
wwda4Cst+=currentCost;
}
if (pars.wwda5!=0 && level==0){
if (addLink) currentCost=pars.wwda5*lit->first.costs[3];
else currentCost=-pars.wwda5*lit->first.costs[3];
wwda5Cst+=currentCost;
}
if (pars.wwda6!=0 && level==0){
if (addLink) currentCost=pars.wwda6*lit->first.costs[3];
else currentCost=-pars.wwda6*lit->first.costs[3];
wwda6Cst+=currentCost;
}
// Part-of-Speech association score
if (pars.wtga!=0 && level==0){
if (addLink) currentCost=pars.wtga*lit->first.pofsCst;
else currentCost=-pars.wtga*lit->first.pofsCst;
wtgaCst+=currentCost;
}
// association rank
if (pars.wrk!=0 && level==0){
if (addLink) currentCost=pars.wrk*(lit->first.trgRankForSrc+lit->first.srcRankForTrg);
else currentCost=-pars.wrk*(lit->first.trgRankForSrc+lit->first.srcRankForTrg);
wrkCst+=currentCost;
}
if (pars.wrks!=0 && level==0){
if (addLink) currentCost=pars.wrks*lit->first.trgRankForSrc;
else currentCost=-pars.wrks*lit->first.trgRankForSrc;
wrksCst+=currentCost;
}
if (pars.wrkt!=0 && level==0){
if (addLink) currentCost=pars.wrkt*lit->first.srcRankForTrg;
else currentCost=-pars.wrkt*lit->first.srcRankForTrg;
wrktCst+=currentCost;
}
float bestRank,worstRank;
if (lit->first.trgRankForSrc>lit->first.srcRankForTrg){
bestRank=lit->first.srcRankForTrg;
worstRank=lit->first.trgRankForSrc;
}else{
bestRank=lit->first.trgRankForSrc;
worstRank=lit->first.srcRankForTrg;
}
if (pars.wrkb!=0 && level==0){
if (addLink) currentCost=pars.wrkb*bestRank;
else currentCost=-pars.wrkb*bestRank;
wrkbCst+=currentCost;
}
if (pars.wrkw!=0 && level==0){
if (addLink) currentCost=pars.wrkw*worstRank;
else currentCost=-pars.wrkw*worstRank;
wrkwCst+=currentCost;
}
// match, stem and syn bonus
if (pars.wmatchb!=0 && level==0){
if (addLink) currentCost=-pars.wmatchb*lit->first.match;
else currentCost=pars.wmatchb*lit->first.match;
wmatchbCst+=currentCost;
}
if (pars.wstemb!=0 && level==0){
if (addLink) currentCost=-pars.wstemb*lit->first.stem;
else currentCost=pars.wstemb*lit->first.stem;
wstembCst+=currentCost;
}
if (pars.wsynb!=0 && level==0){
if (addLink) currentCost=-pars.wsynb*lit->first.syn;
else currentCost=pars.wsynb*lit->first.syn;
wsynbCst+=currentCost;
}
// link bonus
int nsrc=lit->first.send()-lit->first.sbeg()+1;
int ntrg=lit->first.tend()-lit->first.tbeg()+1;
float linkWeight;
if (pars.phLinkWeight=="one"){
linkWeight=1.0/(nsrc*ntrg);
}else if (pars.phLinkWeight=="weighted"){
linkWeight=0.5*(1.0/nsrc+1.0/ntrg);
}else{
linkWeight=1.0;
}
if (pars.wlb[level]!=0){
if (addLink) currentCost=-1.0*nsrc*ntrg*linkWeight*pars.wlb[level];
else currentCost=nsrc*ntrg*linkWeight*pars.wlb[level];
// if (addLink) currentCost=-1.0*nsrc*ntrg*pars.wlb[level];
// else currentCost=nsrc*ntrg*pars.wlb[level];
wlbCst+=currentCost;
}
// phrase-link penalty
if (pars.wpp[level]!=0){
if (addLink) currentCost=(nsrc*ntrg-1)*pars.wpp[level];
else currentCost=-1.0*(nsrc*ntrg-1)*pars.wpp[level];
wppCst+=currentCost;
}
} //for (vector<pair<wlink,bool> >::const_iterator lit=lnks.begin();lit!=lnks.end();++lit){
// unlinked word penalities/model and fertility Delta costs are updated for all links, so we keep this code outside the lnks loop
// unlinked word penalties
if (pars.wup[level]!=0){
currentCost=-pars.wup[level]*(newLinkedSrc+newLinkedTrg);
wupCst+=currentCost;
}
if (pars.wus[level]!=0){
currentCost=-pars.wus[level]*newLinkedSrc;
wusCst+=currentCost;
}
if (pars.wut[level]!=0){
currentCost=-pars.wut[level]*newLinkedTrg;
wutCst+=currentCost;
}
//unlinked word model
if (pars.wum!=0 && level==0){
currentCost=-pars.wum*(sumCost+tumCost);
wumCst+=currentCost;
}
if (pars.wums!=0 && level==0){
currentCost=-pars.wums*sumCost;
wumsCst+=currentCost;
}
if (pars.wumt!=0 && level==0){
currentCost=-pars.wumt*tumCost;
wumtCst+=currentCost;
}
// fertility costs
if (pars.wf!=0 && level==0){
currentCost=pars.wf*(sfertCost+tfertCost);
wfCst+=currentCost;
}
if (pars.wfs!=0 && level==0){
currentCost=pars.wfs*sfertCost;
wfsCst+=currentCost;
}
if (pars.wft!=0 && level==0){
currentCost=pars.wft*tfertCost;
wftCst+=currentCost;
}
// word association scores
if (pars.wwda1[level]!=0){
transmissibleCst+=wwda1Cst;
costs.push_back(father->costs[lastModel++]+wwda1Cst);
}
if (pars.wwda2!=0 && level==0){
transmissibleCst+=wwda2Cst;
costs.push_back(father->costs[lastModel++]+wwda2Cst);
}
if (pars.wwda3!=0 && level==0){
transmissibleCst+=wwda3Cst;
costs.push_back(father->costs[lastModel++]+wwda3Cst);
}
if (pars.wwda4!=0 && level==0){
transmissibleCst+=wwda4Cst;
costs.push_back(father->costs[lastModel++]+wwda4Cst);
}
if (pars.wwda5!=0 && level==0){
transmissibleCst+=wwda5Cst;
costs.push_back(father->costs[lastModel++]+wwda5Cst);
}
if (pars.wwda6!=0 && level==0){
transmissibleCst+=wwda6Cst;
costs.push_back(father->costs[lastModel++]+wwda6Cst);
}
// Part-of-Speech association score
if (pars.wtga!=0 && level==0){
transmissibleCst+=wtgaCst;
costs.push_back(father->costs[lastModel++]+wtgaCst);
}
// association rank
if (pars.wrk!=0 && level==0){
transmissibleCst+=wrkCst;
costs.push_back(father->costs[lastModel++]+wrkCst);
}
if (pars.wrks!=0 && level==0){
transmissibleCst+=wrksCst;
costs.push_back(father->costs[lastModel++]+wrksCst);
}
if (pars.wrkt!=0 && level==0){
transmissibleCst+=wrktCst;
costs.push_back(father->costs[lastModel++]+wrktCst);
}
if (pars.wrkb!=0 && level==0){
transmissibleCst+=wrkbCst;
costs.push_back(father->costs[lastModel++]+wrkbCst);
}
if (pars.wrkw!=0 && level==0){
transmissibleCst+=wrkwCst;
costs.push_back(father->costs[lastModel++]+wrkwCst);
}
// match, stem and syn bonus
if (pars.wmatchb!=0 && level==0){
transmissibleCst+=wmatchbCst;
costs.push_back(father->costs[lastModel++]+wmatchbCst);
}
if (pars.wstemb!=0 && level==0){
transmissibleCst+=wstembCst;
costs.push_back(father->costs[lastModel++]+wstembCst);
}
if (pars.wsynb!=0 && level==0){
transmissibleCst+=wsynbCst;
costs.push_back(father->costs[lastModel++]+wsynbCst);
}
// link bonus
if (pars.wlb[level]!=0){
transmissibleCst+=wlbCst;
costs.push_back(father->costs[lastModel++]+wlbCst);
}
// phrase-link penalty
if (pars.wpp[level]!=0){
transmissibleCst+=wppCst;
costs.push_back(father->costs[lastModel++]+wppCst);
}
// unlinked word penalties
if (pars.wup[level]!=0){
transmissibleCst+=wupCst;
costs.push_back(father->costs[lastModel++]+wupCst);
}
if (pars.wus[level]!=0){
transmissibleCst+=wusCst;
costs.push_back(father->costs[lastModel++]+wusCst);
}
if (pars.wut[level]!=0){
transmissibleCst+=wutCst;
costs.push_back(father->costs[lastModel++]+wutCst);
}
//unlinked word model
if (pars.wum!=0 && level==0){
transmissibleCst+=wumCst;
costs.push_back(father->costs[lastModel++]+wumCst);
}
if (pars.wums!=0 && level==0){
transmissibleCst+=wumsCst;
costs.push_back(father->costs[lastModel++]+wumsCst);
}
if (pars.wumt!=0 && level==0){
transmissibleCst+=wumtCst;
costs.push_back(father->costs[lastModel++]+wumtCst);
}
// fertility costs
if (pars.wf!=0 && level==0){
transmissibleCst+=wfCst;
costs.push_back(father->costs[lastModel++]+wfCst);
}
if (pars.wfs!=0 && level==0){
transmissibleCst+=wfsCst;
costs.push_back(father->costs[lastModel++]+wfsCst);
}
if (pars.wft!=0 && level==0){
transmissibleCst+=wftCst;
costs.push_back(father->costs[lastModel++]+wftCst);
}
cst=transmissibleCst;
// crossing penalties
//cout<<"id:"<<id<<" ncross:"<<ncross<<" intcn:"<<internalCrossNumber<<endl;
if (pars.wcn[level]!=0){
currentCost=pars.wcn[level]*(ncross-internalCrossNumber);
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
if (pars.wcl[level]!=0){
currentCost=pars.wcl[level]*(lcross-internalCrossLength);
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
// gap penalties
if (pars.whp[level]!=0){
currentCost=pars.whp[level]*(nSrcGaps+nTrgGaps);
if (pars.verbose>2){oss<<"previous cst:"<<cst<< " current cst:"<<currentCost<<endl;}
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
if (pars.whs[level]!=0){
currentCost=pars.whs[level]*nSrcGaps;
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
if (pars.wht[level]!=0){
currentCost=pars.wht[level]*nTrgGaps;
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
// zigzag penalty
if (pars.wzp[level]!=0){
currentCost=pars.wzp[level]*nzigzags;
if (pars.verbose>2){oss<<"previous cst:"<<cst<< " current cst:"<<currentCost<<endl;}
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
// link type counts
if (pars.w1to1!=0 && level==0){
currentCost=-pars.w1to1*n1to1;
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
if (pars.w1toN!=0 && level==0){
currentCost=-pars.w1toN*n1toN;
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
if (pars.wNto1!=0 && level==0){
currentCost=-pars.wNto1*nNto1;
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
if (pars.w1toNsum!=0 && level==0){
currentCost=-pars.w1toNsum*(n1toN+nNto1);
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
if (pars.wNtoM!=0 && level==0){
currentCost=-pars.wNtoM*nNtoM;
cst+=currentCost;
++lastModel;
costs.push_back(currentCost);
}
// chunk filter
if (pars.wchfilt!=0){
currentCost=pars.wchfilt*(nChFiltSrc+nChFiltTrg);
cst+=currentCost;
costs.push_back(father->costs[lastModel++]+currentCost);
}
if (pars.wchfilts!=0){
currentCost=pars.wchfilts*nChFiltSrc;
cst+=currentCost;
costs.push_back(father->costs[lastModel++]+currentCost);
}
if (pars.wchfiltt!=0){
currentCost=pars.wchfiltt*nChFiltTrg;
cst+=currentCost;
costs.push_back(father->costs[lastModel++]+currentCost);
}
// future cost
if (fcEstim.enabled()){
if (father->fcHist.size()==0){
if (fcEstim.parsingSource()){fcHist.assign(scov.size(), 0);}
else {fcHist.assign(tcov.size(), 0);}
}else{
fcHist=father->fcHist;
}
// now that we just covered the position, true distortion will be calculated for this position and we remove estimated future costs from that position:
if (fcEstim.parsingSource()){cst-=fcHist[lnks[0].first.sbeg()];}
else {cst-=fcHist[lnks[0].first.tbeg()]*1.0;}
float curFutureCost=0;
curFutureCost+=fcEstim.calculateDistortion(lnks[0].first,scov,tcov,fcHist)*1.0;
fcst=curFutureCost+cst;
}else{
fcst=cst;
}
(void) times (time_buffer);
t2 = time_buffer->tms_utime / clocks_per_sec;
delete time_buffer;
pars.timeControl[4]+=t2-t1;
}
void state::setEmptyAl (linkIndex nstpairs, linkIndex nPossLinks, const sentPair & sp, int level, const param & pars){
id=0;
// lnkids.assign(1,make_pair(-1,false));
int nsrc=sp.numSrcToks(level);
int ntrg=sp.numTrgToks(level);
if (pars.backTrack){lcov.assign(nPossLinks,false);}
stcov.assign(nstpairs,false);
scov.assign(nsrc,0);
nscov=0;
internalCrossNumber=0;
internalCrossLength=0;
nInternalZigzags=0;
tcov.assign(ntrg,0);
ntcov=0;
first=-1;
last=first;
nstcov=0;
father=NULL;
cst=0;
transmissibleCst=0;
nhist=0;
if (pars.wwda1[level]!=0) {costs.push_back(0);}
if (pars.wwda2!=0 && level==0){costs.push_back(0);}
if (pars.wwda3!=0 && level==0){costs.push_back(0);}
if (pars.wwda4!=0 && level==0){costs.push_back(0);}
if (pars.wwda5!=0 && level==0){costs.push_back(0);}
if (pars.wwda6!=0 && level==0){costs.push_back(0);}
if (pars.wtga!=0 && level==0){costs.push_back(0);}
if (pars.wrk!=0 && level==0){costs.push_back(0);}
if (pars.wrks!=0 && level==0){costs.push_back(0);}
if (pars.wrkt!=0 && level==0){costs.push_back(0);}
if (pars.wrkb!=0 && level==0){costs.push_back(0);}
if (pars.wrkw!=0 && level==0){costs.push_back(0);}
if (pars.wmatchb!=0 && level==0){costs.push_back(0);}
if (pars.wstemb!=0 && level==0){costs.push_back(0);}
if (pars.wsynb!=0 && level==0){costs.push_back(0);}
if (pars.wlb[level]!=0){costs.push_back(0);}
if (pars.wpp[level]!=0){costs.push_back(0);}
if (pars.wup[level]!=0){
cost currentCost=pars.wup[level]*(nsrc+ntrg);
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wus[level]!=0){
cost currentCost=pars.wus[level]*nsrc;
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wut[level]!=0){
cost currentCost=pars.wut[level]*ntrg;
cst+=currentCost;
costs.push_back(currentCost);
}
cost sfertCost=0;
cost sumCost=0;
if ((pars.wums!=0 || pars.wum!=0 || pars.wfs!=0 || pars.wf!=0) && level==0){
for (confusionNet<iword>::const_iterator s=sp.src().begin();s!=sp.src().end();++s){
if (pars.wfs!=0 || pars.wf!=0) sfertCost+=s->getFertCst(0);
if (pars.wums!=0 || pars.wum!=0) sumCost+=s->getNullCst();
}
}
cost tfertCost=0;
cost tumCost=0;
if ((pars.wumt!=0 || pars.wum!=0 || pars.wft!=0 || pars.wf!=0) && level==0){
for (confusionNet<iword>::const_iterator s=sp.trg().begin();s!=sp.trg().end();++s){
if (pars.wft!=0 || pars.wf!=0) tfertCost+=s->getFertCst(0);
if (pars.wumt!=0 || pars.wum!=0) tumCost+=s->getNullCst();
}
}
if (pars.wum!=0 && level==0){
cost currentCost=pars.wum*(sumCost+tumCost);
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wums!=0 && level==0){
cost currentCost=pars.wums*sumCost;
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wumt!=0 && level==0){
cost currentCost=pars.wumt*tumCost;
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wf!=0 && level==0){
cost currentCost=pars.wf*(sfertCost+tfertCost);
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wfs!=0 && level==0){
cost currentCost=pars.wfs*sfertCost;
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wft!=0 && level==0){
cost currentCost=pars.wft*tfertCost;
cst+=currentCost;
costs.push_back(currentCost);
}
if (pars.wcn[level]!=0){costs.push_back(0);}
if (pars.wcl[level]!=0){costs.push_back(0);}
if (pars.whp[level]!=0){costs.push_back(0);}
if (pars.whs[level]!=0){costs.push_back(0);}
if (pars.wht[level]!=0){costs.push_back(0);}
if (pars.wzp[level]!=0){costs.push_back(0);}
if (pars.w1to1!=0 && level==0){costs.push_back(0);}
if (pars.w1toN!=0 && level==0){costs.push_back(0);}
if (pars.wNto1!=0 && level==0){costs.push_back(0);}
if (pars.w1toNsum!=0 && level==0){costs.push_back(0);}
if (pars.wNtoM!=0 && level==0){costs.push_back(0);}
fcst=cst;
transmissibleCst=cst;
}
string state::print(const wlinkSequence & possLinks, const param & pars) const {
ostringstream oss("");
oss<<"ID:"<<id<<" ";//<<this;
oss<<" (father:";//<<father<<" ";
if (father!=NULL){
oss<<father->id;
}
oss<<")";
//print links in state:
oss<<"\tCOV:";
int cntlcov=0;
if (lcov.size()>0){
for (linkIndex v=0;v<lcov.size();++v){
if (lcov[v]>0){
oss<<possLinks[v].printTalpDebug()<<" ";
++cntlcov;
}
}
}else{
vector<int> vec(possLinks.size(),0);
retrieveLinkCoverage(vec);
for (linkIndex v=0;v<vec.size();++v){
if (vec[v]){
oss<<possLinks[v].printTalpDebug()<<" ";
}
}
}
oss<<"(lcov "<<cntlcov<<"/"<<lcov.size()<<") nstcov:"<<nstcov<<" (1st,last): ("<<first<<","<<last<<")";
//oss<<" hist ("<<nhist<<"):";
oss<<" hist:";
for (list<vector<int> >::const_iterator h=hist.begin();h!=hist.end();++h){
if (h!=hist.begin()) oss<<",";
for (vector<int>::const_iterator v=h->begin();v!=h->end();++v){
if (v!=h->begin()){oss<<" ";}
oss<<*v;
}
}
oss<<" SRC:";
for (vector<int>::const_iterator v=scov.begin();v!=scov.end();++v){oss<<*v;}
oss<<" TRG:";
for (vector<int>::const_iterator v=tcov.begin();v!=tcov.end();++v){oss<<*v;}
// cost
if (costs.size()>0){
oss<<"\tcosts:f"<<fcst<<" "<<cst<<" (";
//oss<<"\tcosts:f"<<fcst<<" "<<cst<<" "<<transmissibleCst<<" (";
int ncst=0;
for (vector<cost>::const_iterator c=costs.begin();c!=costs.end();++c){
oss<<" "<<pars.models.at(0).at(ncst)<<":"<<*c;
++ncst;
}
oss<<" )";
}
return oss.str();
}
string state::printAlignment (const wlinkSequence & possLinks, const wordLinkSequenceVec & srcLinksVec, const wordLinkSequenceVec & trgLinksVec, string direction, const param & pars ) const {
ostringstream oss("");
bool firstLink=true;
linkClusterDiv clusts;
for (int lid=0;lid<lcov.size();++lid){
if (lcov[lid]){
if (pars.outputType == "clusters"){
//insterting word-to-word links in vector of clusters
for (int sposi=possLinks[lid].sbeg();sposi<=possLinks[lid].send();++sposi){
for (int tposi=possLinks[lid].tbeg();tposi<=possLinks[lid].tend();++tposi){
clusts.addLink(sposi,tposi);
}
}
}else if (pars.outputType == "phrase-links"){
if (firstLink){firstLink=false;}
else{oss<<" ";}
oss<<possLinks[lid].printTalpDebug();
}else{ //pars.outputType == "word-links"
if (direction=="at" || direction=="ts"){
// each target word can be linked to at most 1 source word
//oss<<":"<<possLinks[lid].printTalpDebug()<<":";
for (int tposi=possLinks[lid].tbeg();tposi<=possLinks[lid].tend();++tposi){
if (possLinks[lid].sbeg()==possLinks[lid].send()){
if (firstLink){firstLink=false;}
else{oss<<" ";}
oss<<possLinks[lid].sbeg()<<"-"<<tposi;
}else{
//look for the best linked source
int bestposi=-1;
for (set<wlink>::const_iterator l=trgLinksVec[0][tposi].second.begin(); l!=trgLinksVec[0][tposi].second.end();++l){
if (l->sbeg()==l->send() && l->sbeg()>=possLinks[lid].sbeg() && l->sbeg()<=possLinks[lid].send()){
bestposi=l->sbeg();
//oss<<"\n"<<l->print()<<endl;
break;
}
}
if (bestposi>=0){
if (firstLink){firstLink=false;}
else{oss<<" ";}
oss<<bestposi<<"-"<<tposi;
}
}
}
}else if (direction=="as" || direction=="st"){
// each source word can be linked to at most 1 target word
for (int sposi=possLinks[lid].sbeg();sposi<=possLinks[lid].send();++sposi){
if (possLinks[lid].tbeg()==possLinks[lid].tend()){
if (firstLink){firstLink=false;}
else{oss<<" ";}
oss<<sposi<<"-"<<possLinks[lid].tbeg();
}else{
//look for the best linked target
int bestposi=-1;
for (set<wlink>::const_iterator l=srcLinksVec[0][sposi].second.begin(); l!=srcLinksVec[0][sposi].second.end();++l){
if (l->tbeg()==l->tend() && l->tbeg()>=possLinks[lid].tbeg() && l->tbeg()<=possLinks[lid].tend()){
bestposi=l->tbeg();
//oss<<"\n"<<l->print()<<endl;
break;
}
}
if (bestposi>=0){
if (firstLink){firstLink=false;}
else{oss<<" ";}
oss<<sposi<<"-"<<bestposi;
}
}
}
}else{
// we can have many-to-many links
if (firstLink){firstLink=false;}
else{oss<<" ";}
oss<<possLinks[lid].printExtended();
} // if (direction==
} // if (pars.outputType ==
} //if (lcov[lid]){
} //for
if (pars.outputType == "clusters"){
clusts.sortAndSplitIntoContiguous();
oss<<clusts.print();
}
return oss.str();
}
string state::printForNbest (const wlinkSequence & possLinks, const wordLinkSequenceVec & srcLinksVec, const wordLinkSequenceVec & trgLinksVec, string direction, const param & pars,int numSentPair ) const {
ostringstream oss("");
oss<<numSentPair<<" ||| ";
oss<<printAlignment (possLinks, srcLinksVec, trgLinksVec, direction, pars );
oss<<" |||";
int ncst=0;
for (vector<cost>::const_iterator c=costs.begin();c!=costs.end();++c){
oss<<" "<<pars.models.at(0).at(ncst)<<": "<<*c;
++ncst;
}
oss<<" ||| ";
oss<<cst;
return oss.str();
}
void state::printLinkSequence(const wlinkSequence & posOrderPossLinks, wlinkSequence & linkseq) const {
for (linkIndex v=0;v<stcov.size();++v){
if (stcov[v]){
linkseq.push_back(posOrderPossLinks[v]);
}
}
}
void state::calculateTrgChFiltLimits(vector<pair<int,int> > & trgChFiltLims, const sentPair & sp,const wlinkSequence & possLinks, int level){
// initialize limits
trgChFiltLims.assign(sp.numTrgToks(level+1),make_pair(-1,sp.numSrcToks(level)));
int prevSrc=-1;
int prevChk=-1;
for (linkIndex v=0;v<stcov.size();++v){
if (stcov[v]){
int curChk=sp.trgat(possLinks[v].tbeg()).getPos(level+1);