Skip to content

Commit

Permalink
Move sentence-specific rule lookup state out of PhraseDictionarySCFG and
Browse files Browse the repository at this point in the history
PhraseDictionaryOnDisk and into ChartRuleLookupManager.



git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3842 1f5c12ca-751b-0410-a591-d2e778427230
  • Loading branch information
pjwilliams committed Jan 24, 2011
1 parent f5cc2c4 commit ce4cc28
Show file tree
Hide file tree
Showing 20 changed files with 699 additions and 456 deletions.
15 changes: 13 additions & 2 deletions moses-chart/src/ChartManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,28 @@ namespace MosesChart
Manager::Manager(InputType const& source, const TranslationSystem* system)
:m_source(source)
,m_hypoStackColl(source, *this)
,m_transOptColl(source, system, m_hypoStackColl)
,m_transOptColl(source, system, m_hypoStackColl, m_ruleLookupManagers)
,m_system(system)
,m_start(clock())

{
m_system->InitializeBeforeSentenceProcessing(source);
const std::vector<PhraseDictionaryFeature*> &dictionaries = m_system->GetPhraseDictionaries();
m_ruleLookupManagers.reserve(dictionaries.size());
for (std::vector<PhraseDictionaryFeature*>::const_iterator p = dictionaries.begin();
p != dictionaries.end(); ++p)
{
PhraseDictionaryFeature *pdf = *p;
const PhraseDictionary *dict = pdf->GetDictionary();
PhraseDictionary *nonConstDict = const_cast<PhraseDictionary*>(dict);
m_ruleLookupManagers.push_back(nonConstDict->CreateRuleLookupManager(source, m_hypoStackColl));
}
}

Manager::~Manager()
{
m_system->CleanUpAfterSentenceProcessing();

RemoveAllInColl(m_ruleLookupManagers);

clock_t end = clock();
float et = (end - m_start);
Expand Down
2 changes: 2 additions & 0 deletions moses-chart/src/ChartManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../../moses/src/TrellisPathList.h"
#include "../../moses/src/SentenceStats.h"
#include "../../moses/src/TranslationSystem.h"
#include "../../moses/src/ChartRuleLookupManager.h"

namespace MosesChart
{
Expand All @@ -46,6 +47,7 @@ class Manager
std::auto_ptr<Moses::SentenceStats> m_sentenceStats;
const Moses::TranslationSystem* m_system;
clock_t m_start; /**< starting time, used for logging */
std::vector<Moses::ChartRuleLookupManager*> m_ruleLookupManagers;

public:
Manager(Moses::InputType const& source, const Moses::TranslationSystem* system);
Expand Down
50 changes: 18 additions & 32 deletions moses-chart/src/ChartTranslationOptionCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ namespace MosesChart

TranslationOptionCollection::TranslationOptionCollection(InputType const& source
, const Moses::TranslationSystem* system
, const ChartCellCollection &hypoStackColl)
, const ChartCellCollection &hypoStackColl
, const std::vector<ChartRuleLookupManager*> &ruleLookupManagers)
:m_source(source)
,m_system(system)
,m_decodeGraphList(system->GetDecodeGraphs())
,m_hypoStackColl(hypoStackColl)
,m_collection(source.GetSize())
,m_ruleLookupManagers(ruleLookupManagers)
{
// create 2-d vector
size_t size = source.GetSize();
Expand Down Expand Up @@ -76,14 +78,21 @@ void TranslationOptionCollection::CreateTranslationOptionsForRange(
size_t startPos
, size_t endPos)
{
ChartTranslationOptionList &chartRuleColl = GetTranslationOptionList(startPos, endPos);
const WordsRange &wordsRange = chartRuleColl.GetSourceRange();

assert(m_decodeGraphList.size() == m_ruleLookupManagers.size());
std::vector <DecodeGraph*>::const_iterator iterDecodeGraph;
for (iterDecodeGraph = m_decodeGraphList.begin(); iterDecodeGraph != m_decodeGraphList.end(); ++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;
assert(decodeGraph.GetSize() == 1);
ChartRuleLookupManager &ruleLookupManager = **iterRuleLookupManagers;
size_t maxSpan = decodeGraph.GetMaxChartSpan();
if (maxSpan == 0 || (endPos-startPos+1) <= maxSpan)
{
CreateTranslationOptionsForRange(decodeGraph, startPos, endPos, true);
ruleLookupManager.GetChartRuleCollection(wordsRange, true, chartRuleColl);
}
}

Expand All @@ -104,18 +113,22 @@ void TranslationOptionCollection::ProcessUnknownWord(size_t startPos, size_t end
}

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;
for (iterDecodeGraph = m_decodeGraphList.begin(); iterDecodeGraph != m_decodeGraphList.end(); ++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)
{
CreateTranslationOptionsForRange(decodeGraph, startPos, startPos, false);
ruleLookupManager.GetChartRuleCollection(wordsRange, false, fullList);
}
}
assert(iterRuleLookupManagers == m_ruleLookupManagers.end());

bool alwaysCreateDirectTranslationOption = StaticData::Instance().IsAlwaysCreateDirectTranslationOption();
// create unknown words for 1 word coverage where we don't have any trans options
Expand All @@ -124,33 +137,6 @@ void TranslationOptionCollection::ProcessUnknownWord(size_t startPos, size_t end
}


void TranslationOptionCollection::CreateTranslationOptionsForRange(
const DecodeGraph &decodeGraph
, size_t startPos
, size_t endPos
, bool adhereTableLimit)
{
assert(decodeGraph.GetSize() == 1);
const DecodeStep &decodeStep = **decodeGraph.begin();

// get wordsrange that doesn't go away until after sentence processing
const WordsRange &wordsRange = GetTranslationOptionList(startPos, endPos).GetSourceRange();

ChartTranslationOptionList &translationOptionList = GetTranslationOptionList(startPos, endPos);
const PhraseDictionary* phraseDictionary =
decodeStep.GetPhraseDictionaryFeature()->GetDictionary();
//cerr << phraseDictionary.GetScoreProducerDescription() << endl;

ChartTranslationOptionList &chartRuleCollection = GetTranslationOptionList(startPos, endPos);

phraseDictionary->GetChartRuleCollection(chartRuleCollection
, m_source
, wordsRange
, adhereTableLimit
, m_hypoStackColl);
//cerr << "chartRuleCollection size=" << chartRuleCollection->GetSize();
}

ChartTranslationOptionList &TranslationOptionCollection::GetTranslationOptionList(size_t startPos, size_t endPos)
{
size_t sizeVec = m_collection[startPos].size();
Expand Down
11 changes: 4 additions & 7 deletions moses-chart/src/ChartTranslationOptionCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../../moses/src/InputType.h"
#include "../../moses/src/DecodeGraph.h"
#include "../../moses/src/ChartTranslationOptionList.h"
#include "../../moses/src/ChartRuleLookupManager.h"

namespace Moses
{
Expand All @@ -47,17 +48,13 @@ class TranslationOptionCollection
const Moses::TranslationSystem* m_system;
std::vector <Moses::DecodeGraph*> m_decodeGraphList;
const ChartCellCollection &m_hypoStackColl;
const std::vector<Moses::ChartRuleLookupManager*> &m_ruleLookupManagers;

std::vector< std::vector< Moses::ChartTranslationOptionList > > m_collection; /*< contains translation options */
std::vector<Moses::Phrase*> m_unksrcs;
std::list<Moses::TargetPhrase*> m_cacheTargetPhrase;
std::list<std::vector<Moses::WordConsumed*>* > m_cachedWordsConsumed;

virtual void CreateTranslationOptionsForRange(const Moses::DecodeGraph& decodeGraph,
size_t startPosition
, size_t endPosition
, bool adhereTableLimit);

// for adding 1 trans opt in unknown word proc
void Add(Moses::ChartTranslationOption *transOpt, size_t pos);

Expand All @@ -82,9 +79,9 @@ class TranslationOptionCollection
public:
TranslationOptionCollection(Moses::InputType const& source
, const Moses::TranslationSystem* system
, const ChartCellCollection &hypoStackColl);
, const ChartCellCollection &hypoStackColl
, const std::vector<Moses::ChartRuleLookupManager*> &ruleLookupManagers);
virtual ~TranslationOptionCollection();
//virtual void CreateTranslationOptions(const std::vector <Moses::DecodeGraph*> &decodeGraphList);
void CreateTranslationOptionsForRange(size_t startPos
, size_t endPos);

Expand Down
67 changes: 67 additions & 0 deletions moses/src/ChartRuleLookupManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2011 University of Edinburgh
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/

#pragma once
#ifndef moses_ChartRuleLookupManager_h
#define moses_ChartRuleLookupManager_h

#include "CellCollection.h"
#include "InputType.h"

namespace Moses
{

class ChartTranslationOptionList;
class WordsRange;

// Defines an interface for looking up rules in a rule table. Concrete
// implementation classes should correspond to specific PhraseDictionary
// subclasses (memory or on-disk). Since a ChartRuleLookupManager object
// maintains sentence-specific state, exactly one should be created for
// each sentence that is to be decoded.
class ChartRuleLookupManager
{
public:
ChartRuleLookupManager(const InputType &sentence,
const CellCollection &cellColl)
: m_sentence(sentence)
, m_cellCollection(cellColl) {}

virtual ~ChartRuleLookupManager() {}

const InputType &GetSentence() const { return m_sentence; }
const CellCollection &GetCellCollection() const { return m_cellCollection; }

virtual void GetChartRuleCollection(
const WordsRange &range,
bool adhereTableLimit,
ChartTranslationOptionList &outColl) = 0;

private:
// Non-copyable: copy constructor and assignment operator not implemented.
ChartRuleLookupManager(const ChartRuleLookupManager &);
ChartRuleLookupManager &operator=(const ChartRuleLookupManager &);

const InputType &m_sentence;
const CellCollection &m_cellCollection;
};

} // namespace Moses

#endif
Loading

0 comments on commit ce4cc28

Please sign in to comment.