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
|
/***********************************************************************
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
***********************************************************************/
#include "ScfgRule.h"
#include "Node.h"
#include "Subgraph.h"
#include <algorithm>
namespace Moses {
namespace GHKM {
ScfgRule::ScfgRule(const Subgraph &fragment)
: m_sourceLHS("X", NonTerminal)
, m_targetLHS(fragment.GetRoot()->GetLabel(), NonTerminal)
, m_pcfgScore(fragment.GetPcfgScore())
{
// Source RHS
const std::set<const Node *> &leaves = fragment.GetLeaves();
std::vector<const Node *> sourceRHSNodes;
sourceRHSNodes.reserve(leaves.size());
for (std::set<const Node *>::const_iterator p(leaves.begin());
p != leaves.end(); ++p) {
const Node &leaf = **p;
if (!leaf.GetSpan().empty()) {
sourceRHSNodes.push_back(&leaf);
}
}
std::sort(sourceRHSNodes.begin(), sourceRHSNodes.end(), PartitionOrderComp);
// Build a mapping from target nodes to source-order indices, so that we
// can construct the Alignment object later.
std::map<const Node *, std::vector<int> > sourceOrder;
m_sourceRHS.reserve(sourceRHSNodes.size());
int srcIndex = 0;
for (std::vector<const Node *>::const_iterator p(sourceRHSNodes.begin());
p != sourceRHSNodes.end(); ++p, ++srcIndex) {
const Node &sinkNode = **p;
if (sinkNode.GetType() == TREE) {
m_sourceRHS.push_back(Symbol("X", NonTerminal));
sourceOrder[&sinkNode].push_back(srcIndex);
} else {
assert(sinkNode.GetType() == SOURCE);
m_sourceRHS.push_back(Symbol(sinkNode.GetLabel(), Terminal));
// Add all aligned target words to the sourceOrder map
const std::vector<Node *> &parents(sinkNode.GetParents());
for (std::vector<Node *>::const_iterator q(parents.begin());
q != parents.end(); ++q) {
if ((*q)->GetType() == TARGET) {
sourceOrder[*q].push_back(srcIndex);
}
}
}
}
// Target RHS + alignment
std::vector<const Node *> targetLeaves;
fragment.GetTargetLeaves(targetLeaves);
m_alignment.reserve(targetLeaves.size()); // might be too much but that's OK
m_targetRHS.reserve(targetLeaves.size());
for (std::vector<const Node *>::const_iterator p(targetLeaves.begin());
p != targetLeaves.end(); ++p) {
const Node &leaf = **p;
if (leaf.GetSpan().empty()) {
// The node doesn't cover any source words, so we can only add
// terminals to the target RHS (not a non-terminal).
std::vector<std::string> targetWords(leaf.GetTargetWords());
for (std::vector<std::string>::const_iterator q(targetWords.begin());
q != targetWords.end(); ++q) {
m_targetRHS.push_back(Symbol(*q, Terminal));
}
} else if (leaf.GetType() == SOURCE) {
// Do nothing
} else {
SymbolType type = (leaf.GetType() == TREE) ? NonTerminal : Terminal;
m_targetRHS.push_back(Symbol(leaf.GetLabel(), type));
int tgtIndex = m_targetRHS.size()-1;
std::map<const Node *, std::vector<int> >::iterator q(sourceOrder.find(&leaf));
assert(q != sourceOrder.end());
std::vector<int> &sourceNodes = q->second;
for (std::vector<int>::iterator r(sourceNodes.begin());
r != sourceNodes.end(); ++r) {
int srcIndex = *r;
m_alignment.push_back(std::make_pair(srcIndex, tgtIndex));
}
}
}
}
int ScfgRule::Scope() const
{
int scope = 0;
bool predIsNonTerm = false;
if (m_sourceRHS[0].GetType() == NonTerminal) {
++scope;
predIsNonTerm = true;
}
for (size_t i = 1; i < m_sourceRHS.size(); ++i) {
bool isNonTerm = m_sourceRHS[i].GetType() == NonTerminal;
if (isNonTerm && predIsNonTerm) {
++scope;
}
predIsNonTerm = isNonTerm;
}
if (predIsNonTerm) {
++scope;
}
return scope;
}
bool ScfgRule::PartitionOrderComp(const Node *a, const Node *b)
{
const Span &aSpan = a->GetSpan();
const Span &bSpan = b->GetSpan();
assert(!aSpan.empty() && !bSpan.empty());
return *(aSpan.begin()) < *(bSpan.begin());
}
} // namespace GHKM
} // namespace Moses
|