From d124d4aaa78b52b46f7ac8d7306be342d3405124 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Tue, 1 Jul 2014 13:49:47 -0400 Subject: track spans in t2s translation --- decoder/tree2string_translator.cc | 4 +++- decoder/tree_fragment.cc | 17 +++++++++++++++++ decoder/tree_fragment.h | 7 ++++++- 3 files changed, 26 insertions(+), 2 deletions(-) (limited to 'decoder') diff --git a/decoder/tree2string_translator.cc b/decoder/tree2string_translator.cc index c9c91a37..adc8dc89 100644 --- a/decoder/tree2string_translator.cc +++ b/decoder/tree2string_translator.cc @@ -332,7 +332,9 @@ struct Tree2StringTranslatorImpl { assert(tail.size() == r->Arity()); HG::Edge* new_edge = hg.AddEdge(r, tail); new_edge->feature_values_ = r->GetFeatureValues(); - // TODO: set i and j + auto& inspan = input_tree.nodes[s.task.input_node_idx].span; + new_edge->i_ = inspan.first; + new_edge->j_ = inspan.second; hg.ConnectEdgeToHeadNode(new_edge, &hg.nodes_[node_id]); } for (const auto& n : s.future_work) { diff --git a/decoder/tree_fragment.cc b/decoder/tree_fragment.cc index aad0b2c4..42f7793a 100644 --- a/decoder/tree_fragment.cc +++ b/decoder/tree_fragment.cc @@ -28,12 +28,14 @@ TreeFragment::TreeFragment(const StringPiece& tree, bool allow_frontier_sites) { unsigned cp = 0, symp = 0, np = 0; ParseRec(tree, allow_frontier_sites, cp, symp, np, &cp, &symp, &np); root = nodes.back().lhs; + if (!allow_frontier_sites) SetupSpansRec(open - 1, 0); //cerr << "ROOT: " << TD::Convert(root & ALL_MASK) << endl; //DebugRec(open - 1, &cerr); cerr << "\n"; } void TreeFragment::DebugRec(unsigned cur, ostream* out) const { *out << '(' << TD::Convert(nodes[cur].lhs & ALL_MASK); + // *out << "_{" << nodes[cur].span.first << ',' << nodes[cur].span.second << '}'; for (auto& x : nodes[cur].rhs) { *out << ' '; if (IsFrontier(x)) { @@ -47,6 +49,21 @@ void TreeFragment::DebugRec(unsigned cur, ostream* out) const { *out << ')'; } +// returns left + the number of terminals rooted at NT cur, +int TreeFragment::SetupSpansRec(unsigned cur, int left) { + int right = left; + for (auto& x : nodes[cur].rhs) { + if (IsRHS(x)) { + right = SetupSpansRec(x & ALL_MASK, right); + } else { + ++right; + } + } + nodes[cur].span.first = left; + nodes[cur].span.second = right; + return right; +} + // cp is the character index in the tree // np keeps track of the nodes (nonterminals) that have been built // symp keeps track of the terminal symbols that have been built diff --git a/decoder/tree_fragment.h b/decoder/tree_fragment.h index 8bb7251a..6b4842ee 100644 --- a/decoder/tree_fragment.h +++ b/decoder/tree_fragment.h @@ -43,9 +43,10 @@ inline bool IsTerminal(unsigned x) { struct TreeFragmentProduction { TreeFragmentProduction() {} - TreeFragmentProduction(int nttype, const std::vector& r) : lhs(nttype), rhs(r) {} + TreeFragmentProduction(int nttype, const std::vector& r) : lhs(nttype), rhs(r), span(std::make_pair(-1,-1)) {} unsigned lhs; std::vector rhs; + std::pair span; // the span of the node (in input, or not set for rules) }; // this data structure represents a tree or forest @@ -76,6 +77,10 @@ class TreeFragment { // np keeps track of the nodes (nonterminals) that have been built // symp keeps track of the terminal symbols that have been built void ParseRec(const StringPiece& tree, bool afs, unsigned cp, unsigned symp, unsigned np, unsigned* pcp, unsigned* psymp, unsigned* pnp); + + // used by constructor to set up span indices for logging/alignment purposes + int SetupSpansRec(unsigned cur, int left); + public: unsigned root; unsigned char frontier_sites; -- cgit v1.2.3