Welcome to mirror list, hosted at ThFree Co, Russian Federation.

InputTreeToForest.cpp « T2S « Syntax « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fda988d57b5fa079284086d977b32491d55198b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "InputTreeToForest.h"

#include <boost/unordered_map.hpp>

namespace Moses
{
namespace Syntax
{
namespace T2S
{

const F2S::Forest::Vertex *InputTreeToForest(const InputTree &tree,
                                             F2S::Forest &forest)
{
  forest.Clear();

  // Map from tree vertices to forest vertices.
  boost::unordered_map<const InputTree::Node*, F2S::Forest::Vertex*> vertexMap;

  // Create forest vertices (but not hyperedges) and fill in vertexMap.
  for (std::vector<InputTree::Node>::const_iterator p = tree.nodes.begin();
       p != tree.nodes.end(); ++p) {
    F2S::Forest::Vertex *v = new F2S::Forest::Vertex(p->pvertex);
    forest.vertices.push_back(v);
    vertexMap[&*p] = v;
  }

  // Create the forest hyperedges.
  for (std::vector<InputTree::Node>::const_iterator p = tree.nodes.begin();
       p != tree.nodes.end(); ++p) {
    const InputTree::Node &treeVertex = *p;
    const std::vector<InputTree::Node*> &treeChildren = treeVertex.children;
    if (treeChildren.empty()) {
      continue;
    }
    F2S::Forest::Hyperedge *e = new F2S::Forest::Hyperedge();
    e->head = vertexMap[&treeVertex];
    e->tail.reserve(treeChildren.size());
    for (std::vector<InputTree::Node*>::const_iterator q = treeChildren.begin();
         q != treeChildren.end(); ++q) {
      e->tail.push_back(vertexMap[*q]);
    }
    e->head->incoming.push_back(e);
  }

  // Return a pointer to the forest's root vertex.
  return forest.vertices.back();
}

}  // T2S
}  // Syntax
}  // Moses