File size: 6,732 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 |
/*
* LexicalReordering.cpp
*
* Created on: 15 Dec 2015
* Author: hieu
*/
#include <boost/foreach.hpp>
#include "util/exception.hh"
#include "LexicalReordering.h"
#include "LRModel.h"
#include "PhraseBasedReorderingState.h"
#include "BidirectionalReorderingState.h"
#include "../../TranslationModel/PhraseTable.h"
#include "../../System.h"
#include "../../PhraseBased/PhraseImpl.h"
#include "../../PhraseBased/Manager.h"
#include "../../PhraseBased/Hypothesis.h"
#include "../../PhraseBased/TargetPhrases.h"
#include "../../PhraseBased/TargetPhraseImpl.h"
#include "../../legacy/InputFileStream.h"
#include "../../legacy/Util2.h"
#ifdef HAVE_CMPH
#include "../../TranslationModel/CompactPT/LexicalReorderingTableCompact.h"
#endif
using namespace std;
namespace Moses2
{
///////////////////////////////////////////////////////////////////////
LexicalReordering::LexicalReordering(size_t startInd, const std::string &line)
: StatefulFeatureFunction(startInd, line)
, m_blank(NULL)
, m_propertyInd(-1)
, m_coll(NULL)
, m_configuration(NULL)
#ifdef HAVE_CMPH
, m_compactModel(NULL)
#endif
{
ReadParameters();
assert(m_configuration);
//assert(m_numScores == 6);
}
LexicalReordering::~LexicalReordering()
{
delete m_coll;
delete m_configuration;
#ifdef HAVE_CMPH
delete m_compactModel;
#endif
}
void LexicalReordering::Load(System &system)
{
MemPool &pool = system.GetSystemPool();
if (m_propertyInd >= 0) {
// Using integrate Lex RO. No loading needed
#ifdef HAVE_CMPH
} else if (FileExists(m_path + ".minlexr")) {
m_compactModel = new LexicalReorderingTableCompact(m_path + ".minlexr",
m_FactorsF, m_FactorsE, m_FactorsC);
m_blank = new (pool.Allocate<PhraseImpl>()) PhraseImpl(pool, 0);
#endif
} else {
m_coll = new Coll();
InputFileStream file(m_path);
string line;
size_t lineNum = 0;
while (getline(file, line)) {
if (++lineNum % 1000000 == 0) {
cerr << lineNum << " ";
}
std::vector<std::string> toks = TokenizeMultiCharSeparator(line, "|||");
assert(toks.size() == 3);
PhraseImpl *source = PhraseImpl::CreateFromString(pool, system.GetVocab(),
system, toks[0]);
PhraseImpl *target = PhraseImpl::CreateFromString(pool, system.GetVocab(),
system, toks[1]);
std::vector<SCORE> scores = Tokenize<SCORE>(toks[2]);
std::transform(scores.begin(), scores.end(), scores.begin(),
TransformScore);
std::transform(scores.begin(), scores.end(), scores.begin(), FloorScore);
Key key(source, target);
(*m_coll)[key] = scores;
}
}
}
void LexicalReordering::SetParameter(const std::string& key,
const std::string& value)
{
if (key == "path") {
m_path = value;
} else if (key == "type") {
m_configuration = new LRModel(value, *this);
} else if (key == "input-factor") {
m_FactorsF = Tokenize<FactorType>(value);
} else if (key == "output-factor") {
m_FactorsE = Tokenize<FactorType>(value);
} else if (key == "property-index") {
m_propertyInd = Scan<int>(value);
} else {
StatefulFeatureFunction::SetParameter(key, value);
}
}
FFState* LexicalReordering::BlankState(MemPool &pool, const System &sys) const
{
FFState *ret = m_configuration->CreateLRState(pool);
return ret;
}
void LexicalReordering::EmptyHypothesisState(FFState &state,
const ManagerBase &mgr, const InputType &input,
const Hypothesis &hypo) const
{
BidirectionalReorderingState &stateCast =
static_cast<BidirectionalReorderingState&>(state);
stateCast.Init(NULL, hypo.GetTargetPhrase(), hypo.GetInputPath(), true,
&hypo.GetBitmap());
}
void LexicalReordering::EvaluateInIsolation(MemPool &pool, const System &system,
const Phrase<Moses2::Word> &source, const TargetPhraseImpl &targetPhrase, Scores &scores,
SCORE &estimatedScore) const
{
}
void LexicalReordering::EvaluateInIsolation(MemPool &pool, const System &system, const Phrase<SCFG::Word> &source,
const TargetPhrase<SCFG::Word> &targetPhrase, Scores &scores,
SCORE &estimatedScore) const
{
UTIL_THROW2("Don't use with SCFG models");
}
void LexicalReordering::EvaluateAfterTablePruning(MemPool &pool,
const TargetPhrases &tps, const Phrase<Moses2::Word> &sourcePhrase) const
{
BOOST_FOREACH(const TargetPhraseImpl *tp, tps) {
EvaluateAfterTablePruning(pool, *tp, sourcePhrase);
}
}
void LexicalReordering::EvaluateAfterTablePruning(MemPool &pool,
const TargetPhraseImpl &targetPhrase, const Phrase<Moses2::Word> &sourcePhrase) const
{
if (m_propertyInd >= 0) {
SCORE *scoreArr = targetPhrase.GetScoresProperty(m_propertyInd);
targetPhrase.ffData[m_PhraseTableInd] = scoreArr;
#ifdef HAVE_CMPH
} else if (m_compactModel) {
// using external compact binary model
const Values values = m_compactModel->GetScore(sourcePhrase, targetPhrase,
*m_blank);
if (values.size()) {
assert(values.size() == m_numScores);
SCORE *scoreArr = pool.Allocate<SCORE>(m_numScores);
for (size_t i = 0; i < m_numScores; ++i) {
scoreArr[i] = values[i];
}
targetPhrase.ffData[m_PhraseTableInd] = scoreArr;
} else {
targetPhrase.ffData[m_PhraseTableInd] = NULL;
}
#endif
} else if (m_coll) {
// using external memory model
// cache data in target phrase
const Values *values = GetValues(sourcePhrase, targetPhrase);
assert(values->size() == m_numScores);
if (values) {
SCORE *scoreArr = pool.Allocate<SCORE>(m_numScores);
for (size_t i = 0; i < m_numScores; ++i) {
scoreArr[i] = (*values)[i];
}
targetPhrase.ffData[m_PhraseTableInd] = scoreArr;
} else {
targetPhrase.ffData[m_PhraseTableInd] = NULL;
}
}
}
void LexicalReordering::EvaluateWhenApplied(const ManagerBase &mgr,
const Hypothesis &hypo, const FFState &prevState, Scores &scores,
FFState &state) const
{
const LRState &prevStateCast = static_cast<const LRState&>(prevState);
prevStateCast.Expand(mgr, *this, hypo, m_PhraseTableInd, scores, state);
}
const LexicalReordering::Values *LexicalReordering::GetValues(
const Phrase<Moses2::Word> &source, const Phrase<Moses2::Word> &target) const
{
Key key(&source, &target);
Coll::const_iterator iter;
iter = m_coll->find(key);
if (iter == m_coll->end()) {
return NULL;
} else {
return &iter->second;
}
}
void LexicalReordering::EvaluateWhenApplied(const SCFG::Manager &mgr,
const SCFG::Hypothesis &hypo, int featureID, Scores &scores,
FFState &state) const
{
UTIL_THROW2("Not implemented");
}
} /* namespace Moses2 */
|