Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Dyer committed May 27, 2012
1 parent 425a630 commit bfa5c48
Show file tree
Hide file tree
Showing 29 changed files with 270 additions and 450 deletions.
2 changes: 1 addition & 1 deletion decoder/bottom_up_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ActiveChart {
const GrammarIter* ni = gptr_->Extend(symbol);
if (!ni) return;
Hypergraph::TailNodeVector na(ant_nodes_.size() + 1);
for (int i = 0; i < ant_nodes_.size(); ++i)
for (unsigned i = 0; i < ant_nodes_.size(); ++i)
na[i] = ant_nodes_[i];
na[ant_nodes_.size()] = node_index;
out_cell->push_back(ActiveItem(ni, na, lattice_cost));
Expand Down
132 changes: 61 additions & 71 deletions decoder/hg.cc

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions decoder/hg.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Hypergraph {
Hypergraph() : is_linear_chain_(false) {}

// SmallVector is a fast, small vector<int> implementation for sizes <= 2
typedef SmallVectorInt TailNodeVector; // indices in nodes_
typedef SmallVectorUnsigned TailNodeVector; // indices in nodes_
typedef std::vector<int> EdgesVector; // indices in edges_

// TODO get rid of cat_?
Expand Down Expand Up @@ -457,8 +457,6 @@ class Hypergraph {

void PruneUnreachable(int goal_node_id); // DEPRECATED

void RemoveNoncoaccessibleStates(int goal_node_id = -1);

// remove edges from the hypergraph if prune_edge[edge_id] is true
// note: if run_inside_algorithm is false, then consumers may be unhappy if you pruned nodes that are built on by nodes that are kept.
void PruneEdges(const EdgeMask& prune_edge, bool run_inside_algorithm = false);
Expand Down Expand Up @@ -524,7 +522,7 @@ class Hypergraph {

template <class V>
void visit_edges(V &v) {
for (int i=0;i<edges_.size();++i)
for (unsigned i=0;i<edges_.size();++i)
v(edges_[i].head_node_,i,edges_[i]);
}

Expand Down
28 changes: 14 additions & 14 deletions decoder/hg_intersect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ using namespace std;
struct RuleFilter {
unordered_map<vector<WordID>, bool, boost::hash<vector<WordID> > > exists_;
bool true_lattice;
RuleFilter(const Lattice& target, int max_phrase_size) {
RuleFilter(const Lattice& target, unsigned max_phrase_size) {
true_lattice = false;
for (int i = 0; i < target.size(); ++i) {
for (unsigned i = 0; i < target.size(); ++i) {
vector<WordID> phrase;
int lim = min(static_cast<int>(target.size()), i + max_phrase_size);
for (int j = i; j < lim; ++j) {
const unsigned lim = min(static_cast<unsigned>(target.size()), i + max_phrase_size);
for (unsigned j = i; j < lim; ++j) {
if (target[j].size() > 1) { true_lattice = true; break; }
phrase.push_back(target[j][0].label);
exists_[phrase] = true;
Expand All @@ -37,10 +37,10 @@ struct RuleFilter {
// TODO do some smarter filtering for lattices
if (true_lattice) return false; // don't filter "true lattice" input
const vector<WordID>& e = r.e();
for (int i = 0; i < e.size(); ++i) {
for (unsigned i = 0; i < e.size(); ++i) {
if (e[i] <= 0) continue;
vector<WordID> phrase;
for (int j = i; j < e.size(); ++j) {
for (unsigned j = i; j < e.size(); ++j) {
if (e[j] <= 0) break;
phrase.push_back(e[j]);
if (exists_.count(phrase) == 0) return true;
Expand All @@ -55,7 +55,7 @@ static bool FastLinearIntersect(const Lattice& target, Hypergraph* hg) {
vector<bool> prune(hg->edges_.size(), false);
set<int> cov;
map<const TRule*, TRulePtr> inverted_rules;
for (int i = 0; i < prune.size(); ++i) {
for (unsigned i = 0; i < prune.size(); ++i) {
Hypergraph::Edge& edge = hg->edges_[i];
if (edge.Arity() == 0) {
const int trg_index = edge.prev_i_;
Expand Down Expand Up @@ -87,28 +87,28 @@ bool HG::Intersect(const Lattice& target, Hypergraph* hg) {

vector<bool> rem(hg->edges_.size(), false);
const RuleFilter filter(target, 15); // TODO make configurable
for (int i = 0; i < rem.size(); ++i)
for (unsigned i = 0; i < rem.size(); ++i)
rem[i] = filter(*hg->edges_[i].rule_);
hg->PruneEdges(rem, true);

const int nedges = hg->edges_.size();
const int nnodes = hg->nodes_.size();
const unsigned nedges = hg->edges_.size();
const unsigned nnodes = hg->nodes_.size();

TextGrammar* g = new TextGrammar;
GrammarPtr gp(g);
vector<int> cats(nnodes);
// each node in the translation forest becomes a "non-terminal" in the new
// grammar, create the labels here
const string kSEP = "_";
for (int i = 0; i < nnodes; ++i) {
for (unsigned i = 0; i < nnodes; ++i) {
const char* pstr = "CAT";
if (hg->nodes_[i].cat_ < 0)
pstr = TD::Convert(-hg->nodes_[i].cat_);
cats[i] = TD::Convert(pstr + kSEP + lexical_cast<string>(i)) * -1;
}

// construct the grammar
for (int i = 0; i < nedges; ++i) {
for (unsigned i = 0; i < nedges; ++i) {
const Hypergraph::Edge& edge = hg->edges_[i];
const vector<WordID>& tgt = edge.rule_->e();
const vector<WordID>& src = edge.rule_->f();
Expand All @@ -122,7 +122,7 @@ bool HG::Intersect(const Lattice& target, Hypergraph* hg) {
e.resize(src.size()); // parses using the source side!
Hypergraph::TailNodeVector tn(edge.tail_nodes_.size());
int ntc = 0;
for (int j = 0; j < tgt.size(); ++j) {
for (unsigned j = 0; j < tgt.size(); ++j) {
const WordID& cur = tgt[j];
if (cur > 0) {
f[j] = cur;
Expand All @@ -133,7 +133,7 @@ bool HG::Intersect(const Lattice& target, Hypergraph* hg) {
}
}
ntc = 0;
for (int j = 0; j < src.size(); ++j) {
for (unsigned j = 0; j < src.size(); ++j) {
const WordID& cur = src[j];
if (cur > 0) {
e[j] = cur;
Expand Down
4 changes: 2 additions & 2 deletions decoder/hg_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct HGReader : public JSONParser {
hg.ConnectEdgeToHeadNode(&hg.edges_[in_edges[i]], node);
}
}
void CreateEdge(const TRulePtr& rule, FeatureVector* feats, const SmallVectorInt& tail) {
void CreateEdge(const TRulePtr& rule, FeatureVector* feats, const SmallVectorUnsigned& tail) {
Hypergraph::Edge* edge = hg.AddEdge(rule, tail);
feats->swap(edge->feature_values_);
edge->i_ = spans[0];
Expand Down Expand Up @@ -229,7 +229,7 @@ struct HGReader : public JSONParser {
}
string rp;
string cat;
SmallVectorInt tail;
SmallVectorUnsigned tail;
vector<int> in_edges;
TRulePtr cur_rule;
map<int, TRulePtr> rules;
Expand Down
2 changes: 1 addition & 1 deletion decoder/inside_outside.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void Outside(const Hypergraph& hg,
) {
assert(result);
const int num_nodes = hg.nodes_.size();
assert(inside_score.size() == num_nodes);
assert(static_cast<int>(inside_score.size()) == num_nodes);
std::vector<WeightType>& outside_score = *result;
outside_score.clear();
outside_score.resize(num_nodes);
Expand Down
28 changes: 14 additions & 14 deletions decoder/maxtrans_blunsom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct Candidate {
prob_t p = prob_t::One();
// cerr << "\nEstimating application of " << in_edge.rule_->AsString() << endl;
vector<const vector<WordID>* > ants(tail.size());
for (int i = 0; i < tail.size(); ++i) {
for (unsigned i = 0; i < tail.size(); ++i) {
const Candidate& ant = *D[in_edge.tail_nodes_[i]][j_[i]];
ants[i] = &ant.state_;
assert(ant.IsIncorporatedIntoHypergraph());
Expand All @@ -99,7 +99,7 @@ ostream& operator<<(ostream& os, const Candidate& cand) {
else { os << "+LM_node=" << cand.node_index_; }
os << " edge=" << cand.in_edge_->id_;
os << " j=<";
for (int i = 0; i < cand.j_.size(); ++i)
for (unsigned i = 0; i < cand.j_.size(); ++i)
os << (i==0 ? "" : " ") << cand.j_[i];
os << "> vit=" << log(cand.inside_prob_);
os << " est=" << log(cand.est_prob_);
Expand Down Expand Up @@ -127,7 +127,7 @@ struct CandidateUniquenessHash {
size_t operator()(const Candidate* c) const {
size_t x = 5381;
x = ((x << 5) + x) ^ c->in_edge_->id_;
for (int i = 0; i < c->j_.size(); ++i)
for (unsigned i = 0; i < c->j_.size(); ++i)
x = ((x << 5) + x) ^ c->j_[i];
return x;
}
Expand All @@ -154,12 +154,12 @@ class MaxTransBeamSearch {
}

void Apply() {
int num_nodes = in.nodes_.size();
int goal_id = num_nodes - 1;
int pregoal = goal_id - 1;
const unsigned num_nodes = in.nodes_.size();
const unsigned goal_id = num_nodes - 1;
const unsigned pregoal = goal_id - 1;
assert(in.nodes_[pregoal].out_edges_.size() == 1);
cerr << " ";
for (int i = 0; i < in.nodes_.size(); ++i) {
for (unsigned i = 0; i < in.nodes_.size(); ++i) {
cerr << '.';
KBest(i, i == goal_id);
}
Expand All @@ -174,9 +174,9 @@ class MaxTransBeamSearch {

private:
void FreeAll() {
for (int i = 0; i < D.size(); ++i) {
for (unsigned i = 0; i < D.size(); ++i) {
CandidateList& D_i = D[i];
for (int j = 0; j < D_i.size(); ++j)
for (unsigned j = 0; j < D_i.size(); ++j)
delete D_i[j];
}
D.clear();
Expand Down Expand Up @@ -216,7 +216,7 @@ class MaxTransBeamSearch {
CandidateList freelist;
cand.reserve(in_edges.size());
UniqueCandidateSet unique_cands;
for (int i = 0; i < in_edges.size(); ++i) {
for (unsigned i = 0; i < in_edges.size(); ++i) {
const Hypergraph::Edge& edge = in.edges_[in_edges[i]];
const JVector j(edge.tail_nodes_.size(), 0);
cand.push_back(new Candidate(edge, j, D, is_goal));
Expand All @@ -242,20 +242,20 @@ class MaxTransBeamSearch {
sort(D_v.begin(), D_v.end(), EstProbSorter());
// cerr << " expanded to " << D_v.size() << " nodes\n";

for (int i = 0; i < cand.size(); ++i)
for (unsigned i = 0; i < cand.size(); ++i)
delete cand[i];
// freelist is necessary since even after an item merged, it still stays in
// the unique set so it can't be deleted til now
for (int i = 0; i < freelist.size(); ++i)
for (unsigned i = 0; i < freelist.size(); ++i)
delete freelist[i];
}

void PushSucc(const Candidate& item, const bool is_goal, CandidateHeap* pcand, UniqueCandidateSet* cs) {
CandidateHeap& cand = *pcand;
for (int i = 0; i < item.j_.size(); ++i) {
for (unsigned i = 0; i < item.j_.size(); ++i) {
JVector j = item.j_;
++j[i];
if (j[i] < D[item.in_edge_->tail_nodes_[i]].size()) {
if (static_cast<unsigned>(j[i]) < D[item.in_edge_->tail_nodes_[i]].size()) {
Candidate query_unique(*item.in_edge_, j);
if (cs->count(&query_unique) == 0) {
Candidate* new_cand = new Candidate(*item.in_edge_, j, D, is_goal);
Expand Down
14 changes: 7 additions & 7 deletions decoder/scfg_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct SCFGTranslatorImpl {
{
if(conf.count("grammar")){
vector<string> gfiles = conf["grammar"].as<vector<string> >();
for (int i = 0; i < gfiles.size(); ++i) {
for (unsigned i = 0; i < gfiles.size(); ++i) {
if (!SILENT) cerr << "Reading SCFG grammar from " << gfiles[i] << endl;
TextGrammar* g = new TextGrammar(gfiles[i]);
g->SetMaxSpan(max_span_limit);
Expand Down Expand Up @@ -132,7 +132,7 @@ struct SCFGTranslatorImpl {
g->SetGrammarName("PassThrough");
glist.push_back(GrammarPtr(g));
}
for (int gi = 0; gi < glist.size(); ++gi) {
for (unsigned gi = 0; gi < glist.size(); ++gi) {
if(printGrammarsUsed)
cerr << "Using grammar::" << glist[gi]->GetGrammarName() << endl;
}
Expand All @@ -147,15 +147,15 @@ struct SCFGTranslatorImpl {
forest->Reweight(weights);
if (use_ctf_) {
Hypergraph::Node& goal_node = *(forest->nodes_.end()-1);
foreach(int edge_id, goal_node.in_edges_)
foreach(unsigned edge_id, goal_node.in_edges_)
RefineRule(forest->edges_[edge_id].rule_, ctf_iterations_);
double alpha = ctf_alpha_;
bool found_parse=false;
for (int i=-1; i < ctf_num_widenings_; ++i) {
cerr << "Coarse-to-fine source parse, alpha=" << alpha << endl;
found_parse = true;
Hypergraph refined_forest = *forest;
for (int j=0; j < ctf_iterations_; ++j) {
for (unsigned j=0; j < ctf_iterations_; ++j) {
cerr << viterbi_stats(refined_forest," Coarse forest",true,show_tree_structure_);
cerr << " Iteration " << (j+1) << ": Pruning forest... ";
refined_forest.BeamPruneInsideOutside(1.0, false, alpha, NULL);
Expand All @@ -178,7 +178,7 @@ struct SCFGTranslatorImpl {
if (!found_parse){
if (ctf_exhaustive_){
cerr << "Last resort: refining coarse forest without pruning...";
for (int j=0; j < ctf_iterations_; ++j) {
for (unsigned j=0; j < ctf_iterations_; ++j) {
if (RefineForest(forest)){
cerr << " Refinement succeeded." << endl;
forest->Reweight(weights);
Expand Down Expand Up @@ -213,7 +213,7 @@ struct SCFGTranslatorImpl {
Hypergraph::Edge& edge = forest->edges_[edge_id];
std::vector<int> nt_positions;
TRulePtr& coarse_rule_ptr = edge.rule_;
for(int i=0; i< coarse_rule_ptr->f_.size(); ++i){
for(unsigned i=0; i< coarse_rule_ptr->f_.size(); ++i){
if (coarse_rule_ptr->f_[i] < 0)
nt_positions.push_back(i);
}
Expand All @@ -225,7 +225,7 @@ struct SCFGTranslatorImpl {
// fine rules apply only if state splits on tail nodes match fine rule nonterminals
foreach(TRulePtr& fine_rule_ptr, *(coarse_rule_ptr->fine_rules_)) {
Hypergraph::TailNodeVector tail;
for (int pos_i=0; pos_i<nt_positions.size(); ++pos_i){
for (unsigned pos_i=0; pos_i<nt_positions.size(); ++pos_i){
WordID fine_cat = fine_rule_ptr->f_[nt_positions[pos_i]];
Split2Node::iterator it =
s2n.find(StateSplit(edge.tail_nodes_[pos_i], fine_cat));
Expand Down
2 changes: 2 additions & 0 deletions decoder/trule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ namespace {
// callback for lexer
int n_assigned=0;
void assign_trule(const TRulePtr& new_rule, const unsigned int ctf_level, const TRulePtr& coarse_rule, void* extra) {
(void) ctf_level;
(void) coarse_rule;
TRule *assignto=(TRule *)extra;
*assignto=*new_rule;
++n_assigned;
Expand Down
4 changes: 2 additions & 2 deletions decoder/trule.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class TRule {

void ESubstitute(const std::vector<const std::vector<WordID>* >& var_values,
std::vector<WordID>* result) const {
int vc = 0;
unsigned vc = 0;
result->clear();
for (std::vector<WordID>::const_iterator i = e_.begin(); i != e_.end(); ++i) {
const WordID& c = *i;
Expand All @@ -95,7 +95,7 @@ class TRule {

void FSubstitute(const std::vector<const std::vector<WordID>* >& var_values,
std::vector<WordID>* result) const {
int vc = 0;
unsigned vc = 0;
result->clear();
for (std::vector<WordID>::const_iterator i = f_.begin(); i != f_.end(); ++i) {
const WordID& c = *i;
Expand Down
2 changes: 1 addition & 1 deletion phrasinator/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ gibbs_train_plm_LDADD = $(top_srcdir)/utils/libutils.a -lz
#head_bigram_model_SOURCES = head_bigram_model.cc
#head_bigram_model_LDADD = $(top_srcdir)/utils/libutils.a -lz

AM_CPPFLAGS = -funroll-loops -W -Wall -Wno-sign-compare $(GTEST_CPPFLAGS) -I$(top_srcdir)/utils -I$(top_srcdir)/decoder -I$(top_srcdir)/mteval
AM_CPPFLAGS = -funroll-loops -ffast-math -W -Wall -Werror -I$(top_srcdir)/utils
Loading

0 comments on commit bfa5c48

Please sign in to comment.