recursively extend rule

This commit is contained in:
Hieu Hoang 2014-02-21 17:18:24 +00:00
parent 18e6a0df43
commit 1ccb1a6712
5 changed files with 43 additions and 7 deletions

View File

@ -4,8 +4,8 @@
* Created on: 20 Feb 2014
* Author: hieu
*/
#include <ConsistentPhrases.h>
#include <cassert>
#include "ConsistentPhrases.h"
using namespace std;
@ -36,10 +36,6 @@ void ConsistentPhrases::Add(int sourceStart, int sourceEnd,
targetStart,
targetEnd));
assert(inserted.second);
//const ConsistentPhrase &cp = *inserted;
//m_coll[sourceStart][sourceEnd - sourceStart] = &cp;
}
const ConsistentPhrases::Coll &ConsistentPhrases::GetColl(int sourceStart, int sourceEnd) const

View File

@ -18,6 +18,17 @@ Rule::Rule(const ConsistentPhrase &consistentPhrase, const AlignedSentence &alig
CreateSource();
}
Rule::Rule(const Rule &copy, const ConsistentPhrase &cp)
:m_consistentPhrase(copy.m_consistentPhrase)
,m_alignedSentence(copy.m_alignedSentence)
,m_isValid(true)
,m_canRecurse(true)
,m_nonterms(copy.m_nonterms)
{
m_nonterms.push_back(&cp);
CreateSource();
}
Rule::~Rule() {
// TODO Auto-generated destructor stub
}

View File

@ -18,7 +18,12 @@ class RulePhrase : public std::vector<const RuleSymbol*>
class Rule {
public:
// original rule with no non-term
Rule(const ConsistentPhrase &consistentPhrase, const AlignedSentence &alignedSentence);
// extend a rule, adding 1 new non-term
Rule(const Rule &copy, const ConsistentPhrase &cp);
virtual ~Rule();
bool IsValid() const

View File

@ -13,6 +13,7 @@
using namespace std;
Rules::Rules(const AlignedSentence &alignedSentence)
:m_alignedSentence(alignedSentence)
{
const ConsistentPhrases &allCPS = alignedSentence.GetConsistentPhrases();
@ -52,17 +53,36 @@ void Rules::CreateRules()
void Rules::Extend(const Rule &rule)
{
const ConsistentPhrases &allCPS = m_alignedSentence.GetConsistentPhrases();
int sourceMin = rule.GetNextSourcePosForNonTerm();
int sourceMax = rule.GetConsistentPhrase().corners[1];
for (int sourceStart = sourceMin; sourceStart <= sourceMax; ++sourceStart) {
for (int sourceEnd = sourceStart; sourceEnd <= sourceMax; ++sourceEnd) {
const ConsistentPhrases::Coll cps = allCPS.GetColl(sourceStart, sourceEnd);
Extend(rule, cps);
}
}
}
void Rules::Extend(const Rule &rule, const ConsistentPhrases::Coll &cps)
{
ConsistentPhrases::Coll::const_iterator iter;
for (iter = cps.begin(); iter != cps.end(); ++iter) {
const ConsistentPhrase &cp = *iter;
Extend(rule, cp);
}
}
void Rules::Extend(const Rule &rule, const ConsistentPhrase &cp)
{
Rule *newRule = new Rule(rule, cp);
m_todoRules.insert(newRule);
// recursively extend
Extend(*newRule);
}
void Rules::Debug(std::ostream &out) const
{
std::set<Rule*>::const_iterator iter;

View File

@ -9,6 +9,7 @@
#include <set>
#include <iostream>
#include "ConsistentPhrases.h"
class AlignedSentence;
class Rule;
@ -22,9 +23,12 @@ public:
void Debug(std::ostream &out) const;
protected:
const AlignedSentence &m_alignedSentence;
std::set<Rule*> m_todoRules, m_keepRules;
void Extend(const Rule &rule);
void Extend(const Rule &rule, const ConsistentPhrases::Coll &cps);
void Extend(const Rule &rule, const ConsistentPhrase &cp);
};