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

Manager.cpp « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0cd540991e154f8891cbed0c306c6726a7ff71b5 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2  -*-
// 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
***********************************************************************/
#ifdef WIN32
#include <hash_set>
#else
// #include <ext/hash_set>
#endif

#include <algorithm>
#include <cmath>
#include <limits>
#include <map>
#include <set>
#include "Manager.h"
#include "TypeDef.h"
#include "Util.h"
#include "TargetPhrase.h"
#include "TrellisPath.h"
#include "TrellisPathCollection.h"
#include "TranslationOption.h"
#include "TranslationOptionCollection.h"
#include "Timer.h"
#include "moses/OutputCollector.h"
#include "moses/FF/DistortionScoreProducer.h"
#include "moses/LM/Base.h"
#include "moses/TranslationModel/PhraseDictionary.h"
#include "moses/TranslationAnalysis.h"
#include "moses/TranslationTask.h"
#include "moses/HypergraphOutput.h"
#include "moses/mbr.h"
#include "moses/LatticeMBR.h"
#include "moses/SearchNormal.h"
#include "moses/SearchCubePruning.h"
#include <boost/foreach.hpp>

#ifdef HAVE_PROTOBUF
#include "hypergraph.pb.h"
#include "rule.pb.h"
#endif

#include "util/exception.hh"
#include "util/random.hh"
#include "util/string_stream.hh"

using namespace std;

namespace Moses
{

Manager::Manager(ttasksptr const& ttask)
  : BaseManager(ttask)
  , interrupted_flag(0)
  , m_hypoId(0)
{
  boost::shared_ptr<InputType> source = ttask->GetSource();
  m_transOptColl = source->CreateTranslationOptionCollection(ttask);

  switch(options()->search.algo) {
  case Normal:
    m_search = new SearchNormal(*this, *m_transOptColl);
    break;
  case CubePruning:
    m_search = new SearchCubePruning(*this, *m_transOptColl);
    break;
  default:
    UTIL_THROW2("ERROR: search. Aborting\n");
  }

  StaticData::Instance().InitializeForInput(ttask);
}

Manager::~Manager()
{
  delete m_transOptColl;
  delete m_search;
  StaticData::Instance().CleanUpAfterSentenceProcessing(m_ttask.lock());
}

const InputType&
Manager::GetSource() const
{
  return m_source ;
}

/**
 * Main decoder loop that translates a sentence by expanding
 * hypotheses stack by stack, until the end of the sentence.
 */
void Manager::Decode()
{

  //std::cerr << options().nbest.nbest_size << " "
  //          << options().nbest.enabled << " " << std::endl;

  // initialize statistics
  ResetSentenceStats(m_source);
  IFVERBOSE(2) {
    GetSentenceStats().StartTimeTotal();
  }

  // check if alternate weight setting is used
  // this is not thread safe! it changes StaticData
  if (StaticData::Instance().GetHasAlternateWeightSettings()) {
    if (m_source.GetSpecifiesWeightSetting()) {
      StaticData::Instance().SetWeightSetting(m_source.GetWeightSetting());
    } else {
      StaticData::Instance().SetWeightSetting("default");
    }
  }

  // get translation options
  IFVERBOSE(1) {
    GetSentenceStats().StartTimeCollectOpts();
  }
  m_transOptColl->CreateTranslationOptions();

  // some reporting on how long this took
  IFVERBOSE(1) {
    GetSentenceStats().StopTimeCollectOpts();
    TRACE_ERR("Line "<< m_source.GetTranslationId()
              << ": Collecting options took "
              << GetSentenceStats().GetTimeCollectOpts() << " seconds at "
              << __FILE__ << " Line " << __LINE__ << endl);
  }

  // search for best translation with the specified algorithm
  Timer searchTime;
  searchTime.start();
  m_search->Decode();
  VERBOSE(1, "Line " << m_source.GetTranslationId()
          << ": Search took " << searchTime << " seconds" << endl);
  IFVERBOSE(2) {
    GetSentenceStats().StopTimeTotal();
    TRACE_ERR(GetSentenceStats());
  }
}

/**
 * Print all derivations in search graph. Note: The number of derivations is exponential in the sentence length
 *
 */

void Manager::PrintAllDerivations(long translationId, ostream& outputStream) const
{
  const std::vector < HypothesisStack* > &hypoStackColl = m_search->GetHypothesisStacks();

  vector<const Hypothesis*> sortedPureHypo = hypoStackColl.back()->GetSortedList();

  if (sortedPureHypo.size() == 0)
    return;

  float remainingScore = 0;
  vector<const TargetPhrase*> remainingPhrases;

  // add all pure paths
  vector<const Hypothesis*>::const_iterator iterBestHypo;
  for (iterBestHypo = sortedPureHypo.begin()
                      ; iterBestHypo != sortedPureHypo.end()
       ; ++iterBestHypo) {
    printThisHypothesis(translationId, *iterBestHypo, remainingPhrases, remainingScore, outputStream);
    printDivergentHypothesis(translationId, *iterBestHypo, remainingPhrases, remainingScore, outputStream);
  }
}

const TranslationOptionCollection* Manager::getSntTranslationOptions()
{
  return m_transOptColl;
}

void Manager::printDivergentHypothesis(long translationId, const Hypothesis* hypo, const vector <const TargetPhrase*> & remainingPhrases, float remainingScore , ostream& outputStream ) const
{
  //Backtrack from the predecessor
  if (hypo->GetId()  > 0) {
    vector <const TargetPhrase*> followingPhrases;
    followingPhrases.push_back(& (hypo->GetCurrTargetPhrase()));
    ///((Phrase) hypo->GetPrevHypo()->GetTargetPhrase());
    followingPhrases.insert(followingPhrases.end()--, remainingPhrases.begin(), remainingPhrases.end());
    printDivergentHypothesis(translationId, hypo->GetPrevHypo(), followingPhrases , remainingScore + hypo->GetScore() - hypo->GetPrevHypo()->GetScore(), outputStream);
  }

  //Process the arcs
  const ArcList *pAL = hypo->GetArcList();
  if (pAL) {
    const ArcList &arcList = *pAL;
    // every possible Arc to replace this edge
    ArcList::const_iterator iterArc;
    for (iterArc = arcList.begin() ; iterArc != arcList.end() ; ++iterArc) {
      const Hypothesis *loserHypo = *iterArc;
      const Hypothesis* loserPrevHypo = loserHypo->GetPrevHypo();
      float arcScore = loserHypo->GetScore() - loserPrevHypo->GetScore();
      vector <const TargetPhrase* > followingPhrases;
      followingPhrases.push_back(&(loserHypo->GetCurrTargetPhrase()));
      followingPhrases.insert(followingPhrases.end()--, remainingPhrases.begin(), remainingPhrases.end());
      printThisHypothesis(translationId, loserPrevHypo, followingPhrases, remainingScore + arcScore, outputStream);
      printDivergentHypothesis(translationId, loserPrevHypo, followingPhrases, remainingScore + arcScore, outputStream);
    }
  }
}


void
Manager::
printThisHypothesis(long translationId, const Hypothesis* hypo,
                    const vector <const TargetPhrase*> & remainingPhrases,
                    float remainingScore, ostream& outputStream) const
{

  outputStream << translationId << " ||| ";

  //Yield of this hypothesis
  hypo->ToStream(outputStream);
  for (size_t p = 0; p < remainingPhrases.size(); ++p) {
    const TargetPhrase * phrase = remainingPhrases[p];
    size_t size = phrase->GetSize();
    for (size_t pos = 0 ; pos < size ; pos++) {
      const Factor *factor = phrase->GetFactor(pos, 0);
      outputStream << *factor;
      outputStream << " ";
    }
  }

  outputStream << "||| " << hypo->GetScore() + remainingScore;
  outputStream << endl;
}




/**
 * After decoding, the hypotheses in the stacks and additional arcs
 * form a search graph that can be mined for n-best lists.
 * The heavy lifting is done in the TrellisPath and TrellisPathCollection
 * this function controls this for one sentence.
 *
 * \param count the number of n-best translations to produce
 * \param ret holds the n-best list that was calculated
 */
void Manager::CalcNBest(size_t count, TrellisPathList &ret, bool onlyDistinct) const
{
  if (count <= 0)
    return;

  const std::vector < HypothesisStack* > &hypoStackColl = m_search->GetHypothesisStacks();

  vector<const Hypothesis*> sortedPureHypo = hypoStackColl.back()->GetSortedList();

  if (sortedPureHypo.size() == 0)
    return;

  TrellisPathCollection contenders;

  set<Phrase> distinctHyps;

  // add all pure paths
  vector<const Hypothesis*>::const_iterator iterBestHypo;
  for (iterBestHypo = sortedPureHypo.begin()
                      ; iterBestHypo != sortedPureHypo.end()
       ; ++iterBestHypo) {
    contenders.Add(new TrellisPath(*iterBestHypo));
  }

  // factor defines stopping point for distinct n-best list if too
  // many candidates identical
  size_t nBestFactor = options()->nbest.factor;
  if (nBestFactor < 1) nBestFactor = 1000; // 0 = unlimited

  // MAIN loop
  for (size_t iteration = 0 ; (onlyDistinct ? distinctHyps.size() : ret.GetSize()) < count && contenders.GetSize() > 0 && (iteration < count * nBestFactor) ; iteration++) {
    // get next best from list of contenders
    TrellisPath *path = contenders.pop();
    UTIL_THROW_IF2(path == NULL, "path is NULL");
    // create deviations from current best
    path->CreateDeviantPaths(contenders);
    if(onlyDistinct) {
      Phrase tgtPhrase = path->GetSurfacePhrase();
      if (distinctHyps.insert(tgtPhrase).second) {
        ret.Add(path);
      } else {
        delete path;
        path = NULL;
      }
    } else {
      ret.Add(path);
    }


    if(onlyDistinct) {
      const size_t nBestFactor = options()->nbest.factor;
      if (nBestFactor > 0)
        contenders.Prune(count * nBestFactor);
    } else {
      contenders.Prune(count);
    }
  }
}

struct SGNReverseCompare {
  bool operator() (const SearchGraphNode& s1, const SearchGraphNode& s2) const {
    return s1.hypo->GetId() > s2.hypo->GetId();
  }
};

/**
  * Implements lattice sampling, as in Chatterjee & Cancedda, emnlp 2010
  **/
void Manager::CalcLatticeSamples(size_t count, TrellisPathList &ret) const
{

  vector<SearchGraphNode> searchGraph;
  GetSearchGraph(searchGraph);

  //Calculation of the sigmas of each hypothesis and edge. In C&C notation this is
  //the "log of the cumulative unnormalized probability of all the paths in the
  // lattice for the hypothesis to a final node"
  typedef pair<int, int> Edge;
  map<const Hypothesis*, float> sigmas;
  map<Edge, float> edgeScores;
  map<const Hypothesis*, set<const Hypothesis*> > outgoingHyps;
  map<int,const Hypothesis*> idToHyp;
  map<int,float> fscores;

  //Iterating through the hypos in reverse order of id gives  a reverse
  //topological order. We rely on the fact that hypo ids are given out
  //sequentially, as the search proceeds.
  //NB: Could just sort by stack.
  sort(searchGraph.begin(), searchGraph.end(), SGNReverseCompare());

  //first task is to fill in the outgoing hypos and edge scores.
  for (vector<SearchGraphNode>::const_iterator i = searchGraph.begin();
       i != searchGraph.end(); ++i) {
    const Hypothesis* hypo = i->hypo;
    idToHyp[hypo->GetId()] = hypo;
    fscores[hypo->GetId()] = i->fscore;
    if (hypo->GetId()) {
      //back to  current
      const Hypothesis* prevHypo = i->hypo->GetPrevHypo();
      outgoingHyps[prevHypo].insert(hypo);
      edgeScores[Edge(prevHypo->GetId(),hypo->GetId())] =
        hypo->GetScore() - prevHypo->GetScore();
    }
    //forward from current
    if (i->forward >= 0) {
      map<int,const Hypothesis*>::const_iterator idToHypIter = idToHyp.find(i->forward);
      UTIL_THROW_IF2(idToHypIter == idToHyp.end(),
                     "Couldn't find hypothesis " << i->forward);
      const Hypothesis* nextHypo = idToHypIter->second;
      outgoingHyps[hypo].insert(nextHypo);
      map<int,float>::const_iterator fscoreIter = fscores.find(nextHypo->GetId());
      UTIL_THROW_IF2(fscoreIter == fscores.end(),
                     "Couldn't find scores for hypothsis " << nextHypo->GetId());
      edgeScores[Edge(hypo->GetId(),nextHypo->GetId())] =
        i->fscore - fscoreIter->second;
    }
  }


  //then run through again to calculate sigmas
  for (vector<SearchGraphNode>::const_iterator i = searchGraph.begin();
       i != searchGraph.end(); ++i) {

    if (i->forward == -1) {
      sigmas[i->hypo] = 0;
    } else {
      map<const Hypothesis*, set<const Hypothesis*> >::const_iterator outIter =
        outgoingHyps.find(i->hypo);

      UTIL_THROW_IF2(outIter == outgoingHyps.end(),
                     "Couldn't find hypothesis " << i->hypo->GetId());
      float sigma = 0;
      for (set<const Hypothesis*>::const_iterator j = outIter->second.begin();
           j != outIter->second.end(); ++j) {
        map<const Hypothesis*, float>::const_iterator succIter = sigmas.find(*j);
        UTIL_THROW_IF2(succIter == sigmas.end(),
                       "Couldn't find hypothesis " << (*j)->GetId());
        map<Edge,float>::const_iterator edgeScoreIter =
          edgeScores.find(Edge(i->hypo->GetId(),(*j)->GetId()));
        UTIL_THROW_IF2(edgeScoreIter == edgeScores.end(),
                       "Couldn't find edge for hypothesis " << (*j)->GetId());
        float term = edgeScoreIter->second + succIter->second; // Add sigma(*j)
        if (sigma == 0) {
          sigma = term;
        } else {
          sigma = log_sum(sigma,term);
        }
      }
      sigmas[i->hypo] = sigma;
    }
  }

  //The actual sampling!
  const Hypothesis* startHypo = searchGraph.back().hypo;
  UTIL_THROW_IF2(startHypo->GetId() != 0, "Expecting the start hypothesis ");
  for (size_t i = 0; i < count; ++i) {
    vector<const Hypothesis*> path;
    path.push_back(startHypo);
    while(1) {
      map<const Hypothesis*, set<const Hypothesis*> >::const_iterator outIter =
        outgoingHyps.find(path.back());
      if (outIter == outgoingHyps.end() || !outIter->second.size()) {
        //end of the path
        break;
      }
      //score the possibles
      vector<const Hypothesis*> candidates;
      vector<float> candidateScores;
      float scoreTotal = 0;
      for (set<const Hypothesis*>::const_iterator j = outIter->second.begin();
           j != outIter->second.end(); ++j) {
        candidates.push_back(*j);
        UTIL_THROW_IF2(sigmas.find(*j) == sigmas.end(),
                       "Hypothesis " << (*j)->GetId() << " not found");
        Edge edge(path.back()->GetId(),(*j)->GetId());
        UTIL_THROW_IF2(edgeScores.find(edge) == edgeScores.end(),
                       "Edge not found");
        candidateScores.push_back(sigmas[*j]  + edgeScores[edge]);
        if (scoreTotal == 0) {
          scoreTotal = candidateScores.back();
        } else {
          scoreTotal = log_sum(candidateScores.back(), scoreTotal);
        }
      }

      //normalise
      transform(candidateScores.begin(), candidateScores.end(), candidateScores.begin(), bind2nd(minus<float>(),scoreTotal));
      //copy(candidateScores.begin(),candidateScores.end(),ostream_iterator<float>(cerr," "));
      //cerr << endl;

      //draw the sample
      const float frandom = log(util::rand_incl(0.0f, 1.0f));
      size_t position = 1;
      float sum = candidateScores[0];
      for (; position < candidateScores.size() && sum < frandom; ++position) {
        sum = log_sum(sum,candidateScores[position]);
      }
      //cerr << "Random: " << frandom << " Chose " << position-1 << endl;
      const Hypothesis* chosen =  candidates[position-1];
      path.push_back(chosen);
    }
    //cerr << "Path: " << endl;
    //for (size_t j = 0; j < path.size(); ++j) {
    // cerr << path[j]->GetId() <<  " " << path[j]->GetScoreBreakdown() << endl;
    //}
    //cerr << endl;

    //Convert the hypos to TrellisPath
    ret.Add(new TrellisPath(path));
    //cerr << ret.at(ret.GetSize()-1).GetScoreBreakdown() << endl;
  }

}



void Manager::CalcDecoderStatistics() const
{
  const Hypothesis *hypo = GetBestHypothesis();
  if (hypo != NULL) {
    GetSentenceStats().CalcFinalStats(*hypo);
    IFVERBOSE(2) {
      if (hypo != NULL) {
        string buff;
        string buff2;
        TRACE_ERR( "Source and Target Units:"
                   << hypo->GetInput());
        buff2.insert(0,"] ");
        buff2.insert(0,(hypo->GetCurrTargetPhrase()).ToString());
        buff2.insert(0,":");
        buff2.insert(0,(hypo->GetCurrSourceWordsRange()).ToString());
        buff2.insert(0,"[");

        hypo = hypo->GetPrevHypo();
        while (hypo != NULL) {
          //dont print out the empty final hypo
          buff.insert(0,buff2);
          buff2.clear();
          buff2.insert(0,"] ");
          buff2.insert(0,(hypo->GetCurrTargetPhrase()).ToString());
          buff2.insert(0,":");
          buff2.insert(0,(hypo->GetCurrSourceWordsRange()).ToString());
          buff2.insert(0,"[");
          hypo = hypo->GetPrevHypo();
        }
        TRACE_ERR( buff << endl);
      }
    }
  }
}

void Manager::OutputWordGraph(std::ostream &outputWordGraphStream, const Hypothesis *hypo, size_t &linkId) const
{

  const Hypothesis *prevHypo = hypo->GetPrevHypo();


  outputWordGraphStream << "J=" << linkId++
                        << "\tS=" << prevHypo->GetId()
                        << "\tE=" << hypo->GetId()
                        << "\ta=";

  // phrase table scores
  const std::vector<PhraseDictionary*> &phraseTables = PhraseDictionary::GetColl();
  std::vector<PhraseDictionary*>::const_iterator iterPhraseTable;
  for (iterPhraseTable = phraseTables.begin() ; iterPhraseTable != phraseTables.end() ; ++iterPhraseTable) {
    const PhraseDictionary *phraseTable = *iterPhraseTable;
    vector<float> scores = hypo->GetScoreBreakdown().GetScoresForProducer(phraseTable);

    outputWordGraphStream << scores[0];
    vector<float>::const_iterator iterScore;
    for (iterScore = ++scores.begin() ; iterScore != scores.end() ; ++iterScore) {
      outputWordGraphStream << ", " << *iterScore;
    }
  }

  // language model scores
  outputWordGraphStream << "\tl=";

  const std::vector<const StatefulFeatureFunction*> &statefulFFs = StatefulFeatureFunction::GetStatefulFeatureFunctions();
  for (size_t i = 0; i < statefulFFs.size(); ++i) {
    const StatefulFeatureFunction *ff = statefulFFs[i];
    const LanguageModel *lm = static_cast<const LanguageModel*>(ff);

    vector<float> scores = hypo->GetScoreBreakdown().GetScoresForProducer(lm);

    outputWordGraphStream << scores[0];
    vector<float>::const_iterator iterScore;
    for (iterScore = ++scores.begin() ; iterScore != scores.end() ; ++iterScore) {
      outputWordGraphStream << ", " << *iterScore;
    }
  }

  // re-ordering
  outputWordGraphStream << "\tr=";

  const std::vector<FeatureFunction*> &ffs = FeatureFunction::GetFeatureFunctions();
  std::vector<FeatureFunction*>::const_iterator iter;
  for (iter = ffs.begin(); iter != ffs.end(); ++iter) {
    const FeatureFunction *ff = *iter;

    const DistortionScoreProducer *model = dynamic_cast<const DistortionScoreProducer*>(ff);
    if (model) {
      outputWordGraphStream << hypo->GetScoreBreakdown().GetScoreForProducer(model);
    }
  }

  // output both source and target phrases in the word graph
  outputWordGraphStream << "\tw=" << hypo->GetSourcePhraseStringRep()
                        << "|" << hypo->GetCurrTargetPhrase();

  outputWordGraphStream << endl;
}

// VN put back of OutputPassthroughInformation
void Manager::OutputPassthroughInformation(std::ostream &out, const Hypothesis *hypo) const
{
  const std::string passthrough = hypo->GetManager().GetSource().GetPassthroughInformation();
  out << passthrough;
}
// end of put back

void Manager::GetOutputLanguageModelOrder( std::ostream &out, const Hypothesis *hypo ) const
{
  Phrase translation;
  hypo->GetOutputPhrase(translation);
  const std::vector<const StatefulFeatureFunction*> &statefulFFs = StatefulFeatureFunction::GetStatefulFeatureFunctions();
  for (size_t i = 0; i < statefulFFs.size(); ++i) {
    const StatefulFeatureFunction *ff = statefulFFs[i];
    if (const LanguageModel *lm = dynamic_cast<const LanguageModel*>(ff)) {
      lm->ReportHistoryOrder(out, translation);
    }
  }
}

void Manager::GetWordGraph(long translationId, std::ostream &outputWordGraphStream) const
{
  const StaticData &staticData = StaticData::Instance();
  const PARAM_VEC *params;

  string fileName;
  bool outputNBest = false;
  params = staticData.GetParameter().GetParam("output-word-graph");
  if (params && params->size()) {
    fileName = params->at(0);

    if (params->size() == 2) {
      outputNBest = Scan<bool>(params->at(1));
    }
  }

  const std::vector < HypothesisStack* > &hypoStackColl = m_search->GetHypothesisStacks();

  outputWordGraphStream << "VERSION=1.0" << endl
                        << "UTTERANCE=" << translationId << endl;

  size_t linkId = 0;
  std::vector < HypothesisStack* >::const_iterator iterStack;
  for (iterStack = ++hypoStackColl.begin() ; iterStack != hypoStackColl.end() ; ++iterStack) {
    const HypothesisStack &stack = **iterStack;
    HypothesisStack::const_iterator iterHypo;
    for (iterHypo = stack.begin() ; iterHypo != stack.end() ; ++iterHypo) {
      const Hypothesis *hypo = *iterHypo;
      OutputWordGraph(outputWordGraphStream, hypo, linkId);

      if (outputNBest) {
        const ArcList *arcList = hypo->GetArcList();
        if (arcList != NULL) {
          ArcList::const_iterator iterArcList;
          for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList) {
            const Hypothesis *loserHypo = *iterArcList;
            OutputWordGraph(outputWordGraphStream, loserHypo, linkId);
          }
        }
      } //if (outputNBest)
    } //for (iterHypo
  } // for (iterStack
}

void Manager::GetSearchGraph(vector<SearchGraphNode>& searchGraph) const
{
  std::map < int, bool > connected;
  std::map < int, int > forward;
  std::map < int, double > forwardScore;

  // *** find connected hypotheses ***
  std::vector< const Hypothesis *> connectedList;
  GetConnectedGraph(&connected, &connectedList);

  // ** compute best forward path for each hypothesis *** //

  // forward cost of hypotheses on final stack is 0
  const std::vector < HypothesisStack* > &hypoStackColl = m_search->GetHypothesisStacks();
  const HypothesisStack &finalStack = *hypoStackColl.back();
  HypothesisStack::const_iterator iterHypo;
  for (iterHypo = finalStack.begin() ; iterHypo != finalStack.end() ; ++iterHypo) {
    const Hypothesis *hypo = *iterHypo;
    forwardScore[ hypo->GetId() ] = 0.0f;
    forward[ hypo->GetId() ] = -1;
  }

  // compete for best forward score of previous hypothesis
  std::vector < HypothesisStack* >::const_iterator iterStack;
  for (iterStack = --hypoStackColl.end() ; iterStack != hypoStackColl.begin() ; --iterStack) {
    const HypothesisStack &stack = **iterStack;
    HypothesisStack::const_iterator iterHypo;
    for (iterHypo = stack.begin() ; iterHypo != stack.end() ; ++iterHypo) {
      const Hypothesis *hypo = *iterHypo;
      if (connected.find( hypo->GetId() ) != connected.end()) {
        // make a play for previous hypothesis
        const Hypothesis *prevHypo = hypo->GetPrevHypo();
        double fscore = forwardScore[ hypo->GetId() ] +
                        hypo->GetScore() - prevHypo->GetScore();
        if (forwardScore.find( prevHypo->GetId() ) == forwardScore.end()
            || forwardScore.find( prevHypo->GetId() )->second < fscore) {
          forwardScore[ prevHypo->GetId() ] = fscore;
          forward[ prevHypo->GetId() ] = hypo->GetId();
        }
        // all arcs also make a play
        const ArcList *arcList = hypo->GetArcList();
        if (arcList != NULL) {
          ArcList::const_iterator iterArcList;
          for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList) {
            const Hypothesis *loserHypo = *iterArcList;
            // make a play
            const Hypothesis *loserPrevHypo = loserHypo->GetPrevHypo();
            double fscore = forwardScore[ hypo->GetId() ] +
                            loserHypo->GetScore() - loserPrevHypo->GetScore();
            if (forwardScore.find( loserPrevHypo->GetId() ) == forwardScore.end()
                || forwardScore.find( loserPrevHypo->GetId() )->second < fscore) {
              forwardScore[ loserPrevHypo->GetId() ] = fscore;
              forward[ loserPrevHypo->GetId() ] = loserHypo->GetId();
            }
          } // end for arc list
        } // end if arc list empty
      } // end if hypo connected
    } // end for hypo
  } // end for stack

  // *** output all connected hypotheses *** //

  connected[ 0 ] = true;
  for (iterStack = hypoStackColl.begin() ; iterStack != hypoStackColl.end() ; ++iterStack) {
    const HypothesisStack &stack = **iterStack;
    HypothesisStack::const_iterator iterHypo;
    for (iterHypo = stack.begin() ; iterHypo != stack.end() ; ++iterHypo) {
      const Hypothesis *hypo = *iterHypo;
      if (connected.find( hypo->GetId() ) != connected.end()) {
        searchGraph.push_back(SearchGraphNode(hypo,NULL,forward[hypo->GetId()],
                                              forwardScore[hypo->GetId()]));

        const ArcList *arcList = hypo->GetArcList();
        if (arcList != NULL) {
          ArcList::const_iterator iterArcList;
          for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList) {
            const Hypothesis *loserHypo = *iterArcList;
            searchGraph.push_back(SearchGraphNode(loserHypo,hypo,
                                                  forward[hypo->GetId()], forwardScore[hypo->GetId()]));
          }
        } // end if arcList empty
      } // end if connected
    } // end for iterHypo
  } // end for iterStack

}

void Manager::OutputFeatureWeightsForSLF(std::ostream &outputSearchGraphStream) const
{
  outputSearchGraphStream.setf(std::ios::fixed);
  outputSearchGraphStream.precision(6);

  const vector<const StatelessFeatureFunction*>& slf  = StatelessFeatureFunction::GetStatelessFeatureFunctions();
  const vector<const StatefulFeatureFunction*>& sff   = StatefulFeatureFunction::GetStatefulFeatureFunctions();
  size_t featureIndex = 1;
  for (size_t i = 0; i < sff.size(); ++i) {
    featureIndex = OutputFeatureWeightsForSLF(featureIndex, sff[i], outputSearchGraphStream);
  }
  for (size_t i = 0; i < slf.size(); ++i) {
    /*
    if (slf[i]->GetScoreProducerWeightShortName() != "u" &&
          slf[i]->GetScoreProducerWeightShortName() != "tm" &&
          slf[i]->GetScoreProducerWeightShortName() != "I" &&
          slf[i]->GetScoreProducerWeightShortName() != "g")
    */
    {
      featureIndex = OutputFeatureWeightsForSLF(featureIndex, slf[i], outputSearchGraphStream);
    }
  }
  const vector<PhraseDictionary*>& pds = PhraseDictionary::GetColl();
  for( size_t i=0; i<pds.size(); i++ ) {
    featureIndex = OutputFeatureWeightsForSLF(featureIndex, pds[i], outputSearchGraphStream);
  }
  const vector<GenerationDictionary*>& gds = GenerationDictionary::GetColl();
  for( size_t i=0; i<gds.size(); i++ ) {
    featureIndex = OutputFeatureWeightsForSLF(featureIndex, gds[i], outputSearchGraphStream);
  }
}

void Manager::OutputFeatureValuesForSLF(const Hypothesis* hypo, bool zeros, std::ostream &outputSearchGraphStream) const
{
  outputSearchGraphStream.setf(std::ios::fixed);
  outputSearchGraphStream.precision(6);

  // outputSearchGraphStream << endl;
  // outputSearchGraphStream << (*hypo) << endl;
  // const ScoreComponentCollection& scoreCollection = hypo->GetScoreBreakdown();
  // outputSearchGraphStream << scoreCollection << endl;

  const vector<const StatelessFeatureFunction*>& slf =StatelessFeatureFunction::GetStatelessFeatureFunctions();
  const vector<const StatefulFeatureFunction*>& sff = StatefulFeatureFunction::GetStatefulFeatureFunctions();
  size_t featureIndex = 1;
  for (size_t i = 0; i < sff.size(); ++i) {
    featureIndex = OutputFeatureValuesForSLF(featureIndex, zeros, hypo, sff[i], outputSearchGraphStream);
  }
  for (size_t i = 0; i < slf.size(); ++i) {
    /*
    if (slf[i]->GetScoreProducerWeightShortName() != "u" &&
          slf[i]->GetScoreProducerWeightShortName() != "tm" &&
          slf[i]->GetScoreProducerWeightShortName() != "I" &&
          slf[i]->GetScoreProducerWeightShortName() != "g")
    */
    {
      featureIndex = OutputFeatureValuesForSLF(featureIndex, zeros, hypo, slf[i], outputSearchGraphStream);
    }
  }
  const vector<PhraseDictionary*>& pds = PhraseDictionary::GetColl();
  for( size_t i=0; i<pds.size(); i++ ) {
    featureIndex = OutputFeatureValuesForSLF(featureIndex, zeros, hypo, pds[i], outputSearchGraphStream);
  }
  const vector<GenerationDictionary*>& gds = GenerationDictionary::GetColl();
  for( size_t i=0; i<gds.size(); i++ ) {
    featureIndex = OutputFeatureValuesForSLF(featureIndex, zeros, hypo, gds[i], outputSearchGraphStream);
  }

}

void Manager::OutputFeatureValuesForHypergraph(const Hypothesis* hypo, std::ostream &outputSearchGraphStream) const
{
  outputSearchGraphStream.setf(std::ios::fixed);
  outputSearchGraphStream.precision(6);
  ScoreComponentCollection scores = hypo->GetScoreBreakdown();
  const Hypothesis *prevHypo = hypo->GetPrevHypo();
  if (prevHypo) {
    scores.MinusEquals(prevHypo->GetScoreBreakdown());
  }
  scores.Save(outputSearchGraphStream, false);
}


size_t Manager::OutputFeatureWeightsForSLF(size_t index, const FeatureFunction* ff, std::ostream &outputSearchGraphStream) const
{
  size_t numScoreComps = ff->GetNumScoreComponents();
  if (numScoreComps != 0) {
    vector<float> values = StaticData::Instance().GetAllWeights().GetScoresForProducer(ff);
    for (size_t i = 0; i < numScoreComps; ++i) {
      outputSearchGraphStream << "# " << ff->GetScoreProducerDescription()
                              << " "  << ff->GetScoreProducerDescription()
                              << " "  << (i+1) << " of " << numScoreComps << endl
                              << "x"  << (index+i) << "scale=" << values[i] << endl;
    }
    return index+numScoreComps;
  } else {
    cerr << "Sparse features are not supported when outputting HTK standard lattice format" << endl;
    assert(false);
    return 0;
  }
}

size_t
Manager::
OutputFeatureValuesForSLF(size_t index, bool zeros, const Hypothesis* hypo,
                          const FeatureFunction* ff, std::ostream &out) const
{
  const ScoreComponentCollection& scoreCollection = hypo->GetScoreBreakdown();
  vector<float> featureValues = scoreCollection.GetScoresForProducer(ff);
  size_t numScoreComps = featureValues.size();
  for (size_t i = 0; i < numScoreComps; ++i) {
    out << "x"  << (index+i) << "=" << ((zeros) ? 0.0 : featureValues[i]) << " ";
  }
  return index + numScoreComps;
}

/**! Output search graph in hypergraph format of Kenneth Heafield's lazy hypergraph decoder */
void
Manager::
OutputSearchGraphAsHypergraph(std::ostream &outputSearchGraphStream) const
{

  VERBOSE(2,"Getting search graph to output as hypergraph for sentence " << m_source.GetTranslationId() << std::endl)

  vector<SearchGraphNode> searchGraph;
  GetSearchGraph(searchGraph);


  map<int,int> mosesIDToHypergraphID;
  // map<int,int> hypergraphIDToMosesID;
  set<int> terminalNodes;
  multimap<int,int> hypergraphIDToArcs;

  VERBOSE(2,"Gathering information about search graph to output as hypergraph for sentence " << m_source.GetTranslationId() << std::endl)

  long numNodes = 0;
  long endNode = 0;
  {
    long hypergraphHypothesisID = 0;
    for (size_t arcNumber = 0, size=searchGraph.size(); arcNumber < size; ++arcNumber) {

      // Get an id number for the previous hypothesis
      const Hypothesis *prevHypo = searchGraph[arcNumber].hypo->GetPrevHypo();
      if (prevHypo!=NULL) {
        int mosesPrevHypothesisID = prevHypo->GetId();
        if (mosesIDToHypergraphID.count(mosesPrevHypothesisID) == 0) {
          mosesIDToHypergraphID[mosesPrevHypothesisID] = hypergraphHypothesisID;
          //	hypergraphIDToMosesID[hypergraphHypothesisID] = mosesPrevHypothesisID;
          hypergraphHypothesisID += 1;
        }
      }

      // Get an id number for this hypothesis
      int mosesHypothesisID;
      if (searchGraph[arcNumber].recombinationHypo) {
        mosesHypothesisID = searchGraph[arcNumber].recombinationHypo->GetId();
      } else {
        mosesHypothesisID = searchGraph[arcNumber].hypo->GetId();
      }

      if (mosesIDToHypergraphID.count(mosesHypothesisID) == 0) {

        mosesIDToHypergraphID[mosesHypothesisID] = hypergraphHypothesisID;
        //      hypergraphIDToMosesID[hypergraphHypothesisID] = mosesHypothesisID;

        bool terminalNode = (searchGraph[arcNumber].forward == -1);
        if (terminalNode) {
          // Final arc to end node, representing the end of the sentence </s>
          terminalNodes.insert(hypergraphHypothesisID);
        }

        hypergraphHypothesisID += 1;
      }

      // Record that this arc ends at this node
      hypergraphIDToArcs.insert(pair<int,int>(mosesIDToHypergraphID[mosesHypothesisID],arcNumber));

    }

    // Unique end node
    endNode = hypergraphHypothesisID;
    //    mosesIDToHypergraphID[hypergraphHypothesisID] = hypergraphHypothesisID;
    numNodes = endNode + 1;

  }


  long numArcs = searchGraph.size() + terminalNodes.size();

  //Header
  outputSearchGraphStream << "# target ||| features ||| source-covered" << endl;

  // Print number of nodes and arcs
  outputSearchGraphStream << numNodes << " " << numArcs << endl;

  VERBOSE(2,"Search graph to output as hypergraph for sentence " << m_source.GetTranslationId()
          << " contains " << numArcs << " arcs and " << numNodes << " nodes" << std::endl)

  VERBOSE(2,"Outputting search graph to output as hypergraph for sentence " << m_source.GetTranslationId() << std::endl)


  for (int hypergraphHypothesisID=0; hypergraphHypothesisID < endNode; hypergraphHypothesisID+=1) {
    if (hypergraphHypothesisID % 100000 == 0) {
      VERBOSE(2,"Processed " << hypergraphHypothesisID << " of " << numNodes << " hypergraph nodes for sentence " << m_source.GetTranslationId() << std::endl);
    }
    //    int mosesID = hypergraphIDToMosesID[hypergraphHypothesisID];
    size_t count = hypergraphIDToArcs.count(hypergraphHypothesisID);
    //    VERBOSE(2,"Hypergraph node " << hypergraphHypothesisID << " has " << count << " incoming arcs" << std::endl)
    if (count > 0) {
      outputSearchGraphStream << "# node " << hypergraphHypothesisID << endl;
      outputSearchGraphStream << count << "\n";

      pair<multimap<int,int>::iterator, multimap<int,int>::iterator> range =
        hypergraphIDToArcs.equal_range(hypergraphHypothesisID);
      for (multimap<int,int>::iterator it=range.first; it!=range.second; ++it) {
        int lineNumber = (*it).second;
        const Hypothesis *thisHypo = searchGraph[lineNumber].hypo;
        int mosesHypothesisID;// = thisHypo->GetId();
        if (searchGraph[lineNumber].recombinationHypo) {
          mosesHypothesisID = searchGraph[lineNumber].recombinationHypo->GetId();
        } else {
          mosesHypothesisID = searchGraph[lineNumber].hypo->GetId();
        }
        //	int actualHypergraphHypothesisID = mosesIDToHypergraphID[mosesHypothesisID];
        UTIL_THROW_IF2(
          (hypergraphHypothesisID != mosesIDToHypergraphID[mosesHypothesisID]),
          "Error while writing search lattice as hypergraph for sentence " << m_source.GetTranslationId() << ". " <<
          "Moses node " << mosesHypothesisID << " was expected to have hypergraph id " << hypergraphHypothesisID <<
          ", but actually had hypergraph id " << mosesIDToHypergraphID[mosesHypothesisID] <<
          ". There are " << numNodes << " nodes in the search lattice."
        );

        const Hypothesis *prevHypo = thisHypo->GetPrevHypo();
        if (prevHypo==NULL) {
          //	VERBOSE(2,"Hypergraph node " << hypergraphHypothesisID << " start of sentence" << std::endl)
          outputSearchGraphStream << "<s> |||  ||| 0\n";
        } else {
          int startNode = mosesIDToHypergraphID[prevHypo->GetId()];
          //	  VERBOSE(2,"Hypergraph node " << hypergraphHypothesisID << " has parent node " << startNode << std::endl)
          UTIL_THROW_IF2(
            (startNode >= hypergraphHypothesisID),
            "Error while writing search lattice as hypergraph for sentence" << m_source.GetTranslationId() << ". " <<
            "The nodes must be output in topological order. The code attempted to violate this restriction."
          );

          const TargetPhrase &targetPhrase = thisHypo->GetCurrTargetPhrase();
          int targetWordCount = targetPhrase.GetSize();

          outputSearchGraphStream << "[" << startNode << "] ";
          for (int targetWordIndex=0; targetWordIndex<targetWordCount; targetWordIndex+=1) {
            outputSearchGraphStream << targetPhrase.GetWord(targetWordIndex)[0]->GetString() << " ";
          }
          outputSearchGraphStream << " ||| ";
          OutputFeatureValuesForHypergraph(thisHypo, outputSearchGraphStream);
          outputSearchGraphStream << " ||| " << thisHypo->GetWordsBitmap().GetNumWordsCovered();
          outputSearchGraphStream << "\n";
        }
      }
    }
  }

  // Print node and arc(s) for end of sentence </s>
  outputSearchGraphStream << "# node " << endNode << endl;
  outputSearchGraphStream << terminalNodes.size() << "\n";
  for (set<int>::iterator it=terminalNodes.begin(); it!=terminalNodes.end(); ++it) {
    outputSearchGraphStream << "[" << (*it) << "] </s> |||  ||| " << GetSource().GetSize() << "\n";
  }

}


/**! Output search graph in HTK standard lattice format (SLF) */
void Manager::OutputSearchGraphAsSLF(long translationId, std::ostream &outputSearchGraphStream) const
{

  vector<SearchGraphNode> searchGraph;
  GetSearchGraph(searchGraph);

  long numArcs = 0;
  long numNodes = 0;

  map<int,int> nodes;
  set<int> terminalNodes;

  // Unique start node
  nodes[0] = 0;

  for (size_t arcNumber = 0; arcNumber < searchGraph.size(); ++arcNumber) {

    int targetWordCount = searchGraph[arcNumber].hypo->GetCurrTargetPhrase().GetSize();
    numArcs += targetWordCount;

    int hypothesisID = searchGraph[arcNumber].hypo->GetId();
    if (nodes.count(hypothesisID) == 0) {

      numNodes += targetWordCount;
      nodes[hypothesisID] = numNodes;
      //numNodes += 1;

      bool terminalNode = (searchGraph[arcNumber].forward == -1);
      if (terminalNode) {
        numArcs += 1;
      }
    }

  }
  numNodes += 1;

  // Unique end node
  nodes[numNodes] = numNodes;

  outputSearchGraphStream << "UTTERANCE=Sentence_" << translationId << endl;
  outputSearchGraphStream << "VERSION=1.1" << endl;
  outputSearchGraphStream << "base=2.71828182845905" << endl;
  outputSearchGraphStream << "NODES=" << (numNodes+1) << endl;
  outputSearchGraphStream << "LINKS=" << numArcs  << endl;

  OutputFeatureWeightsForSLF(outputSearchGraphStream);

  for (size_t arcNumber = 0, lineNumber = 0; lineNumber < searchGraph.size(); ++lineNumber) {
    const Hypothesis *thisHypo = searchGraph[lineNumber].hypo;
    const Hypothesis *prevHypo = thisHypo->GetPrevHypo();
    if (prevHypo) {

      int startNode = nodes[prevHypo->GetId()];
      int endNode   = nodes[thisHypo->GetId()];
      bool terminalNode = (searchGraph[lineNumber].forward == -1);
      const TargetPhrase &targetPhrase = thisHypo->GetCurrTargetPhrase();
      int targetWordCount = targetPhrase.GetSize();

      for (int targetWordIndex=0; targetWordIndex<targetWordCount; targetWordIndex+=1) {
        int x = (targetWordCount-targetWordIndex);

        outputSearchGraphStream <<  "J=" << arcNumber;

        if (targetWordIndex==0) {
          outputSearchGraphStream << " S=" << startNode;
        } else {
          outputSearchGraphStream << " S=" << endNode - x;
        }

        outputSearchGraphStream << " E=" << endNode - (x-1)
                                << " W=" << targetPhrase.GetWord(targetWordIndex);

        OutputFeatureValuesForSLF(thisHypo, (targetWordIndex>0), outputSearchGraphStream);

        outputSearchGraphStream  << endl;

        arcNumber += 1;
      }

      if (terminalNode && terminalNodes.count(endNode) == 0) {
        terminalNodes.insert(endNode);
        outputSearchGraphStream <<  "J="   << arcNumber
                                << " S="   << endNode
                                << " E="   << numNodes
                                << endl;
        arcNumber += 1;
      }
    }
  }

}


void
OutputSearchNode(AllOptions const& opts, long translationId,
                 std::ostream &out,
                 SearchGraphNode const& searchNode)
{
  const vector<FactorType> &outputFactorOrder = opts.output.factor_order;
  bool extendedFormat = opts.output.SearchGraphExtended.size();
  out << translationId;

  // special case: initial hypothesis
  if ( searchNode.hypo->GetId() == 0 ) {
    out << " hyp=0 stack=0";
    if (extendedFormat) {
      out << " forward=" << searchNode.forward	<< " fscore=" << searchNode.fscore;
    }
    out << endl;
    return;
  }

  const Hypothesis *prevHypo = searchNode.hypo->GetPrevHypo();

  // output in traditional format
  if (!extendedFormat) {
    out << " hyp=" << searchNode.hypo->GetId()
        << " stack=" << searchNode.hypo->GetWordsBitmap().GetNumWordsCovered()
        << " back=" << prevHypo->GetId()
        << " score=" << searchNode.hypo->GetScore()
        << " transition=" << (searchNode.hypo->GetScore() - prevHypo->GetScore());

    if (searchNode.recombinationHypo != NULL)
      out << " recombined=" << searchNode.recombinationHypo->GetId();

    out << " forward=" << searchNode.forward	<< " fscore=" << searchNode.fscore
        << " covered=" << searchNode.hypo->GetCurrSourceWordsRange().GetStartPos()
        << "-" << searchNode.hypo->GetCurrSourceWordsRange().GetEndPos()
        << " out=" << searchNode.hypo->GetCurrTargetPhrase().GetStringRep(outputFactorOrder)
        << endl;
    return;
  }

  out << " hyp=" << searchNode.hypo->GetId();
  out << " stack=" << searchNode.hypo->GetWordsBitmap().GetNumWordsCovered()
      << " back=" << prevHypo->GetId()
      << " score=" << searchNode.hypo->GetScore()
      << " transition=" << (searchNode.hypo->GetScore() - prevHypo->GetScore());

  if (searchNode.recombinationHypo != NULL)
    out << " recombined=" << searchNode.recombinationHypo->GetId();

  out << " forward=" << searchNode.forward	<< " fscore=" << searchNode.fscore
      << " covered=" << searchNode.hypo->GetCurrSourceWordsRange().GetStartPos()
      << "-" << searchNode.hypo->GetCurrSourceWordsRange().GetEndPos();

  // Modified so that -osgx is a superset of -osg (GST Oct 2011)
  ScoreComponentCollection scoreBreakdown = searchNode.hypo->GetScoreBreakdown();
  scoreBreakdown.MinusEquals( prevHypo->GetScoreBreakdown() );
  out << " scores=\"" << scoreBreakdown << "\""
      << " out=\"" << searchNode.hypo->GetSourcePhraseStringRep()
      << "|" << searchNode.hypo->GetCurrTargetPhrase().GetStringRep(outputFactorOrder) << "\"" << endl;
}

void Manager::GetConnectedGraph(
  std::map< int, bool >* pConnected,
  std::vector< const Hypothesis* >* pConnectedList) const
{
  std::map < int, bool >& connected = *pConnected;
  std::vector< const Hypothesis *>& connectedList = *pConnectedList;

  // start with the ones in the final stack
  const std::vector < HypothesisStack* > &hypoStackColl
  = m_search->GetHypothesisStacks();
  const HypothesisStack &finalStack = *hypoStackColl.back();
  HypothesisStack::const_iterator iterHypo;
  for (iterHypo = finalStack.begin() ; iterHypo != finalStack.end() ; ++iterHypo) {
    const Hypothesis *hypo = *iterHypo;
    connected[ hypo->GetId() ] = true;
    connectedList.push_back( hypo );
  }
  // move back from known connected hypotheses
  for(size_t i=0; i<connectedList.size(); i++) {
    const Hypothesis *hypo = connectedList[i];

    // add back pointer
    const Hypothesis *prevHypo = hypo->GetPrevHypo();
    if (prevHypo && prevHypo->GetId() > 0 // don't add empty hypothesis
        && connected.find( prevHypo->GetId() ) == connected.end()) { // don't add already added
      connected[ prevHypo->GetId() ] = true;
      connectedList.push_back( prevHypo );
    }

    // add arcs
    const ArcList *arcList = hypo->GetArcList();
    if (arcList != NULL) {
      ArcList::const_iterator iterArcList;
      for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList) {
        const Hypothesis *loserHypo = *iterArcList;
        if (connected.find( loserHypo->GetId() ) == connected.end()) { // don't add already added
          connected[ loserHypo->GetId() ] = true;
          connectedList.push_back( loserHypo );
        }
      }
    }
  }
}

void Manager::GetWinnerConnectedGraph(
  std::map< int, bool >* pConnected,
  std::vector< const Hypothesis* >* pConnectedList) const
{
  std::map < int, bool >& connected = *pConnected;
  std::vector< const Hypothesis *>& connectedList = *pConnectedList;

  // start with the ones in the final stack
  const std::vector < HypothesisStack* > &hypoStackColl = m_search->GetHypothesisStacks();
  const HypothesisStack &finalStack = *hypoStackColl.back();
  HypothesisStack::const_iterator iterHypo;
  for (iterHypo = finalStack.begin() ; iterHypo != finalStack.end() ; ++iterHypo) {
    const Hypothesis *hypo = *iterHypo;
    connected[ hypo->GetId() ] = true;
    connectedList.push_back( hypo );
  }

  // move back from known connected hypotheses
  for(size_t i=0; i<connectedList.size(); i++) {
    const Hypothesis *hypo = connectedList[i];

    // add back pointer
    const Hypothesis *prevHypo = hypo->GetPrevHypo();
    if (prevHypo->GetId() > 0 // don't add empty hypothesis
        && connected.find( prevHypo->GetId() ) == connected.end()) { // don't add already added
      connected[ prevHypo->GetId() ] = true;
      connectedList.push_back( prevHypo );
    }

    // add arcs
    const ArcList *arcList = hypo->GetArcList();
    if (arcList != NULL) {
      ArcList::const_iterator iterArcList;
      for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList) {
        const Hypothesis *loserHypo = *iterArcList;
        if (connected.find( loserHypo->GetPrevHypo()->GetId() ) == connected.end() && loserHypo->GetPrevHypo()->GetId() > 0) { // don't add already added & don't add hyp 0
          connected[ loserHypo->GetPrevHypo()->GetId() ] = true;
          connectedList.push_back( loserHypo->GetPrevHypo() );
        }
      }
    }
  }
}


#ifdef HAVE_PROTOBUF

void SerializeEdgeInfo(const Hypothesis* hypo, hgmert::Hypergraph_Edge* edge)
{
  hgmert::Rule* rule = edge->mutable_rule();
  hypo->GetCurrTargetPhrase().WriteToRulePB(rule);
  const Hypothesis* prev = hypo->GetPrevHypo();
  // if the feature values are empty, they default to 0
  if (!prev) return;
  // score breakdown is an aggregate (forward) quantity, but the exported
  // graph object just wants the feature values on the edges
  const ScoreComponentCollection& scores = hypo->GetScoreBreakdown();
  const ScoreComponentCollection& pscores = prev->GetScoreBreakdown();
  for (unsigned int i = 0; i < scores.size(); ++i)
    edge->add_feature_values((scores[i] - pscores[i]) * -1.0);
}

hgmert::Hypergraph_Node* GetHGNode(
  const Hypothesis* hypo,
  std::map< int, int>* i2hgnode,
  hgmert::Hypergraph* hg,
  int* hgNodeIdx)
{
  hgmert::Hypergraph_Node* hgnode;
  std::map < int, int >::iterator idxi = i2hgnode->find(hypo->GetId());
  if (idxi == i2hgnode->end()) {
    *hgNodeIdx = ((*i2hgnode)[hypo->GetId()] = hg->nodes_size());
    hgnode = hg->add_nodes();
  } else {
    *hgNodeIdx = idxi->second;
    hgnode = hg->mutable_nodes(*hgNodeIdx);
  }
  return hgnode;
}

void Manager::SerializeSearchGraphPB(
  long translationId,
  std::ostream& outputStream) const
{
  using namespace hgmert;
  std::map < int, bool > connected;
  std::map < int, int > i2hgnode;
  std::vector< const Hypothesis *> connectedList;
  GetConnectedGraph(&connected, &connectedList);
  connected[ 0 ] = true;
  Hypergraph hg;
  hg.set_is_sorted(false);
  int num_feats = (*m_search->GetHypothesisStacks().back()->begin())->GetScoreBreakdown().size();
  hg.set_num_features(num_feats);
  StaticData::Instance().GetScoreIndexManager().SerializeFeatureNamesToPB(&hg);
  Hypergraph_Node* goal = hg.add_nodes();  // idx=0 goal node must have idx 0
  Hypergraph_Node* source = hg.add_nodes();  // idx=1
  i2hgnode[-1] = 1; // source node
  const std::vector < HypothesisStack* > &hypoStackColl = m_search->GetHypothesisStacks();
  const HypothesisStack &finalStack = *hypoStackColl.back();
  for (std::vector < HypothesisStack* >::const_iterator iterStack = hypoStackColl.begin();
       iterStack != hypoStackColl.end() ; ++iterStack) {
    const HypothesisStack &stack = **iterStack;
    HypothesisStack::const_iterator iterHypo;

    for (iterHypo = stack.begin() ; iterHypo != stack.end() ; ++iterHypo) {
      const Hypothesis *hypo = *iterHypo;
      bool is_goal = hypo->GetWordsBitmap().IsComplete();
      if (connected.find( hypo->GetId() ) != connected.end()) {
        int headNodeIdx;
        Hypergraph_Node* headNode = GetHGNode(hypo, &i2hgnode, &hg, &headNodeIdx);
        if (is_goal) {
          Hypergraph_Edge* ge = hg.add_edges();
          ge->set_head_node(0);  // goal
          ge->add_tail_nodes(headNodeIdx);
          ge->mutable_rule()->add_trg_words("[X,1]");
        }
        Hypergraph_Edge* edge = hg.add_edges();
        SerializeEdgeInfo(hypo, edge);
        edge->set_head_node(headNodeIdx);
        const Hypothesis* prev = hypo->GetPrevHypo();
        int tailNodeIdx = 1; // source
        if (prev)
          tailNodeIdx = i2hgnode.find(prev->GetId())->second;
        edge->add_tail_nodes(tailNodeIdx);

        const ArcList *arcList = hypo->GetArcList();
        if (arcList != NULL) {
          ArcList::const_iterator iterArcList;
          for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList) {
            const Hypothesis *loserHypo = *iterArcList;
            UTIL_THROW_IF2(!connected[loserHypo->GetId()],
                           "Hypothesis " << loserHypo->GetId() << " is not connected");
            Hypergraph_Edge* edge = hg.add_edges();
            SerializeEdgeInfo(loserHypo, edge);
            edge->set_head_node(headNodeIdx);
            tailNodeIdx = i2hgnode.find(loserHypo->GetPrevHypo()->GetId())->second;
            edge->add_tail_nodes(tailNodeIdx);
          }
        } // end if arcList empty
      } // end if connected
    } // end for iterHypo
  } // end for iterStack
  hg.SerializeToOstream(&outputStream);
}
#endif

void
Manager::
OutputSearchGraph(long translationId, std::ostream &out) const
{
  vector<SearchGraphNode> searchGraph;
  GetSearchGraph(searchGraph);
  for (size_t i = 0; i < searchGraph.size(); ++i) {
    OutputSearchNode(*options(),translationId,out,searchGraph[i]);
  }
}

void
Manager::
GetForwardBackwardSearchGraph
( std::map< int, bool >* pConnected,
  std::vector<Hypothesis const* >* pConnectedList,
  std::map<Hypothesis const*, set<Hypothesis const*> >* pOutgoingHyps,
  vector< float>* pFwdBwdScores) const
{
  std::map < int, bool > &connected = *pConnected;
  std::vector< const Hypothesis *>& connectedList = *pConnectedList;
  std::map < int, int > forward;
  std::map < int, double > forwardScore;

  std::map < const Hypothesis*, set <const Hypothesis*> > & outgoingHyps
  = *pOutgoingHyps;
  vector< float> & estimatedScores = *pFwdBwdScores;

  // *** find connected hypotheses ***
  GetWinnerConnectedGraph(&connected, &connectedList);

  // ** compute best forward path for each hypothesis *** //

  // forward cost of hypotheses on final stack is 0
  const std::vector < HypothesisStack* > &hypoStackColl
  = m_search->GetHypothesisStacks();
  const HypothesisStack &finalStack = *hypoStackColl.back();
  HypothesisStack::const_iterator iterHypo;
  for (iterHypo = finalStack.begin() ; iterHypo != finalStack.end() ; ++iterHypo) {
    const Hypothesis *hypo = *iterHypo;
    forwardScore[ hypo->GetId() ] = 0.0f;
    forward[ hypo->GetId() ] = -1;
  }

  // compete for best forward score of previous hypothesis
  std::vector < HypothesisStack* >::const_iterator iterStack;
  for (iterStack = --hypoStackColl.end() ; iterStack != hypoStackColl.begin() ; --iterStack) {
    const HypothesisStack &stack = **iterStack;
    HypothesisStack::const_iterator iterHypo;
    for (iterHypo = stack.begin() ; iterHypo != stack.end() ; ++iterHypo) {
      const Hypothesis *hypo = *iterHypo;
      if (connected.find( hypo->GetId() ) != connected.end()) {
        // make a play for previous hypothesis
        const Hypothesis *prevHypo = hypo->GetPrevHypo();
        double fscore = forwardScore[ hypo->GetId() ] +
                        hypo->GetScore() - prevHypo->GetScore();
        if (forwardScore.find( prevHypo->GetId() ) == forwardScore.end()
            || forwardScore.find( prevHypo->GetId() )->second < fscore) {
          forwardScore[ prevHypo->GetId() ] = fscore;
          forward[ prevHypo->GetId() ] = hypo->GetId();
        }
        //store outgoing info
        outgoingHyps[prevHypo].insert(hypo);

        // all arcs also make a play
        const ArcList *arcList = hypo->GetArcList();
        if (arcList != NULL) {
          ArcList::const_iterator iterArcList;
          for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList) {
            const Hypothesis *loserHypo = *iterArcList;
            // make a play
            const Hypothesis *loserPrevHypo = loserHypo->GetPrevHypo();
            double fscore = forwardScore[ hypo->GetId() ] +
                            loserHypo->GetScore() - loserPrevHypo->GetScore();
            if (forwardScore.find( loserPrevHypo->GetId() ) == forwardScore.end()
                || forwardScore.find( loserPrevHypo->GetId() )->second < fscore) {
              forwardScore[ loserPrevHypo->GetId() ] = fscore;
              forward[ loserPrevHypo->GetId() ] = loserHypo->GetId();
            }
            //store outgoing info
            outgoingHyps[loserPrevHypo].insert(hypo);


          } // end for arc list
        } // end if arc list empty
      } // end if hypo connected
    } // end for hypo
  } // end for stack

  for (std::vector< const Hypothesis *>::iterator it = connectedList.begin(); it != connectedList.end(); ++it) {
    float estimatedScore = (*it)->GetScore() + forwardScore[(*it)->GetId()];
    estimatedScores.push_back(estimatedScore);
  }
}


const Hypothesis *Manager::GetBestHypothesis() const
{
  return m_search->GetBestHypothesis();
}

int Manager::GetNextHypoId()
{
  GetSentenceStats().AddCreated(); // count created hypotheses
  return m_hypoId++;
}

void Manager::ResetSentenceStats(const InputType& source)
{
  m_sentenceStats = std::auto_ptr<SentenceStats>(new SentenceStats(source));
}
SentenceStats& Manager::GetSentenceStats() const
{
  return *m_sentenceStats;

}

void Manager::OutputBest(OutputCollector *collector)  const
{
  long translationId = m_source.GetTranslationId();

  Timer additionalReportingTime;

  // apply decision rule and output best translation(s)
  if (collector) {
    ostringstream out;
    ostringstream debug;
    FixPrecision(debug,PRECISION);

    // all derivations - send them to debug stream
    if (options()->output.PrintAllDerivations) {
      additionalReportingTime.start();
      PrintAllDerivations(translationId, debug);
      additionalReportingTime.stop();
    }

    Timer decisionRuleTime;
    decisionRuleTime.start();

    // MAP decoding: best hypothesis
    const Hypothesis* bestHypo = NULL;
    if (!options()->mbr.enabled) {
      bestHypo = GetBestHypothesis();
      if (bestHypo) {
        if (options()->output.ReportHypoScore) {
          out << bestHypo->GetFutureScore() << ' ';
        }
        if (options()->output.RecoverPath) {
          bestHypo->OutputInput(out);
          out << "||| ";
        }

        if (options()->output.PrintID) {
          out << translationId << " ";
        }

        // VN : I put back the code for OutputPassthroughInformation
        if (options()->output.PrintPassThrough) {
          OutputPassthroughInformation(out, bestHypo);
        }
        // end of add back

        if (options()->output.ReportSegmentation == 2) {
          GetOutputLanguageModelOrder(out, bestHypo);
        }
        OutputSurface(out,*bestHypo, true);
        if (options()->output.PrintAlignmentInfo) {
          out << "||| ";
          bestHypo->OutputAlignment(out, true);
        }

        IFVERBOSE(1) {
          debug << "BEST TRANSLATION: " << *bestHypo << endl;
        }
      } else {
        VERBOSE(1, "NO BEST TRANSLATION" << endl);
      }

      out << endl;
    } // if (!staticData.UseMBR())

    // MBR decoding (n-best MBR, lattice MBR, consensus)
    else {
      // we first need the n-best translations
      size_t nBestSize = options()->mbr.size;
      if (nBestSize <= 0) {
        cerr << "ERROR: negative size for number of MBR candidate translations not allowed (option mbr-size)" << endl;
        exit(1);
      }
      TrellisPathList nBestList;
      CalcNBest(nBestSize, nBestList, true);
      VERBOSE(2,"size of n-best: " << nBestList.GetSize() << " (" << nBestSize << ")" << endl);
      IFVERBOSE(2) {
        PrintUserTime("calculated n-best list for (L)MBR decoding");
      }

      // lattice MBR
      if (options()->lmbr.enabled) {
        if (options()->nbest.enabled) {
          //lattice mbr nbest
          vector<LatticeMBRSolution> solutions;
          size_t n  = min(nBestSize, options()->nbest.nbest_size);
          getLatticeMBRNBest(*this,nBestList,solutions,n);
          OutputLatticeMBRNBest(m_latticeNBestOut, solutions, translationId);
        } else {
          //Lattice MBR decoding
          vector<Word> mbrBestHypo = doLatticeMBR(*this,nBestList);
          OutputBestHypo(mbrBestHypo, out);
          IFVERBOSE(2) {
            PrintUserTime("finished Lattice MBR decoding");
          }
        }
      }

      // consensus decoding
      else if (options()->search.consensus) {
        const TrellisPath &conBestHypo = doConsensusDecoding(*this,nBestList);
        OutputBestHypo(conBestHypo, out);
        OutputAlignment(m_alignmentOut, conBestHypo);
        IFVERBOSE(2) {
          PrintUserTime("finished Consensus decoding");
        }
      }

      // n-best MBR decoding
      else {
        const TrellisPath &mbrBestHypo = doMBR(nBestList, *options());
        OutputBestHypo(mbrBestHypo, out);
        OutputAlignment(m_alignmentOut, mbrBestHypo);
        IFVERBOSE(2) {
          PrintUserTime("finished MBR decoding");
        }
      }
    }

    // report best translation to output collector
    collector->Write(translationId,out.str(),debug.str());

    decisionRuleTime.stop();
    VERBOSE(1, "Line " << translationId << ": Decision rule took " << decisionRuleTime << " seconds total" << endl);
  } // if (m_ioWrapper.GetSingleBestOutputCollector())

}

void Manager::OutputNBest(OutputCollector *collector) const
{
  if (collector == NULL) {
    return;
  }

  if (options()->lmbr.enabled) {
    if (options()->nbest.enabled) {
      collector->Write(m_source.GetTranslationId(), m_latticeNBestOut.str());
    }
  } else {
    TrellisPathList nBestList;
    ostringstream out;
    NBestOptions const& nbo = options()->nbest;
    CalcNBest(nbo.nbest_size, nBestList, nbo.only_distinct);
    OutputNBest(out, nBestList);
    collector->Write(m_source.GetTranslationId(), out.str());
  }

}

void
Manager::
OutputNBest(std::ostream& out, Moses::TrellisPathList const& nBestList) const
{
  NBestOptions const& nbo = options()->nbest;
  bool reportAllFactors     = nbo.include_all_factors;
  bool includeSegmentation  = nbo.include_segmentation;
  bool includeWordAlignment = nbo.include_alignment_info;

  TrellisPathList::const_iterator iter;
  for (iter = nBestList.begin() ; iter != nBestList.end() ; ++iter) {
    const TrellisPath &path = **iter;
    const std::vector<const Hypothesis *> &edges = path.GetEdges();

    // print the surface factor of the translation
    out << m_source.GetTranslationId() << " ||| ";
    for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
      const Hypothesis &edge = *edges[currEdge];
      OutputSurface(out, edge);
    }
    out << " |||";

    // print scores with feature names
    bool with_labels = options()->nbest.include_feature_labels;
    path.GetScoreBreakdown()->OutputAllFeatureScores(out, with_labels);

    // total
    out << " ||| " << path.GetFutureScore();

    //phrase-to-phrase segmentation
    if (includeSegmentation) {
      out << " |||";
      for (int currEdge = (int)edges.size() - 2 ; currEdge >= 0 ; currEdge--) {
        const Hypothesis &edge = *edges[currEdge];
        const Range &sourceRange = edge.GetCurrSourceWordsRange();
        Range targetRange = path.GetTargetWordsRange(edge);
        out << " " << sourceRange.GetStartPos();
        if (sourceRange.GetStartPos() < sourceRange.GetEndPos()) {
          out << "-" << sourceRange.GetEndPos();
        }
        out<< "=" << targetRange.GetStartPos();
        if (targetRange.GetStartPos() < targetRange.GetEndPos()) {
          out<< "-" << targetRange.GetEndPos();
        }
      }
    }

    if (includeWordAlignment) {
      out << " ||| ";
      for (int currEdge = (int)edges.size() - 2 ; currEdge >= 0 ; currEdge--) {
        const Hypothesis &edge = *edges[currEdge];
        const Range &sourceRange = edge.GetCurrSourceWordsRange();
        Range targetRange = path.GetTargetWordsRange(edge);
        const int sourceOffset = sourceRange.GetStartPos();
        const int targetOffset = targetRange.GetStartPos();
        const AlignmentInfo &ai = edge.GetCurrTargetPhrase().GetAlignTerm();

        OutputAlignment(out, ai, sourceOffset, targetOffset);

      }
    }

    if (options()->output.RecoverPath) {
      out << " ||| ";
      OutputInput(out, edges[0]);
    }

    out << endl;
  }

  out << std::flush;
}

//////////////////////////////////////////////////////////////////////////
/***
 * print surface factor only for the given phrase
 */
void
Manager::
OutputSurface(std::ostream &out, Hypothesis const& edge, bool const recursive) const
{
  if (recursive && edge.GetPrevHypo()) {
    OutputSurface(out,*edge.GetPrevHypo(), true);
  }

  std::vector<FactorType> outputFactorOrder = options()->output.factor_order;
  UTIL_THROW_IF2(outputFactorOrder.size() == 0,
                 "Must specific at least 1 output factor");

  FactorType placeholderFactor = options()->input.placeholder_factor;
  std::map<size_t, const Factor*> placeholders;
  if (placeholderFactor != NOT_FOUND) {
    // creates map of target position -> factor for placeholders
    placeholders = GetPlaceholders(edge, placeholderFactor);
  }

  bool markUnknown = options()->unk.mark;
  std::string const& fd = options()->output.factor_delimiter;

  TargetPhrase const& phrase = edge.GetCurrTargetPhrase();
  size_t size = phrase.GetSize();
  for (size_t pos = 0 ; pos < size ; pos++) {
    const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[0]);
    if (placeholders.size()) {
      // do placeholders
      std::map<size_t, const Factor*>::const_iterator iter = placeholders.find(pos);
      if (iter != placeholders.end()) {
        factor = iter->second;
      }
    }

    UTIL_THROW_IF2(factor == NULL, "No factor 0 at position " << pos);

    //preface surface form with UNK if marking unknowns
    const Word &word = phrase.GetWord(pos);
    if(markUnknown && word.IsOOV()) {
      out << options()->unk.prefix;
    }

    out << *factor;
    for (size_t i = 1 ; i < outputFactorOrder.size() ; i++) {
      const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[i]);
      if (factor) out << fd << *factor;
      //else        out << fd << UNKNOWN_FACTOR;
    }

    if(markUnknown && word.IsOOV()) {
      out << options()->unk.suffix;
    }

    out << " ";

  }

  // trace ("report segmentation") option "-t" / "-tt"
  int reportSegmentation = options()->output.ReportSegmentation;
  if (reportSegmentation > 0 && phrase.GetSize() > 0) {
    const Range &sourceRange = edge.GetCurrSourceWordsRange();
    const int sourceStart = sourceRange.GetStartPos();
    const int sourceEnd = sourceRange.GetEndPos();
    out << "|" << sourceStart << "-" << sourceEnd;    // enriched "-tt"
    if (reportSegmentation == 2) {
      out << ",wa=";
      const AlignmentInfo &ai = edge.GetCurrTargetPhrase().GetAlignTerm();
      OutputAlignment(out, ai, 0, 0);
      out << ",total=";
      out << edge.GetScore() - edge.GetPrevHypo()->GetScore();
      out << ",";
      ScoreComponentCollection scoreBreakdown(edge.GetScoreBreakdown());
      scoreBreakdown.MinusEquals(edge.GetPrevHypo()->GetScoreBreakdown());
      bool with_labels = options()->nbest.include_feature_labels;
      scoreBreakdown.OutputAllFeatureScores(out, with_labels);
    }
    out << "| ";
  }
}

void
Manager::
OutputAlignment(ostream &out, const AlignmentInfo &ai,
                size_t sourceOffset, size_t targetOffset) const
{
  typedef std::vector< const std::pair<size_t,size_t>* > AlignVec;
  AlignVec alignments = ai.GetSortedAlignments(options()->output.WA_SortOrder);

  AlignVec::const_iterator it;
  for (it = alignments.begin(); it != alignments.end(); ++it) {
    const std::pair<size_t,size_t> &alignment = **it;
    out << alignment.first  + sourceOffset << "-"
        << alignment.second + targetOffset << " ";
  }

}

void
Manager::
OutputInput(std::ostream& os, const Hypothesis* hypo) const
{
  size_t len = hypo->GetInput().GetSize();
  std::vector<const Phrase*> inp_phrases(len, 0);
  OutputInput(inp_phrases, hypo);
  for (size_t i=0; i<len; ++i)
    if (inp_phrases[i]) os << *inp_phrases[i];
}

void Manager::OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo) const
{
  if (hypo->GetPrevHypo()) {
    OutputInput(map, hypo->GetPrevHypo());
    map[hypo->GetCurrSourceWordsRange().GetStartPos()] = &hypo->GetTranslationOption().GetInputPath().GetPhrase();
  }
}

std::map<size_t, const Factor*> Manager::GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor) const
{
  const InputPath &inputPath = hypo.GetTranslationOption().GetInputPath();
  const Phrase &inputPhrase = inputPath.GetPhrase();

  std::map<size_t, const Factor*> ret;

  for (size_t sourcePos = 0; sourcePos < inputPhrase.GetSize(); ++sourcePos) {
    const Factor *factor = inputPhrase.GetFactor(sourcePos, placeholderFactor);
    if (factor) {
      TargetPhrase const& tp = hypo.GetTranslationOption().GetTargetPhrase();
      std::set<size_t> targetPos = tp.GetAlignTerm().GetAlignmentsForSource(sourcePos);
      UTIL_THROW_IF2(targetPos.size() != 1,
                     "Placeholder should be aligned to 1, and only 1, word");
      ret[*targetPos.begin()] = factor;
    }
  }

  return ret;
}

void Manager::OutputLatticeSamples(OutputCollector *collector) const
{
  if (collector) {
    TrellisPathList latticeSamples;
    ostringstream out;
    CalcLatticeSamples(options()->output.lattice_sample_size, latticeSamples);
    OutputNBest(out,latticeSamples);
    collector->Write(m_source.GetTranslationId(), out.str());
  }

}

void Manager::OutputAlignment(OutputCollector *collector) const
{
  if (collector == NULL) {
    return;
  }

  if (!m_alignmentOut.str().empty()) {
    collector->Write(m_source.GetTranslationId(), m_alignmentOut.str());
  } else {
    std::vector<const Hypothesis *> edges;
    const Hypothesis *currentHypo = GetBestHypothesis();
    while (currentHypo) {
      edges.push_back(currentHypo);
      currentHypo = currentHypo->GetPrevHypo();
    }
    ostringstream out;
    size_t targetOffset = 0;
    BOOST_REVERSE_FOREACH(Hypothesis const* e, edges) {
      const TargetPhrase &tp = e->GetCurrTargetPhrase();
      size_t sourceOffset = e->GetCurrSourceWordsRange().GetStartPos();
      OutputAlignment(out, tp.GetAlignTerm(), sourceOffset, targetOffset);
      targetOffset += tp.GetSize();
    }
    out << std::endl; // Used by --alignment-output-file so requires endl
    collector->Write(m_source.GetTranslationId(), out.str());

  }
}

void
Manager::
OutputDetailedTranslationReport(OutputCollector *collector) const
{
  if (collector) {
    ostringstream out;
    FixPrecision(out,PRECISION);
    TranslationAnalysis::PrintTranslationAnalysis(out, GetBestHypothesis());
    collector->Write(m_source.GetTranslationId(),out.str());
  }

}

void
Manager::
OutputUnknowns(OutputCollector *collector) const
{
  if (collector) {
    long translationId = m_source.GetTranslationId();
    const vector<const Phrase*>& unknowns = m_transOptColl->GetUnknownSources();
    ostringstream out;
    for (size_t i = 0; i < unknowns.size(); ++i) {
      out << *(unknowns[i]);
    }
    out << endl;
    collector->Write(translationId, out.str());
  }

}

void
Manager::
OutputWordGraph(OutputCollector *collector) const
{
  if (collector) {
    long translationId = m_source.GetTranslationId();
    ostringstream out;
    FixPrecision(out,PRECISION);
    GetWordGraph(translationId, out);
    collector->Write(translationId, out.str());
  }
}

void
Manager::
OutputSearchGraph(OutputCollector *collector) const
{
  if (collector) {
    long translationId = m_source.GetTranslationId();
    ostringstream out;
    FixPrecision(out,PRECISION);
    OutputSearchGraph(translationId, out);
    collector->Write(translationId, out.str());

#ifdef HAVE_PROTOBUF
    const StaticData &staticData = StaticData::Instance();
    if (staticData.GetOutputSearchGraphPB()) {
      ostringstream sfn;
      sfn << staticData.GetParam("output-search-graph-pb")[0] << '/' << translationId << ".pb" << ends;
      string fn = sfn.str();
      VERBOSE(2, "Writing search graph to " << fn << endl);
      fstream output(fn.c_str(), ios::trunc | ios::binary | ios::out);
      SerializeSearchGraphPB(translationId, output);
    }
#endif
  }

}

void Manager::OutputSearchGraphSLF() const
{
  // const StaticData &staticData = StaticData::Instance();
  long translationId = m_source.GetTranslationId();

  // Output search graph in HTK standard lattice format (SLF)
  std::string const& slf = options()->output.SearchGraphSLF;
  if (slf.size()) {
    util::StringStream fileName;
    fileName << slf << "/" << translationId << ".slf";
    ofstream *file = new ofstream;
    file->open(fileName.str().c_str());
    if (file->is_open() && file->good()) {
      ostringstream out;
      FixPrecision(out,PRECISION);
      OutputSearchGraphAsSLF(translationId, out);
      *file << out.str();
      file -> flush();
    } else {
      TRACE_ERR("Cannot output HTK standard lattice for line " << translationId << " because the output file is not open or not ready for writing" << endl);
    }
    delete file;
  }

}

void Manager::OutputLatticeMBRNBest(std::ostream& out, const vector<LatticeMBRSolution>& solutions,long translationId) const
{
  for (vector<LatticeMBRSolution>::const_iterator si = solutions.begin(); si != solutions.end(); ++si) {
    out << translationId;
    out << " |||";
    const vector<Word> mbrHypo = si->GetWords();
    for (size_t i = 0 ; i < mbrHypo.size() ; i++) {
      const Factor *factor = mbrHypo[i].GetFactor(options()->output.factor_order[0]);
      if (i>0) out << " " << *factor;
      else     out << *factor;
    }
    out << " |||";
    out << " map: " << si->GetMapScore();
    out << " w: " << mbrHypo.size();
    const vector<float>& ngramScores = si->GetNgramScores();
    for (size_t i = 0; i < ngramScores.size(); ++i) {
      out << " " << ngramScores[i];
    }
    out << " ||| " << si->GetScore();

    out << endl;
  }
}

void
Manager::
OutputBestHypo(const std::vector<Word>&  mbrBestHypo, ostream& out) const
{
  FactorType f = options()->output.factor_order[0];
  for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) {
    const Factor *factor = mbrBestHypo[i].GetFactor(f);
    UTIL_THROW_IF2(factor == NULL, "No factor " << f << " at position " << i);
    if (i) out << " ";
    out << *factor;
  }
  out << endl;
}

void
Manager::
OutputBestHypo(const Moses::TrellisPath &path, std::ostream &out) const
{
  std::vector<const Hypothesis *> const& edges = path.GetEdges();
  for (int currEdge = (int)edges.size() - 1 ; currEdge >= 0 ; currEdge--) {
    Hypothesis const& edge = *edges[currEdge];
    OutputSurface(out, edge);
  }
  out << endl;
}

void
Manager::
OutputAlignment(std::ostringstream &out, const TrellisPath &path) const
{
  WordAlignmentSort waso = options()->output.WA_SortOrder;
  BOOST_REVERSE_FOREACH(Hypothesis const* e, path.GetEdges())
  e->OutputAlignment(out, false);
  // Hypothesis::OutputAlignment(out, path.GetEdges(), waso);
  // Used by --alignment-output-file so requires endl
  out << std::endl;
}

} // namespace