diff options
author | Hieu Hoang <hieu@hoang.co.uk> | 2013-06-28 18:43:56 +0400 |
---|---|---|
committer | Hieu Hoang <hieu@hoang.co.uk> | 2013-06-28 18:43:56 +0400 |
commit | 2b357f91e3ad1a0688a9c16e71632c31018a8d68 (patch) | |
tree | c92fbf039e29d2c0d53d265504da7d61b6350c69 /moses/TranslationOptionCollectionText.cpp | |
parent | fd1a70739ff7e5bb771156d869bb59148f137ba6 (diff) |
separate CreateTranslationOptionsForRange() for text and confusion network input
Diffstat (limited to 'moses/TranslationOptionCollectionText.cpp')
-rw-r--r-- | moses/TranslationOptionCollectionText.cpp | 115 |
1 files changed, 114 insertions, 1 deletions
diff --git a/moses/TranslationOptionCollectionText.cpp b/moses/TranslationOptionCollectionText.cpp index 9920ac275..ebd35a876 100644 --- a/moses/TranslationOptionCollectionText.cpp +++ b/moses/TranslationOptionCollectionText.cpp @@ -22,8 +22,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "TranslationOptionCollectionText.h" #include "Sentence.h" #include "DecodeStep.h" +#include "DecodeStepTranslation.h" #include "FactorCollection.h" #include "WordsRange.h" +#include <list> using namespace std; @@ -46,7 +48,6 @@ TranslationOptionCollectionText::TranslationOptionCollectionText(Sentence const } } - for (size_t phaseSize = 1; phaseSize <= size; ++phaseSize) { for (size_t startPos = 0; startPos < size - phaseSize + 1; ++startPos) { size_t endPos = startPos + phaseSize -1; @@ -101,6 +102,118 @@ InputLatticeNode &TranslationOptionCollectionText::GetInputLatticeNode(size_t st return m_targetPhrasesfromPt[startPos][offset]; } +/** create translation options that exactly cover a specific input span. + * Called by CreateTranslationOptions() and ProcessUnknownWord() + * \param decodeGraph list of decoding steps + * \param factorCollection input sentence with all factors + * \param startPos first position in input sentence + * \param lastPos last position in input sentence + * \param adhereTableLimit whether phrase & generation table limits are adhered to + */ +void TranslationOptionCollectionText::CreateTranslationOptionsForRange( + const DecodeGraph &decodeGraph + , size_t startPos + , size_t endPos + , bool adhereTableLimit + , size_t graphInd) +{ + if ((StaticData::Instance().GetXmlInputType() != XmlExclusive) || !HasXmlOptionsOverlappingRange(startPos,endPos)) { + Phrase *sourcePhrase = NULL; // can't initialise with substring, in case it's confusion network + + // consult persistent (cross-sentence) cache for stored translation options + bool skipTransOptCreation = false + , useCache = StaticData::Instance().GetUseTransOptCache(); + if (useCache) { + const WordsRange wordsRange(startPos, endPos); + sourcePhrase = new Phrase(m_source.GetSubString(wordsRange)); + + const TranslationOptionList *transOptList = StaticData::Instance().FindTransOptListInCache(decodeGraph, *sourcePhrase); + // is phrase in cache? + if (transOptList != NULL) { + skipTransOptCreation = true; + TranslationOptionList::const_iterator iterTransOpt; + for (iterTransOpt = transOptList->begin() ; iterTransOpt != transOptList->end() ; ++iterTransOpt) { + TranslationOption *transOpt = new TranslationOption(**iterTransOpt, wordsRange); + Add(transOpt); + } + } + } // useCache + + if (!skipTransOptCreation) { + // partial trans opt stored in here + PartialTranslOptColl* oldPtoc = new PartialTranslOptColl; + size_t totalEarlyPruned = 0; + + // initial translation step + list <const DecodeStep* >::const_iterator iterStep = decodeGraph.begin(); + const DecodeStep &decodeStep = **iterStep; + + static_cast<const DecodeStepTranslation&>(decodeStep).ProcessInitialTranslation + (m_source, *oldPtoc + , startPos, endPos, adhereTableLimit ); + + // do rest of decode steps + int indexStep = 0; + + for (++iterStep ; iterStep != decodeGraph.end() ; ++iterStep) { + + const DecodeStep &decodeStep = **iterStep; + PartialTranslOptColl* newPtoc = new PartialTranslOptColl; + + // go thru each intermediate trans opt just created + const vector<TranslationOption*>& partTransOptList = oldPtoc->GetList(); + vector<TranslationOption*>::const_iterator iterPartialTranslOpt; + for (iterPartialTranslOpt = partTransOptList.begin() ; iterPartialTranslOpt != partTransOptList.end() ; ++iterPartialTranslOpt) { + TranslationOption &inputPartialTranslOpt = **iterPartialTranslOpt; + + decodeStep.Process(inputPartialTranslOpt + , decodeStep + , *newPtoc + , this + , adhereTableLimit + , *sourcePhrase); + } + + // last but 1 partial trans not required anymore + totalEarlyPruned += newPtoc->GetPrunedCount(); + delete oldPtoc; + oldPtoc = newPtoc; + + indexStep++; + } // for (++iterStep + + // add to fully formed translation option list + PartialTranslOptColl &lastPartialTranslOptColl = *oldPtoc; + const vector<TranslationOption*>& partTransOptList = lastPartialTranslOptColl.GetList(); + vector<TranslationOption*>::const_iterator iterColl; + for (iterColl = partTransOptList.begin() ; iterColl != partTransOptList.end() ; ++iterColl) { + TranslationOption *transOpt = *iterColl; + Add(transOpt); + } + + // storing translation options in persistent cache (kept across sentences) + if (useCache) { + if (partTransOptList.size() > 0) { + TranslationOptionList &transOptList = GetTranslationOptionList(startPos, endPos); + StaticData::Instance().AddTransOptListToCache(decodeGraph, *sourcePhrase, transOptList); + } + } + + lastPartialTranslOptColl.DetachAll(); + totalEarlyPruned += oldPtoc->GetPrunedCount(); + delete oldPtoc; + // TRACE_ERR( "Early translation options pruned: " << totalEarlyPruned << endl); + } // if (!skipTransOptCreation) + + if (useCache) + delete sourcePhrase; + } // if ((StaticData::Instance().GetXmlInputType() != XmlExclusive) || !HasXmlOptionsOverlappingRange(startPos,endPos)) + + if (graphInd == 0 && StaticData::Instance().GetXmlInputType() != XmlPassThrough && HasXmlOptionsOverlappingRange(startPos,endPos)) { + CreateXmlOptionsForRange(startPos, endPos); + } +} + } |