00001 #include "TranslationOptionList.h" 00002 #include "Util.h" 00003 #include "TranslationOption.h" 00004 #include <boost/foreach.hpp> 00005 00006 using namespace std; 00007 00008 namespace Moses 00009 { 00010 00011 TranslationOptionList:: 00012 TranslationOptionList(const TranslationOptionList ©) 00013 { 00014 const_iterator iter; 00015 for (iter = copy.begin(); iter != copy.end(); ++iter) { 00016 const TranslationOption &origTransOpt = **iter; 00017 TranslationOption *newTransOpt = new TranslationOption(origTransOpt); 00018 Add(newTransOpt); 00019 } 00020 } 00021 00022 TranslationOptionList:: 00023 ~TranslationOptionList() 00024 { 00025 RemoveAllInColl(m_coll); 00026 } 00027 00028 TO_STRING_BODY(TranslationOptionList); 00029 00030 std::ostream& operator<<(std::ostream& out, const TranslationOptionList& coll) 00031 { 00032 TranslationOptionList::const_iterator iter; 00033 for (iter = coll.begin(); iter != coll.end(); ++iter) { 00034 const TranslationOption &transOpt = **iter; 00035 out << transOpt << endl; 00036 } 00037 00038 return out; 00039 } 00040 00041 size_t 00042 TranslationOptionList:: 00043 SelectNBest(size_t const N) 00044 { 00045 if (N == 0 || N >= m_coll.size()) return 0; 00046 static TranslationOption::Better cmp; 00047 NTH_ELEMENT4(m_coll.begin(), m_coll.begin() + N, m_coll.end(), cmp); 00048 // delete the rest 00049 for (size_t i = N ; i < m_coll.size() ; ++i) delete m_coll[i]; 00050 size_t ret = m_coll.size() - N; 00051 m_coll.resize(N); 00052 return ret; 00053 } 00054 00055 size_t 00056 TranslationOptionList:: 00057 PruneByThreshold(float const th) 00058 { 00059 if (m_coll.size() <= 1) return 0; 00060 if (th == -std::numeric_limits<float>::infinity()) return 0; 00061 00062 // first, find the best score 00063 float bestScore = -std::numeric_limits<float>::infinity(); 00064 BOOST_FOREACH(TranslationOption const* t, m_coll) { 00065 if (t->GetFutureScore() > bestScore) 00066 bestScore = t->GetFutureScore(); 00067 } 00068 00069 size_t old_size = m_coll.size(); 00070 00071 // then, remove items that are worse than best score + threshold 00072 // why '+' th ??? Does this ever hold? 00073 for (size_t i=0; i < m_coll.size() ; ++i) { 00074 if (m_coll[i]->GetFutureScore() < bestScore + th) { 00075 delete m_coll[i]; 00076 if(i + 1 < m_coll.size()) 00077 std::swap(m_coll[i],m_coll.back()); 00078 m_coll.pop_back(); 00079 } 00080 } 00081 00082 m_coll.resize(m_coll.size()); 00083 return old_size - m_coll.size(); 00084 } 00085 00086 00087 } // namespace