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
|
|
|
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-07-12 18:03:01 +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-07-12 18:03:01 +04:00
|
|
|
p->second.Prune(tableLimit);
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// prune TargetPhraseCollection in this node
|
2013-07-12 17:49:52 +04:00
|
|
|
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-07-12 18:03:01 +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-07-12 18:03:01 +04:00
|
|
|
p->second.Sort(tableLimit);
|
2011-06-27 19:13:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// prune TargetPhraseCollection in this node
|
2013-07-12 17:49:52 +04:00
|
|
|
m_targetPhraseCollection.Sort(true, tableLimit);
|
2011-06-27 19:13:15 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:33:30 +04:00
|
|
|
PhraseDictionaryNodeMemory *PhraseDictionaryNodeMemory::GetOrCreateChild(const Word &sourceTerm)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2013-07-12 18:03:01 +04:00
|
|
|
return &m_sourceTermMap[sourceTerm];
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2014-03-21 14:53:15 +04:00
|
|
|
#if defined(UNLABELLED_SOURCE)
|
|
|
|
PhraseDictionaryNodeMemory *PhraseDictionaryNodeMemory::GetOrCreateNonTerminalChild(const Word &targetNonTerm)
|
|
|
|
{
|
|
|
|
UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(),
|
2014-05-19 17:34:27 +04:00
|
|
|
"Not a non-terminal: " << targetNonTerm);
|
2014-03-21 14:53:15 +04:00
|
|
|
|
|
|
|
return &m_nonTermMap[targetNonTerm];
|
|
|
|
}
|
|
|
|
#else
|
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
|
|
|
{
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(!sourceNonTerm.IsNonTerminal(),
|
2014-01-15 19:42:02 +04:00
|
|
|
"Not a non-terminal: " << sourceNonTerm);
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(),
|
2014-01-15 19:42:02 +04:00
|
|
|
"Not a non-terminal: " << targetNonTerm);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
NonTerminalMapKey key(sourceNonTerm, targetNonTerm);
|
2013-07-12 18:03:01 +04:00
|
|
|
return &m_nonTermMap[NonTerminalMapKey(sourceNonTerm, targetNonTerm)];
|
2010-08-17 17:41:46 +04:00
|
|
|
}
|
2014-03-21 14:53:15 +04:00
|
|
|
#endif
|
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
|
|
|
{
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(sourceTerm.IsNonTerminal(),
|
2014-01-15 19:42:02 +04:00
|
|
|
"Not a terminal: " << sourceTerm);
|
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-07-12 18:03:01 +04:00
|
|
|
return (p == m_sourceTermMap.end()) ? NULL : &p->second;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2014-03-21 14:53:15 +04:00
|
|
|
#if defined(UNLABELLED_SOURCE)
|
|
|
|
const PhraseDictionaryNodeMemory *PhraseDictionaryNodeMemory::GetNonTerminalChild(const Word &targetNonTerm) const
|
|
|
|
{
|
|
|
|
UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(),
|
2014-05-19 17:34:27 +04:00
|
|
|
"Not a non-terminal: " << targetNonTerm);
|
2014-03-21 14:53:15 +04:00
|
|
|
|
|
|
|
NonTerminalMap::const_iterator p = m_nonTermMap.find(targetNonTerm);
|
|
|
|
return (p == m_nonTermMap.end()) ? NULL : &p->second;
|
|
|
|
}
|
|
|
|
#else
|
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
|
|
|
{
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(!sourceNonTerm.IsNonTerminal(),
|
2014-01-15 19:42:02 +04:00
|
|
|
"Not a non-terminal: " << sourceNonTerm);
|
2013-11-23 00:27:46 +04:00
|
|
|
UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(),
|
2014-01-15 19:42:02 +04:00
|
|
|
"Not a non-terminal: " << targetNonTerm);
|
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-07-12 18:03:01 +04:00
|
|
|
return (p == m_nonTermMap.end()) ? NULL : &p->second;
|
2010-08-17 17:41:46 +04:00
|
|
|
}
|
2014-03-21 14:53:15 +04:00
|
|
|
#endif
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2013-07-18 23:23:44 +04:00
|
|
|
void PhraseDictionaryNodeMemory::Remove()
|
2011-11-06 13:08:37 +04:00
|
|
|
{
|
|
|
|
m_sourceTermMap.clear();
|
|
|
|
m_nonTermMap.clear();
|
2013-07-18 23:23:44 +04:00
|
|
|
m_targetPhraseCollection.Remove();
|
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
|
|
|
}
|
|
|
|
|