2010-07-18 03:23:09 +04:00
|
|
|
// $Id$
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2006 University of Edinburgh
|
|
|
|
|
|
|
|
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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2013-05-24 19:23:31 +04:00
|
|
|
#include "PhraseDictionaryNodeMemory.h"
|
2012-11-12 23:56:18 +04:00
|
|
|
#include "moses/TargetPhrase.h"
|
2012-11-27 19:08:31 +04:00
|
|
|
#include "moses/TranslationModel/PhraseDictionary.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2013-05-22 20:32:06 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory::~PhraseDictionaryNodeMemory()
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2013-05-23 15:50:57 +04:00
|
|
|
for (TerminalMap::iterator iter = m_sourceTermMap.begin(); iter != m_sourceTermMap.end(); ++iter) {
|
2013-05-29 21:16:15 +04:00
|
|
|
const PhraseDictionaryNodeMemory *node = iter->second;
|
|
|
|
delete node;
|
2013-05-23 15:50:57 +04:00
|
|
|
}
|
|
|
|
for (NonTerminalMap::iterator iter = m_nonTermMap.begin(); iter != m_nonTermMap.end(); ++iter) {
|
2013-05-24 19:33:30 +04:00
|
|
|
const PhraseDictionaryNodeMemory *node = iter->second;
|
2013-05-23 15:50:57 +04:00
|
|
|
delete node;
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
delete m_targetPhraseCollection;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
TargetPhraseCollection &PhraseDictionaryNodeMemory::GetOrCreateTargetPhraseCollection()
|
|
|
|
{
|
2013-05-23 15:50:57 +04:00
|
|
|
if (m_targetPhraseCollection == NULL)
|
|
|
|
m_targetPhraseCollection = new TargetPhraseCollection();
|
|
|
|
return *m_targetPhraseCollection;
|
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
void PhraseDictionaryNodeMemory::Prune(size_t tableLimit)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
// recusively prune
|
|
|
|
for (TerminalMap::iterator p = m_sourceTermMap.begin(); p != m_sourceTermMap.end(); ++p) {
|
2013-05-23 15:50:57 +04:00
|
|
|
p->second->Prune(tableLimit);
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
for (NonTerminalMap::iterator p = m_nonTermMap.begin(); p != m_nonTermMap.end(); ++p) {
|
2013-05-23 15:50:57 +04:00
|
|
|
p->second->Prune(tableLimit);
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// prune TargetPhraseCollection in this node
|
|
|
|
if (m_targetPhraseCollection != NULL)
|
|
|
|
m_targetPhraseCollection->Prune(true, tableLimit);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
void PhraseDictionaryNodeMemory::Sort(size_t tableLimit)
|
2011-06-27 19:13:15 +04:00
|
|
|
{
|
|
|
|
// recusively sort
|
|
|
|
for (TerminalMap::iterator p = m_sourceTermMap.begin(); p != m_sourceTermMap.end(); ++p) {
|
2013-05-23 15:50:57 +04:00
|
|
|
p->second->Sort(tableLimit);
|
2011-06-27 19:13:15 +04:00
|
|
|
}
|
|
|
|
for (NonTerminalMap::iterator p = m_nonTermMap.begin(); p != m_nonTermMap.end(); ++p) {
|
2013-05-23 15:50:57 +04:00
|
|
|
p->second->Sort(tableLimit);
|
2011-06-27 19:13:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// prune TargetPhraseCollection in this node
|
|
|
|
if (m_targetPhraseCollection != NULL) {
|
|
|
|
m_targetPhraseCollection->Sort(true, tableLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory *PhraseDictionaryNodeMemory::GetOrCreateChild(const Word &sourceTerm)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-11-18 16:07:41 +04:00
|
|
|
//CHECK(!sourceTerm.IsNonTerminal());
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
std::pair <TerminalMap::iterator,bool> insResult;
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory *node = new PhraseDictionaryNodeMemory();
|
2013-05-23 18:29:25 +04:00
|
|
|
insResult = m_sourceTermMap.insert( std::make_pair(sourceTerm, node) );
|
|
|
|
|
|
|
|
if (!insResult.second) {
|
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
const TerminalMap::iterator &iter = insResult.first;
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory &ret = *iter->second;
|
2011-02-24 16:14:42 +03:00
|
|
|
return &ret;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory *PhraseDictionaryNodeMemory::GetOrCreateChild(const Word &sourceNonTerm, const Word &targetNonTerm)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(sourceNonTerm.IsNonTerminal());
|
|
|
|
CHECK(targetNonTerm.IsNonTerminal());
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
NonTerminalMapKey key(sourceNonTerm, targetNonTerm);
|
|
|
|
std::pair <NonTerminalMap::iterator,bool> insResult;
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory *node = new PhraseDictionaryNodeMemory();
|
2013-05-23 18:29:25 +04:00
|
|
|
|
|
|
|
insResult = m_nonTermMap.insert( std::make_pair(key, node) );
|
|
|
|
|
|
|
|
if (!insResult.second) {
|
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
const NonTerminalMap::iterator &iter = insResult.first;
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory &ret = *iter->second;
|
2011-02-24 16:14:42 +03:00
|
|
|
return &ret;
|
2010-08-17 17:41:46 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
const PhraseDictionaryNodeMemory *PhraseDictionaryNodeMemory::GetChild(const Word &sourceTerm) const
|
2010-08-17 17:41:46 +04:00
|
|
|
{
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(!sourceTerm.IsNonTerminal());
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
TerminalMap::const_iterator p = m_sourceTermMap.find(sourceTerm);
|
2013-05-23 15:50:57 +04:00
|
|
|
return (p == m_sourceTermMap.end()) ? NULL : p->second;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
const PhraseDictionaryNodeMemory *PhraseDictionaryNodeMemory::GetChild(const Word &sourceNonTerm, const Word &targetNonTerm) const
|
2010-08-17 17:41:46 +04:00
|
|
|
{
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(sourceNonTerm.IsNonTerminal());
|
|
|
|
CHECK(targetNonTerm.IsNonTerminal());
|
2010-08-17 17:41:46 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
NonTerminalMapKey key(sourceNonTerm, targetNonTerm);
|
|
|
|
NonTerminalMap::const_iterator p = m_nonTermMap.find(key);
|
2013-05-23 15:50:57 +04:00
|
|
|
return (p == m_nonTermMap.end()) ? NULL : p->second;
|
2010-08-17 17:41:46 +04:00
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
void PhraseDictionaryNodeMemory::Clear()
|
2011-11-06 13:08:37 +04:00
|
|
|
{
|
|
|
|
m_sourceTermMap.clear();
|
|
|
|
m_nonTermMap.clear();
|
|
|
|
delete m_targetPhraseCollection;
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-11-06 13:08:37 +04:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
std::ostream& operator<<(std::ostream &out, const PhraseDictionaryNodeMemory &node)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
out << node.GetTargetPhraseCollection();
|
|
|
|
return out;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
TO_STRING_BODY(PhraseDictionaryNodeMemory)
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|