diff options
Diffstat (limited to 'decoder')
| -rw-r--r-- | decoder/tree2string_translator.cc | 4 | ||||
| -rw-r--r-- | decoder/tree_fragment.cc | 17 | ||||
| -rw-r--r-- | decoder/tree_fragment.h | 7 | 
3 files changed, 26 insertions, 2 deletions
| 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<unsigned>& r) : lhs(nttype), rhs(r) {} +  TreeFragmentProduction(int nttype, const std::vector<unsigned>& r) : lhs(nttype), rhs(r), span(std::make_pair<short,short>(-1,-1)) {}    unsigned lhs;    std::vector<unsigned> rhs; +  std::pair<short, short> 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; | 
