File size: 4,597 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * SearchNormal.cpp
 *
 *  Created on: 25 Oct 2015
 *      Author: hieu
 */

#include "Search.h"
#include <algorithm>
#include <boost/foreach.hpp>
#include "Stack.h"
#include "../Manager.h"
#include "../TrellisPath.h"
#include "../Sentence.h"
#include "../../TrellisPaths.h"
#include "../../InputPathsBase.h"
#include "../../Phrase.h"
#include "../../System.h"
#include "../../PhraseBased/TargetPhrases.h"

using namespace std;

namespace Moses2
{
namespace NSNormal
{

Search::Search(Manager &mgr)
  :Moses2::Search(mgr)
  , m_stacks(mgr)
{
  // TODO Auto-generated constructor stub

}

Search::~Search()
{
  // TODO Auto-generated destructor stub
}

void Search::Decode()
{
  // init stacks
  const Sentence &sentence = static_cast<const Sentence&>(mgr.GetInput());
  m_stacks.Init(mgr, sentence.GetSize() + 1);

  const Bitmap &initBitmap = mgr.GetBitmaps().GetInitialBitmap();
  Hypothesis *initHypo = Hypothesis::Create(mgr.GetSystemPool(), mgr);
  initHypo->Init(mgr, mgr.GetInputPaths().GetBlank(), mgr.GetInitPhrase(),
                 initBitmap);
  initHypo->EmptyHypothesisState(mgr.GetInput());

  m_stacks.Add(initHypo, mgr.GetHypoRecycle(), mgr.arcLists);

  for (size_t stackInd = 0; stackInd < m_stacks.GetSize(); ++stackInd) {
    Decode(stackInd);
    //cerr << m_stacks << endl;

    // delete stack to save mem
    if (stackInd < m_stacks.GetSize() - 1) {
      m_stacks.Delete(stackInd);
    }
    //cerr << m_stacks.Debug(mgr.system) << endl;
  }
}

void Search::Decode(size_t stackInd)
{
  //cerr << "stackInd=" << stackInd << endl;
  Stack &stack = m_stacks[stackInd];
  if (&stack == &m_stacks.Back()) {
    // last stack. don't do anythin
    return;
  }

  const Hypotheses &hypos = stack.GetSortedAndPrunedHypos(mgr, mgr.arcLists);
  //cerr << "hypos=" << hypos.size() << endl;

  const InputPaths &paths = mgr.GetInputPaths();

  BOOST_FOREACH(const InputPathBase *path, paths) {
    BOOST_FOREACH(const HypothesisBase *hypo, hypos) {
      Extend(*static_cast<const Hypothesis*>(hypo), *static_cast<const InputPath*>(path));
    }
  }
}

void Search::Extend(const Hypothesis &hypo, const InputPath &path)
{
  const Bitmap &hypoBitmap = hypo.GetBitmap();
  const Range &hypoRange = hypo.GetInputPath().range;
  const Range &pathRange = path.range;

  if (!CanExtend(hypoBitmap, hypoRange.GetEndPos(), pathRange)) {
    return;
  }

  const ReorderingConstraint &reorderingConstraint = mgr.GetInput().GetReorderingConstraint();
  if (!reorderingConstraint.Check(hypoBitmap, pathRange.GetStartPos(), pathRange.GetEndPos())) {
    return;
  }

  // extend this hypo
  const Bitmap &newBitmap = mgr.GetBitmaps().GetBitmap(hypoBitmap, pathRange);
  //SCORE estimatedScore = mgr.GetEstimatedScores().CalcFutureScore2(bitmap, pathRange.GetStartPos(), pathRange.GetEndPos());
  SCORE estimatedScore = mgr.GetEstimatedScores().CalcEstimatedScore(newBitmap);

  size_t numPt = mgr.system.mappings.size();
  const TargetPhrases **tpsAllPt = path.targetPhrases;
  for (size_t i = 0; i < numPt; ++i) {
    const TargetPhrases *tps = tpsAllPt[i];
    if (tps) {
      Extend(hypo, *tps, path, newBitmap, estimatedScore);
    }
  }
}

void Search::Extend(const Hypothesis &hypo, const TargetPhrases &tps,
                    const InputPath &path, const Bitmap &newBitmap, SCORE estimatedScore)
{
  BOOST_FOREACH(const TargetPhraseImpl *tp, tps) {
    Extend(hypo, *tp, path, newBitmap, estimatedScore);
  }
}

void Search::Extend(const Hypothesis &hypo, const TargetPhraseImpl &tp,
                    const InputPath &path, const Bitmap &newBitmap, SCORE estimatedScore)
{
  Hypothesis *newHypo = Hypothesis::Create(mgr.GetSystemPool(), mgr);
  newHypo->Init(mgr, hypo, path, tp, newBitmap, estimatedScore);
  newHypo->EvaluateWhenApplied();

  m_stacks.Add(newHypo, mgr.GetHypoRecycle(), mgr.arcLists);

  //m_arcLists.AddArc(stackAdded.added, newHypo, stackAdded.other);
  //stack.Prune(mgr.GetHypoRecycle(), mgr.system.stackSize, mgr.system.stackSize * 2);

}

const Hypothesis *Search::GetBestHypo() const
{
  const Stack &lastStack = m_stacks.Back();
  const Hypothesis *best = lastStack.GetBestHypo<Hypothesis>();
  return best;
}

void Search::AddInitialTrellisPaths(TrellisPaths<TrellisPath> &paths) const
{
  const Stack &lastStack = m_stacks.Back();
  const Hypotheses &hypos = lastStack.GetSortedAndPrunedHypos(mgr, mgr.arcLists);

  BOOST_FOREACH(const HypothesisBase *hypoBase, hypos) {
    const Hypothesis *hypo = static_cast<const Hypothesis*>(hypoBase);
    TrellisPath *path = new TrellisPath(hypo, mgr.arcLists);
    paths.Add(path);
  }
}

} // namespace
}