mosesdecoder/moses/TranslationModel/PhraseDictionaryTreeAdaptor.cpp

125 lines
3.1 KiB
C++
Raw Normal View History

// $Id$
#include <sys/stat.h>
#include <algorithm>
#include "moses/TranslationModel/PhraseDictionaryTreeAdaptor.h"
#include "moses/TranslationModel/PhraseDictionaryTree.h"
#include "moses/Phrase.h"
#include "moses/FactorCollection.h"
#include "moses/InputFileStream.h"
#include "moses/InputType.h"
#include "moses/ConfusionNet.h"
#include "moses/Sentence.h"
#include "moses/StaticData.h"
#include "moses/UniqueObject.h"
#include "moses/PDTAimp.h"
#include "moses/UserMessage.h"
#include "util/check.hh"
2013-02-25 15:49:34 +04:00
using namespace std;
namespace Moses
{
/*************************************************************
function definitions of the interface class
virtually everything is forwarded to the implementation class
*************************************************************/
PhraseDictionaryTreeAdaptor::
2013-02-22 23:17:57 +04:00
PhraseDictionaryTreeAdaptor(const std::string &line)
: PhraseDictionary("PhraseDictionaryBinary", line)
{
size_t ind = 0;
while (ind < m_args.size()) {
vector<string> &args = m_args[ind];
bool consumed = SetParameter(args[0], args[1]);
if (consumed) {
m_args.erase(m_args.begin() + ind);
} else {
++ind;
}
}
}
PhraseDictionaryTreeAdaptor::~PhraseDictionaryTreeAdaptor()
{
}
void PhraseDictionaryTreeAdaptor::Load()
{
SetFeaturesToApply();
}
2013-03-04 22:15:05 +04:00
void PhraseDictionaryTreeAdaptor::InitializeForInput(InputType const& source)
{
2013-02-25 15:49:34 +04:00
const StaticData &staticData = StaticData::Instance();
PDTAimp *obj = new PDTAimp(this);
2013-03-04 22:15:05 +04:00
vector<float> weight = staticData.GetWeights(this);
2013-02-22 23:17:57 +04:00
if(m_numScoreComponents!=weight.size()) {
std::stringstream strme;
strme << "ERROR: mismatch of number of scaling factors: "<<weight.size()
2013-02-22 23:17:57 +04:00
<<" "<<m_numScoreComponents<<"\n";
UserMessage::Add(strme.str());
2013-03-04 22:15:05 +04:00
abort();
}
2013-05-21 15:47:26 +04:00
obj->Create(m_input, m_output, m_filePath, weight);
2013-03-04 22:15:05 +04:00
obj->CleanUp();
// caching only required for confusion net
if(ConfusionNet const* cn=dynamic_cast<ConfusionNet const*>(&source))
2013-03-04 22:15:05 +04:00
obj->CacheSource(*cn);
m_implementation.reset(obj);
}
void PhraseDictionaryTreeAdaptor::CleanUpAfterSentenceProcessing(InputType const& source)
{
PDTAimp &obj = GetImplementation();
obj.CleanUp();
}
TargetPhraseCollection const*
PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(Phrase const &src) const
{
return GetImplementation().GetTargetPhraseCollection(src);
}
TargetPhraseCollection const*
PhraseDictionaryTreeAdaptor::GetTargetPhraseCollection(InputType const& src,WordsRange const &range) const
{
if(GetImplementation().m_rangeCache.empty()) {
return GetImplementation().GetTargetPhraseCollection(src.GetSubString(range));
} else {
return GetImplementation().m_rangeCache[range.GetStartPos()][range.GetEndPos()];
}
}
void PhraseDictionaryTreeAdaptor::EnableCache()
{
GetImplementation().useCache=1;
}
void PhraseDictionaryTreeAdaptor::DisableCache()
{
GetImplementation().useCache=0;
2013-03-04 22:15:05 +04:00
}
PDTAimp& PhraseDictionaryTreeAdaptor::GetImplementation()
2013-03-04 22:15:05 +04:00
{
PDTAimp* dict;
dict = m_implementation.get();
CHECK(dict);
return *dict;
2013-03-04 22:15:05 +04:00
}
const PDTAimp& PhraseDictionaryTreeAdaptor::GetImplementation() const
2013-03-04 22:15:05 +04:00
{
PDTAimp* dict;
dict = m_implementation.get();
CHECK(dict);
return *dict;
}
}