mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-09-11 11:25:40 +03:00
In PhraseDictionarySCFG, only add non-leaf nodes to the running node lists.
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3424 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
parent
c73dddfb5b
commit
130ae457dd
@ -20,23 +20,15 @@
|
||||
#include "DotChart.h"
|
||||
#include "Util.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Moses
|
||||
{
|
||||
ProcessedRuleStack::ProcessedRuleStack(size_t size)
|
||||
:m_coll(size)
|
||||
ProcessedRuleColl::~ProcessedRuleColl()
|
||||
{
|
||||
for (size_t ind = 0; ind < size; ++ind)
|
||||
{
|
||||
m_coll[ind] = new ProcessedRuleColl();
|
||||
}
|
||||
}
|
||||
|
||||
ProcessedRuleStack::~ProcessedRuleStack()
|
||||
{
|
||||
RemoveAllInColl(m_coll);
|
||||
RemoveAllInColl(m_savedNode);
|
||||
std::for_each(m_coll.begin(), m_coll.end(), RemoveAllInColl<CollType::value_type>);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const ProcessedRule &rule)
|
||||
@ -47,9 +39,9 @@ std::ostream& operator<<(std::ostream &out, const ProcessedRule &rule)
|
||||
return out;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const ProcessedRuleColl &coll)
|
||||
std::ostream& operator<<(std::ostream &out, const ProcessedRuleList &coll)
|
||||
{
|
||||
ProcessedRuleColl::CollType::const_iterator iter;
|
||||
ProcessedRuleList::const_iterator iter;
|
||||
for (iter = coll.begin(); iter != coll.end(); ++iter)
|
||||
{
|
||||
const ProcessedRule &rule = **iter;
|
||||
|
@ -55,37 +55,20 @@ public:
|
||||
{
|
||||
return m_wordsConsumed;
|
||||
}
|
||||
|
||||
bool IsCurrNonTerminal() const
|
||||
{
|
||||
assert(m_wordsConsumed);
|
||||
return m_wordsConsumed->IsNonTerminal();
|
||||
}
|
||||
|
||||
/*
|
||||
inline int Compare(const ProcessedRule &compare) const
|
||||
{
|
||||
if (m_lastNode < compare.m_lastNode)
|
||||
return -1;
|
||||
if (m_lastNode > compare.m_lastNode)
|
||||
return 1;
|
||||
|
||||
return m_wordsConsumed < compare.m_wordsConsumed;
|
||||
}
|
||||
inline bool operator<(const ProcessedRule &compare) const
|
||||
{
|
||||
return Compare(compare) < 0;
|
||||
}
|
||||
*/
|
||||
};
|
||||
|
||||
typedef std::vector<const ProcessedRule*> ProcessedRuleList;
|
||||
|
||||
// Collection of all ProcessedRules that share a common start point,
|
||||
// grouped by end point. Additionally, maintains a list of all
|
||||
// ProcessedRules that could be expanded further, i.e. for which the
|
||||
// corresponding PhraseDictionaryNodeSCFG is not a leaf.
|
||||
class ProcessedRuleColl
|
||||
{
|
||||
friend std::ostream& operator<<(std::ostream&, const ProcessedRuleColl&);
|
||||
|
||||
protected:
|
||||
typedef std::vector<const ProcessedRule*> CollType;
|
||||
typedef std::vector<ProcessedRuleList> CollType;
|
||||
CollType m_coll;
|
||||
ProcessedRuleList m_runningNodes;
|
||||
|
||||
public:
|
||||
typedef CollType::iterator iterator;
|
||||
@ -96,85 +79,29 @@ public:
|
||||
iterator begin() { return m_coll.begin(); }
|
||||
iterator end() { return m_coll.end(); }
|
||||
|
||||
const ProcessedRule &Get(size_t ind) const
|
||||
{ return *m_coll[ind]; }
|
||||
ProcessedRuleColl(size_t size)
|
||||
: m_coll(size)
|
||||
{}
|
||||
|
||||
void Add(const ProcessedRule *processedRule)
|
||||
{
|
||||
m_coll.push_back(processedRule);
|
||||
}
|
||||
void Delete(size_t ind)
|
||||
{
|
||||
//delete m_coll[ind];
|
||||
m_coll.erase(m_coll.begin() + ind);
|
||||
}
|
||||
|
||||
size_t GetSize() const
|
||||
{ return m_coll.size(); }
|
||||
|
||||
};
|
||||
~ProcessedRuleColl();
|
||||
|
||||
class SavedNode
|
||||
{
|
||||
const ProcessedRule *m_processedRule;
|
||||
|
||||
public:
|
||||
SavedNode(const ProcessedRule *processedRule)
|
||||
:m_processedRule(processedRule)
|
||||
{
|
||||
assert(m_processedRule);
|
||||
}
|
||||
|
||||
~SavedNode()
|
||||
{
|
||||
delete m_processedRule;
|
||||
}
|
||||
|
||||
const ProcessedRule &GetProcessedRule() const
|
||||
{ return *m_processedRule; }
|
||||
};
|
||||
|
||||
class ProcessedRuleStack
|
||||
{ // coll of coll of processed rules
|
||||
public:
|
||||
typedef std::vector<SavedNode*> SavedNodeColl;
|
||||
|
||||
protected:
|
||||
typedef std::vector<ProcessedRuleColl*> CollType;
|
||||
CollType m_coll;
|
||||
|
||||
SavedNodeColl m_savedNode;
|
||||
|
||||
public:
|
||||
typedef CollType::iterator iterator;
|
||||
typedef CollType::const_iterator const_iterator;
|
||||
|
||||
const_iterator begin() const { return m_coll.begin(); }
|
||||
const_iterator end() const { return m_coll.end(); }
|
||||
iterator begin() { return m_coll.begin(); }
|
||||
iterator end() { return m_coll.end(); }
|
||||
|
||||
ProcessedRuleStack(size_t size);
|
||||
~ProcessedRuleStack();
|
||||
|
||||
const ProcessedRuleColl &Get(size_t pos) const
|
||||
{ return *m_coll[pos]; }
|
||||
ProcessedRuleColl &Get(size_t pos)
|
||||
{ return *m_coll[pos]; }
|
||||
|
||||
const ProcessedRuleColl &back() const
|
||||
{ return *m_coll.back(); }
|
||||
const ProcessedRuleList &Get(size_t pos) const
|
||||
{ return m_coll[pos]; }
|
||||
ProcessedRuleList &Get(size_t pos)
|
||||
{ return m_coll[pos]; }
|
||||
|
||||
void Add(size_t pos, const ProcessedRule *processedRule)
|
||||
{
|
||||
assert(processedRule);
|
||||
|
||||
m_coll[pos]->Add(processedRule);
|
||||
m_savedNode.push_back(new SavedNode(processedRule));
|
||||
m_coll[pos].push_back(processedRule);
|
||||
if (!processedRule->GetLastNode().IsLeaf())
|
||||
{
|
||||
m_runningNodes.push_back(processedRule);
|
||||
}
|
||||
}
|
||||
|
||||
const SavedNodeColl &GetSavedNodeColl() const
|
||||
{ return m_savedNode; }
|
||||
const ProcessedRuleList &GetRunningNodes() const
|
||||
{ return m_runningNodes; }
|
||||
|
||||
};
|
||||
|
||||
|
@ -60,6 +60,9 @@ protected:
|
||||
public:
|
||||
virtual ~PhraseDictionaryNodeSCFG();
|
||||
|
||||
bool IsLeaf() const
|
||||
{ return m_sourceTermMap.empty() && m_nonTermMap.empty(); }
|
||||
|
||||
void Sort(size_t tableLimit);
|
||||
PhraseDictionaryNodeSCFG *GetOrCreateChild(const Word &sourceTerm);
|
||||
PhraseDictionaryNodeSCFG *GetOrCreateChild(const Word &sourceNonTerm, const Word &targetNonTerm);
|
||||
|
@ -261,18 +261,18 @@ const TargetPhraseCollection *PhraseDictionarySCFG::GetTargetPhraseCollection(co
|
||||
|
||||
void PhraseDictionarySCFG::InitializeForInput(const InputType& input)
|
||||
{
|
||||
assert(m_runningNodesVec.size() == 0);
|
||||
assert(m_processedRuleColls.size() == 0);
|
||||
size_t sourceSize = input.GetSize();
|
||||
m_runningNodesVec.resize(sourceSize);
|
||||
m_processedRuleColls.resize(sourceSize);
|
||||
|
||||
for (size_t ind = 0; ind < m_runningNodesVec.size(); ++ind)
|
||||
for (size_t ind = 0; ind < m_processedRuleColls.size(); ++ind)
|
||||
{
|
||||
ProcessedRule *initProcessedRule = new ProcessedRule(m_collection);
|
||||
|
||||
ProcessedRuleStack *processedStack = new ProcessedRuleStack(sourceSize - ind + 1);
|
||||
processedStack->Add(0, initProcessedRule); // init rule. stores the top node in tree
|
||||
ProcessedRuleColl *processedRuleColl = new ProcessedRuleColl(sourceSize - ind + 1);
|
||||
processedRuleColl->Add(0, initProcessedRule); // init rule. stores the top node in tree
|
||||
|
||||
m_runningNodesVec[ind] = processedStack;
|
||||
m_processedRuleColls[ind] = processedRuleColl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -298,7 +298,7 @@ void PhraseDictionarySCFG::CleanUp()
|
||||
}
|
||||
m_chartTargetPhraseColl.clear();
|
||||
|
||||
RemoveAllInColl(m_runningNodesVec);
|
||||
RemoveAllInColl(m_processedRuleColls);
|
||||
}
|
||||
|
||||
TO_STRING_BODY(PhraseDictionarySCFG);
|
||||
|
@ -43,7 +43,7 @@ namespace Moses
|
||||
protected:
|
||||
PhraseDictionaryNodeSCFG m_collection;
|
||||
mutable std::vector<ChartRuleCollection*> m_chartTargetPhraseColl;
|
||||
mutable std::vector<ProcessedRuleStack*> m_runningNodesVec;
|
||||
mutable std::vector<ProcessedRuleColl*> m_processedRuleColls;
|
||||
|
||||
Phrase m_prevSource;
|
||||
TargetPhraseCollection *m_prevPhraseColl;
|
||||
|
@ -64,13 +64,13 @@ const ChartRuleCollection *PhraseDictionarySCFG::GetChartRuleCollection(
|
||||
size_t absEndPos = range.GetEndPos();
|
||||
|
||||
// MAIN LOOP. create list of nodes of target phrases
|
||||
ProcessedRuleStack &runningNodes = *m_runningNodesVec[range.GetStartPos()];
|
||||
|
||||
const ProcessedRuleStack::SavedNodeColl &savedNodeColl = runningNodes.GetSavedNodeColl();
|
||||
for (size_t ind = 0; ind < savedNodeColl.size(); ++ind)
|
||||
|
||||
ProcessedRuleColl &processedRuleCol = *m_processedRuleColls[range.GetStartPos()];
|
||||
const ProcessedRuleList &runningNodes = processedRuleCol.GetRunningNodes();
|
||||
// Note that runningNodes can be expanded as the loop runs (through calls to processedRuleCol::Add())
|
||||
for (size_t ind = 0; ind < runningNodes.size(); ++ind)
|
||||
{
|
||||
const SavedNode &savedNode = *savedNodeColl[ind];
|
||||
const ProcessedRule &prevProcessedRule = savedNode.GetProcessedRule();
|
||||
const ProcessedRule &prevProcessedRule = *runningNodes[ind];
|
||||
const PhraseDictionaryNodeSCFG &prevNode = prevProcessedRule.GetLastNode();
|
||||
const WordConsumed *prevWordConsumed = prevProcessedRule.GetLastWordConsumed();
|
||||
size_t startPos = (prevWordConsumed == NULL) ? range.GetStartPos() : prevWordConsumed->GetWordsRange().GetEndPos() + 1;
|
||||
@ -86,7 +86,7 @@ const ChartRuleCollection *PhraseDictionarySCFG::GetChartRuleCollection(
|
||||
, sourceWord
|
||||
, prevWordConsumed);
|
||||
ProcessedRule *processedRule = new ProcessedRule(*node, newWordConsumed);
|
||||
runningNodes.Add(relEndPos+1, processedRule);
|
||||
processedRuleCol.Add(relEndPos+1, processedRule);
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,17 +130,17 @@ const ChartRuleCollection *PhraseDictionarySCFG::GetChartRuleCollection(
|
||||
, prevWordConsumed);
|
||||
|
||||
ProcessedRule *processedRule = new ProcessedRule(*node, newWordConsumed);
|
||||
runningNodes.Add(stackInd, processedRule);
|
||||
processedRuleCol.Add(stackInd, processedRule);
|
||||
}
|
||||
} // for (iterHeadWords
|
||||
} // for (iterLabelList
|
||||
}
|
||||
|
||||
// return list of target phrases
|
||||
ProcessedRuleColl &nodes = runningNodes.Get(relEndPos + 1);
|
||||
ProcessedRuleList &nodes = processedRuleCol.Get(relEndPos + 1);
|
||||
|
||||
size_t rulesLimit = StaticData::Instance().GetRuleLimit();
|
||||
ProcessedRuleColl::const_iterator iterNode;
|
||||
ProcessedRuleList::const_iterator iterNode;
|
||||
for (iterNode = nodes.begin(); iterNode != nodes.end(); ++iterNode)
|
||||
{
|
||||
const ProcessedRule &processedRule = **iterNode;
|
||||
|
Loading…
Reference in New Issue
Block a user