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
|
/***********************************************************************
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
#include <set>
#include <vector>
#include "Node.h"
namespace MosesTraining
{
namespace GHKM
{
class Node;
class Subgraph
{
public:
Subgraph(const Node *root)
: m_root(root)
, m_depth(0)
, m_size(root->GetType() == TREE ? 1 : 0)
, m_nodeCount(1)
, m_pcfgScore(0.0f) {}
Subgraph(const Node *root, const std::set<const Node *> &leaves)
: m_root(root)
, m_leaves(leaves)
, m_depth(-1)
, m_size(-1)
, m_nodeCount(-1)
, m_pcfgScore(0.0f) {
m_depth = CalcDepth(m_root);
m_size = CalcSize(m_root);
m_nodeCount = CountNodes(m_root);
m_pcfgScore = CalcPcfgScore();
}
Subgraph(const Subgraph &other, bool targetOnly=false)
: m_root(other.m_root)
, m_leaves(other.m_leaves)
, m_depth(other.m_depth)
, m_size(other.m_size)
, m_nodeCount(other.m_nodeCount)
, m_pcfgScore(other.m_pcfgScore) {
if (targetOnly && m_root->GetType() != SOURCE) {
// Replace any source-word sink nodes with their parents (except for
// the special case where the parent is a non-word tree node -- see
// below).
std::set<const Node *> targetLeaves;
for (std::set<const Node *>::const_iterator p = m_leaves.begin();
p != m_leaves.end(); ++p) {
const Node *leaf = *p;
if (leaf->GetType() != SOURCE) {
targetLeaves.insert(leaf);
} else {
const std::vector<Node*> &parents = leaf->GetParents();
for (std::vector<Node*>::const_iterator q = parents.begin();
q != parents.end(); ++q) {
const Node *parent = *q;
// Only add parents that are words, not tree nodes since those
// are never sink nodes. (A source word can have a tree node as
// its parent due to the heuristic for handling unaligned source
// words).
if (parent->GetType() == TARGET) {
targetLeaves.insert(*q);
}
}
}
}
m_leaves.swap(targetLeaves);
}
}
const Node *GetRoot() const {
return m_root;
}
const std::set<const Node *> &GetLeaves() const {
return m_leaves;
}
int GetDepth() const {
return m_depth;
}
int GetSize() const {
return m_size;
}
int GetNodeCount() const {
return m_nodeCount;
}
float GetPcfgScore() const {
return m_pcfgScore;
}
bool IsTrivial() const {
return m_leaves.empty();
}
void GetTargetLeaves(std::vector<const Node *> &) const;
void PrintTree(std::ostream &out) const;
void PrintPartsOfSpeech(std::ostream &out) const;
void GetPartsOfSpeech(std::vector<std::string> &out) const;
private:
void GetTargetLeaves(const Node *, std::vector<const Node *> &) const;
int CalcDepth(const Node *) const;
int CalcSize(const Node *) const;
float CalcPcfgScore() const;
int CountNodes(const Node *) const;
void RecursivelyPrintTree(const Node *n, std::ostream &out) const;
void RecursivelyPrintPartsOfSpeech(const Node *n, std::ostream &out) const;
void RecursivelyGetPartsOfSpeech(const Node *n, std::vector<std::string> &out) const;
const Node *m_root;
std::set<const Node *> m_leaves;
int m_depth;
int m_size;
int m_nodeCount;
float m_pcfgScore;
};
} // namespace GHKM
} // namespace MosesTraining
|