00001 // $Id$ 00002 00003 /*********************************************************************** 00004 Moses - factored phrase-based language decoder 00005 Copyright (C) 2006 University of Edinburgh 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2.1 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 ***********************************************************************/ 00021 00022 #ifndef moses_HypothesisStackCubePruning_h 00023 #define moses_HypothesisStackCubePruning_h 00024 00025 #include <limits> 00026 #include <set> 00027 #include <boost/unordered_map.hpp> 00028 #include "Hypothesis.h" 00029 #include "BitmapContainer.h" 00030 #include "HypothesisStack.h" 00031 #include "Util.h" 00032 00033 namespace Moses 00034 { 00035 00036 class BitmapContainer; 00037 class TranslationOptionList; 00038 class Manager; 00039 00040 //typedef boost::unordered_map<Bitmap, BitmapContainer*, UnorderedComparer<Bitmap>, UnorderedComparer<Bitmap> > _BMType; 00041 typedef boost::unordered_map<const Bitmap*, BitmapContainer*> _BMType; 00042 // can compare Bitmap* 'cos all bitmaps are created from bitmaps factory class. MUST ensure this is the case 00043 00045 class HypothesisStackCubePruning : public HypothesisStack 00046 { 00047 public: 00048 friend std::ostream& operator<<(std::ostream&, const HypothesisStackCubePruning&); 00049 00050 protected: 00051 _BMType m_bitmapAccessor; 00052 00053 float m_bestScore; 00054 float m_worstScore; 00055 float m_beamWidth; 00056 size_t m_maxHypoStackSize; 00057 bool m_nBestIsEnabled; 00058 bool m_deterministic; 00063 std::pair<HypothesisStackCubePruning::iterator, bool> Add(Hypothesis *hypothesis); 00064 00066 void RemoveAll(); 00067 00068 BitmapContainer *AddBitmapContainer(const Bitmap &bitmap, HypothesisStackCubePruning &stack); 00069 00070 public: 00071 HypothesisStackCubePruning(Manager& manager); 00072 ~HypothesisStackCubePruning() { 00073 RemoveAll(); 00074 m_bitmapAccessor.clear(); 00075 } 00076 00085 bool AddPrune(Hypothesis *hypothesis); 00086 00087 void AddInitial(Hypothesis *hypo); 00088 00092 inline void SetMaxHypoStackSize(size_t maxHypoStackSize) { 00093 m_maxHypoStackSize = maxHypoStackSize; 00094 } 00095 00096 inline size_t GetMaxHypoStackSize() const { 00097 return m_maxHypoStackSize; 00098 } 00099 00104 inline void SetBeamWidth(float beamWidth) { 00105 m_beamWidth = beamWidth; 00106 } 00107 00109 inline float GetBestScore() const { 00110 return m_bestScore; 00111 } 00112 00114 inline float GetWorstScore() const { 00115 return m_worstScore; 00116 } 00117 00118 void AddHypothesesToBitmapContainers(); 00119 00120 const _BMType& GetBitmapAccessor() const { 00121 return m_bitmapAccessor; 00122 } 00123 00124 void SetBitmapAccessor(const Bitmap &newBitmap 00125 , HypothesisStackCubePruning &stack 00126 , const Range &range 00127 , BitmapContainer &bitmapContainer 00128 , const SquareMatrix &estimatedScores 00129 , const TranslationOptionList &transOptList); 00130 00137 void PruneToSize(size_t newSize); 00138 00140 const Hypothesis *GetBestHypothesis() const; 00142 std::vector<const Hypothesis*> GetSortedList() const; 00143 00147 void CleanupArcList(); 00148 00149 TO_STRING(); 00150 }; 00151 00152 } 00153 #endif