2013-05-29 21:16:15 +04:00
|
|
|
// vim:tabstop=2
|
2010-04-12 14:15:49 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2010 Hieu Hoang
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
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.
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
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.
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
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
|
|
|
|
***********************************************************************/
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
#include "PhraseDictionaryOnDisk.h"
|
2012-11-12 23:56:18 +04:00
|
|
|
#include "moses/InputFileStream.h"
|
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/TargetPhraseCollection.h"
|
2013-07-07 01:42:52 +04:00
|
|
|
#include "moses/InputPath.h"
|
2012-11-27 21:23:31 +04:00
|
|
|
#include "moses/TranslationModel/CYKPlusParser/DotChartOnDisk.h"
|
|
|
|
#include "moses/TranslationModel/CYKPlusParser/ChartRuleLookupManagerOnDisk.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2013-07-05 13:52:12 +04:00
|
|
|
#include "OnDiskPt/OnDiskWrapper.h"
|
|
|
|
#include "OnDiskPt/Word.h"
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2010-08-10 17:12:00 +04:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2013-06-20 15:50:41 +04:00
|
|
|
PhraseDictionaryOnDisk::PhraseDictionaryOnDisk(const std::string &line)
|
2013-10-29 22:20:55 +04:00
|
|
|
: MyBase(line)
|
2013-06-26 20:19:09 +04:00
|
|
|
{
|
2013-06-20 16:06:03 +04:00
|
|
|
ReadParameters();
|
2013-06-20 15:50:41 +04:00
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
PhraseDictionaryOnDisk::~PhraseDictionaryOnDisk()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-14 21:34:47 +04:00
|
|
|
void PhraseDictionaryOnDisk::Load()
|
|
|
|
{
|
|
|
|
SetFeaturesToApply();
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-04-13 14:38:27 +04:00
|
|
|
ChartRuleLookupManager *PhraseDictionaryOnDisk::CreateRuleLookupManager(
|
2013-07-31 15:25:34 +04:00
|
|
|
const ChartParser &parser,
|
2012-10-11 17:27:30 +04:00
|
|
|
const ChartCellCollectionBase &cellCollection)
|
2011-04-13 14:38:27 +04:00
|
|
|
{
|
2013-07-31 15:25:34 +04:00
|
|
|
return new ChartRuleLookupManagerOnDisk(parser, cellCollection, *this,
|
2013-05-21 15:47:26 +04:00
|
|
|
GetImplementation(),
|
2013-05-21 16:44:19 +04:00
|
|
|
m_input,
|
2013-02-25 18:19:48 +04:00
|
|
|
m_output, m_filePath);
|
2011-04-13 14:38:27 +04:00
|
|
|
}
|
|
|
|
|
2013-03-05 18:02:55 +04:00
|
|
|
OnDiskPt::OnDiskWrapper &PhraseDictionaryOnDisk::GetImplementation()
|
|
|
|
{
|
|
|
|
OnDiskPt::OnDiskWrapper* dict;
|
|
|
|
dict = m_implementation.get();
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(dict == NULL, "Dictionary object not yet created for this thread");
|
2013-03-05 18:02:55 +04:00
|
|
|
return *dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OnDiskPt::OnDiskWrapper &PhraseDictionaryOnDisk::GetImplementation() const
|
|
|
|
{
|
|
|
|
OnDiskPt::OnDiskWrapper* dict;
|
|
|
|
dict = m_implementation.get();
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(dict == NULL, "Dictionary object not yet created for this thread");
|
2013-03-05 18:02:55 +04:00
|
|
|
return *dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhraseDictionaryOnDisk::InitializeForInput(InputType const& source)
|
|
|
|
{
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
|
2013-08-16 18:05:36 +04:00
|
|
|
ReduceCache();
|
|
|
|
|
2013-03-05 18:02:55 +04:00
|
|
|
OnDiskPt::OnDiskWrapper *obj = new OnDiskPt::OnDiskWrapper();
|
2013-11-18 21:08:58 +04:00
|
|
|
obj->BeginLoad(m_filePath);
|
2013-03-05 18:02:55 +04:00
|
|
|
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(obj->GetMisc("Version") != OnDiskPt::OnDiskWrapper::VERSION_NUM,
|
2013-11-20 22:18:02 +04:00
|
|
|
"On-disk phrase table is version " << obj->GetMisc("Version")
|
|
|
|
<< ". It is not compatible with version " << OnDiskPt::OnDiskWrapper::VERSION_NUM);
|
|
|
|
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(obj->GetMisc("NumSourceFactors") != m_input.size(),
|
2013-11-20 22:18:02 +04:00
|
|
|
"On-disk phrase table has " << obj->GetMisc("NumSourceFactors") << " source factors."
|
|
|
|
<< ". The ini file specified " << m_input.size() << " source factors");
|
|
|
|
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(obj->GetMisc("NumTargetFactors") != m_output.size(),
|
2013-11-20 22:18:02 +04:00
|
|
|
"On-disk phrase table has " << obj->GetMisc("NumTargetFactors") << " target factors."
|
|
|
|
<< ". The ini file specified " << m_output.size() << " target factors");
|
|
|
|
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(obj->GetMisc("NumScores") != m_numScoreComponents,
|
2013-11-20 22:18:02 +04:00
|
|
|
"On-disk phrase table has " << obj->GetMisc("NumScores") << " scores."
|
|
|
|
<< ". The ini file specified " << m_numScoreComponents << " scores");
|
2013-03-05 18:02:55 +04:00
|
|
|
|
|
|
|
m_implementation.reset(obj);
|
2013-07-05 02:36:17 +04:00
|
|
|
}
|
2013-07-05 13:52:12 +04:00
|
|
|
|
2013-08-16 15:34:31 +04:00
|
|
|
void PhraseDictionaryOnDisk::GetTargetPhraseCollectionBatch(const InputPathList &inputPathQueue) const
|
2013-07-05 02:36:17 +04:00
|
|
|
{
|
2013-07-09 17:19:35 +04:00
|
|
|
InputPathList::const_iterator iter;
|
2013-08-16 15:34:31 +04:00
|
|
|
for (iter = inputPathQueue.begin(); iter != inputPathQueue.end(); ++iter) {
|
2013-08-15 23:57:04 +04:00
|
|
|
InputPath &inputPath = **iter;
|
2013-08-16 15:34:31 +04:00
|
|
|
GetTargetPhraseCollectionBatch(inputPath);
|
|
|
|
}
|
2014-01-21 21:11:16 +04:00
|
|
|
|
|
|
|
// delete nodes that's been saved
|
|
|
|
for (iter = inputPathQueue.begin(); iter != inputPathQueue.end(); ++iter) {
|
|
|
|
InputPath &inputPath = **iter;
|
|
|
|
const OnDiskPt::PhraseNode *ptNode = static_cast<const OnDiskPt::PhraseNode*>(inputPath.GetPtNode(*this));
|
|
|
|
delete ptNode;
|
|
|
|
}
|
|
|
|
|
2013-08-16 15:34:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhraseDictionaryOnDisk::GetTargetPhraseCollectionBatch(InputPath &inputPath) const
|
|
|
|
{
|
2013-08-23 16:53:30 +04:00
|
|
|
OnDiskPt::OnDiskWrapper &wrapper = const_cast<OnDiskPt::OnDiskWrapper&>(GetImplementation());
|
|
|
|
const Phrase &phrase = inputPath.GetPhrase();
|
2013-10-02 19:51:16 +04:00
|
|
|
const InputPath *prevInputPath = inputPath.GetPrevPath();
|
2013-08-23 16:53:30 +04:00
|
|
|
|
|
|
|
const OnDiskPt::PhraseNode *prevPtNode = NULL;
|
|
|
|
|
|
|
|
if (prevInputPath) {
|
|
|
|
prevPtNode = static_cast<const OnDiskPt::PhraseNode*>(prevInputPath->GetPtNode(*this));
|
|
|
|
} else {
|
|
|
|
// Starting subphrase.
|
|
|
|
assert(phrase.GetSize() == 1);
|
|
|
|
prevPtNode = &wrapper.GetRootSourceNode();
|
|
|
|
}
|
2013-07-05 02:38:18 +04:00
|
|
|
|
2013-08-23 16:53:30 +04:00
|
|
|
if (prevPtNode) {
|
|
|
|
Word lastWord = phrase.GetWord(phrase.GetSize() - 1);
|
|
|
|
lastWord.OnlyTheseFactors(m_inputFactors);
|
|
|
|
OnDiskPt::Word *lastWordOnDisk = wrapper.ConvertFromMoses(m_input, lastWord);
|
2013-07-05 02:38:18 +04:00
|
|
|
|
2013-08-23 16:53:30 +04:00
|
|
|
if (lastWordOnDisk == NULL) {
|
|
|
|
// OOV according to this phrase table. Not possible to extend
|
|
|
|
inputPath.SetTargetPhrases(*this, NULL, NULL);
|
2013-07-05 02:38:18 +04:00
|
|
|
} else {
|
2013-08-23 16:53:30 +04:00
|
|
|
const OnDiskPt::PhraseNode *ptNode = prevPtNode->GetChild(*lastWordOnDisk, wrapper);
|
|
|
|
if (ptNode) {
|
|
|
|
const TargetPhraseCollection *targetPhrases = GetTargetPhraseCollection(ptNode);
|
|
|
|
inputPath.SetTargetPhrases(*this, targetPhrases, ptNode);
|
2013-07-08 22:02:18 +04:00
|
|
|
} else {
|
2013-08-23 16:53:30 +04:00
|
|
|
inputPath.SetTargetPhrases(*this, NULL, NULL);
|
2013-07-08 20:58:22 +04:00
|
|
|
}
|
2013-08-23 16:53:30 +04:00
|
|
|
|
|
|
|
delete lastWordOnDisk;
|
2013-07-05 02:38:18 +04:00
|
|
|
}
|
2013-08-23 16:53:30 +04:00
|
|
|
}
|
2013-03-05 18:02:55 +04:00
|
|
|
}
|
2013-07-05 13:52:12 +04:00
|
|
|
|
2013-08-16 16:47:39 +04:00
|
|
|
const TargetPhraseCollection *PhraseDictionaryOnDisk::GetTargetPhraseCollection(const OnDiskPt::PhraseNode *ptNode) const
|
|
|
|
{
|
2013-08-23 16:53:30 +04:00
|
|
|
const TargetPhraseCollection *ret;
|
|
|
|
|
|
|
|
if (m_maxCacheSize) {
|
|
|
|
CacheColl &cache = GetCache();
|
|
|
|
size_t hash = (size_t) ptNode->GetFilePos();
|
|
|
|
|
|
|
|
std::map<size_t, std::pair<const TargetPhraseCollection*, clock_t> >::iterator iter;
|
|
|
|
|
|
|
|
iter = cache.find(hash);
|
|
|
|
|
|
|
|
if (iter == cache.end()) {
|
|
|
|
// not in cache, need to look up from phrase table
|
|
|
|
ret = GetTargetPhraseCollectionNonCache(ptNode);
|
|
|
|
if (ret) {
|
|
|
|
ret = new TargetPhraseCollection(*ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<const TargetPhraseCollection*, clock_t> value(ret, clock());
|
|
|
|
cache[hash] = value;
|
|
|
|
} else {
|
|
|
|
// in cache. just use it
|
|
|
|
std::pair<const TargetPhraseCollection*, clock_t> &value = iter->second;
|
|
|
|
value.second = clock();
|
|
|
|
|
|
|
|
ret = value.first;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = GetTargetPhraseCollectionNonCache(ptNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-08-16 16:47:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const TargetPhraseCollection *PhraseDictionaryOnDisk::GetTargetPhraseCollectionNonCache(const OnDiskPt::PhraseNode *ptNode) const
|
2013-08-16 16:26:21 +04:00
|
|
|
{
|
2013-08-23 16:53:30 +04:00
|
|
|
OnDiskPt::OnDiskWrapper &wrapper = const_cast<OnDiskPt::OnDiskWrapper&>(GetImplementation());
|
2013-08-16 16:26:21 +04:00
|
|
|
|
2013-08-23 16:53:30 +04:00
|
|
|
vector<float> weightT = StaticData::Instance().GetWeights(this);
|
|
|
|
OnDiskPt::Vocab &vocab = wrapper.GetVocab();
|
2013-08-16 16:26:21 +04:00
|
|
|
|
2013-08-23 16:53:30 +04:00
|
|
|
const OnDiskPt::TargetPhraseCollection *targetPhrasesOnDisk = ptNode->GetTargetPhraseCollection(m_tableLimit, wrapper);
|
|
|
|
TargetPhraseCollection *targetPhrases
|
|
|
|
= targetPhrasesOnDisk->ConvertToMoses(m_input, m_output, *this, weightT, vocab, false);
|
2013-08-16 16:26:21 +04:00
|
|
|
|
2013-08-23 16:53:30 +04:00
|
|
|
delete targetPhrasesOnDisk;
|
2013-08-16 16:26:21 +04:00
|
|
|
|
2013-08-23 16:53:30 +04:00
|
|
|
return targetPhrases;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-16 16:26:21 +04:00
|
|
|
} // namespace
|
|
|
|
|