diff options
author | graehl@gmail.com <graehl@gmail.com@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-08-15 08:50:14 +0000 |
---|---|---|
committer | graehl@gmail.com <graehl@gmail.com@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-08-15 08:50:14 +0000 |
commit | c7f1a4e30a42ad0c03ac6c439e04e6aa7efafefe (patch) | |
tree | b98b35e5c5a0fb3039a307efb9f5b91911824b20 /decoder/cfg.cc | |
parent | 6d3cf2f3aeaa5d008f5031f70da8d728181486bc (diff) |
files required for test
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@556 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/cfg.cc')
-rwxr-xr-x | decoder/cfg.cc | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/decoder/cfg.cc b/decoder/cfg.cc index be07c2c5..3076c75e 100755 --- a/decoder/cfg.cc +++ b/decoder/cfg.cc @@ -7,12 +7,12 @@ #include <limits> #include "fast_lexical_cast.hpp" //#include "indices_after.h" +#include "show.h" + +#define DUNIQ(x) x +#define DBIN(x) +#define DCFG(x) IF_CFG_DEBUG(x) -#define CFGPRINT(x) IF_CFG_DEBUG(std::cerr<<x) -#define CFGSHOWC(x,s) CFGPRINT(#x<<"="<<x<<s) -#define CFGSHOW(x) CFGSHOWC(x,"\n") -#define CFGSHOWS(x) CFGSHOWC(x," ") -#define CFGSHOW2(x,y) CFGSHOWS(x) CFGSHOW(y) using namespace std; @@ -38,7 +38,6 @@ namespace { typedef std::vector<char> Seen; // 0 = unseen, 1 = seen+finished, 2 = open (for cycle detection; seen but not finished) enum { UNSEEN=0,SEEN,OPEN }; - // bottom -> top topo order (rev head->tails topo) template <class OutOrder> struct CFGTopo { @@ -120,22 +119,28 @@ int CFG::UniqRules(NTHandle ni) { HASH_MAP_EMPTY(bestp,null_rhs); Ruleids &adj=nts[ni].ruleids; Ruleids oldadj=adj; - int oi=0; + int newpos=0; for (int i=0,e=oldadj.size();i!=e;++i) { // this beautiful complexity is to ensure that adj' is a subsequence of adj (without duplicates) int ri=oldadj[i]; Rule const& r=rules[ri]; - prob_pos pi(r.p,oi); + prob_pos pi(r.p,newpos); prob_pos &oldpi=get_default(bestp,r.rhs,pi); - if (oldpi.pos==oi) {// newly inserted - adj[oi++]=ri; - } else if (oldpi.prob<pi.prob) { // we improve prev. best (overwrite it @old pos) - oldpi.prob=pi.prob; - adj[oldpi.pos]=ri; // replace worse rule w/ better + if (oldpi.pos==newpos) {// newly inserted + adj[newpos++]=ri; + } else { + SHOWP(DUNIQ,"Uniq duplicate: ") SHOW4(DUNIQ,oldpi.prob,pi.prob,oldpi.pos,newpos); + SHOW(DUNIQ,ShowRule(ri)); + SHOW(DUNIQ,ShowRule(adj[oldpi.pos])); + if (oldpi.prob<pi.prob) { // we improve prev. best (overwrite it @old pos) + oldpi.prob=pi.prob; + adj[oldpi.pos]=ri; // replace worse rule w/ better + } } + } - // post: oi = number of new adj - adj.resize(oi); - return oi; + // post: newpos = number of new adj + adj.resize(newpos); + return newpos; } void CFG::SortLocalBestFirst(NTHandle ni) { @@ -201,8 +206,9 @@ void CFG::Binarize(CFGBinarize const& b) { for (NTs::const_iterator n=nts.begin(),nn=nts.end();n!=nn;++n) { NT const& nt=*n; for (Ruleids::const_iterator ir=nt.ruleids.begin(),er=nt.ruleids.end();ir!=er;++ir) { - CFGPRINT("Rule id# ") CFGSHOWS(*ir);IF_CFG_DEBUG(PrintRule(cerr<<" '",*ir,CFGFormat());cerr<<"'\n"); - RHS &rhs=rules[*ir].rhs; // we're going to binarize this while adding newly created rules to new_... + RuleHandle ruleid=*ir; + SHOW2(DBIN,ruleid,ShowRule(ruleid)) + RHS &rhs=rules[ruleid].rhs; // we're going to binarize this while adding newly created rules to new_... if (rhs.empty()) continue; int r=rhs.size()-2; // loop below: [r,r+1) is to be reduced into a (maybe new) binary NT if (rhsmin<=r) { // means r>=0 also @@ -214,7 +220,7 @@ void CFG::Binarize(CFGBinarize const& b) { bin.first=rhs[r]; bin_to=get_default(bin2lhs,bin,newnt); - CFGSHOWS(r) CFGSHOWS(newnt) CFGPRINT("bin="<<BinStr(bin,nts,new_nts)<<"=>") CFGSHOW(bin_to); + SHOW(DBIN,r) SHOW(DBIN,newnt) SHOWP(DBIN,"bin="<<BinStr(bin,nts,new_nts)<<"=>") SHOW(DBIN,bin_to); if (newnt==bin_to) { // it's new! new_nts.push_back(NT(newruleid++)); //now -newnt is the index of the last (after new_nts is appended) nt. bin is its rhs. bin_to is its lhs @@ -232,7 +238,7 @@ void CFG::Binarize(CFGBinarize const& b) { } } } -#if 0 +#if 1 batched_append_swap(nts,new_nts); batched_append_swap(rules,new_rules); #else @@ -304,6 +310,10 @@ void CFG::Clear() { hg_=0; } +namespace { +CFGFormat form; +} + void CFG::PrintRule(std::ostream &o,RuleHandle rulei,CFGFormat const& f) const { Rule const& r=rules[rulei]; f.print_lhs(o,*this,r.lhs); @@ -311,6 +321,12 @@ void CFG::PrintRule(std::ostream &o,RuleHandle rulei,CFGFormat const& f) const { f.print_features(o,r.p,r.f); IF_CFG_TRULE(if (r.rule) o<<f.partsep<<*r.rule;) } +void CFG::PrintRule(std::ostream &o,RuleHandle rulei) const { + PrintRule(o,rulei,form); +} +string CFG::ShowRule(RuleHandle i) const { + ostringstream o;PrintRule(o,i);return o.str(); +} void CFG::Print(std::ostream &o,CFGFormat const& f) const { assert(!uninit); @@ -332,10 +348,9 @@ void CFG::Print(std::ostream &o,CFGFormat const& f) const { } void CFG::Print(std::ostream &o) const { - Print(o,CFGFormat()); + Print(o,form); } - std::ostream &operator<<(std::ostream &o,CFG const &x) { x.Print(o); return o; |