00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021 #include "CompletedRuleCollection.h"
00022
00023 #include "moses/StaticData.h"
00024
00025 using namespace std;
00026
00027 namespace Moses
00028 {
00029
00030 CompletedRuleCollection::CompletedRuleCollection(size_t rule_limit)
00031 : m_ruleLimit(rule_limit)
00032 {
00033 m_scoreThreshold = numeric_limits<float>::infinity();
00034 }
00035
00036 CompletedRuleCollection::~CompletedRuleCollection()
00037 {
00038 Clear();
00039 }
00040
00041
00042 void CompletedRuleCollection::Add(const TargetPhraseCollection &tpc,
00043 const StackVec &stackVec,
00044 const ChartParserCallback &outColl)
00045 {
00046 if (tpc.IsEmpty()) {
00047 return;
00048 }
00049
00050 const TargetPhrase &targetPhrase = **(tpc.begin());
00051 float score = targetPhrase.GetFutureScore();
00052 for (StackVec::const_iterator p = stackVec.begin(); p != stackVec.end(); ++p) {
00053 float stackScore = (*p)->GetBestScore(&outColl);
00054 score += stackScore;
00055 }
00056
00057
00058
00059 if (m_ruleLimit && m_collection.size() > m_ruleLimit && score < m_scoreThreshold) {
00060 return;
00061 }
00062
00063 CompletedRule *completedRule = new CompletedRule(tpc, stackVec, score);
00064 m_collection.push_back(completedRule);
00065
00066
00067 if (!m_ruleLimit || m_collection.size() <= m_ruleLimit) {
00068 m_scoreThreshold = (score < m_scoreThreshold) ? score : m_scoreThreshold;
00069 }
00070
00071
00072 if (m_ruleLimit && m_collection.size() == m_ruleLimit * 2) {
00073 NTH_ELEMENT4(m_collection.begin(),
00074 m_collection.begin() + m_ruleLimit - 1,
00075 m_collection.end(),
00076 CompletedRuleOrdered());
00077 m_scoreThreshold = m_collection[m_ruleLimit-1]->GetScoreEstimate();
00078 for (size_t i = 0 + m_ruleLimit; i < m_collection.size(); i++) {
00079 delete m_collection[i];
00080
00081 }
00082 m_collection.resize(m_ruleLimit);
00083 }
00084 }
00085
00086
00087
00088 void CompletedRuleCollection::Add(const TargetPhraseCollection &tpc,
00089 const StackVec &stackVec,
00090 const std::vector<float> &stackScores,
00091 const ChartParserCallback &outColl)
00092 {
00093 if (tpc.IsEmpty()) {
00094 return;
00095 }
00096
00097 const TargetPhrase &targetPhrase = **(tpc.begin());
00098 float score = std::accumulate(stackScores.begin(), stackScores.end(), targetPhrase.GetFutureScore());
00099
00100
00101
00102 if (m_collection.size() > m_ruleLimit && score < m_scoreThreshold) {
00103 return;
00104 }
00105
00106 CompletedRule *completedRule = new CompletedRule(tpc, stackVec, score);
00107 m_collection.push_back(completedRule);
00108
00109
00110 if (m_collection.size() <= m_ruleLimit) {
00111 m_scoreThreshold = (score < m_scoreThreshold) ? score : m_scoreThreshold;
00112 }
00113
00114
00115 if (m_collection.size() == m_ruleLimit * 2) {
00116 NTH_ELEMENT4(m_collection.begin(),
00117 m_collection.begin() + m_ruleLimit - 1,
00118 m_collection.end(),
00119 CompletedRuleOrdered());
00120 m_scoreThreshold = m_collection[m_ruleLimit-1]->GetScoreEstimate();
00121 for (size_t i = 0 + m_ruleLimit; i < m_collection.size(); i++) {
00122 delete m_collection[i];
00123
00124 }
00125 m_collection.resize(m_ruleLimit);
00126 }
00127 }
00128
00129 }