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 #include "PartialTranslOptColl.h" 00023 #include <algorithm> 00024 00025 namespace Moses 00026 { 00028 PartialTranslOptColl::PartialTranslOptColl() 00029 { 00030 m_bestScore = -std::numeric_limits<float>::infinity(); 00031 m_worstScore = -std::numeric_limits<float>::infinity(); 00032 m_maxSize = StaticData::Instance().GetMaxNoPartTransOpt(); 00033 m_totalPruned = 0; 00034 } 00035 00036 00038 void PartialTranslOptColl::AddNoPrune(const TranslationSystem* system, TranslationOption *partialTranslOpt) 00039 { 00040 partialTranslOpt->CalcScore(system); 00041 if (partialTranslOpt->GetFutureScore() >= m_worstScore) { 00042 m_list.push_back(partialTranslOpt); 00043 if (partialTranslOpt->GetFutureScore() > m_bestScore) 00044 m_bestScore = partialTranslOpt->GetFutureScore(); 00045 } else { 00046 m_totalPruned++; 00047 delete partialTranslOpt; 00048 } 00049 } 00050 00054 void PartialTranslOptColl::Add(const TranslationSystem* system, TranslationOption *partialTranslOpt) 00055 { 00056 // add 00057 AddNoPrune(system,partialTranslOpt ); 00058 00059 // done if not too large (lazy pruning, only if twice as large as max) 00060 if ( m_list.size() > 2 * m_maxSize ) { 00061 Prune(); 00062 } 00063 } 00064 00065 00067 bool ComparePartialTranslationOption(const TranslationOption *a, const TranslationOption *b) 00068 { 00069 return a->GetFutureScore() > b->GetFutureScore(); 00070 } 00071 00073 void PartialTranslOptColl::Prune() 00074 { 00075 // done if not too big 00076 if ( m_list.size() <= m_maxSize ) { 00077 return; 00078 } 00079 00080 // TRACE_ERR( "pruning partial translation options from size " << m_list.size() << std::endl); 00081 00082 // find nth element 00083 nth_element(m_list.begin(), 00084 m_list.begin() + m_maxSize, 00085 m_list.end(), 00086 ComparePartialTranslationOption); 00087 00088 m_worstScore = m_list[ m_maxSize-1 ]->GetFutureScore(); 00089 // delete the rest 00090 for (size_t i = m_maxSize ; i < m_list.size() ; ++i) { 00091 delete m_list[i]; 00092 m_totalPruned++; 00093 } 00094 m_list.resize(m_maxSize); 00095 // TRACE_ERR( "pruned to size " << m_list.size() << ", total pruned: " << m_totalPruned << std::endl); 00096 } 00097 00098 } 00099 00100
1.5.9