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
|
|
|
|
***********************************************************************/
|
|
|
|
|
2010-07-18 02:29:06 +04:00
|
|
|
#include "PhraseDictionaryNodeSCFG.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
#include "TargetPhrase.h"
|
2010-08-17 17:41:46 +04:00
|
|
|
#include "PhraseDictionary.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
2010-07-18 02:29:06 +04:00
|
|
|
PhraseDictionaryNodeSCFG::~PhraseDictionaryNodeSCFG()
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
|
|
|
delete m_targetPhraseCollection;
|
|
|
|
}
|
|
|
|
|
2010-07-18 02:29:06 +04:00
|
|
|
void PhraseDictionaryNodeSCFG::Sort(size_t tableLimit)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
|
|
|
// recusively sort
|
2010-08-17 17:41:46 +04:00
|
|
|
for (TerminalMap::iterator p = m_sourceTermMap.begin(); p != m_sourceTermMap.end(); ++p)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2010-08-17 17:41:46 +04:00
|
|
|
p->second.Sort(tableLimit);
|
|
|
|
}
|
|
|
|
for (NonTerminalMap::iterator p = m_nonTermMap.begin(); p != m_nonTermMap.end(); ++p)
|
|
|
|
{
|
|
|
|
p->second.Sort(tableLimit);
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort TargetPhraseCollection in this node
|
|
|
|
if (m_targetPhraseCollection != NULL)
|
|
|
|
m_targetPhraseCollection->NthElement(tableLimit);
|
|
|
|
}
|
|
|
|
|
2010-08-17 17:41:46 +04:00
|
|
|
PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetOrCreateChild(const Word &sourceTerm)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2010-08-17 17:41:46 +04:00
|
|
|
assert(!sourceTerm.IsNonTerminal());
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2010-08-17 17:41:46 +04:00
|
|
|
std::pair <TerminalMap::iterator,bool> insResult;
|
|
|
|
insResult = m_sourceTermMap.insert( std::make_pair(sourceTerm, PhraseDictionaryNodeSCFG()) );
|
|
|
|
const TerminalMap::iterator &iter = insResult.first;
|
2010-07-18 02:29:06 +04:00
|
|
|
PhraseDictionaryNodeSCFG &ret = iter->second;
|
2010-04-08 21:16:10 +04:00
|
|
|
return &ret;
|
|
|
|
}
|
|
|
|
|
2010-08-17 17:41:46 +04:00
|
|
|
PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetOrCreateChild(const Word &sourceNonTerm, const Word &targetNonTerm)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2010-08-17 17:41:46 +04:00
|
|
|
assert(sourceNonTerm.IsNonTerminal());
|
|
|
|
assert(targetNonTerm.IsNonTerminal());
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2010-08-17 17:41:46 +04:00
|
|
|
NonTerminalMapKey key(sourceNonTerm, targetNonTerm);
|
|
|
|
std::pair <NonTerminalMap::iterator,bool> insResult;
|
|
|
|
insResult = m_nonTermMap.insert( std::make_pair(key, PhraseDictionaryNodeSCFG()) );
|
|
|
|
const NonTerminalMap::iterator &iter = insResult.first;
|
|
|
|
PhraseDictionaryNodeSCFG &ret = iter->second;
|
|
|
|
return &ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetChild(const Word &sourceTerm) const
|
|
|
|
{
|
|
|
|
assert(!sourceTerm.IsNonTerminal());
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2010-08-17 17:41:46 +04:00
|
|
|
TerminalMap::const_iterator p = m_sourceTermMap.find(sourceTerm);
|
|
|
|
return (p == m_sourceTermMap.end()) ? NULL : &p->second;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2010-08-17 17:41:46 +04:00
|
|
|
const PhraseDictionaryNodeSCFG *PhraseDictionaryNodeSCFG::GetChild(const Word &sourceNonTerm, const Word &targetNonTerm) const
|
|
|
|
{
|
|
|
|
assert(sourceNonTerm.IsNonTerminal());
|
|
|
|
assert(targetNonTerm.IsNonTerminal());
|
|
|
|
|
|
|
|
NonTerminalMapKey key(sourceNonTerm, targetNonTerm);
|
|
|
|
NonTerminalMap::const_iterator p = m_nonTermMap.find(key);
|
|
|
|
return (p == m_nonTermMap.end()) ? NULL : &p->second;
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2010-07-18 02:29:06 +04:00
|
|
|
std::ostream& operator<<(std::ostream &out, const PhraseDictionaryNodeSCFG &node)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
|
|
|
out << node.GetTargetPhraseCollection();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-07-18 02:29:06 +04:00
|
|
|
TO_STRING_BODY(PhraseDictionaryNodeSCFG)
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|