00001 /*********************************************************************** 00002 Moses - statistical machine translation system 00003 Copyright (C) 2006-2012 University of Edinburgh 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 ***********************************************************************/ 00019 00020 #include "moses/NonTerminal.h" 00021 #include "moses/TargetPhrase.h" 00022 #include "moses/TargetPhraseCollection.h" 00023 #include "moses/Util.h" 00024 #include "moses/Word.h" 00025 #include "UTrieNode.h" 00026 #include "Trie.h" 00027 #include "moses/TranslationModel/PhraseDictionaryNodeMemory.h" // For TerminalHasher and TerminalEqualityPred 00028 00029 #include <boost/functional/hash.hpp> 00030 #include <boost/unordered_map.hpp> 00031 #include <boost/version.hpp> 00032 00033 #include <map> 00034 #include <vector> 00035 00036 namespace Moses 00037 { 00038 00039 void UTrieNode::Prune(size_t tableLimit) 00040 { 00041 // Recusively prune child node values. 00042 for (TerminalMap::iterator p = m_terminalMap.begin(); 00043 p != m_terminalMap.end(); ++p) { 00044 p->second.Prune(tableLimit); 00045 } 00046 if (m_gapNode) { 00047 m_gapNode->Prune(tableLimit); 00048 } 00049 00050 // Prune TargetPhraseCollections at this node. 00051 for (LabelMap::iterator p = m_labelMap.begin(); p != m_labelMap.end(); ++p) { 00052 p->second->Prune(true, tableLimit); 00053 } 00054 } 00055 00056 void UTrieNode::Sort(size_t tableLimit) 00057 { 00058 // Recusively sort child node values. 00059 for (TerminalMap::iterator p = m_terminalMap.begin(); 00060 p != m_terminalMap.end(); ++p) { 00061 p->second.Sort(tableLimit); 00062 } 00063 if (m_gapNode) { 00064 m_gapNode->Sort(tableLimit); 00065 } 00066 00067 // Sort TargetPhraseCollections at this node. 00068 for (LabelMap::iterator p = m_labelMap.begin(); p != m_labelMap.end(); ++p) { 00069 p->second->Sort(true, tableLimit); 00070 } 00071 } 00072 00073 UTrieNode *UTrieNode::GetOrCreateTerminalChild(const Word &sourceTerm) 00074 { 00075 assert(!sourceTerm.IsNonTerminal()); 00076 std::pair<TerminalMap::iterator, bool> result; 00077 result = m_terminalMap.insert(std::make_pair(sourceTerm, UTrieNode())); 00078 const TerminalMap::iterator &iter = result.first; 00079 UTrieNode &child = iter->second; 00080 return &child; 00081 } 00082 00083 UTrieNode *UTrieNode::GetOrCreateNonTerminalChild(const Word &targetNonTerm) 00084 { 00085 assert(targetNonTerm.IsNonTerminal()); 00086 if (m_gapNode == NULL) { 00087 m_gapNode = new UTrieNode(); 00088 } 00089 return m_gapNode; 00090 } 00091 00092 TargetPhraseCollection::shared_ptr 00093 UTrieNode:: 00094 GetOrCreateTargetPhraseCollection(const TargetPhrase &target) 00095 { 00096 const AlignmentInfo &alignmentInfo = target.GetAlignNonTerm(); 00097 const size_t rank = alignmentInfo.GetSize(); 00098 00099 std::vector<int> vec; 00100 vec.reserve(rank); 00101 00102 m_labelTable.resize(rank); 00103 00104 int i = 0; 00105 for (AlignmentInfo::const_iterator p = alignmentInfo.begin(); 00106 p != alignmentInfo.end(); ++p) { 00107 size_t targetNonTermIndex = p->second; 00108 const Word &targetNonTerm = target.GetWord(targetNonTermIndex); 00109 vec.push_back(InsertLabel(i++, targetNonTerm)); 00110 } 00111 TargetPhraseCollection::shared_ptr& ret = m_labelMap[vec]; 00112 if (ret == NULL) ret.reset(new TargetPhraseCollection); 00113 return ret; 00114 } 00115 00116 } // namespace Moses