mosesdecoder/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.cpp

150 lines
4.7 KiB
C++
Raw Normal View History

2013-05-29 21:16:15 +04:00
// vim:tabstop=2
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2010 Hieu Hoang
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
#include "PhraseDictionaryOnDisk.h"
2012-11-12 23:56:18 +04:00
#include "moses/InputFileStream.h"
#include "moses/StaticData.h"
#include "moses/TargetPhraseCollection.h"
#include "moses/InputLatticeNode.h"
2012-11-27 21:23:31 +04:00
#include "moses/TranslationModel/CYKPlusParser/DotChartOnDisk.h"
#include "moses/TranslationModel/CYKPlusParser/ChartRuleLookupManagerOnDisk.h"
2013-07-05 13:52:12 +04:00
#include "OnDiskPt/OnDiskWrapper.h"
#include "OnDiskPt/Word.h"
using namespace std;
namespace Moses
{
PhraseDictionaryOnDisk::PhraseDictionaryOnDisk(const std::string &line)
2013-06-26 20:19:09 +04:00
: MyBase("PhraseDictionaryOnDisk", line)
{
ReadParameters();
}
PhraseDictionaryOnDisk::~PhraseDictionaryOnDisk()
{
}
void PhraseDictionaryOnDisk::Load()
{
SetFeaturesToApply();
}
//! find list of translations that can translates src. Only for phrase input
const TargetPhraseCollection *PhraseDictionaryOnDisk::GetTargetPhraseCollection(const Phrase& /* src */) const
{
CHECK(false);
return NULL;
}
ChartRuleLookupManager *PhraseDictionaryOnDisk::CreateRuleLookupManager(
const InputType &sentence,
const ChartCellCollectionBase &cellCollection)
{
return new ChartRuleLookupManagerOnDisk(sentence, cellCollection, *this,
2013-05-21 15:47:26 +04:00
GetImplementation(),
m_input,
2013-02-25 18:19:48 +04:00
m_output, m_filePath);
}
2013-03-05 18:02:55 +04:00
OnDiskPt::OnDiskWrapper &PhraseDictionaryOnDisk::GetImplementation()
{
OnDiskPt::OnDiskWrapper* dict;
dict = m_implementation.get();
CHECK(dict);
return *dict;
}
const OnDiskPt::OnDiskWrapper &PhraseDictionaryOnDisk::GetImplementation() const
{
OnDiskPt::OnDiskWrapper* dict;
dict = m_implementation.get();
CHECK(dict);
return *dict;
}
void PhraseDictionaryOnDisk::InitializeForInput(InputType const& source)
{
const StaticData &staticData = StaticData::Instance();
OnDiskPt::OnDiskWrapper *obj = new OnDiskPt::OnDiskWrapper();
if (!obj->BeginLoad(m_filePath))
return;
CHECK(obj->GetMisc("Version") == OnDiskPt::OnDiskWrapper::VERSION_NUM);
CHECK(obj->GetMisc("NumSourceFactors") == m_input.size());
CHECK(obj->GetMisc("NumTargetFactors") == m_output.size());
CHECK(obj->GetMisc("NumScores") == m_numScoreComponents);
m_implementation.reset(obj);
}
2013-07-05 13:52:12 +04:00
void PhraseDictionaryOnDisk::SetTargetPhraseFromPtMatrix(const std::vector<InputLatticeNode*> &phraseDictionaryQueue) const
{
2013-07-05 13:52:12 +04:00
OnDiskPt::OnDiskWrapper &wrapper = const_cast<OnDiskPt::OnDiskWrapper&>(GetImplementation());
for (size_t i = 0; i < phraseDictionaryQueue.size(); ++i) {
2013-07-05 02:38:18 +04:00
InputLatticeNode &node = *phraseDictionaryQueue[i];
const Phrase &phrase = node.GetPhrase();
2013-07-05 02:38:18 +04:00
const InputLatticeNode *prevNode = node.GetPrevNode();
2013-07-05 12:59:50 +04:00
const OnDiskPt::PhraseNode *prevPtNode = NULL;
2013-07-05 02:38:18 +04:00
if (prevNode) {
2013-07-05 12:59:50 +04:00
prevPtNode = static_cast<const OnDiskPt::PhraseNode*>(prevNode->GetPtNode(*this));
2013-07-05 02:38:18 +04:00
} else {
// Starting subphrase.
assert(phrase.GetSize() == 1);
2013-07-05 13:52:12 +04:00
prevPtNode = &wrapper.GetRootSourceNode();
2013-07-05 02:38:18 +04:00
}
if (prevPtNode) {
Word lastWord = phrase.GetWord(phrase.GetSize() - 1);
lastWord.OnlyTheseFactors(m_inputFactors);
2013-07-05 14:10:10 +04:00
OnDiskPt::Word *lastWordOnDisk = wrapper.ConvertFromMoses(m_input, lastWord);
2013-07-05 02:38:18 +04:00
2013-07-05 13:52:12 +04:00
const OnDiskPt::PhraseNode *ptNode = prevPtNode->GetChild(*lastWordOnDisk, wrapper);
2013-07-05 02:38:18 +04:00
if (ptNode) {
2013-07-05 13:57:45 +04:00
vector<float> weightT = StaticData::Instance().GetWeights(this);
OnDiskPt::Vocab &vocab = wrapper.GetVocab();
2013-07-05 13:52:12 +04:00
const OnDiskPt::TargetPhraseCollection *targetPhrasesOnDisk = ptNode->GetTargetPhraseCollection(m_tableLimit, wrapper);
2013-07-05 13:57:45 +04:00
const TargetPhraseCollection *targetPhrases
2013-07-05 14:10:10 +04:00
= targetPhrasesOnDisk->ConvertToMoses(m_input, m_output, *this, weightT, vocab);
2013-07-05 13:52:12 +04:00
2013-07-05 13:57:45 +04:00
node.SetTargetPhrases(*this, targetPhrases, ptNode);
delete targetPhrasesOnDisk;
2013-07-05 02:38:18 +04:00
} else {
node.SetTargetPhrases(*this, NULL, NULL);
}
delete lastWordOnDisk;
2013-07-05 02:38:18 +04:00
}
}
2013-03-05 18:02:55 +04:00
}
2013-07-05 13:52:12 +04:00
}