2012-10-11 19:38:39 +04:00
|
|
|
#include "Incremental/Manager.h"
|
|
|
|
|
|
|
|
#include "Incremental/Fill.h"
|
|
|
|
|
|
|
|
#include "ChartCell.h"
|
|
|
|
#include "TranslationSystem.h"
|
|
|
|
#include "StaticData.h"
|
|
|
|
|
|
|
|
#include "search/context.hh"
|
|
|
|
#include "search/config.hh"
|
|
|
|
#include "search/weights.hh"
|
|
|
|
|
2012-10-12 17:38:07 +04:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
2012-10-11 19:38:39 +04:00
|
|
|
namespace Moses {
|
|
|
|
namespace Incremental {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ChartCellBaseFactory {
|
|
|
|
ChartCellBase *operator()(size_t startPos, size_t endPos) const {
|
|
|
|
return new ChartCellBase(startPos, endPos);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Manager::Manager(const InputType &source, const TranslationSystem &system) :
|
|
|
|
source_(source),
|
|
|
|
system_(system),
|
|
|
|
cells_(source, ChartCellBaseFactory()),
|
|
|
|
parser_(source, system, cells_) {
|
|
|
|
|
|
|
|
system.InitializeBeforeSentenceProcessing(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::~Manager() {
|
|
|
|
system_.CleanUpAfterSentenceProcessing(source_);
|
|
|
|
}
|
|
|
|
|
2012-10-12 17:25:39 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void ConstructString(const search::Final &final, std::ostringstream &stream) {
|
2012-10-13 23:32:55 +04:00
|
|
|
const TargetPhrase &phrase = *static_cast<const TargetPhrase*>(final.GetNote().vp);
|
2012-10-12 17:25:39 +04:00
|
|
|
size_t child = 0;
|
|
|
|
for (std::size_t i = 0; i < phrase.GetSize(); ++i) {
|
|
|
|
const Word &word = phrase.GetWord(i);
|
|
|
|
if (word.IsNonTerminal()) {
|
|
|
|
ConstructString(*final.Children()[child++], stream);
|
|
|
|
} else {
|
|
|
|
stream << word[0]->GetString() << ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BestString(const ChartCellLabelSet &labels, std::string &out) {
|
|
|
|
const search::Final *best = NULL;
|
|
|
|
for (ChartCellLabelSet::const_iterator i = labels.begin(); i != labels.end(); ++i) {
|
|
|
|
const search::Final *child = i->second.GetStack().incr->BestChild();
|
|
|
|
if (child && (!best || (child->Bound() > best->Bound()))) {
|
|
|
|
best = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!best) {
|
|
|
|
out.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::ostringstream stream;
|
|
|
|
ConstructString(*best, stream);
|
|
|
|
out = stream.str();
|
|
|
|
CHECK(out.size() > 9);
|
|
|
|
// <s>
|
|
|
|
out.erase(0, 4);
|
|
|
|
// </s>
|
|
|
|
out.erase(out.size() - 5);
|
2012-10-12 17:38:07 +04:00
|
|
|
// Hack: include model score
|
|
|
|
out += " ||| ";
|
|
|
|
out += boost::lexical_cast<std::string>(best->Bound());
|
2012-10-12 17:25:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2012-10-11 19:38:39 +04:00
|
|
|
template <class Model> void Manager::LMCallback(const Model &model, const std::vector<lm::WordIndex> &words) {
|
|
|
|
const LanguageModel &abstract = **system_.GetLanguageModels().begin();
|
|
|
|
search::Weights weights(
|
|
|
|
abstract.GetWeight(),
|
|
|
|
abstract.OOVFeatureEnabled() ? abstract.GetOOVWeight() : 0.0,
|
|
|
|
system_.GetWeightWordPenalty());
|
2012-10-12 16:53:08 +04:00
|
|
|
search::Config config(weights, StaticData::Instance().GetCubePruningPopLimit());
|
2012-10-11 19:38:39 +04:00
|
|
|
search::Context<Model> context(config, model);
|
|
|
|
|
|
|
|
size_t size = source_.GetSize();
|
|
|
|
for (size_t width = 1; width <= size; ++width) {
|
|
|
|
for (size_t startPos = 0; startPos <= size-width; ++startPos) {
|
|
|
|
size_t endPos = startPos + width - 1;
|
|
|
|
WordsRange range(startPos, endPos);
|
|
|
|
Fill<Model> filler(context, words, owner_);
|
2012-10-12 16:53:08 +04:00
|
|
|
parser_.Create(range, filler);
|
2012-10-11 19:38:39 +04:00
|
|
|
filler.Search(cells_.MutableBase(range).MutableTargetLabelSet());
|
|
|
|
}
|
|
|
|
}
|
2012-10-12 17:25:39 +04:00
|
|
|
BestString(cells_.GetBase(WordsRange(0, source_.GetSize() - 1)).GetTargetLabelSet(), output_);
|
2012-10-11 19:38:39 +04:00
|
|
|
}
|
|
|
|
|
2012-10-12 16:53:08 +04:00
|
|
|
template void Manager::LMCallback<lm::ngram::ProbingModel>(const lm::ngram::ProbingModel &model, const std::vector<lm::WordIndex> &words);
|
|
|
|
template void Manager::LMCallback<lm::ngram::RestProbingModel>(const lm::ngram::RestProbingModel &model, const std::vector<lm::WordIndex> &words);
|
|
|
|
template void Manager::LMCallback<lm::ngram::TrieModel>(const lm::ngram::TrieModel &model, const std::vector<lm::WordIndex> &words);
|
|
|
|
template void Manager::LMCallback<lm::ngram::QuantTrieModel>(const lm::ngram::QuantTrieModel &model, const std::vector<lm::WordIndex> &words);
|
|
|
|
template void Manager::LMCallback<lm::ngram::ArrayTrieModel>(const lm::ngram::ArrayTrieModel &model, const std::vector<lm::WordIndex> &words);
|
|
|
|
template void Manager::LMCallback<lm::ngram::QuantArrayTrieModel>(const lm::ngram::QuantArrayTrieModel &model, const std::vector<lm::WordIndex> &words);
|
|
|
|
|
2012-10-11 19:38:39 +04:00
|
|
|
void Manager::ProcessSentence() {
|
|
|
|
const LMList &lms = system_.GetLanguageModels();
|
|
|
|
UTIL_THROW_IF(lms.size() != 1, util::Exception, "Incremental search only supports one language model.");
|
|
|
|
(*lms.begin())->IncrementalCallback(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Incremental
|
|
|
|
} // namespace Moses
|