mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2025-01-08 12:36:35 +03:00
Use selection algorithm (O(N)) instead of sort (O(NlogN)) to filter translation options, fix segv
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@407 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
parent
38aea6cb7f
commit
ac9dffb069
@ -54,6 +54,9 @@ StaticData::StaticData()
|
||||
,m_fLMsLoaded(false)
|
||||
,m_inputType(0)
|
||||
,m_numInputScores(0)
|
||||
,m_distortionScoreProducer(0)
|
||||
,m_wpProducer(0)
|
||||
|
||||
{
|
||||
s_instance = this;
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#include "Input.h"
|
||||
#include "Util.h"
|
||||
|
||||
#include "StaticData.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
TranslationOptionCollection::TranslationOptionCollection(InputType const& src, size_t maxNoTransOptPerCoverage)
|
||||
@ -71,34 +73,37 @@ void TranslationOptionCollection::Prune()
|
||||
{
|
||||
if (m_maxNoTransOptPerCoverage == 0)
|
||||
return;
|
||||
|
||||
|
||||
size_t total = 0;
|
||||
size_t totalPruned = 0;
|
||||
size_t size = m_source.GetSize();
|
||||
for (size_t startPos = 0 ; startPos < size ; ++startPos)
|
||||
{
|
||||
for (size_t endPos = startPos ; endPos < size ; ++endPos)
|
||||
{
|
||||
TranslationOptionList &fullList = GetTranslationOptionList(startPos, endPos);
|
||||
total += fullList.size();
|
||||
if (fullList.size() <= m_maxNoTransOptPerCoverage)
|
||||
continue;
|
||||
|
||||
// sort in vector
|
||||
vector<const TranslationOption*> sortedVector;
|
||||
copy(fullList.begin(), fullList.end(), back_inserter(sortedVector));
|
||||
sort(sortedVector.begin(), sortedVector.end(), CompareTranslationOption);
|
||||
|
||||
// put back into list
|
||||
fullList.clear();
|
||||
const size_t maxIndex = std::min(m_maxNoTransOptPerCoverage, sortedVector.size());
|
||||
for (size_t i = 0 ; i < maxIndex ; ++i)
|
||||
{
|
||||
fullList.push_back(sortedVector[i]);
|
||||
}
|
||||
nth_element(fullList.begin(), fullList.begin() + m_maxNoTransOptPerCoverage, fullList.end(), CompareTranslationOption);
|
||||
|
||||
totalPruned += fullList.size() - m_maxNoTransOptPerCoverage;
|
||||
|
||||
// delete the rest
|
||||
for (size_t i = maxIndex ; i < sortedVector.size() ; ++i)
|
||||
for (size_t i = m_maxNoTransOptPerCoverage ; i < fullList.size() ; ++i)
|
||||
{
|
||||
delete sortedVector[i];
|
||||
delete fullList[i];
|
||||
}
|
||||
fullList.resize(m_maxNoTransOptPerCoverage);
|
||||
}
|
||||
}
|
||||
if (StaticData::Instance()->GetVerboseLevel() >= 1)
|
||||
{
|
||||
std::cerr << " Total translation options: " << total << std::endl;
|
||||
std::cerr << "Total translation options pruned: " << totalPruned << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void TranslationOptionCollection::CalcFutureScore(size_t verboseLevel)
|
||||
|
@ -37,7 +37,7 @@ class GenerationDictionary;
|
||||
class InputType;
|
||||
class LMList;
|
||||
|
||||
typedef std::list<const TranslationOption*> TranslationOptionList;
|
||||
typedef std::vector<const TranslationOption*> TranslationOptionList;
|
||||
|
||||
class TranslationOptionCollection
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user