2010-04-08 21:16:10 +04:00
|
|
|
// vim:tabstop=2
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2006 University of Edinburgh
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 21:16:10 +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-08 21:16:10 +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-08 21:16:10 +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
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
2013-05-24 19:23:31 +04:00
|
|
|
#include "PhraseDictionaryMemory.h"
|
2012-11-12 23:56:18 +04:00
|
|
|
#include "moses/FactorCollection.h"
|
|
|
|
#include "moses/Word.h"
|
|
|
|
#include "moses/Util.h"
|
|
|
|
#include "moses/InputFileStream.h"
|
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/WordsRange.h"
|
|
|
|
#include "moses/UserMessage.h"
|
2013-05-24 19:23:31 +04:00
|
|
|
#include "moses/TranslationModel/RuleTable/LoaderFactory.h"
|
|
|
|
#include "moses/TranslationModel/RuleTable/Loader.h"
|
2012-11-27 21:23:31 +04:00
|
|
|
#include "moses/TranslationModel/CYKPlusParser/ChartRuleLookupManagerMemory.h"
|
2013-07-07 01:42:52 +04:00
|
|
|
#include "moses/InputPath.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
2013-06-20 15:50:41 +04:00
|
|
|
PhraseDictionaryMemory::PhraseDictionaryMemory(const std::string &line)
|
2013-10-29 22:20:55 +04:00
|
|
|
: RuleTableTrie(line)
|
2013-06-20 15:50:41 +04:00
|
|
|
{
|
2013-06-20 16:06:03 +04:00
|
|
|
ReadParameters();
|
2013-08-16 18:05:36 +04:00
|
|
|
|
|
|
|
// caching for memory pt is pointless
|
|
|
|
m_maxCacheSize = 0;
|
|
|
|
|
2013-06-20 15:50:41 +04:00
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
TargetPhraseCollection &PhraseDictionaryMemory::GetOrCreateTargetPhraseCollection(
|
2013-05-29 21:16:15 +04:00
|
|
|
const Phrase &source
|
|
|
|
, const TargetPhrase &target
|
|
|
|
, const Word *sourceLHS)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory &currNode = GetOrCreateNode(source, target, sourceLHS);
|
2013-07-16 19:55:56 +04:00
|
|
|
return currNode.GetTargetPhraseCollection();
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-09-25 02:57:49 +04:00
|
|
|
const TargetPhraseCollection*
|
|
|
|
PhraseDictionaryMemory::
|
|
|
|
GetTargetPhraseCollectionLEGACY(const Phrase& sourceOrig) const
|
2013-03-08 20:33:36 +04:00
|
|
|
{
|
2013-06-17 21:31:06 +04:00
|
|
|
Phrase source(sourceOrig);
|
|
|
|
source.OnlyTheseFactors(m_inputFactors);
|
|
|
|
|
2013-03-08 20:33:36 +04:00
|
|
|
// exactly like CreateTargetPhraseCollection, but don't create
|
|
|
|
const size_t size = source.GetSize();
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
const PhraseDictionaryNodeMemory *currNode = &m_collection;
|
2013-03-08 20:33:36 +04:00
|
|
|
for (size_t pos = 0 ; pos < size ; ++pos) {
|
|
|
|
const Word& word = source.GetWord(pos);
|
|
|
|
currNode = currNode->GetChild(word);
|
|
|
|
if (currNode == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:55:56 +04:00
|
|
|
return &currNode->GetTargetPhraseCollection();
|
2013-03-08 20:33:36 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory &PhraseDictionaryMemory::GetOrCreateNode(const Phrase &source
|
2013-05-29 21:16:15 +04:00
|
|
|
, const TargetPhrase &target
|
|
|
|
, const Word *sourceLHS)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
const size_t size = source.GetSize();
|
|
|
|
|
2012-10-19 18:10:10 +04:00
|
|
|
const AlignmentInfo &alignmentInfo = target.GetAlignNonTerm();
|
2012-10-05 22:12:29 +04:00
|
|
|
AlignmentInfo::const_iterator iterAlign = alignmentInfo.begin();
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory *currNode = &m_collection;
|
2011-02-24 16:14:42 +03:00
|
|
|
for (size_t pos = 0 ; pos < size ; ++pos) {
|
|
|
|
const Word& word = source.GetWord(pos);
|
|
|
|
|
|
|
|
if (word.IsNonTerminal()) {
|
|
|
|
// indexed by source label 1st
|
|
|
|
const Word &sourceNonTerm = word;
|
|
|
|
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(iterAlign == alignmentInfo.end(),
|
2014-01-15 19:42:02 +04:00
|
|
|
"No alignment for non-term at position " << pos);
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(iterAlign->first != pos,
|
2014-01-15 19:42:02 +04:00
|
|
|
"Alignment info incorrect at position " << pos);
|
2013-11-20 21:19:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
size_t targetNonTermInd = iterAlign->second;
|
2012-10-05 22:12:29 +04:00
|
|
|
++iterAlign;
|
2011-02-24 16:14:42 +03:00
|
|
|
const Word &targetNonTerm = target.GetWord(targetNonTermInd);
|
2014-03-21 14:53:15 +04:00
|
|
|
#if defined(UNLABELLED_SOURCE)
|
|
|
|
currNode = currNode->GetOrCreateNonTerminalChild(targetNonTerm);
|
|
|
|
#else
|
2011-02-24 16:14:42 +03:00
|
|
|
currNode = currNode->GetOrCreateChild(sourceNonTerm, targetNonTerm);
|
2014-03-21 14:53:15 +04:00
|
|
|
#endif
|
2011-02-24 16:14:42 +03:00
|
|
|
} else {
|
|
|
|
currNode = currNode->GetOrCreateChild(word);
|
|
|
|
}
|
|
|
|
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(currNode == NULL,
|
2014-01-15 19:42:02 +04:00
|
|
|
"Node not found at position " << pos);
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-10-24 15:54:42 +04:00
|
|
|
// finally, the source LHS
|
2011-10-24 16:13:17 +04:00
|
|
|
//currNode = currNode->GetOrCreateChild(sourceLHS);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
return *currNode;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
ChartRuleLookupManager *PhraseDictionaryMemory::CreateRuleLookupManager(
|
2013-07-31 15:25:34 +04:00
|
|
|
const ChartParser &parser,
|
2014-03-14 12:49:09 +04:00
|
|
|
const ChartCellCollectionBase &cellCollection,
|
|
|
|
std::size_t /*maxChartSpan */)
|
2011-01-24 22:14:19 +03:00
|
|
|
{
|
2013-07-31 15:25:34 +04:00
|
|
|
return new ChartRuleLookupManagerMemory(parser, cellCollection, *this);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
void PhraseDictionaryMemory::SortAndPrune()
|
2011-09-27 04:34:46 +04:00
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
if (GetTableLimit()) {
|
2011-09-27 04:34:46 +04:00
|
|
|
m_collection.Sort(GetTableLimit());
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 12:24:13 +04:00
|
|
|
|
2013-09-27 12:35:24 +04:00
|
|
|
void
|
2013-09-25 02:57:49 +04:00
|
|
|
PhraseDictionaryMemory::
|
2013-10-03 21:58:45 +04:00
|
|
|
GetTargetPhraseCollectionBatch(const InputPathList &inputPathQueue) const
|
2013-07-03 22:07:36 +04:00
|
|
|
{
|
2013-07-09 17:19:35 +04:00
|
|
|
InputPathList::const_iterator iter;
|
2013-10-03 21:58:45 +04:00
|
|
|
for (iter = inputPathQueue.begin(); iter != inputPathQueue.end(); ++iter) {
|
2013-07-09 17:19:35 +04:00
|
|
|
InputPath &node = **iter;
|
2013-07-03 22:07:36 +04:00
|
|
|
const Phrase &phrase = node.GetPhrase();
|
2013-10-02 19:51:16 +04:00
|
|
|
const InputPath *prevPath = node.GetPrevPath();
|
2013-07-05 02:38:18 +04:00
|
|
|
|
|
|
|
const PhraseDictionaryNodeMemory *prevPtNode = NULL;
|
|
|
|
|
2013-10-02 19:51:16 +04:00
|
|
|
if (prevPath) {
|
|
|
|
prevPtNode = static_cast<const PhraseDictionaryNodeMemory*>(prevPath->GetPtNode(*this));
|
2013-07-05 02:38:18 +04:00
|
|
|
} else {
|
|
|
|
// Starting subphrase.
|
|
|
|
assert(phrase.GetSize() == 1);
|
|
|
|
prevPtNode = &GetRootNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevPtNode) {
|
|
|
|
Word lastWord = phrase.GetWord(phrase.GetSize() - 1);
|
|
|
|
lastWord.OnlyTheseFactors(m_inputFactors);
|
|
|
|
|
|
|
|
const PhraseDictionaryNodeMemory *ptNode = prevPtNode->GetChild(lastWord);
|
|
|
|
if (ptNode) {
|
2013-07-16 20:11:12 +04:00
|
|
|
const TargetPhraseCollection &targetPhrases = ptNode->GetTargetPhraseCollection();
|
|
|
|
node.SetTargetPhrases(*this, &targetPhrases, ptNode);
|
2013-07-05 02:38:18 +04:00
|
|
|
} else {
|
|
|
|
node.SetTargetPhrases(*this, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 22:07:36 +04:00
|
|
|
}
|
|
|
|
}
|
2013-07-04 12:24:13 +04:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
TO_STRING_BODY(PhraseDictionaryMemory);
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
// friend
|
2013-05-24 19:33:30 +04:00
|
|
|
ostream& operator<<(ostream& out, const PhraseDictionaryMemory& phraseDict)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2013-05-24 19:33:30 +04:00
|
|
|
typedef PhraseDictionaryNodeMemory::TerminalMap TermMap;
|
|
|
|
typedef PhraseDictionaryNodeMemory::NonTerminalMap NonTermMap;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
const PhraseDictionaryNodeMemory &coll = phraseDict.m_collection;
|
2011-02-24 16:14:42 +03:00
|
|
|
for (NonTermMap::const_iterator p = coll.m_nonTermMap.begin(); p != coll.m_nonTermMap.end(); ++p) {
|
2014-03-21 14:53:15 +04:00
|
|
|
#if defined(UNLABELLED_SOURCE)
|
|
|
|
const Word &targetNonTerm = p->first;
|
|
|
|
out << targetNonTerm;
|
|
|
|
#else
|
2011-02-24 16:14:42 +03:00
|
|
|
const Word &sourceNonTerm = p->first.first;
|
|
|
|
out << sourceNonTerm;
|
2014-03-21 14:53:15 +04:00
|
|
|
#endif
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
for (TermMap::const_iterator p = coll.m_sourceTermMap.begin(); p != coll.m_sourceTermMap.end(); ++p) {
|
|
|
|
const Word &sourceTerm = p->first;
|
|
|
|
out << sourceTerm;
|
|
|
|
}
|
|
|
|
return out;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|