From 6ca64b5cf2d4c0c2698d4298e88dde274f766ac4 Mon Sep 17 00:00:00 2001 From: graehl Date: Fri, 13 Aug 2010 03:30:49 +0000 Subject: named_enum, itoa, cdec replace --a-b=x with --a_b=x git-svn-id: https://ws10smt.googlecode.com/svn/trunk@536 ec762483-ff6d-05da-a07a-a48fb63a330f --- decoder/apply_fsa_models.cc | 20 ++++++++++++++++---- decoder/apply_fsa_models.h | 27 +++++++++++++++++++-------- decoder/cdec.cc | 2 +- decoder/cfg.cc | 22 +++++++++++++++++----- decoder/cfg.h | 16 ++++++++-------- decoder/cfg_options.h | 1 + decoder/program_options.h | 25 +++++++++++++++++++++++++ 7 files changed, 87 insertions(+), 26 deletions(-) (limited to 'decoder') diff --git a/decoder/apply_fsa_models.cc b/decoder/apply_fsa_models.cc index 1c30eb90..2854b28b 100755 --- a/decoder/apply_fsa_models.cc +++ b/decoder/apply_fsa_models.cc @@ -13,6 +13,8 @@ using namespace std; +DEFINE_NAMED_ENUM(FSA_BY) + struct ApplyFsa { ApplyFsa(HgCFG &i, const SentenceMetadata& smeta, @@ -74,6 +76,7 @@ void ApplyFsaModels(HgCFG &i, a.Compute(); } +/* namespace { char const* anames[]={ "BU_CUBE", @@ -82,14 +85,18 @@ char const* anames[]={ 0 }; } +*/ //TODO: named enum type in boost? std::string ApplyFsaBy::name() const { - return anames[algorithm]; +// return anames[algorithm]; + return GetName(algorithm); } std::string ApplyFsaBy::all_names() { + return FsaByNames(" "); + /* std::ostringstream o; for (int i=0;i=N_ALGORITHMS) +ApplyFsaBy::ApplyFsaBy(FsaBy i, int pop_limit) : pop_limit(pop_limit) { +/* if (i<0 || i>=N_ALGORITHMS) throw std::runtime_error("Unknown ApplyFsaBy type id: "+itos(i)+" - legal types: "+all_names()); +*/ + GetName(i); // checks validity algorithm=i; } diff --git a/decoder/apply_fsa_models.h b/decoder/apply_fsa_models.h index 5120fb4e..6561c70c 100755 --- a/decoder/apply_fsa_models.h +++ b/decoder/apply_fsa_models.h @@ -4,25 +4,36 @@ #include #include #include "feature_vector.h" +#include "named_enum.h" struct FsaFeatureFunction; struct Hypergraph; struct SentenceMetadata; struct HgCFG; + +#define FSA_BY(X,t) \ + X(t,BU_CUBE,) \ + X(t,BU_FULL,) \ + X(t,EARLEY,) \ + +#define FSA_BY_TYPE FsaBy + +DECLARE_NAMED_ENUM(FSA_BY) + struct ApplyFsaBy { - enum { - BU_CUBE, - BU_FULL, - EARLEY, - N_ALGORITHMS - }; +/*enum { + BU_CUBE, + BU_FULL, + EARLEY, + N_ALGORITHMS + };*/ int pop_limit; // only applies to BU_FULL so far bool IsBottomUp() const { return algorithm==BU_FULL || algorithm==BU_CUBE; } int BottomUpAlgorithm() const; - int algorithm; + FsaBy algorithm; std::string name() const; friend inline std::ostream &operator << (std::ostream &o,ApplyFsaBy const& c) { o << c.name(); @@ -30,7 +41,7 @@ struct ApplyFsaBy { o << "("<::min(),std::numeric_limits::min()); -} - -WordID CFG::BinName(BinRhs const& b) +// index i >= N.size()? then it's in M[i-N.size()] +WordID BinName(CFG::BinRhs const& b,CFG::NTs const& N,CFG::NTs const& M) { + int nn=N.size(); ostringstream o; -#define BinNameOWORD(w) do { int n=w; if (n>0) o << TD::Convert(n); else { o << 'V' << -n; } } while(0) +#define BinNameOWORD(w) \ + do { \ + int n=w; if (n>0) o << TD::Convert(n); \ + else { \ + int i=-n; \ + CFG::NT const&nt = i Ruleids; void print_nt_name(std::ostream &o,NTHandle n) const { - o << nts[n].from; + o << nts[n].from << n; } typedef std::pair BinRhs; - WordID BinName(BinRhs const& b); struct Rule { // for binarizing - no costs/probs @@ -106,16 +105,17 @@ struct CFG { swap(goal_nt,o.goal_nt); } void Binarize(CFGBinarize const& binarize_options); + + typedef std::vector NTs; + NTs nts; + typedef std::vector Rules; + Rules rules; + int goal_nt; + prob_t goal_inside,pushed_inside; // when we push viterbi weights to goal, we store the removed probability in pushed_inside protected: bool uninit; Hypergraph const* hg_; // shouldn't be used for anything, esp. after binarization - prob_t goal_inside,pushed_inside; // when we push viterbi weights to goal, we store the removed probability in pushed_inside // rules/nts will have same index as hg edges/nodes - typedef std::vector Rules; - Rules rules; - typedef std::vector NTs; - NTs nts; - int goal_nt; }; inline void swap(CFG &a,CFG &b) { diff --git a/decoder/cfg_options.h b/decoder/cfg_options.h index 956586f0..acd8d05b 100755 --- a/decoder/cfg_options.h +++ b/decoder/cfg_options.h @@ -28,6 +28,7 @@ struct CFGOptions { void Validate() { format.Validate(); binarize.Validate(); +// if (cfg_output.empty()) binarize.bin_name_nts=false; } char const* description() const { return "CFG output options"; diff --git a/decoder/program_options.h b/decoder/program_options.h index 251f5680..87afb320 100755 --- a/decoder/program_options.h +++ b/decoder/program_options.h @@ -13,6 +13,31 @@ #include +// change --opt-name=x --opt_name=x for all strings x. danger: probably the argv from int main isn't supposed to be modified? +inline int arg_minusto_underscore(char *s) { + if (!*s || *s++ != '-') return 0; + if (!*s || *s++ != '-') return 0; + int chars_replaced=0; + for(;*s;++s) { + if (*s=='=') + break; + if (*s=='-') { + *s='_'; + ++chars_replaced; + } + } + return chars_replaced; +} + +inline +int argv_minus_to_underscore(int argc, char **argv) { + int chars_replaced=0; + for (int i=1;i boost::program_options::typed_value* defaulted_value(T *v) -- cgit v1.2.3