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

Stack.cpp « SCFG « moses2 « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03347dea09069fb5cc8b9f33a72080a494dcddb1 (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
#include <boost/foreach.hpp>
#include "Stacks.h"
#include "Hypothesis.h"
#include "TargetPhraseImpl.h"
#include "Manager.h"

using namespace std;

namespace Moses2
{

namespace SCFG
{

Stack::Stack(const Manager &mgr)
:m_mgr(mgr)
{
}

Stack::~Stack()
{
  BOOST_FOREACH (const Coll::value_type &valPair, m_coll) {
    Moses2::HypothesisColl *hypos = valPair.second;
    delete hypos;
  }
}

void Stack::Add(SCFG::Hypothesis *hypo, Recycler<HypothesisBase*> &hypoRecycle,
    ArcLists &arcLists)
{
  const SCFG::TargetPhraseImpl &tp = hypo->GetTargetPhrase();
  const SCFG::Word &lhs = tp.lhs;
  //cerr << "lhs=" << lhs << endl;

  HypothesisColl &coll = GetColl(lhs);
  coll.Add(m_mgr.system, hypo, hypoRecycle, arcLists);
}

size_t Stack::GetSize() const
{
  size_t ret = 0;
  BOOST_FOREACH (const Coll::value_type &valPair, m_coll) {
    Moses2::HypothesisColl &hypos = *valPair.second;
    ret += hypos.GetSize();
  }
  return ret;
}

const Moses2::HypothesisColl *Stack::GetColl(const SCFG::Word &nt) const
{
  assert(nt.isNonTerminal);
  Coll::const_iterator iter = m_coll.find(nt);
  if (iter != m_coll.end()) {
    return NULL;
  }
  else {
    return iter->second;
  }
}

Moses2::HypothesisColl &Stack::GetColl(const SCFG::Word &nt)
{
  Moses2::HypothesisColl *ret;
  Coll::iterator iter;
  iter = m_coll.find(nt);
  if (iter == m_coll.end()) {
    ret = new Moses2::HypothesisColl(m_mgr);
    m_coll[nt] = ret;
  }
  else {
    ret = iter->second;
  }
  return *ret;
}

const Hypothesis *Stack::GetBestHypo() const
{
  SCORE bestScore = -std::numeric_limits<SCORE>::infinity();
  const HypothesisBase *bestHypo;
  BOOST_FOREACH(const Coll::value_type &val, m_coll){
    const Moses2::HypothesisColl &hypos = *val.second;
    const Moses2::HypothesisBase *hypo = hypos.GetBestHypo();

    if (hypo->GetFutureScore() > bestScore) {
      bestScore = hypo->GetFutureScore();
      bestHypo = hypo;
    }
  }
  return &bestHypo->Cast<SCFG::Hypothesis>();
}

std::string Stack::Debug(const System &system) const
{
  stringstream out;
  BOOST_FOREACH (const SCFG::Stack::Coll::value_type &valPair, m_coll) {
    const SCFG::Word &lhs = valPair.first;
    const Moses2::HypothesisColl &hypos = *valPair.second;
    out << "lhs=" << lhs.Debug(system);
    out << "=" << hypos.GetSize() << endl;
    out << hypos.Debug(system);
    out << endl;
  }

  return out.str();
}

}
}