00001 /*********************************************************************** 00002 Moses - statistical machine translation system 00003 Copyright (C) 2006-2011 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 "DotChart.h" 00023 #include "moses/TranslationModel/PhraseDictionaryNodeMemory.h" 00024 00025 #include <vector> 00026 00027 namespace Moses 00028 { 00029 00032 class DottedRuleInMemory : public DottedRule 00033 { 00034 public: 00035 // used only to init dot stack. 00036 explicit DottedRuleInMemory(const PhraseDictionaryNodeMemory &node) 00037 : DottedRule() 00038 , m_node(node) {} 00039 00040 DottedRuleInMemory(const PhraseDictionaryNodeMemory &node, 00041 const ChartCellLabel &cellLabel, 00042 const DottedRuleInMemory &prev) 00043 : DottedRule(cellLabel, prev) 00044 , m_node(node) {} 00045 00046 const PhraseDictionaryNodeMemory &GetLastNode() const { 00047 return m_node; 00048 } 00049 00050 private: 00051 const PhraseDictionaryNodeMemory &m_node; 00052 }; 00053 00054 typedef std::vector<const DottedRuleInMemory*> DottedRuleList; 00055 typedef std::map<size_t, DottedRuleList> DottedRuleMap; 00056 00057 // Collection of all in-memory DottedRules that share a common start point, 00058 // grouped by end point. Additionally, maintains a list of all 00059 // DottedRules that could be expanded further, i.e. for which the 00060 // corresponding PhraseDictionaryNodeMemory is not a leaf. 00061 class DottedRuleColl 00062 { 00063 protected: 00064 typedef std::vector<DottedRuleList> CollType; 00065 CollType m_coll; 00066 DottedRuleList m_expandableDottedRuleList; 00067 DottedRuleMap m_expandableDottedRuleListTerminalsOnly; 00068 00069 public: 00070 typedef CollType::iterator iterator; 00071 typedef CollType::const_iterator const_iterator; 00072 00073 const_iterator begin() const { 00074 return m_coll.begin(); 00075 } 00076 const_iterator end() const { 00077 return m_coll.end(); 00078 } 00079 iterator begin() { 00080 return m_coll.begin(); 00081 } 00082 iterator end() { 00083 return m_coll.end(); 00084 } 00085 00086 DottedRuleColl(size_t size) 00087 : m_coll(size) { 00088 } 00089 00090 ~DottedRuleColl(); 00091 00092 const DottedRuleList &Get(size_t pos) const { 00093 return m_coll[pos]; 00094 } 00095 DottedRuleList &Get(size_t pos) { 00096 return m_coll[pos]; 00097 } 00098 00099 void Add(size_t pos, const DottedRuleInMemory *dottedRule) { 00100 UTIL_THROW_IF2(dottedRule == NULL, "Dotted rule is null"); 00101 m_coll[pos].push_back(dottedRule); 00102 if (!dottedRule->GetLastNode().IsLeaf()) { 00103 if (dottedRule->GetLastNode().GetNonTerminalMap().empty() && !dottedRule->IsRoot()) { 00104 size_t startPos = dottedRule->GetWordsRange().GetEndPos() + 1; 00105 m_expandableDottedRuleListTerminalsOnly[startPos].push_back(dottedRule); 00106 } else { 00107 m_expandableDottedRuleList.push_back(dottedRule); 00108 } 00109 } 00110 } 00111 00112 void Clear(size_t pos) { 00113 #ifdef USE_BOOST_POOL 00114 m_coll[pos].clear(); 00115 #endif 00116 } 00117 00118 const DottedRuleList &GetExpandableDottedRuleList() const { 00119 return m_expandableDottedRuleList; 00120 } 00121 00122 DottedRuleMap &GetExpandableDottedRuleListTerminalsOnly() { 00123 return m_expandableDottedRuleListTerminalsOnly; 00124 } 00125 00126 }; 00127 00128 }