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

Node.h « extract-ghkm « phrase-extract « training « scripts - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 228fdc812588a296c3c471979ae353b81c091478 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/***********************************************************************
 Moses - statistical machine translation system
 Copyright (C) 2006-2011 University of Edinburgh
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#pragma once
#ifndef EXTRACT_GHKM_NODE_H_
#define EXTRACT_GHKM_NODE_H_

#include "Span.h"

#include <cassert>
#include <iterator>
#include <string>
#include <vector>

namespace Moses {
namespace GHKM {

class Subgraph;

enum NodeType { SOURCE, TARGET, TREE };

class Node
{
 public:
  Node(const std::string &label, NodeType type)
      : m_label(label)
      , m_type(type)
      , m_children()
      , m_parents() {}

  ~Node();

  const std::string &GetLabel() const { return m_label; }
  NodeType GetType() const { return m_type; }
  const std::vector<Node*> &GetChildren() const { return m_children; }
  const std::vector<Node*> &GetParents() const { return m_parents; }
  const Span &GetSpan() const { return m_span; }
  const Span &GetComplementSpan() const { return m_complementSpan; }
  const std::vector<const Subgraph*> &GetRules() const { return m_rules; }

  void SetChildren(const std::vector<Node*> &c) { m_children = c; }
  void SetParents(const std::vector<Node*> &p) { m_parents = p; }
  void SetSpan(const Span &s) { m_span = s; }
  void SetComplementSpan(const Span &cs) { m_complementSpan = cs; }

  void AddChild(Node *c) { m_children.push_back(c); }
  void AddParent(Node *p) { m_parents.push_back(p); }
  void AddRule(const Subgraph *s) { m_rules.push_back(s); }

  bool IsSink() const { return m_children.empty(); }
  bool IsPreterminal() const;

  void PropagateIndex(int);

  std::vector<std::string> GetTargetWords() const;

  // Gets the path from this node's parent to the root.  This node is
  // required to be part of the original parse tree (i.e. not a source word,
  // which can have multiple parents).
  template<typename OutputIterator>
  void GetTreeAncestors(OutputIterator result, bool includeSelf=false);

  // Returns the lowest common ancestor given a sequence of nodes belonging to
  // the target tree.
  template<typename InputIterator>
  static Node *LowestCommonAncestor(InputIterator first, InputIterator last);

 private:
  // Disallow copying
  Node(const Node &);
  Node &operator=(const Node &);

  void GetTargetWords(std::vector<std::string> &) const;

  std::string m_label;
  NodeType m_type;
  std::vector<Node*> m_children;
  std::vector<Node*> m_parents;
  Span m_span;
  Span m_complementSpan;
  std::vector<const Subgraph*> m_rules;
};

template<typename OutputIterator>
void Node::GetTreeAncestors(OutputIterator result, bool includeSelf)
{
  // This function assumes the node is part of the parse tree.
  assert(m_type == TARGET || m_type == TREE);

  if (includeSelf) {
    *result++ = this;
  }

  Node *ancestor = !(m_parents.empty()) ? m_parents[0] : 0;
  while (ancestor != 0) {
    *result++ = ancestor;
    ancestor = !(ancestor->m_parents.empty()) ? ancestor->m_parents[0] : 0;
  }
}

template<typename InputIterator>
Node *Node::LowestCommonAncestor(InputIterator first, InputIterator last)
{
  // Check for an empty sequence.
  if (first == last) {
    return 0;
  }

  // Check for the case that the sequence contains only one distinct node.
  // Also check that every node belongs to the target tree.
  InputIterator p = first;
  Node *lca = *p++;
  for (; p != last; ++p) {
    Node *node = *p;
    assert(node->m_type != SOURCE);
    if (node != lca) {
      lca = 0;
    }
  }
  if (lca) {
    return lca;
  }

  // Now construct an ancestor path for each node, from itself to the root.
  size_t minPathLength = 0;
  std::vector<std::vector<Node *> > paths;
  for (p = first; p != last; ++p) {
    paths.resize(paths.size()+1);
    (*p)->GetTreeAncestors(std::back_inserter(paths.back()), true);
    size_t pathLength = paths.back().size();
    assert(pathLength > 0);
    if (paths.size() == 1 || pathLength < minPathLength) {
      minPathLength = pathLength;
    }
  }

  // Search for the start of the longest common suffix by working forward from
  // the the earliest possible starting point to the root.
  for (size_t i = 0; i < minPathLength; ++i) {
    bool match = true;
    for (size_t j = 0; j < paths.size(); ++j) {
      size_t index = paths[j].size() - minPathLength + i;
      assert(index >= 0);
      assert(index < paths[j].size());
      if (j == 0) {
        lca = paths[j][index];
        assert(lca);
      } else if (lca != paths[j][index]) {
        match = false;
        break;
      }
    }
    if (match) {
      return lca;
    }
  }

  // A lowest common ancestor should have been found.
  assert(false);
  return 0;
}

}  // namespace GHKM
}  // namespace Moses

#endif