From 0b598b997a7c1d2d9dc255cc2ff1bf9bb2c425a1 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Thu, 15 Mar 2012 22:47:04 -0400 Subject: bayes bayes bayes --- utils/ccrp.h | 4 ++++ utils/mfcr.h | 4 ++++ 2 files changed, 8 insertions(+) (limited to 'utils') diff --git a/utils/ccrp.h b/utils/ccrp.h index 4a8b80e7..390d4994 100644 --- a/utils/ccrp.h +++ b/utils/ccrp.h @@ -55,6 +55,10 @@ class CCRP { double discount() const { return discount_; } double strength() const { return strength_; } + void set_hyperparameters(double d, double s) { + discount_ = d; strength_ = s; + check_hyperparameters(); + } void set_discount(double d) { discount_ = d; check_hyperparameters(); } void set_strength(double a) { strength_ = a; check_hyperparameters(); } diff --git a/utils/mfcr.h b/utils/mfcr.h index 886f01ef..4aacb567 100644 --- a/utils/mfcr.h +++ b/utils/mfcr.h @@ -73,6 +73,10 @@ class MFCR { double discount() const { return discount_; } double strength() const { return strength_; } + void set_hyperparameters(double d, double s) { + discount_ = d; strength_ = s; + check_hyperparameters(); + } void set_discount(double d) { discount_ = d; check_hyperparameters(); } void set_strength(double a) { strength_ = a; check_hyperparameters(); } -- cgit v1.2.3 From 34b4752a1eefc002166e95782c2c52747bb08b3a Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Tue, 20 Mar 2012 15:37:54 -0400 Subject: make c++11 compatible --- decoder/decoder.cc | 31 +++++++++++++++---------------- decoder/earley_composer.cc | 4 +--- decoder/phrasetable_fst.cc | 3 +-- dpmert/ces.cc | 3 +-- dpmert/lo_test.cc | 13 ++++++------- dpmert/mert_geometry.cc | 15 +++++++-------- extools/extract.cc | 8 ++++---- extools/featurize_grammar.cc | 6 +++--- extools/sentence_pair.cc | 5 ++--- gi/pf/align-tl.cc | 2 +- gi/pf/brat.cc | 2 +- gi/pf/cfg_wfst_composer.cc | 3 +-- gi/pf/condnaive.cc | 2 +- gi/pf/dpnaive.cc | 2 +- gi/pf/itg.cc | 2 +- gi/pf/learn_cfg.cc | 2 +- gi/pf/pfbrat.cc | 2 +- gi/pf/pfdist.cc | 2 +- gi/pf/pfnaive.cc | 2 +- gi/pf/pyp_lm.cc | 2 +- mira/kbest_mira.cc | 19 +++++++++---------- mteval/ns.cc | 9 ++++----- phrasinator/gibbs_train_plm.cc | 2 +- phrasinator/gibbs_train_plm.notables.cc | 2 +- training/mpi_batch_optimize.cc | 3 +-- training/mr_optimize_reduce.cc | 3 +-- training/optimize_test.cc | 2 +- utils/atools.cc | 7 +++---- 28 files changed, 72 insertions(+), 86 deletions(-) (limited to 'utils') diff --git a/decoder/decoder.cc b/decoder/decoder.cc index 69fbaf85..d4f8f06d 100644 --- a/decoder/decoder.cc +++ b/decoder/decoder.cc @@ -57,7 +57,6 @@ static const double kMINUS_EPSILON = -1e-6; // don't be too strict using namespace std; using namespace std::tr1; -using boost::shared_ptr; namespace po = boost::program_options; static bool verbose_feature_functions=true; @@ -101,7 +100,7 @@ inline string str(char const* name,po::variables_map const& conf) { // print just the --long_opt names suitable for bash compgen inline void print_options(std::ostream &out,po::options_description const& opts) { - typedef std::vector< shared_ptr > Ds; + typedef std::vector< boost::shared_ptr > Ds; Ds const& ds=opts.options(); out << '"'; for (unsigned i=0;i make_ff(string const& ffp,bool verbose_feature_functions,char const* pre="") { +inline boost::shared_ptr make_ff(string const& ffp,bool verbose_feature_functions,char const* pre="") { string ff, param; SplitCommandAndParam(ffp, &ff, ¶m); cerr << pre << "feature: " << ff; if (param.size() > 0) cerr << " (with config parameters '" << param << "')\n"; else cerr << " (no config parameters)\n"; - shared_ptr pf = ff_registry.Create(ff, param); + boost::shared_ptr pf = ff_registry.Create(ff, param); if (!pf) exit(1); int nbyte=pf->NumBytesContext(); if (verbose_feature_functions) @@ -135,13 +134,13 @@ inline shared_ptr make_ff(string const& ffp,bool verbose_featur } #ifdef FSA_RESCORING -inline shared_ptr make_fsa_ff(string const& ffp,bool verbose_feature_functions,char const* pre="") { +inline boost::shared_ptr make_fsa_ff(string const& ffp,bool verbose_feature_functions,char const* pre="") { string ff, param; SplitCommandAndParam(ffp, &ff, ¶m); cerr << "FSA Feature: " << ff; if (param.size() > 0) cerr << " (with config parameters '" << param << "')\n"; else cerr << " (no config parameters)\n"; - shared_ptr pf = fsa_ff_registry.Create(ff, param); + boost::shared_ptr pf = fsa_ff_registry.Create(ff, param); if (!pf) exit(1); if (verbose_feature_functions) cerr<<"State is "<state_bytes()<<" bytes for "< make_fsa_ff(string const& ffp,bool verbose // passes are carried over into subsequent passes (where they may have different weights). struct RescoringPass { RescoringPass() : fid_summary(), density_prune(), beam_prune() {} - shared_ptr models; - shared_ptr inter_conf; + boost::shared_ptr models; + boost::shared_ptr inter_conf; vector ffs; - shared_ptr > weight_vector; + boost::shared_ptr > weight_vector; int fid_summary; // 0 == no summary feature double density_prune; // 0 == don't density prune double beam_prune; // 0 == don't beam prune @@ -293,15 +292,15 @@ struct DecoderImpl { po::variables_map& conf; OracleBleu oracle; string formalism; - shared_ptr translator; - shared_ptr > init_weights; // weights used with initial parse - vector > pffs; + boost::shared_ptr translator; + boost::shared_ptr > init_weights; // weights used with initial parse + vector > pffs; #ifdef FSA_RESCORING CFGOptions cfg_options; - vector > fsa_ffs; + vector > fsa_ffs; vector fsa_names; #endif - shared_ptr > rng; + boost::shared_ptr > rng; int sample_max_trans; bool aligner_mode; bool graphviz; @@ -310,7 +309,7 @@ struct DecoderImpl { bool kbest; bool unique_kbest; bool get_oracle_forest; - shared_ptr extract_file; + boost::shared_ptr extract_file; int combine_size; int sent_id; SparseVector acc_vec; // accumulate gradient @@ -622,7 +621,7 @@ DecoderImpl::DecoderImpl(po::variables_map& conf, int argc, char** argv, istream } // set up weight vectors since later phases may reuse weights from earlier phases - shared_ptr > prev_weights = init_weights; + boost::shared_ptr > prev_weights = init_weights; for (int pass = 0; pass < rescoring_passes.size(); ++pass) { RescoringPass& rp = rescoring_passes[pass]; if (!rp.weight_vector) { diff --git a/decoder/earley_composer.cc b/decoder/earley_composer.cc index b7af801a..385baf8b 100644 --- a/decoder/earley_composer.cc +++ b/decoder/earley_composer.cc @@ -16,8 +16,6 @@ #include "tdict.h" #include "hg.h" -using boost::shared_ptr; -namespace po = boost::program_options; using namespace std; using namespace std::tr1; @@ -111,7 +109,7 @@ struct Edge { const Edge* const active_parent; // back pointer, NULL for PREDICT items const Edge* const passive_parent; // back pointer, NULL for SCAN and PREDICT items const TargetPhraseSet* const tps; // translations - shared_ptr > features; // features from CFG rule + boost::shared_ptr > features; // features from CFG rule bool IsPassive() const { // when a rule is completed, this value will be set diff --git a/decoder/phrasetable_fst.cc b/decoder/phrasetable_fst.cc index f421e941..b3bec86b 100644 --- a/decoder/phrasetable_fst.cc +++ b/decoder/phrasetable_fst.cc @@ -9,7 +9,6 @@ #include "filelib.h" #include "tdict.h" -using boost::shared_ptr; using namespace std; TargetPhraseSet::~TargetPhraseSet() {} @@ -46,7 +45,7 @@ class TextFSTNode : public FSTNode { void ClearPassThroughTranslations(); private: vector passthroughs; - shared_ptr data; + boost::shared_ptr data; map ptr; }; diff --git a/dpmert/ces.cc b/dpmert/ces.cc index a85454da..c6cb1cdf 100644 --- a/dpmert/ces.cc +++ b/dpmert/ces.cc @@ -11,7 +11,6 @@ #include "error_surface.h" #include "ns.h" -using boost::shared_ptr; using namespace std; const bool minimize_segments = true; // if adjacent segments have equal scores, merge them @@ -22,7 +21,7 @@ void ComputeErrorSurface(const SegmentEvaluator& ss, const EvaluationMetric* metric, const Hypergraph& hg) { vector prev_trans; - const vector >& ienv = ve.GetSortedSegs(); + const vector >& ienv = ve.GetSortedSegs(); env->resize(ienv.size()); SufficientStats prev_score; // defaults to 0 int j = 0; diff --git a/dpmert/lo_test.cc b/dpmert/lo_test.cc index d9b909b8..5d90aabb 100644 --- a/dpmert/lo_test.cc +++ b/dpmert/lo_test.cc @@ -19,7 +19,6 @@ #include "line_optimizer.h" using namespace std; -using boost::shared_ptr; class OptTest : public testing::Test { protected: @@ -44,12 +43,12 @@ TEST_F(OptTest, TestCheckNaN) { } TEST_F(OptTest,TestConvexHull) { - shared_ptr a1(new MERTPoint(-1, 0)); - shared_ptr b1(new MERTPoint(1, 0)); - shared_ptr a2(new MERTPoint(-1, 1)); - shared_ptr b2(new MERTPoint(1, -1)); - vector > sa; sa.push_back(a1); sa.push_back(b1); - vector > sb; sb.push_back(a2); sb.push_back(b2); + boost::shared_ptr a1(new MERTPoint(-1, 0)); + boost::shared_ptr b1(new MERTPoint(1, 0)); + boost::shared_ptr a2(new MERTPoint(-1, 1)); + boost::shared_ptr b2(new MERTPoint(1, -1)); + vector > sa; sa.push_back(a1); sa.push_back(b1); + vector > sb; sb.push_back(a2); sb.push_back(b2); ConvexHull a(sa); cerr << a << endl; ConvexHull b(sb); diff --git a/dpmert/mert_geometry.cc b/dpmert/mert_geometry.cc index 81b25af9..d6973658 100644 --- a/dpmert/mert_geometry.cc +++ b/dpmert/mert_geometry.cc @@ -4,13 +4,12 @@ #include using namespace std; -using boost::shared_ptr; ConvexHull::ConvexHull(int i) { if (i == 0) { // do nothing - <> } else if (i == 1) { - points.push_back(shared_ptr(new MERTPoint(0, 0, 0, shared_ptr(), shared_ptr()))); + points.push_back(boost::shared_ptr(new MERTPoint(0, 0, 0, boost::shared_ptr(), boost::shared_ptr()))); assert(this->IsMultiplicativeIdentity()); } else { cerr << "Only can create ConvexHull semiring 0 and 1 with this constructor!\n"; @@ -27,7 +26,7 @@ const ConvexHull ConvexHullWeightFunction::operator()(const Hypergraph::Edge& e) ostream& operator<<(ostream& os, const ConvexHull& env) { os << '<'; - const vector >& points = env.GetSortedSegs(); + const vector >& points = env.GetSortedSegs(); for (int i = 0; i < points.size(); ++i) os << (i==0 ? "" : "|") << "x=" << points[i]->x << ",b=" << points[i]->b << ",m=" << points[i]->m << ",p1=" << points[i]->p1 << ",p2=" << points[i]->p2; return os << '>'; @@ -37,7 +36,7 @@ ostream& operator<<(ostream& os, const ConvexHull& env) { #ifdef ORIGINAL_MERT_IMPLEMENTATION struct SlopeCompare { - bool operator() (const shared_ptr& a, const shared_ptr& b) const { + bool operator() (const boost::shared_ptr& a, const boost::shared_ptr& b) const { return a->m < b->m; } }; @@ -93,7 +92,7 @@ const ConvexHull& ConvexHull::operator*=(const ConvexHull& other) { if (this->IsEdgeEnvelope()) { // if (other.size() > 1) // cerr << *this << " (TIMES) " << other << endl; - shared_ptr edge_parent = points[0]; + boost::shared_ptr edge_parent = points[0]; const double& edge_b = edge_parent->b; const double& edge_m = edge_parent->m; points.clear(); @@ -102,13 +101,13 @@ const ConvexHull& ConvexHull::operator*=(const ConvexHull& other) { const double m = p.m + edge_m; const double b = p.b + edge_b; const double& x = p.x; // x's don't change with * - points.push_back(shared_ptr(new MERTPoint(x, m, b, edge_parent, other.points[i]))); + points.push_back(boost::shared_ptr(new MERTPoint(x, m, b, edge_parent, other.points[i]))); assert(points.back()->p1->edge); } // if (other.size() > 1) // cerr << " = " << *this << endl; } else { - vector > new_points; + vector > new_points; int this_i = 0; int other_i = 0; const int this_size = points.size(); @@ -124,7 +123,7 @@ const ConvexHull& ConvexHull::operator*=(const ConvexHull& other) { const double m = this_point.m + other_point.m; const double b = this_point.b + other_point.b; - new_points.push_back(shared_ptr(new MERTPoint(cur_x, m, b, points[this_i], other.points[other_i]))); + new_points.push_back(boost::shared_ptr(new MERTPoint(cur_x, m, b, points[this_i], other.points[other_i]))); int comp = 0; if (this_next_val < other_next_val) comp = -1; else if (this_next_val > other_next_val) comp = 1; diff --git a/extools/extract.cc b/extools/extract.cc index f6c121b4..49542fed 100644 --- a/extools/extract.cc +++ b/extools/extract.cc @@ -131,16 +131,16 @@ lookup_and_append(const map &dict, const K &key, V &output) // phrases if there is more than one annotation. // TODO: support source annotation void Extract::AnnotatePhrasesWithCategoryTypes(const WordID default_cat, - const map< tuple, vector > &types, + const map< boost::tuple, vector > &types, vector* phrases) { const int num_unannotated_phrases = phrases->size(); // have to use num_unannotated_phrases since we may grow the vector for (int i = 0; i < num_unannotated_phrases; ++i) { ParallelSpan& phrase = (*phrases)[i]; vector cats; - lookup_and_append(types, make_tuple(phrase.i1, phrase.i2, phrase.j1, phrase.j2), cats); - lookup_and_append(types, make_tuple((short)-1, (short)-1, phrase.j1, phrase.j2), cats); - lookup_and_append(types, make_tuple(phrase.i1, phrase.i2, (short)-1, (short)-1), cats); + lookup_and_append(types, boost::make_tuple(phrase.i1, phrase.i2, phrase.j1, phrase.j2), cats); + lookup_and_append(types, boost::make_tuple((short)-1, (short)-1, phrase.j1, phrase.j2), cats); + lookup_and_append(types, boost::make_tuple(phrase.i1, phrase.i2, (short)-1, (short)-1), cats); if (cats.empty() && default_cat != 0) { cats = vector(1, default_cat); } diff --git a/extools/featurize_grammar.cc b/extools/featurize_grammar.cc index ebae9fdc..78175202 100644 --- a/extools/featurize_grammar.cc +++ b/extools/featurize_grammar.cc @@ -136,8 +136,8 @@ class FERegistry { public: FERegistry() {} boost::shared_ptr Create(const std::string& ffname) const { - map >::const_iterator it = reg_.find(ffname); - shared_ptr res; + map >::const_iterator it = reg_.find(ffname); + boost::shared_ptr res; if (it == reg_.end()) { cerr << "I don't know how to create feature " << ffname << endl; } else { @@ -147,7 +147,7 @@ class FERegistry { } void DisplayList(ostream* out) const { bool first = true; - for (map >::const_iterator it = reg_.begin(); + for (map >::const_iterator it = reg_.begin(); it != reg_.end(); ++it) { if (first) {first=false;} else {*out << ' ';} *out << it->first; diff --git a/extools/sentence_pair.cc b/extools/sentence_pair.cc index d5ebe48f..7d60715a 100644 --- a/extools/sentence_pair.cc +++ b/extools/sentence_pair.cc @@ -71,8 +71,7 @@ int AnnotatedParallelSentence::ReadAlignmentPoint(const char* buf, exit(1); } (*b) = 0; - //TODO: is this what is intended? parses as A && B || C && D. - while(ch < end && (c == 0 && (!permit_col || (permit_col && buf[ch] != ':')) || c != 0 && buf[ch] != '-')) { + while((ch < end) && (c == 0 && (!permit_col || (permit_col && buf[ch] != ':')) || c != 0 && buf[ch] != '-')) { if ((buf[ch] < '0') || (buf[ch] > '9')) { cerr << "Alignment point badly formed 4: " << string(buf, start, end-start) << endl << buf << endl << buf[ch] << endl; exit(1); @@ -151,7 +150,7 @@ void AnnotatedParallelSentence::ParseSpanLabel(const char* buf, int start, int e exit(1); } // cerr << a << " " << b << " " << string(buf,c,end-c) << endl; - span_types[make_tuple(a,b,c,d)].push_back(-TD::Convert(string(buf, ch, end-ch))); + span_types[boost::make_tuple(a,b,c,d)].push_back(-TD::Convert(string(buf, ch, end-ch))); } // INPUT FORMAT diff --git a/gi/pf/align-tl.cc b/gi/pf/align-tl.cc index cbe8c6c8..f6608f1d 100644 --- a/gi/pf/align-tl.cc +++ b/gi/pf/align-tl.cc @@ -58,7 +58,7 @@ void InitCommandLine(int argc, char** argv, po::variables_map* conf) { } } -shared_ptr prng; +boost::shared_ptr prng; struct LexicalAlignment { unsigned char src_index; diff --git a/gi/pf/brat.cc b/gi/pf/brat.cc index c2c52760..832f22cf 100644 --- a/gi/pf/brat.cc +++ b/gi/pf/brat.cc @@ -489,7 +489,7 @@ int main(int argc, char** argv) { cerr << argv[0] << "Please use --model1 to specify model 1 parameters\n"; return 1; } - shared_ptr prng; + boost::shared_ptr prng; if (conf.count("random_seed")) prng.reset(new MT19937(conf["random_seed"].as())); else diff --git a/gi/pf/cfg_wfst_composer.cc b/gi/pf/cfg_wfst_composer.cc index a31b5be8..20520c81 100644 --- a/gi/pf/cfg_wfst_composer.cc +++ b/gi/pf/cfg_wfst_composer.cc @@ -16,7 +16,6 @@ #include "tdict.h" #include "hg.h" -using boost::shared_ptr; namespace po = boost::program_options; using namespace std; using namespace std::tr1; @@ -114,7 +113,7 @@ struct Edge { const Edge* const active_parent; // back pointer, NULL for PREDICT items const Edge* const passive_parent; // back pointer, NULL for SCAN and PREDICT items TRulePtr tps; // translations - shared_ptr > features; // features from CFG rule + boost::shared_ptr > features; // features from CFG rule bool IsPassive() const { // when a rule is completed, this value will be set diff --git a/gi/pf/condnaive.cc b/gi/pf/condnaive.cc index 3ea88016..419731ac 100644 --- a/gi/pf/condnaive.cc +++ b/gi/pf/condnaive.cc @@ -55,7 +55,7 @@ void InitCommandLine(int argc, char** argv, po::variables_map* conf) { } } -shared_ptr prng; +boost::shared_ptr prng; struct ModelAndData { explicit ModelAndData(ConditionalParallelSegementationModel& m, const vector >& ce, const vector >& cf, const set& ve, const set& vf) : diff --git a/gi/pf/dpnaive.cc b/gi/pf/dpnaive.cc index 469dff5c..75ccad72 100644 --- a/gi/pf/dpnaive.cc +++ b/gi/pf/dpnaive.cc @@ -55,7 +55,7 @@ void InitCommandLine(int argc, char** argv, po::variables_map* conf) { } } -shared_ptr prng; +boost::shared_ptr prng; template struct ModelAndData { diff --git a/gi/pf/itg.cc b/gi/pf/itg.cc index a38fe672..29ec3860 100644 --- a/gi/pf/itg.cc +++ b/gi/pf/itg.cc @@ -231,7 +231,7 @@ int main(int argc, char** argv) { cerr << argv[0] << "Please use --model1 to specify model 1 parameters\n"; return 1; } - shared_ptr prng; + boost::shared_ptr prng; if (conf.count("random_seed")) prng.reset(new MT19937(conf["random_seed"].as())); else diff --git a/gi/pf/learn_cfg.cc b/gi/pf/learn_cfg.cc index ed1772bf..44eaa162 100644 --- a/gi/pf/learn_cfg.cc +++ b/gi/pf/learn_cfg.cc @@ -24,7 +24,7 @@ using namespace std; using namespace tr1; namespace po = boost::program_options; -shared_ptr prng; +boost::shared_ptr prng; vector nt_vocab; vector nt_id_to_index; static unsigned kMAX_RULE_SIZE = 0; diff --git a/gi/pf/pfbrat.cc b/gi/pf/pfbrat.cc index c2c52760..832f22cf 100644 --- a/gi/pf/pfbrat.cc +++ b/gi/pf/pfbrat.cc @@ -489,7 +489,7 @@ int main(int argc, char** argv) { cerr << argv[0] << "Please use --model1 to specify model 1 parameters\n"; return 1; } - shared_ptr prng; + boost::shared_ptr prng; if (conf.count("random_seed")) prng.reset(new MT19937(conf["random_seed"].as())); else diff --git a/gi/pf/pfdist.cc b/gi/pf/pfdist.cc index 3d578db2..a3e46064 100644 --- a/gi/pf/pfdist.cc +++ b/gi/pf/pfdist.cc @@ -23,7 +23,7 @@ using namespace std; using namespace tr1; namespace po = boost::program_options; -shared_ptr prng; +boost::shared_ptr prng; void InitCommandLine(int argc, char** argv, po::variables_map* conf) { po::options_description opts("Configuration options"); diff --git a/gi/pf/pfnaive.cc b/gi/pf/pfnaive.cc index e1a53f5c..958ec4e2 100644 --- a/gi/pf/pfnaive.cc +++ b/gi/pf/pfnaive.cc @@ -25,7 +25,7 @@ using namespace std; using namespace tr1; namespace po = boost::program_options; -shared_ptr prng; +boost::shared_ptr prng; void InitCommandLine(int argc, char** argv, po::variables_map* conf) { po::options_description opts("Configuration options"); diff --git a/gi/pf/pyp_lm.cc b/gi/pf/pyp_lm.cc index 91029688..e2b67e17 100644 --- a/gi/pf/pyp_lm.cc +++ b/gi/pf/pyp_lm.cc @@ -25,7 +25,7 @@ using namespace std; using namespace tr1; namespace po = boost::program_options; -shared_ptr prng; +boost::shared_ptr prng; void InitCommandLine(int argc, char** argv, po::variables_map* conf) { po::options_description opts("Configuration options"); diff --git a/mira/kbest_mira.cc b/mira/kbest_mira.cc index 904eba74..dc0200d6 100644 --- a/mira/kbest_mira.cc +++ b/mira/kbest_mira.cc @@ -3,10 +3,10 @@ #include #include #include +#include #include "config.h" -#include #include #include @@ -27,11 +27,10 @@ #include "sampler.h" using namespace std; -using boost::shared_ptr; namespace po = boost::program_options; bool invert_score; -boost::shared_ptr rng; +std::tr1::shared_ptr rng; void RandomPermutation(int len, vector* p_ids) { vector& ids = *p_ids; @@ -89,15 +88,15 @@ struct HypothesisInfo { }; struct GoodBadOracle { - shared_ptr good; - shared_ptr bad; + std::tr1::shared_ptr good; + std::tr1::shared_ptr bad; }; struct TrainingObserver : public DecoderObserver { TrainingObserver(const int k, const DocScorer& d, bool sf, vector* o) : ds(d), oracles(*o), kbest_size(k), sample_forest(sf) {} const DocScorer& ds; vector& oracles; - shared_ptr cur_best; + std::tr1::shared_ptr cur_best; const int kbest_size; const bool sample_forest; @@ -109,16 +108,16 @@ struct TrainingObserver : public DecoderObserver { UpdateOracles(smeta.GetSentenceID(), *hg); } - shared_ptr MakeHypothesisInfo(const SparseVector& feats, const double score) { - shared_ptr h(new HypothesisInfo); + std::tr1::shared_ptr MakeHypothesisInfo(const SparseVector& feats, const double score) { + std::tr1::shared_ptr h(new HypothesisInfo); h->features = feats; h->mt_metric = score; return h; } void UpdateOracles(int sent_id, const Hypergraph& forest) { - shared_ptr& cur_good = oracles[sent_id].good; - shared_ptr& cur_bad = oracles[sent_id].bad; + std::tr1::shared_ptr& cur_good = oracles[sent_id].good; + std::tr1::shared_ptr& cur_bad = oracles[sent_id].bad; cur_bad.reset(); // TODO get rid of?? if (sample_forest) { diff --git a/mteval/ns.cc b/mteval/ns.cc index 788f809a..8d354677 100644 --- a/mteval/ns.cc +++ b/mteval/ns.cc @@ -14,7 +14,6 @@ #include "stringlib.h" using namespace std; -using boost::shared_ptr; map EvaluationMetric::instances_; @@ -35,8 +34,8 @@ struct DefaultSegmentEvaluator : public SegmentEvaluator { const EvaluationMetric* em_; }; -shared_ptr EvaluationMetric::CreateSegmentEvaluator(const vector >& refs) const { - return shared_ptr(new DefaultSegmentEvaluator(refs, this)); +boost::shared_ptr EvaluationMetric::CreateSegmentEvaluator(const vector >& refs) const { + return boost::shared_ptr(new DefaultSegmentEvaluator(refs, this)); } #define MAX_SS_VECTOR_SIZE 50 @@ -184,8 +183,8 @@ template struct BleuMetric : public EvaluationMetric { BleuMetric() : EvaluationMetric(BrevityType == IBM ? "IBM_BLEU" : (BrevityType == Koehn ? "KOEHN_BLEU" : "NIST_BLEU")) {} unsigned SufficientStatisticsVectorSize() const { return N*2 + 2; } - shared_ptr CreateSegmentEvaluator(const vector >& refs) const { - return shared_ptr(new BleuSegmentEvaluator(refs, this)); + boost::shared_ptr CreateSegmentEvaluator(const vector >& refs) const { + return boost::shared_ptr(new BleuSegmentEvaluator(refs, this)); } float ComputeBreakdown(const SufficientStats& stats, float* bp, vector* out) const { if (out) { out->clear(); } diff --git a/phrasinator/gibbs_train_plm.cc b/phrasinator/gibbs_train_plm.cc index 3b99e1b6..86fd7865 100644 --- a/phrasinator/gibbs_train_plm.cc +++ b/phrasinator/gibbs_train_plm.cc @@ -269,7 +269,7 @@ struct UniphraseLM { int main(int argc, char** argv) { po::variables_map conf; InitCommandLine(argc, argv, &conf); - shared_ptr prng; + boost::shared_ptr prng; if (conf.count("random_seed")) prng.reset(new MT19937(conf["random_seed"].as())); else diff --git a/phrasinator/gibbs_train_plm.notables.cc b/phrasinator/gibbs_train_plm.notables.cc index 4b431b90..9dca9e8d 100644 --- a/phrasinator/gibbs_train_plm.notables.cc +++ b/phrasinator/gibbs_train_plm.notables.cc @@ -293,7 +293,7 @@ struct UniphraseLM { int main(int argc, char** argv) { po::variables_map conf; InitCommandLine(argc, argv, &conf); - shared_ptr prng; + boost::shared_ptr prng; if (conf.count("random_seed")) prng.reset(new MT19937(conf["random_seed"].as())); else diff --git a/training/mpi_batch_optimize.cc b/training/mpi_batch_optimize.cc index 046e921c..9f12dba9 100644 --- a/training/mpi_batch_optimize.cc +++ b/training/mpi_batch_optimize.cc @@ -29,7 +29,6 @@ namespace mpi = boost::mpi; #include "sparse_vector.h" using namespace std; -using boost::shared_ptr; namespace po = boost::program_options; bool InitCommandLine(int argc, char** argv, po::variables_map* conf) { @@ -270,7 +269,7 @@ int main(int argc, char** argv) { } Weights::InitFromFile(conf["means"].as(), &means); } - shared_ptr o; + boost::shared_ptr o; if (rank == 0) { const string omethod = conf["optimization_method"].as(); if (omethod == "rprop") diff --git a/training/mr_optimize_reduce.cc b/training/mr_optimize_reduce.cc index 15e28fa1..461e6b5f 100644 --- a/training/mr_optimize_reduce.cc +++ b/training/mr_optimize_reduce.cc @@ -15,7 +15,6 @@ #include "sparse_vector.h" using namespace std; -using boost::shared_ptr; namespace po = boost::program_options; void SanityCheck(const vector& w) { @@ -102,7 +101,7 @@ int main(int argc, char** argv) { } Weights::InitFromFile(conf["means"].as(), &means); } - shared_ptr o; + boost::shared_ptr o; const string omethod = conf["optimization_method"].as(); if (omethod == "rprop") o.reset(new RPropOptimizer(num_feats)); // TODO add configuration diff --git a/training/optimize_test.cc b/training/optimize_test.cc index fe7ca70f..bff2ca03 100644 --- a/training/optimize_test.cc +++ b/training/optimize_test.cc @@ -102,7 +102,7 @@ void TestOnline() { size_t N = 20; double C = 1.0; double eta0 = 0.2; - shared_ptr r(new ExponentialDecayLearningRate(N, eta0, 0.85)); + std::tr1::shared_ptr r(new ExponentialDecayLearningRate(N, eta0, 0.85)); //shared_ptr r(new StandardLearningRate(N, eta0)); CumulativeL1OnlineOptimizer opt(r, N, C, std::vector()); assert(r->eta(10) < r->eta(1)); diff --git a/utils/atools.cc b/utils/atools.cc index c0a91731..ba56dd6c 100644 --- a/utils/atools.cc +++ b/utils/atools.cc @@ -12,7 +12,6 @@ namespace po = boost::program_options; using namespace std; -using boost::shared_ptr; struct Command { virtual ~Command() {} @@ -348,10 +347,10 @@ int main(int argc, char **argv) { } } if (line1.empty() && !*in1) break; - shared_ptr > out(new Array2D); - shared_ptr > a1 = AlignmentPharaoh::ReadPharaohAlignmentGrid(line1); + boost::shared_ptr > out(new Array2D); + boost::shared_ptr > a1 = AlignmentPharaoh::ReadPharaohAlignmentGrid(line1); if (in2) { - shared_ptr > a2 = AlignmentPharaoh::ReadPharaohAlignmentGrid(line2); + boost::shared_ptr > a2 = AlignmentPharaoh::ReadPharaohAlignmentGrid(line2); cmd.Apply(*a1, *a2, out.get()); } else { Array2D dummy; -- cgit v1.2.3 From 0c7e078d14dd7078ec4a5b3e77007609aec5e54c Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Fri, 23 Mar 2012 17:48:38 -0400 Subject: pf test --- gi/pf/mh_test.cc | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gi/pf/pf_test.cc | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/ccrp.h | 6 ++- 3 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 gi/pf/mh_test.cc create mode 100644 gi/pf/pf_test.cc (limited to 'utils') diff --git a/gi/pf/mh_test.cc b/gi/pf/mh_test.cc new file mode 100644 index 00000000..296e7285 --- /dev/null +++ b/gi/pf/mh_test.cc @@ -0,0 +1,148 @@ +#include "ccrp.h" + +#include +#include + +#include "tdict.h" +#include "transliterations.h" + +using namespace std; + +MT19937 rng; + +static bool verbose = false; + +struct Model { + + Model() : bp(), base(0.2, 0.6) , ccrps(5, CCRP(0.8, 0.5)) {} + + double p0(int x) const { + assert(x > 0); + assert(x < 5); + return 1.0/4.0; + } + + double llh() const { + double lh = bp + base.log_crp_prob(); + for (int ctx = 1; ctx < 5; ++ctx) + lh += ccrps[ctx].log_crp_prob(); + return lh; + } + + double prob(int ctx, int x) const { + assert(ctx > 0 && ctx < 5); + return ccrps[ctx].prob(x, base.prob(x, p0(x))); + } + + void increment(int ctx, int x) { + assert(ctx > 0 && ctx < 5); + if (ccrps[ctx].increment(x, base.prob(x, p0(x)), &rng)) { + if (base.increment(x, p0(x), &rng)) { + bp += log(1.0 / 4.0); + } + } + } + + // this is just a biased estimate + double est_base_prob(int x) { + return (x + 1) * x / 40.0; + } + + void increment_is(int ctx, int x) { + assert(ctx > 0 && ctx < 5); + SampleSet ss; + const int PARTICLES = 25; + vector > s1s(PARTICLES, CCRP(0.5,0.5)); + vector > sbs(PARTICLES, CCRP(0.5,0.5)); + vector sp0s(PARTICLES); + + CCRP s1 = ccrps[ctx]; + CCRP sb = base; + double sp0 = bp; + for (int pp = 0; pp < PARTICLES; ++pp) { + if (pp > 0) { + ccrps[ctx] = s1; + base = sb; + bp = sp0; + } + + double q = 1; + double gamma = 1; + double est_p = est_base_prob(x); + //base.prob(x, p0(x)) + rng.next() * 0.1; + if (ccrps[ctx].increment(x, est_p, &rng, &q)) { + gamma = q * base.prob(x, p0(x)); + q *= est_p; + if (verbose) cerr << "(DP-base draw) "; + double qq = -1; + if (base.increment(x, p0(x), &rng, &qq)) { + if (verbose) cerr << "(G0 draw) "; + bp += log(p0(x)); + qq *= p0(x); + } + } else { gamma = q; } + double w = gamma / q; + if (verbose) + cerr << "gamma=" << gamma << " q=" << q << "\tw=" << w << endl; + ss.add(w); + s1s[pp] = ccrps[ctx]; + sbs[pp] = base; + sp0s[pp] = bp; + } + int ps = rng.SelectSample(ss); + ccrps[ctx] = s1s[ps]; + base = sbs[ps]; + bp = sp0s[ps]; + if (verbose) { + cerr << "SELECTED: " << ps << endl; + static int cc = 0; cc++; if (cc ==10) exit(1); + } + } + + void decrement(int ctx, int x) { + assert(ctx > 0 && ctx < 5); + if (ccrps[ctx].decrement(x, &rng)) { + if (base.decrement(x, &rng)) { + bp -= log(p0(x)); + } + } + } + + double bp; + CCRP base; + vector > ccrps; + +}; + +int main(int argc, char** argv) { + if (argc > 1) { verbose = true; } + vector counts(15, 0); + vector tcounts(15, 0); + int points[] = {1,2, 2,2, 3,2, 4,1, 3, 4, 3, 3, 2, 3, 4, 1, 4, 1, 3, 2, 1, 3, 1, 4, 0, 0}; + double tlh = 0; + double tt = 0; + for (int n = 0; n < 1000; ++n) { + if (n % 10 == 0) cerr << '.'; + if ((n+1) % 400 == 0) cerr << " [" << (n+1) << "]\n"; + Model m; + for (int *x = points; *x; x += 2) + m.increment(x[0], x[1]); + + for (int j = 0; j < 24; ++j) { + for (int *x = points; *x; x += 2) { + if (rng.next() < 0.8) { + m.decrement(x[0], x[1]); + m.increment_is(x[0], x[1]); + } + } + } + counts[m.base.num_customers()]++; + tcounts[m.base.num_tables()]++; + tlh += m.llh(); + tt += 1.0; + } + cerr << "mean LLH = " << (tlh / tt) << endl; + for (int i = 0; i < 15; ++i) + cerr << i << ": " << (counts[i] / tt) << "\t" << (tcounts[i] / tt) << endl; +} + diff --git a/gi/pf/pf_test.cc b/gi/pf/pf_test.cc new file mode 100644 index 00000000..296e7285 --- /dev/null +++ b/gi/pf/pf_test.cc @@ -0,0 +1,148 @@ +#include "ccrp.h" + +#include +#include + +#include "tdict.h" +#include "transliterations.h" + +using namespace std; + +MT19937 rng; + +static bool verbose = false; + +struct Model { + + Model() : bp(), base(0.2, 0.6) , ccrps(5, CCRP(0.8, 0.5)) {} + + double p0(int x) const { + assert(x > 0); + assert(x < 5); + return 1.0/4.0; + } + + double llh() const { + double lh = bp + base.log_crp_prob(); + for (int ctx = 1; ctx < 5; ++ctx) + lh += ccrps[ctx].log_crp_prob(); + return lh; + } + + double prob(int ctx, int x) const { + assert(ctx > 0 && ctx < 5); + return ccrps[ctx].prob(x, base.prob(x, p0(x))); + } + + void increment(int ctx, int x) { + assert(ctx > 0 && ctx < 5); + if (ccrps[ctx].increment(x, base.prob(x, p0(x)), &rng)) { + if (base.increment(x, p0(x), &rng)) { + bp += log(1.0 / 4.0); + } + } + } + + // this is just a biased estimate + double est_base_prob(int x) { + return (x + 1) * x / 40.0; + } + + void increment_is(int ctx, int x) { + assert(ctx > 0 && ctx < 5); + SampleSet ss; + const int PARTICLES = 25; + vector > s1s(PARTICLES, CCRP(0.5,0.5)); + vector > sbs(PARTICLES, CCRP(0.5,0.5)); + vector sp0s(PARTICLES); + + CCRP s1 = ccrps[ctx]; + CCRP sb = base; + double sp0 = bp; + for (int pp = 0; pp < PARTICLES; ++pp) { + if (pp > 0) { + ccrps[ctx] = s1; + base = sb; + bp = sp0; + } + + double q = 1; + double gamma = 1; + double est_p = est_base_prob(x); + //base.prob(x, p0(x)) + rng.next() * 0.1; + if (ccrps[ctx].increment(x, est_p, &rng, &q)) { + gamma = q * base.prob(x, p0(x)); + q *= est_p; + if (verbose) cerr << "(DP-base draw) "; + double qq = -1; + if (base.increment(x, p0(x), &rng, &qq)) { + if (verbose) cerr << "(G0 draw) "; + bp += log(p0(x)); + qq *= p0(x); + } + } else { gamma = q; } + double w = gamma / q; + if (verbose) + cerr << "gamma=" << gamma << " q=" << q << "\tw=" << w << endl; + ss.add(w); + s1s[pp] = ccrps[ctx]; + sbs[pp] = base; + sp0s[pp] = bp; + } + int ps = rng.SelectSample(ss); + ccrps[ctx] = s1s[ps]; + base = sbs[ps]; + bp = sp0s[ps]; + if (verbose) { + cerr << "SELECTED: " << ps << endl; + static int cc = 0; cc++; if (cc ==10) exit(1); + } + } + + void decrement(int ctx, int x) { + assert(ctx > 0 && ctx < 5); + if (ccrps[ctx].decrement(x, &rng)) { + if (base.decrement(x, &rng)) { + bp -= log(p0(x)); + } + } + } + + double bp; + CCRP base; + vector > ccrps; + +}; + +int main(int argc, char** argv) { + if (argc > 1) { verbose = true; } + vector counts(15, 0); + vector tcounts(15, 0); + int points[] = {1,2, 2,2, 3,2, 4,1, 3, 4, 3, 3, 2, 3, 4, 1, 4, 1, 3, 2, 1, 3, 1, 4, 0, 0}; + double tlh = 0; + double tt = 0; + for (int n = 0; n < 1000; ++n) { + if (n % 10 == 0) cerr << '.'; + if ((n+1) % 400 == 0) cerr << " [" << (n+1) << "]\n"; + Model m; + for (int *x = points; *x; x += 2) + m.increment(x[0], x[1]); + + for (int j = 0; j < 24; ++j) { + for (int *x = points; *x; x += 2) { + if (rng.next() < 0.8) { + m.decrement(x[0], x[1]); + m.increment_is(x[0], x[1]); + } + } + } + counts[m.base.num_customers()]++; + tcounts[m.base.num_tables()]++; + tlh += m.llh(); + tt += 1.0; + } + cerr << "mean LLH = " << (tlh / tt) << endl; + for (int i = 0; i < 15; ++i) + cerr << i << ": " << (counts[i] / tt) << "\t" << (tcounts[i] / tt) << endl; +} + diff --git a/utils/ccrp.h b/utils/ccrp.h index 390d4994..8635b422 100644 --- a/utils/ccrp.h +++ b/utils/ccrp.h @@ -97,8 +97,10 @@ class CCRP { } // returns +1 or 0 indicating whether a new table was opened + // p = probability with which the particular table was selected + // excluding p0 template - int increment(const Dish& dish, const T& p0, MT19937* rng) { + int increment(const Dish& dish, const T& p0, MT19937* rng, T* p = NULL) { DishLocations& loc = dish_locs_[dish]; bool share_table = false; if (loc.total_dish_count_) { @@ -112,6 +114,7 @@ class CCRP { ti != loc.table_counts_.end(); ++ti) { r -= (*ti - discount_); if (r <= 0.0) { + if (p) { *p = T(*ti - discount_) / T(strength_ + num_customers_); } ++(*ti); break; } @@ -123,6 +126,7 @@ class CCRP { } } else { loc.table_counts_.push_back(1u); + if (p) { *p = T(strength_ + discount_ * num_tables_) / T(strength_ + num_customers_); } ++num_tables_; } ++loc.total_dish_count_; -- cgit v1.2.3 From b6eede632af4fa58a6f5325ee0d059c02a898b9f Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Sat, 24 Mar 2012 23:04:46 -0400 Subject: rename aligner, add support for distinguishing translation / transliteration --- decoder/aligner.cc | 4 +- decoder/ff_wordalign.cc | 1 - mteval/aer_scorer.cc | 6 +-- utils/Makefile.am | 2 +- utils/alignment_io.cc | 97 ++++++++++++++++++++++++++++++++++++++++++++++ utils/alignment_io.h | 42 ++++++++++++++++++++ utils/alignment_pharaoh.cc | 77 ------------------------------------ utils/alignment_pharaoh.h | 14 ------- utils/atools.cc | 8 ++-- 9 files changed, 149 insertions(+), 102 deletions(-) create mode 100644 utils/alignment_io.cc create mode 100644 utils/alignment_io.h delete mode 100644 utils/alignment_pharaoh.cc delete mode 100644 utils/alignment_pharaoh.h (limited to 'utils') diff --git a/decoder/aligner.cc b/decoder/aligner.cc index 53e059fb..232e022a 100644 --- a/decoder/aligner.cc +++ b/decoder/aligner.cc @@ -11,7 +11,7 @@ #include "sentence_metadata.h" #include "inside_outside.h" #include "viterbi.h" -#include "alignment_pharaoh.h" +#include "alignment_io.h" using namespace std; @@ -300,7 +300,7 @@ void AlignerTools::WriteAlignment(const Lattice& src_lattice, cerr << grid << endl; } (*out) << TD::GetString(src_sent) << " ||| " << TD::GetString(trg_sent) << " ||| "; - AlignmentPharaoh::SerializePharaohFormat(grid, out); + AlignmentIO::SerializePharaohFormat(grid, out); } }; diff --git a/decoder/ff_wordalign.cc b/decoder/ff_wordalign.cc index 9e7c618e..decdf9bc 100644 --- a/decoder/ff_wordalign.cc +++ b/decoder/ff_wordalign.cc @@ -15,7 +15,6 @@ #include "factored_lexicon_helper.h" #include "verbose.h" -#include "alignment_pharaoh.h" #include "stringlib.h" #include "sentence_metadata.h" #include "hg.h" diff --git a/mteval/aer_scorer.cc b/mteval/aer_scorer.cc index edd4390f..ae3192d4 100644 --- a/mteval/aer_scorer.cc +++ b/mteval/aer_scorer.cc @@ -5,7 +5,7 @@ #include #include "tdict.h" -#include "alignment_pharaoh.h" +#include "alignment_io.h" using namespace std; @@ -85,7 +85,7 @@ AERScorer::AERScorer(const vector >& refs, const string& src) : s cerr << "AERScorer can only take a single reference!\n"; abort(); } - ref_ = AlignmentPharaoh::ReadPharaohAlignmentGrid(TD::GetString(refs.front())); + ref_ = AlignmentIO::ReadPharaohAlignmentGrid(TD::GetString(refs.front())); } static inline bool Safe(const Array2D& a, int i, int j) { @@ -101,7 +101,7 @@ ScoreP AERScorer::ScoreCCandidate(const vector& shyp) const { ScoreP AERScorer::ScoreCandidate(const vector& shyp) const { boost::shared_ptr > hyp = - AlignmentPharaoh::ReadPharaohAlignmentGrid(TD::GetString(shyp)); + AlignmentIO::ReadPharaohAlignmentGrid(TD::GetString(shyp)); int m = 0; int r = 0; diff --git a/utils/Makefile.am b/utils/Makefile.am index 3ea21835..2fc6ae21 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -23,7 +23,7 @@ atools_SOURCES = atools.cc noinst_LIBRARIES = libutils.a libutils_a_SOURCES = \ - alignment_pharaoh.cc \ + alignment_io.cc \ b64tools.cc \ corpus_tools.cc \ dict.cc \ diff --git a/utils/alignment_io.cc b/utils/alignment_io.cc new file mode 100644 index 00000000..1d923f7f --- /dev/null +++ b/utils/alignment_io.cc @@ -0,0 +1,97 @@ +#include "utils/alignment_io.h" + +using namespace std; + +static bool is_digit(char x) { return x >= '0' && x <= '9'; } + +boost::shared_ptr > AlignmentIO::ReadPharaohAlignmentGrid(const string& al) { + int max_x = 0; + int max_y = 0; + int i = 0; + size_t pos = al.rfind(" ||| "); + if (pos != string::npos) { i = pos + 5; } + while (i < al.size()) { + if (al[i] == '\n' || al[i] == '\r') break; + int x = 0; + while(i < al.size() && is_digit(al[i])) { + x *= 10; + x += al[i] - '0'; + ++i; + } + if (x > max_x) max_x = x; + assert(i < al.size()); + if(al[i] != '-') { + cerr << "BAD ALIGNMENT: " << al << endl; + abort(); + } + ++i; + int y = 0; + while(i < al.size() && is_digit(al[i])) { + y *= 10; + y += al[i] - '0'; + ++i; + } + if (y > max_y) max_y = y; + while(i < al.size() && al[i] == ' ') { ++i; } + } + + boost::shared_ptr > grid(new Array2D(max_x + 1, max_y + 1)); + i = 0; + if (pos != string::npos) { i = pos + 5; } + while (i < al.size()) { + if (al[i] == '\n' || al[i] == '\r') break; + int x = 0; + while(i < al.size() && is_digit(al[i])) { + x *= 10; + x += al[i] - '0'; + ++i; + } + assert(i < al.size()); + assert(al[i] == '-'); + ++i; + int y = 0; + while(i < al.size() && is_digit(al[i])) { + y *= 10; + y += al[i] - '0'; + ++i; + } + (*grid)(x, y) = true; + while(i < al.size() && al[i] == ' ') { ++i; } + } + // cerr << *grid << endl; + return grid; +} + +void AlignmentIO::SerializePharaohFormat(const Array2D& alignment, ostream* o) { + ostream& out = *o; + bool need_space = false; + for (int i = 0; i < alignment.width(); ++i) + for (int j = 0; j < alignment.height(); ++j) + if (alignment(i,j)) { + if (need_space) out << ' '; else need_space = true; + out << i << '-' << j; + } + out << endl; +} + +void AlignmentIO::SerializeTypedAlignment(const Array2D& alignment, ostream* o) { + ostream& out = *o; + bool need_space = false; + for (int i = 0; i < alignment.width(); ++i) + for (int j = 0; j < alignment.height(); ++j) { + const AlignmentType& aij = alignment(i,j); + if (aij != kNONE) { + if (need_space) out << ' '; else need_space = true; + if (aij == kTRANSLATION) {} + else if (aij == kTRANSLITERATION) { + out << 'T' << ':'; + } else { + cerr << "\nUnexpected alignment point type: " << static_cast(aij) << endl; + abort(); + } + out << i << '-' << j; + } + } + out << endl; +} + diff --git a/utils/alignment_io.h b/utils/alignment_io.h new file mode 100644 index 00000000..36bcecd7 --- /dev/null +++ b/utils/alignment_io.h @@ -0,0 +1,42 @@ +#ifndef _ALIGNMENT_IO_H_ +#define _ALIGNMENT_IO_H_ + +#include +#include +#include +#include "array2d.h" + +struct AlignmentIO { + enum AlignmentType { kNONE = 0, kTRANSLATION = 1, kTRANSLITERATION = 2 }; + + static boost::shared_ptr > ReadPharaohAlignmentGrid(const std::string& al); + static void SerializePharaohFormat(const Array2D& alignment, std::ostream* out); + static void SerializeTypedAlignment(const Array2D& alignment, std::ostream* out); +}; + +inline std::ostream& operator<<(std::ostream& os, const Array2D& m) { + os << ' '; + for (int j=0; j - -using namespace std; - -static bool is_digit(char x) { return x >= '0' && x <= '9'; } - -boost::shared_ptr > AlignmentPharaoh::ReadPharaohAlignmentGrid(const string& al) { - int max_x = 0; - int max_y = 0; - int i = 0; - size_t pos = al.rfind(" ||| "); - if (pos != string::npos) { i = pos + 5; } - while (i < al.size()) { - if (al[i] == '\n' || al[i] == '\r') break; - int x = 0; - while(i < al.size() && is_digit(al[i])) { - x *= 10; - x += al[i] - '0'; - ++i; - } - if (x > max_x) max_x = x; - assert(i < al.size()); - if(al[i] != '-') { - cerr << "BAD ALIGNMENT: " << al << endl; - abort(); - } - ++i; - int y = 0; - while(i < al.size() && is_digit(al[i])) { - y *= 10; - y += al[i] - '0'; - ++i; - } - if (y > max_y) max_y = y; - while(i < al.size() && al[i] == ' ') { ++i; } - } - - boost::shared_ptr > grid(new Array2D(max_x + 1, max_y + 1)); - i = 0; - if (pos != string::npos) { i = pos + 5; } - while (i < al.size()) { - if (al[i] == '\n' || al[i] == '\r') break; - int x = 0; - while(i < al.size() && is_digit(al[i])) { - x *= 10; - x += al[i] - '0'; - ++i; - } - assert(i < al.size()); - assert(al[i] == '-'); - ++i; - int y = 0; - while(i < al.size() && is_digit(al[i])) { - y *= 10; - y += al[i] - '0'; - ++i; - } - (*grid)(x, y) = true; - while(i < al.size() && al[i] == ' ') { ++i; } - } - // cerr << *grid << endl; - return grid; -} - -void AlignmentPharaoh::SerializePharaohFormat(const Array2D& alignment, ostream* out) { - bool need_space = false; - for (int i = 0; i < alignment.width(); ++i) - for (int j = 0; j < alignment.height(); ++j) - if (alignment(i,j)) { - if (need_space) (*out) << ' '; else need_space = true; - (*out) << i << '-' << j; - } - (*out) << endl; -} - diff --git a/utils/alignment_pharaoh.h b/utils/alignment_pharaoh.h deleted file mode 100644 index d111c8bf..00000000 --- a/utils/alignment_pharaoh.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _PHARAOH_ALIGNMENT_H_ -#define _PHARAOH_ALIGNMENT_H_ - -#include -#include -#include -#include "array2d.h" - -struct AlignmentPharaoh { - static boost::shared_ptr > ReadPharaohAlignmentGrid(const std::string& al); - static void SerializePharaohFormat(const Array2D& alignment, std::ostream* out); -}; - -#endif diff --git a/utils/atools.cc b/utils/atools.cc index ba56dd6c..bce7822e 100644 --- a/utils/atools.cc +++ b/utils/atools.cc @@ -8,7 +8,7 @@ #include #include "filelib.h" -#include "alignment_pharaoh.h" +#include "alignment_io.h" namespace po = boost::program_options; using namespace std; @@ -348,9 +348,9 @@ int main(int argc, char **argv) { } if (line1.empty() && !*in1) break; boost::shared_ptr > out(new Array2D); - boost::shared_ptr > a1 = AlignmentPharaoh::ReadPharaohAlignmentGrid(line1); + boost::shared_ptr > a1 = AlignmentIO::ReadPharaohAlignmentGrid(line1); if (in2) { - boost::shared_ptr > a2 = AlignmentPharaoh::ReadPharaohAlignmentGrid(line2); + boost::shared_ptr > a2 = AlignmentIO::ReadPharaohAlignmentGrid(line2); cmd.Apply(*a1, *a2, out.get()); } else { Array2D dummy; @@ -358,7 +358,7 @@ int main(int argc, char **argv) { } if (cmd.Result() == 1) { - AlignmentPharaoh::SerializePharaohFormat(*out, &cout); + AlignmentIO::SerializePharaohFormat(*out, &cout); } } if (cmd.Result() == 2) -- cgit v1.2.3