|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream> |
|
|
#include "CompletedRuleCollection.h" |
|
|
|
|
|
#include "moses/StaticData.h" |
|
|
|
|
|
using namespace std; |
|
|
|
|
|
namespace Moses |
|
|
{ |
|
|
|
|
|
CompletedRuleCollection::CompletedRuleCollection(size_t rule_limit) |
|
|
: m_ruleLimit(rule_limit) |
|
|
{ |
|
|
m_scoreThreshold = numeric_limits<float>::infinity(); |
|
|
} |
|
|
|
|
|
CompletedRuleCollection::~CompletedRuleCollection() |
|
|
{ |
|
|
Clear(); |
|
|
} |
|
|
|
|
|
|
|
|
void CompletedRuleCollection::Add(const TargetPhraseCollection &tpc, |
|
|
const StackVec &stackVec, |
|
|
const ChartParserCallback &outColl) |
|
|
{ |
|
|
if (tpc.IsEmpty()) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const TargetPhrase &targetPhrase = **(tpc.begin()); |
|
|
float score = targetPhrase.GetFutureScore(); |
|
|
for (StackVec::const_iterator p = stackVec.begin(); p != stackVec.end(); ++p) { |
|
|
float stackScore = (*p)->GetBestScore(&outColl); |
|
|
score += stackScore; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (m_ruleLimit && m_collection.size() > m_ruleLimit && score < m_scoreThreshold) { |
|
|
return; |
|
|
} |
|
|
|
|
|
CompletedRule *completedRule = new CompletedRule(tpc, stackVec, score); |
|
|
m_collection.push_back(completedRule); |
|
|
|
|
|
|
|
|
if (!m_ruleLimit || m_collection.size() <= m_ruleLimit) { |
|
|
m_scoreThreshold = (score < m_scoreThreshold) ? score : m_scoreThreshold; |
|
|
} |
|
|
|
|
|
|
|
|
if (m_ruleLimit && m_collection.size() == m_ruleLimit * 2) { |
|
|
NTH_ELEMENT4(m_collection.begin(), |
|
|
m_collection.begin() + m_ruleLimit - 1, |
|
|
m_collection.end(), |
|
|
CompletedRuleOrdered()); |
|
|
m_scoreThreshold = m_collection[m_ruleLimit-1]->GetScoreEstimate(); |
|
|
for (size_t i = 0 + m_ruleLimit; i < m_collection.size(); i++) { |
|
|
delete m_collection[i]; |
|
|
|
|
|
} |
|
|
m_collection.resize(m_ruleLimit); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void CompletedRuleCollection::Add(const TargetPhraseCollection &tpc, |
|
|
const StackVec &stackVec, |
|
|
const std::vector<float> &stackScores, |
|
|
const ChartParserCallback &outColl) |
|
|
{ |
|
|
if (tpc.IsEmpty()) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const TargetPhrase &targetPhrase = **(tpc.begin()); |
|
|
float score = std::accumulate(stackScores.begin(), stackScores.end(), targetPhrase.GetFutureScore()); |
|
|
|
|
|
|
|
|
|
|
|
if (m_collection.size() > m_ruleLimit && score < m_scoreThreshold) { |
|
|
return; |
|
|
} |
|
|
|
|
|
CompletedRule *completedRule = new CompletedRule(tpc, stackVec, score); |
|
|
m_collection.push_back(completedRule); |
|
|
|
|
|
|
|
|
if (m_collection.size() <= m_ruleLimit) { |
|
|
m_scoreThreshold = (score < m_scoreThreshold) ? score : m_scoreThreshold; |
|
|
} |
|
|
|
|
|
|
|
|
if (m_collection.size() == m_ruleLimit * 2) { |
|
|
NTH_ELEMENT4(m_collection.begin(), |
|
|
m_collection.begin() + m_ruleLimit - 1, |
|
|
m_collection.end(), |
|
|
CompletedRuleOrdered()); |
|
|
m_scoreThreshold = m_collection[m_ruleLimit-1]->GetScoreEstimate(); |
|
|
for (size_t i = 0 + m_ruleLimit; i < m_collection.size(); i++) { |
|
|
delete m_collection[i]; |
|
|
|
|
|
} |
|
|
m_collection.resize(m_ruleLimit); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|