moses_chart: simplify ChartTranslationOptionCollection a bit.

This commit is contained in:
Phil Williams 2012-02-06 16:39:30 +00:00
parent 0fcb87629b
commit 7cdc0b26c0
2 changed files with 9 additions and 70 deletions

View File

@ -66,12 +66,12 @@ void ChartTranslationOptionCollection::CreateTranslationOptionsForRange(
ChartTranslationOptionList &chartRuleColl = GetTranslationOptionList(startPos, endPos); ChartTranslationOptionList &chartRuleColl = GetTranslationOptionList(startPos, endPos);
const WordsRange &wordsRange = chartRuleColl.GetSourceRange(); const WordsRange &wordsRange = chartRuleColl.GetSourceRange();
CHECK(m_decodeGraphList.size() == m_ruleLookupManagers.size()); assert(m_decodeGraphList.size() == m_ruleLookupManagers.size());
std::vector <DecodeGraph*>::const_iterator iterDecodeGraph; std::vector <DecodeGraph*>::const_iterator iterDecodeGraph;
std::vector <ChartRuleLookupManager*>::const_iterator iterRuleLookupManagers = m_ruleLookupManagers.begin(); std::vector <ChartRuleLookupManager*>::const_iterator iterRuleLookupManagers = m_ruleLookupManagers.begin();
for (iterDecodeGraph = m_decodeGraphList.begin(); iterDecodeGraph != m_decodeGraphList.end(); ++iterDecodeGraph, ++iterRuleLookupManagers) { for (iterDecodeGraph = m_decodeGraphList.begin(); iterDecodeGraph != m_decodeGraphList.end(); ++iterDecodeGraph, ++iterRuleLookupManagers) {
const DecodeGraph &decodeGraph = **iterDecodeGraph; const DecodeGraph &decodeGraph = **iterDecodeGraph;
CHECK(decodeGraph.GetSize() == 1); assert(decodeGraph.GetSize() == 1);
ChartRuleLookupManager &ruleLookupManager = **iterRuleLookupManagers; ChartRuleLookupManager &ruleLookupManager = **iterRuleLookupManagers;
size_t maxSpan = decodeGraph.GetMaxChartSpan(); size_t maxSpan = decodeGraph.GetMaxChartSpan();
if (maxSpan == 0 || (endPos-startPos+1) <= maxSpan) { if (maxSpan == 0 || (endPos-startPos+1) <= maxSpan) {
@ -79,50 +79,18 @@ void ChartTranslationOptionCollection::CreateTranslationOptionsForRange(
} }
} }
ProcessUnknownWord(startPos, endPos); if (startPos == endPos && startPos != 0 && startPos != m_source.GetSize()-1) {
bool alwaysCreateDirectTranslationOption = StaticData::Instance().IsAlwaysCreateDirectTranslationOption();
Prune(startPos, endPos); if (chartRuleColl.GetSize() == 0 || alwaysCreateDirectTranslationOption) {
// create unknown words for 1 word coverage where we don't have any trans options
Sort(startPos, endPos); const Word &sourceWord = m_source.GetWord(startPos);
ProcessOneUnknownWord(sourceWord, startPos);
}
//! Force a creation of a translation option where there are none for a particular source position.
void ChartTranslationOptionCollection::ProcessUnknownWord(size_t startPos, size_t endPos)
{
if (startPos != endPos) {
// only for 1 word phrases
return;
}
if (startPos == 0 || startPos == m_source.GetSize() - 1)
{ // don't create unknown words for <S> or </S> tags. Otherwise they can be moved. Should only be translated by glue rules
return;
}
ChartTranslationOptionList &fullList = GetTranslationOptionList(startPos, startPos);
const WordsRange &wordsRange = fullList.GetSourceRange();
// try to translation for coverage with no trans by expanding table limit
std::vector <DecodeGraph*>::const_iterator iterDecodeGraph;
std::vector <ChartRuleLookupManager*>::const_iterator iterRuleLookupManagers = m_ruleLookupManagers.begin();
for (iterDecodeGraph = m_decodeGraphList.begin(); iterDecodeGraph != m_decodeGraphList.end(); ++iterDecodeGraph, ++iterRuleLookupManagers) {
//const DecodeGraph &decodeGraph = **iterDecodeGraph;
ChartRuleLookupManager &ruleLookupManager = **iterRuleLookupManagers;
size_t numTransOpt = fullList.GetSize();
if (numTransOpt == 0) {
ruleLookupManager.GetChartRuleCollection(wordsRange, fullList);
} }
} }
CHECK(iterRuleLookupManagers == m_ruleLookupManagers.end());
bool alwaysCreateDirectTranslationOption = StaticData::Instance().IsAlwaysCreateDirectTranslationOption(); Sort(startPos, endPos);
// create unknown words for 1 word coverage where we don't have any trans options
if (fullList.GetSize() == 0 || alwaysCreateDirectTranslationOption)
ProcessUnknownWord(startPos);
} }
ChartTranslationOptionList &ChartTranslationOptionCollection::GetTranslationOptionList(size_t startPos, size_t endPos) ChartTranslationOptionList &ChartTranslationOptionCollection::GetTranslationOptionList(size_t startPos, size_t endPos)
{ {
size_t sizeVec = m_collection[startPos].size(); size_t sizeVec = m_collection[startPos].size();
@ -153,13 +121,6 @@ std::ostream& operator<<(std::ostream &out, const ChartTranslationOptionCollecti
return out; return out;
} }
// taken from ChartTranslationOptionCollectionText.
void ChartTranslationOptionCollection::ProcessUnknownWord(size_t sourcePos)
{
const Word &sourceWord = m_source.GetWord(sourcePos);
ProcessOneUnknownWord(sourceWord,sourcePos);
}
//! special handling of ONE unknown words. //! special handling of ONE unknown words.
void ChartTranslationOptionCollection::ProcessOneUnknownWord(const Word &sourceWord, size_t sourcePos, size_t /* length */) void ChartTranslationOptionCollection::ProcessOneUnknownWord(const Word &sourceWord, size_t sourcePos, size_t /* length */)
{ {
@ -262,18 +223,6 @@ void ChartTranslationOptionCollection::ProcessOneUnknownWord(const Word &sourceW
} }
} }
void ChartTranslationOptionCollection::Add(ChartTranslationOption *transOpt, size_t pos)
{
ChartTranslationOptionList &transOptList = GetTranslationOptionList(pos, pos);
transOptList.Add(transOpt);
}
//! pruning: only keep the top n (m_maxNoTransOptPerCoverage) elements */
void ChartTranslationOptionCollection::Prune(size_t /* startPos */, size_t /* endPos */)
{
}
//! sort all trans opt in each list for cube pruning */ //! sort all trans opt in each list for cube pruning */
void ChartTranslationOptionCollection::Sort(size_t startPos, size_t endPos) void ChartTranslationOptionCollection::Sort(size_t startPos, size_t endPos)
{ {

View File

@ -51,24 +51,14 @@ protected:
std::list<TargetPhraseCollection*> m_cacheTargetPhraseCollection; std::list<TargetPhraseCollection*> m_cacheTargetPhraseCollection;
StackVec m_emptyStackVec; StackVec m_emptyStackVec;
// for adding 1 trans opt in unknown word proc
void Add(ChartTranslationOption *transOpt, size_t pos);
ChartTranslationOptionList &GetTranslationOptionList(size_t startPos, size_t endPos); ChartTranslationOptionList &GetTranslationOptionList(size_t startPos, size_t endPos);
const ChartTranslationOptionList &GetTranslationOptionList(size_t startPos, size_t endPos) const; const ChartTranslationOptionList &GetTranslationOptionList(size_t startPos, size_t endPos) const;
void ProcessUnknownWord(size_t startPos, size_t endPos);
// taken from ChartTranslationOptionCollectionText.
void ProcessUnknownWord(size_t sourcePos);
//! special handling of ONE unknown words. //! special handling of ONE unknown words.
virtual void ProcessOneUnknownWord(const Word &sourceWord virtual void ProcessOneUnknownWord(const Word &sourceWord
, size_t sourcePos, size_t length = 1); , size_t sourcePos, size_t length = 1);
//! pruning: only keep the top n (m_maxNoTransOptPerCoverage) elements */
void Prune(size_t startPos, size_t endPos);
//! sort all trans opt in each list for cube pruning */ //! sort all trans opt in each list for cube pruning */
void Sort(size_t startPos, size_t endPos); void Sort(size_t startPos, size_t endPos);