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 #pragma once 00021 00022 #include "moses/NonTerminal.h" 00023 #include "moses/TargetPhrase.h" 00024 #include "moses/TargetPhraseCollection.h" 00025 #include "moses/Terminal.h" 00026 #include "moses/Util.h" 00027 #include "moses/Word.h" 00028 #include "Trie.h" 00029 00030 #include <boost/functional/hash.hpp> 00031 #include <boost/unordered_map.hpp> 00032 #include <boost/version.hpp> 00033 00034 #include <map> 00035 #include <vector> 00036 00037 namespace Moses 00038 { 00039 00040 class RuleTableUTrie; 00041 00043 class UTrieNode 00044 { 00045 public: 00046 typedef std::vector<std::vector<Word> > LabelTable; 00047 #if defined(BOOST_VERSION) && (BOOST_VERSION >= 104200) 00048 typedef boost::unordered_map<Word, 00049 UTrieNode, 00050 TerminalHasher, 00051 TerminalEqualityPred> TerminalMap; 00052 00053 typedef boost::unordered_map<std::vector<int>, 00054 TargetPhraseCollection::shared_ptr> LabelMap; 00055 #else 00056 typedef std::map<Word, UTrieNode> TerminalMap; 00057 typedef std::map<std::vector<int>, TargetPhraseCollection::shared_ptr> LabelMap; 00058 #endif 00059 00060 ~UTrieNode() { 00061 delete m_gapNode; 00062 } 00063 00064 const LabelTable &GetLabelTable() const { 00065 return m_labelTable; 00066 } 00067 const LabelMap &GetLabelMap() const { 00068 return m_labelMap; 00069 } 00070 const TerminalMap &GetTerminalMap() const { 00071 return m_terminalMap; 00072 } 00073 00074 const UTrieNode *GetNonTerminalChild() const { 00075 return m_gapNode; 00076 } 00077 00078 UTrieNode *GetOrCreateTerminalChild(const Word &sourceTerm); 00079 UTrieNode *GetOrCreateNonTerminalChild(const Word &targetNonTerm); 00080 00081 TargetPhraseCollection::shared_ptr 00082 GetOrCreateTargetPhraseCollection(const TargetPhrase &); 00083 00084 bool IsLeaf() const { 00085 return m_terminalMap.empty() && m_gapNode == NULL; 00086 } 00087 00088 bool HasRules() const { 00089 return !m_labelMap.empty(); 00090 } 00091 00092 void Prune(size_t tableLimit); 00093 void Sort(size_t tableLimit); 00094 00095 private: 00096 friend class RuleTableUTrie; 00097 00098 UTrieNode() : m_gapNode(NULL) {} 00099 00100 int InsertLabel(int i, const Word &w) { 00101 std::vector<Word> &inner = m_labelTable[i]; 00102 for (size_t j = 0; j < inner.size(); ++j) { 00103 if (inner[j] == w) { 00104 return j; 00105 } 00106 } 00107 inner.push_back(w); 00108 return inner.size()-1; 00109 } 00110 00111 LabelTable m_labelTable; 00112 LabelMap m_labelMap; 00113 TerminalMap m_terminalMap; 00114 UTrieNode *m_gapNode; 00115 }; 00116 00117 } // namespace Moses