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 #include <iostream> 00025 00026 using namespace std; 00027 00028 namespace Moses 00029 { 00031 PartialTranslOptColl::PartialTranslOptColl(size_t const maxSize) 00032 { 00033 m_bestScore = -std::numeric_limits<float>::infinity(); 00034 m_worstScore = -std::numeric_limits<float>::infinity(); 00035 m_maxSize = maxSize; // StaticData::Instance().GetMaxNoPartTransOpt(); 00036 m_totalPruned = 0; 00037 } 00038 00039 00041 void PartialTranslOptColl::AddNoPrune(TranslationOption *partialTranslOpt) 00042 { 00043 if (partialTranslOpt->GetFutureScore() >= m_worstScore) { 00044 m_list.push_back(partialTranslOpt); 00045 if (partialTranslOpt->GetFutureScore() > m_bestScore) 00046 m_bestScore = partialTranslOpt->GetFutureScore(); 00047 } else { 00048 m_totalPruned++; 00049 delete partialTranslOpt; 00050 } 00051 } 00052 00056 void PartialTranslOptColl::Add(TranslationOption *partialTranslOpt) 00057 { 00058 // add 00059 AddNoPrune(partialTranslOpt ); 00060 00061 // done if not too large (lazy pruning, only if twice as large as max) 00062 if ( m_list.size() > 2 * m_maxSize ) { 00063 Prune(); 00064 } 00065 } 00066 00067 00069 bool ComparePartialTranslationOption(const TranslationOption *a, const TranslationOption *b) 00070 { 00071 return a->GetFutureScore() > b->GetFutureScore(); 00072 } 00073 00075 void PartialTranslOptColl::Prune() 00076 { 00077 // done if not too big 00078 if ( m_list.size() <= m_maxSize ) { 00079 return; 00080 } 00081 00082 // TRACE_ERR( "pruning partial translation options from size " << m_list.size() << std::endl); 00083 00084 // find nth element 00085 NTH_ELEMENT4(m_list.begin(), 00086 m_list.begin() + m_maxSize, 00087 m_list.end(), 00088 ComparePartialTranslationOption); 00089 00090 m_worstScore = m_list[ m_maxSize-1 ]->GetFutureScore(); 00091 // delete the rest 00092 for (size_t i = m_maxSize ; i < m_list.size() ; ++i) { 00093 delete m_list[i]; 00094 m_totalPruned++; 00095 } 00096 m_list.resize(m_maxSize); 00097 // TRACE_ERR( "pruned to size " << m_list.size() << ", total pruned: " << m_totalPruned << std::endl); 00098 } 00099 00100 // friend 00101 ostream& operator<<(ostream& out, const PartialTranslOptColl& possibleTranslation) 00102 { 00103 for (size_t i = 0; i < possibleTranslation.m_list.size(); ++i) { 00104 const TranslationOption &transOpt = *possibleTranslation.m_list[i]; 00105 out << transOpt << endl; 00106 } 00107 return out; 00108 } 00109 00110 } 00111 00112