File size: 5,616 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 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 |
/*
* Misc.cpp
*
* Created on: 2 Jun 2016
* Author: hieu
*/
#include <boost/foreach.hpp>
#include <boost/functional/hash.hpp>
#include "Misc.h"
#include "Manager.h"
#include "TargetPhrases.h"
using namespace std;
namespace Moses2
{
namespace SCFG
{
////////////////////////////////////////////////////////
SeenPosition::SeenPosition(MemPool &pool,
const SymbolBind &vSymbolBind,
const SCFG::TargetPhrases &vtps,
size_t numNT)
:symbolBind(vSymbolBind)
,tps(vtps)
,tpInd(0)
,hypoIndColl(pool, numNT, 0)
{
}
SeenPosition::SeenPosition(MemPool &pool,
const SymbolBind &vSymbolBind,
const SCFG::TargetPhrases &vtps,
size_t vtpInd,
const Vector<size_t> &vhypoIndColl)
:symbolBind(vSymbolBind)
,tps(vtps)
,tpInd(vtpInd)
,hypoIndColl(pool, vhypoIndColl.size())
{
for (size_t i = 0; i < hypoIndColl.size(); ++i) {
hypoIndColl[i] = vhypoIndColl[i];
}
}
std::string SeenPosition::Debug(const System &system) const
{
stringstream out;
out << &tps << " " << tpInd << " ";
for (size_t i = 0; i < hypoIndColl.size(); ++i) {
out << hypoIndColl[i] << " ";
}
return out.str();
}
bool SeenPosition::operator==(const SeenPosition &compare) const
{
if (&symbolBind != &compare.symbolBind) {
return false;
}
if (&tps != &compare.tps) {
return false;
}
if (tpInd != compare.tpInd) {
return false;
}
if (hypoIndColl != compare.hypoIndColl) {
return false;
}
return true;
}
size_t SeenPosition::hash() const
{
size_t ret = (size_t) &symbolBind;
boost::hash_combine(ret, &tps);
boost::hash_combine(ret, tpInd);
boost::hash_combine(ret, hypoIndColl);
return ret;
}
////////////////////////////////////////////////////////
bool SeenPositions::Add(const SeenPosition *item)
{
std::pair<Coll::iterator, bool> ret = m_coll.insert(item);
return ret.second;
}
////////////////////////////////////////////////////////
QueueItem *QueueItem::Create(MemPool &pool, SCFG::Manager &mgr)
{
//QueueItem *item = new (pool.Allocate<QueueItem>()) QueueItem(pool);
//return item;
QueueItemRecycler &queueItemRecycler = mgr.GetQueueItemRecycler();
QueueItem *ret;
if (!queueItemRecycler.empty()) {
// use item from recycle bin
ret = queueItemRecycler.back();
queueItemRecycler.pop_back();
} else {
// create new item
ret = new (pool.Allocate<QueueItem>()) QueueItem(pool);
}
return ret;
}
QueueItem::QueueItem(MemPool &pool)
:m_hypoIndColl(NULL)
{
}
void QueueItem::Init(
MemPool &pool,
const SymbolBind &vSymbolBind,
const SCFG::TargetPhrases &vTPS,
const Vector<size_t> &hypoIndColl)
{
symbolBind = &vSymbolBind;
tps = &vTPS;
tpInd = 0;
m_hyposColl = new (pool.Allocate<HyposColl>()) HyposColl(pool);
m_hypoIndColl = &hypoIndColl;
}
void QueueItem::Init(
MemPool &pool,
const SymbolBind &vSymbolBind,
const SCFG::TargetPhrases &vTPS,
size_t vTPInd,
const Vector<size_t> &hypoIndColl)
{
symbolBind = &vSymbolBind;
tps = &vTPS;
tpInd = vTPInd;
m_hyposColl = NULL;
m_hypoIndColl = &hypoIndColl;
}
void QueueItem::AddHypos(const Moses2::Hypotheses &hypos)
{
m_hyposColl->push_back(&hypos);
}
void QueueItem::CreateHypo(
MemPool &systemPool,
SCFG::Manager &mgr,
const SCFG::InputPath &path,
const SCFG::SymbolBind &symbolBind)
{
const SCFG::TargetPhraseImpl &tp = (*tps)[tpInd];
hypo = SCFG::Hypothesis::Create(systemPool, mgr);
hypo->Init(mgr, path, symbolBind, tp, *m_hypoIndColl);
hypo->EvaluateWhenApplied();
}
void QueueItem::CreateNext(
MemPool &systemPool,
MemPool &mgrPool,
SCFG::Manager &mgr,
SCFG::Queue &queue,
SeenPositions &seenPositions,
const SCFG::InputPath &path)
{
//cerr << "tpInd=" << tpInd << " " << tps->GetSize() << endl;
if (tpInd + 1 < tps->GetSize()) {
const SCFG::TargetPhraseImpl &tp = (*tps)[tpInd + 1];
SeenPosition *seenItem = new (mgrPool.Allocate<SeenPosition>()) SeenPosition(mgrPool, *symbolBind, *tps, tpInd + 1, *m_hypoIndColl);
bool unseen = seenPositions.Add(seenItem);
if (unseen) {
QueueItem *item = QueueItem::Create(mgrPool, mgr);
item->Init(mgrPool, *symbolBind, *tps, tpInd + 1, *m_hypoIndColl);
item->m_hyposColl = m_hyposColl;
item->CreateHypo(systemPool, mgr, path, *symbolBind);
queue.push(item);
}
}
assert(m_hyposColl->size() == m_hypoIndColl->size());
const SCFG::TargetPhraseImpl &tp = (*tps)[tpInd];
for (size_t i = 0; i < m_hyposColl->size(); ++i) {
const Moses2::Hypotheses &hypos = *(*m_hyposColl)[i];
size_t hypoInd = (*m_hypoIndColl)[i] + 1; // increment hypo
if (hypoInd < hypos.size()) {
SeenPosition *seenItem = new (mgrPool.Allocate<SeenPosition>()) SeenPosition(mgrPool, *symbolBind, *tps, tpInd, *m_hypoIndColl);
seenItem->hypoIndColl[i] = hypoInd;
bool unseen = seenPositions.Add(seenItem);
if (unseen) {
QueueItem *item = QueueItem::Create(mgrPool, mgr);
item->Init(mgrPool, *symbolBind, *tps, tpInd, seenItem->hypoIndColl);
item->m_hyposColl = m_hyposColl;
item->CreateHypo(systemPool, mgr, path, *symbolBind);
queue.push(item);
}
}
}
}
std::string QueueItem::Debug(const System &system) const
{
stringstream out;
out << hypo << " " << &(*tps)[tpInd] << "(" << tps << " " << tpInd << ") ";
for (size_t i = 0; i < m_hypoIndColl->size(); ++i) {
out << (*m_hypoIndColl)[i] << " ";
}
return out.str();
}
}
}
|