blob: e6fc5214cbf826b4874b3f56b57b80f898ba910d (
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
|
#include "RuleTrie.h"
#include <map>
#include <vector>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <boost/version.hpp>
#include "moses/NonTerminal.h"
#include "moses/TargetPhrase.h"
#include "moses/TargetPhraseCollection.h"
#include "moses/Util.h"
#include "moses/Word.h"
namespace Moses
{
namespace Syntax
{
namespace T2S
{
void RuleTrie::Node::Prune(std::size_t tableLimit)
{
// Recusively prune child nodes.
for (SymbolMap::iterator p = m_sourceTermMap.begin();
p != m_sourceTermMap.end(); ++p) {
p->second.Prune(tableLimit);
}
for (SymbolMap::iterator p = m_nonTermMap.begin();
p != m_nonTermMap.end(); ++p) {
p->second.Prune(tableLimit);
}
// Prune TargetPhraseCollections at this node.
for (TPCMap::iterator p = m_targetPhraseCollections.begin();
p != m_targetPhraseCollections.end(); ++p) {
p->second->Prune(true, tableLimit);
}
}
void RuleTrie::Node::Sort(std::size_t tableLimit)
{
// Recusively sort child nodes.
for (SymbolMap::iterator p = m_sourceTermMap.begin();
p != m_sourceTermMap.end(); ++p) {
p->second.Sort(tableLimit);
}
for (SymbolMap::iterator p = m_nonTermMap.begin();
p != m_nonTermMap.end(); ++p) {
p->second.Sort(tableLimit);
}
// Sort TargetPhraseCollections at this node.
for (TPCMap::iterator p = m_targetPhraseCollections.begin();
p != m_targetPhraseCollections.end(); ++p) {
p->second->Sort(true, tableLimit);
}
}
RuleTrie::Node*
RuleTrie::Node::
GetOrCreateChild(const Word &sourceTerm)
{
return &m_sourceTermMap[sourceTerm];
}
RuleTrie::Node *
RuleTrie::
Node::
GetOrCreateNonTerminalChild(const Word &targetNonTerm)
{
UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(),
"Not a non-terminal: " << targetNonTerm);
return &m_nonTermMap[targetNonTerm];
}
TargetPhraseCollection::shared_ptr
RuleTrie::
Node::
GetOrCreateTargetPhraseCollection(const Word &sourceLHS)
{
UTIL_THROW_IF2(!sourceLHS.IsNonTerminal(),
"Not a non-terminal: " << sourceLHS);
TargetPhraseCollection::shared_ptr& foo
= m_targetPhraseCollections[sourceLHS];
if (!foo) foo.reset(new TargetPhraseCollection);
return foo;
}
RuleTrie::Node const*
RuleTrie::
Node::
GetChild(const Word &sourceTerm) const
{
UTIL_THROW_IF2(sourceTerm.IsNonTerminal(), "Not a terminal: " << sourceTerm);
SymbolMap::const_iterator p = m_sourceTermMap.find(sourceTerm);
return (p == m_sourceTermMap.end()) ? NULL : &p->second;
}
RuleTrie::Node const*
RuleTrie::
Node::
GetNonTerminalChild(const Word &targetNonTerm) const
{
UTIL_THROW_IF2(!targetNonTerm.IsNonTerminal(),
"Not a non-terminal: " << targetNonTerm);
SymbolMap::const_iterator p = m_nonTermMap.find(targetNonTerm);
return (p == m_nonTermMap.end()) ? NULL : &p->second;
}
TargetPhraseCollection::shared_ptr
RuleTrie::
GetOrCreateTargetPhraseCollection
( const Word &sourceLHS, const Phrase &sourceRHS )
{
Node &currNode = GetOrCreateNode(sourceRHS);
return currNode.GetOrCreateTargetPhraseCollection(sourceLHS);
}
RuleTrie::Node &
RuleTrie::
GetOrCreateNode(const Phrase &sourceRHS)
{
const std::size_t size = sourceRHS.GetSize();
Node *currNode = &m_root;
for (std::size_t pos = 0 ; pos < size ; ++pos) {
const Word& word = sourceRHS.GetWord(pos);
if (word.IsNonTerminal()) {
currNode = currNode->GetOrCreateNonTerminalChild(word);
} else {
currNode = currNode->GetOrCreateChild(word);
}
UTIL_THROW_IF2(currNode == NULL, "Node not found at position " << pos);
}
return *currNode;
}
void RuleTrie::SortAndPrune(std::size_t tableLimit)
{
if (tableLimit) {
m_root.Sort(tableLimit);
}
}
} // namespace T2S
} // namespace Syntax
} // namespace Moses
|