00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <algorithm>
00023 #include "TargetPhraseCollection.h"
00024 #include "util/exception.hh"
00025
00026 using namespace std;
00027
00028 namespace Moses
00029 {
00030
00031 bool
00032 CompareTargetPhrase::
00033 operator() (const TargetPhrase *a, const TargetPhrase *b) const
00034 {
00035 return a->GetFutureScore() > b->GetFutureScore();
00036 }
00037
00038 bool
00039 CompareTargetPhrase::
00040 operator() (const TargetPhrase &a, const TargetPhrase &b) const
00041 {
00042 return a.GetFutureScore() > b.GetFutureScore();
00043 }
00044
00045
00046 TargetPhraseCollection::TargetPhraseCollection(const TargetPhraseCollection ©)
00047 {
00048 for (const_iterator iter = copy.begin(); iter != copy.end(); ++iter) {
00049 const TargetPhrase &origTP = **iter;
00050 TargetPhrase *newTP = new TargetPhrase(origTP);
00051 Add(newTP);
00052 }
00053 }
00054
00055 void TargetPhraseCollection::NthElement(size_t tableLimit)
00056 {
00057 CollType::iterator nth;
00058 nth = (tableLimit && tableLimit <= m_collection.size()
00059 ? m_collection.begin() + tableLimit
00060 : m_collection.end());
00061 NTH_ELEMENT4(m_collection.begin(), nth, m_collection.end(), CompareTargetPhrase());
00062 }
00063
00064 void TargetPhraseCollection::Prune(bool adhereTableLimit, size_t tableLimit)
00065 {
00066 NthElement(tableLimit);
00067
00068 if (adhereTableLimit && m_collection.size() > tableLimit) {
00069 for (size_t ind = tableLimit; ind < m_collection.size(); ++ind) {
00070 const TargetPhrase *targetPhrase = m_collection[ind];
00071 delete targetPhrase;
00072 }
00073 m_collection.erase(m_collection.begin() + tableLimit, m_collection.end());
00074 }
00075 }
00076
00077 void TargetPhraseCollection::Sort(bool adhereTableLimit, size_t tableLimit)
00078 {
00079 CollType::iterator iterMiddle;
00080 iterMiddle = (tableLimit == 0 || m_collection.size() < tableLimit)
00081 ? m_collection.end()
00082 : m_collection.begin()+tableLimit;
00083
00084 std::partial_sort(m_collection.begin(), iterMiddle, m_collection.end(),
00085 CompareTargetPhrase());
00086
00087 if (adhereTableLimit && tableLimit && m_collection.size() > tableLimit) {
00088 for (size_t i = tableLimit; i < m_collection.size(); ++i) {
00089 const TargetPhrase *targetPhrase = m_collection[i];
00090 delete targetPhrase;
00091 }
00092 m_collection.erase(m_collection.begin()+tableLimit, m_collection.end());
00093 }
00094 }
00095
00096 std::ostream& operator<<(std::ostream &out, const TargetPhraseCollection &obj)
00097 {
00098 TargetPhraseCollection::const_iterator iter;
00099 for (iter = obj.begin(); iter != obj.end(); ++iter) {
00100 const TargetPhrase &tp = **iter;
00101 out << tp << endl;
00102 }
00103 return out;
00104 }
00105
00106
00107 void TargetPhraseCollectionWithSourcePhrase::Add(TargetPhrase *targetPhrase)
00108 {
00109 UTIL_THROW(util::Exception, "Must use method Add(TargetPhrase*, const Phrase&)");
00110 }
00111
00112 void TargetPhraseCollectionWithSourcePhrase::Add(TargetPhrase *targetPhrase, const Phrase &sourcePhrase)
00113 {
00114 m_collection.push_back(targetPhrase);
00115 m_sourcePhrases.push_back(sourcePhrase);
00116 }
00117
00118 }
00119
00120