mosesdecoder/contrib/other-builds/moses2/TranslationModel/ProbingPT.cpp

198 lines
5.4 KiB
C++
Raw Normal View History

2015-11-03 18:04:26 +03:00
/*
* ProbingPT.cpp
*
* Created on: 3 Nov 2015
* Author: hieu
*/
2015-12-07 21:30:17 +03:00
#include <boost/foreach.hpp>
2015-11-03 18:04:26 +03:00
#include "ProbingPT.h"
2015-11-04 16:09:53 +03:00
#include "../System.h"
#include "../Scores.h"
#include "../FF/FeatureFunctions.h"
#include "../Search/Manager.h"
2015-11-13 03:05:54 +03:00
#include "../legacy/FactorCollection.h"
#include "../legacy/ProbingPT/quering.hh"
2015-11-13 13:40:55 +03:00
#include "../legacy/Util2.h"
2015-11-03 18:04:26 +03:00
using namespace std;
ProbingPT::ProbingPT(size_t startInd, const std::string &line)
:PhraseTable(startInd, line)
{
ReadParameters();
}
2015-11-04 03:37:35 +03:00
ProbingPT::~ProbingPT()
{
delete m_engine;
2015-11-03 18:04:26 +03:00
}
void ProbingPT::Load(System &system)
{
m_engine = new QueryEngine(m_path.c_str());
m_unkId = 456456546456;
2015-11-03 18:04:26 +03:00
2015-11-18 18:33:42 +03:00
FactorCollection &vocab = system.GetVocab();
2015-11-03 18:04:26 +03:00
// source vocab
const std::map<uint64_t, std::string> &sourceVocab = m_engine->getSourceVocab();
std::map<uint64_t, std::string>::const_iterator iterSource;
for (iterSource = sourceVocab.begin(); iterSource != sourceVocab.end(); ++iterSource) {
const string &wordStr = iterSource->second;
2015-11-18 16:07:16 +03:00
const Factor *factor = vocab.AddFactor(wordStr, system);
2015-11-03 18:04:26 +03:00
uint64_t probingId = iterSource->first;
size_t factorId = factor->GetId();
2015-11-03 18:04:26 +03:00
if (factorId >= m_sourceVocab.size()) {
m_sourceVocab.resize(factorId + 1, m_unkId);
}
m_sourceVocab[factorId] = probingId;
2015-11-03 18:04:26 +03:00
}
// target vocab
const std::map<unsigned int, std::string> &probingVocab = m_engine->getVocab();
std::map<unsigned int, std::string>::const_iterator iter;
for (iter = probingVocab.begin(); iter != probingVocab.end(); ++iter) {
const string &wordStr = iter->second;
2015-11-18 16:07:16 +03:00
const Factor *factor = vocab.AddFactor(wordStr, system);
2015-11-03 18:04:26 +03:00
unsigned int probingId = iter->first;
2015-12-08 16:07:31 +03:00
if (probingId >= m_targetVocab.size()) {
m_targetVocab.resize(probingId + 1, NULL);
}
2015-12-08 16:36:17 +03:00
m_targetVocab[probingId] = factor;
2015-11-03 18:04:26 +03:00
}
}
2015-11-13 01:51:13 +03:00
uint64_t ProbingPT::GetSourceProbingId(const Factor *factor) const
2015-11-03 18:04:26 +03:00
{
size_t factorId = factor->GetId();
if (factorId >= m_sourceVocab.size()) {
return m_unkId;
}
return m_sourceVocab[factorId];
2015-11-03 18:04:26 +03:00
}
2015-12-07 21:30:17 +03:00
void ProbingPT::Lookup(const Manager &mgr, InputPaths &inputPaths) const
{
BOOST_FOREACH(InputPath &path, inputPaths) {
const SubPhrase &phrase = path.subPhrase;
TargetPhrases *tpsPtr;
2015-12-07 21:30:17 +03:00
tpsPtr = Lookup(mgr, mgr.GetPool(), path);
path.AddTargetPhrases(*this, tpsPtr);
}
}
TargetPhrases* ProbingPT::Lookup(const Manager &mgr, MemPool &pool, InputPath &inputPath) const
2015-11-03 18:04:26 +03:00
{
2015-11-06 22:06:41 +03:00
const Phrase &sourcePhrase = inputPath.subPhrase;
TargetPhrases *ret = CreateTargetPhrase(pool, mgr.system, sourcePhrase);
2015-11-04 03:37:35 +03:00
return ret;
2015-11-03 19:09:49 +03:00
}
TargetPhrases* ProbingPT::CreateTargetPhrase(MemPool &pool, const System &system, const Phrase &sourcePhrase) const
2015-11-03 19:09:49 +03:00
{
2015-11-03 18:04:26 +03:00
// create a target phrase from the 1st word of the source, prefix with 'ProbingPT:'
assert(sourcePhrase.GetSize());
TargetPhrases *tpSharedPtr = NULL;
2015-11-03 18:04:26 +03:00
bool ok;
vector<uint64_t> probingSource = ConvertToProbingSourcePhrase(sourcePhrase, ok);
if (!ok) {
// source phrase contains a word unknown in the pt.
// We know immediately there's no translation for it
2015-11-06 13:55:04 +03:00
return tpSharedPtr;
2015-11-03 18:04:26 +03:00
}
std::pair<bool, std::vector<target_text> > query_result;
//Actual lookup
query_result = m_engine->query(probingSource);
if (query_result.first) {
//m_engine->printTargetInfo(query_result.second);
2015-12-08 19:42:15 +03:00
const std::vector<target_text> &probingTargetPhrases = query_result.second;
tpSharedPtr = new (pool.Allocate<TargetPhrases>()) TargetPhrases(pool, probingTargetPhrases.size());
2015-11-03 18:04:26 +03:00
for (size_t i = 0; i < probingTargetPhrases.size(); ++i) {
const target_text &probingTargetPhrase = probingTargetPhrases[i];
2015-11-03 19:09:49 +03:00
TargetPhrase *tp = CreateTargetPhrase(pool, system, sourcePhrase, probingTargetPhrase);
2015-11-03 18:04:26 +03:00
2015-11-06 13:55:04 +03:00
tpSharedPtr->AddTargetPhrase(*tp);
2015-11-03 18:04:26 +03:00
}
2015-11-06 13:55:04 +03:00
tpSharedPtr->SortAndPrune(m_tableLimit);
2015-11-03 18:04:26 +03:00
}
2015-11-06 13:55:04 +03:00
return tpSharedPtr;
2015-11-03 19:09:49 +03:00
2015-11-03 18:04:26 +03:00
}
2015-11-03 19:09:49 +03:00
TargetPhrase *ProbingPT::CreateTargetPhrase(MemPool &pool, const System &system, const Phrase &sourcePhrase, const target_text &probingTargetPhrase) const
2015-11-03 18:04:26 +03:00
{
2015-11-03 19:09:49 +03:00
const std::vector<unsigned int> &probingPhrase = probingTargetPhrase.target_phrase;
size_t size = probingPhrase.size();
TargetPhrase *tp = new (pool.Allocate<TargetPhrase>()) TargetPhrase(pool, system, size);
// words
for (size_t i = 0; i < size; ++i) {
uint64_t probingId = probingPhrase[i];
2015-11-13 01:51:13 +03:00
const Factor *factor = GetTargetFactor(probingId);
2015-11-03 19:09:49 +03:00
assert(factor);
Word &word = (*tp)[i];
word[0] = factor;
}
// score for this phrase table
vector<SCORE> scores = probingTargetPhrase.prob;
2015-11-13 13:40:55 +03:00
std::transform(scores.begin(), scores.end(), scores.begin(), TransformScore);
2015-11-03 19:09:49 +03:00
tp->GetScores().PlusEquals(system, *this, scores);
2015-11-03 18:04:26 +03:00
// // alignment
// /*
// const std::vector<unsigned char> &alignments = probingTargetPhrase.word_all1;
//
// AlignmentInfo &aligns = tp->GetAlignTerm();
// for (size_t i = 0; i < alignS.size(); i += 2 ) {
// aligns.Add((size_t) alignments[i], (size_t) alignments[i+1]);
// }
// */
2015-11-03 19:09:49 +03:00
// score of all other ff when this rule is being loaded
2015-11-05 14:19:37 +03:00
const FeatureFunctions &ffs = system.featureFunctions;
2015-11-03 19:09:49 +03:00
ffs.EvaluateInIsolation(pool, system, sourcePhrase, *tp);
return tp;
2015-11-03 18:04:26 +03:00
}
std::vector<uint64_t> ProbingPT::ConvertToProbingSourcePhrase(const Phrase &sourcePhrase, bool &ok) const
{
size_t size = sourcePhrase.GetSize();
std::vector<uint64_t> ret(size);
for (size_t i = 0; i < size; ++i) {
2015-11-13 01:51:13 +03:00
const Factor *factor = sourcePhrase[i][0];
2015-11-03 18:04:26 +03:00
uint64_t probingId = GetSourceProbingId(factor);
if (probingId == m_unkId) {
ok = false;
return ret;
} else {
ret[i] = probingId;
}
}
ok = true;
return ret;
}