determinise.C
8.59 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*******************************************************************/
/* */
/* FILE determinise.C */
/* MODULE determinise */
/* PROGRAM SFST */
/* AUTHOR Helmut Schmid, IMS, University of Stuttgart */
/* */
/*******************************************************************/
#include "fst.h"
using std::vector;
using std::pair;
using std::set;
/***************** class NodeSet *********************************/
class NodeSet {
// This class is used to store a set of nodes.
// Whenever a new node is added, all nodes accessible
// through epsilon transitions are added as well.
private:
set<Node*> ht;
public:
typedef set<Node*>::iterator iterator;
NodeSet() {};
void add( Node* );
bool insert(Node *node) {
pair<iterator, bool> result = ht.insert(node);
return result.second;
};
iterator begin() const { return ht.begin(); }
iterator end() const { return ht.end(); }
size_t size() const { return ht.size(); }
void clear() { ht.clear(); }
};
/***************** class NodeArray *******************************/
class NodeArray {
private:
size_t sizev;
bool final;
Node **node;
public:
NodeArray( NodeSet& );
~NodeArray() { delete[] node; };
size_t size() const { return sizev; }
bool is_final() const { return final; };
Node* &operator[]( size_t i ) const { return node[i]; }
};
/***************** class Transition ******************************/
class Transition {
public:
Label label;
NodeArray *nodes;
Transition(Label l, NodeArray *na) { label = l; nodes = na; };
};
/***************** class NodeMapping ****************************/
class NodeMapping {
// This class is used to map a node set from one transducer
// to a single node in another transducer
private:
struct hashf {
size_t operator()(const NodeArray *na) const {
size_t key=na->size() ^ na->is_final();
for( size_t i=0; i<na->size(); i++)
key = (key<<1) ^ (size_t)(*na)[i];
return key;
}
};
struct equalf {
int operator()(const NodeArray *na1, const NodeArray *na2) const {
if (na1->size() != na2->size() || na1->is_final() != na2->is_final())
return 0;
for( size_t i=0; i<na1->size(); i++)
if ((*na1)[i] != (*na2)[i])
return 0;
return 1;
}
};
typedef hash_map<NodeArray*, Node*, hashf, equalf> NodeMap;
NodeMap hm;
public:
typedef NodeMap::iterator iterator;
~NodeMapping();
iterator begin() { return hm.begin(); };
iterator end() { return hm.end(); };
iterator find( NodeArray *na) { return hm.find( na ); };
Node* &operator[]( NodeArray *na ) { return hm.operator[](na); };
};
/***************** class LabelMapping ****************************/
class LabelMapping {
// This class is used to map a label to a node set
private:
struct hashf {
size_t operator()(const Label l) const {
return l.lower_char() | (l.upper_char() << 16);
}
};
struct equalf {
int operator()(const Label l1, const Label l2) const {
return l1==l2;
}
};
typedef hash_map<const Label, NodeSet, hashf, equalf> LabelMap;
LabelMap lm;
public:
LabelMapping(): lm(8) {};
typedef LabelMap::iterator iterator;
iterator begin() { return lm.begin(); };
iterator end() { return lm.end(); };
size_t size() { return lm.size(); };
iterator find( Label l) { return lm.find( l ); };
NodeSet &operator[]( const Label l ) { return lm.operator[]( l ); };
};
static void determinise_node( NodeArray&, Node*, Transducer*, NodeMapping&, long );
/*******************************************************************/
/* */
/* NodeSet::add */
/* */
/*******************************************************************/
void NodeSet::add( Node *node )
{
pair<iterator, bool> result = ht.insert(node);
if (result.second) {
// new node, add nodes reachable with epsilon transitions
for( ArcsIter p(node->arcs(),ArcsIter::eps); p; p++ ) {
Arc *arc=p;
if (!arc->label().is_epsilon())
break;
add(arc->target_node());
}
}
}
/*******************************************************************/
/* */
/* NodeArray::NodeArray */
/* */
/*******************************************************************/
NodeArray::NodeArray( NodeSet &ns )
{
sizev = 0;
NodeSet::iterator it;
final = false;
node = new Node*[ns.size()];
for( it=ns.begin(); it!=ns.end(); it++ ) {
Node *nn = *it;
if (nn->arcs()->non_epsilon_transition_exists())
node[sizev++] = nn;
final |= nn->is_final();
}
std::sort(node, node+sizev);
}
/*******************************************************************/
/* */
/* NodeMapping::~NodeMapping */
/* */
/*******************************************************************/
NodeMapping::~NodeMapping()
{
// if we delete NodeArrays without removing them from NodeMapping,
// the system will crash when NodeMapping is deleted.
for( iterator it=hm.begin(); it!=hm.end(); ) {
NodeArray *na=it->first;
iterator old = it++;
hm.erase(old);
delete na;
}
}
/*******************************************************************/
/* */
/* compute_transitions */
/* */
/*******************************************************************/
static void compute_transitions( NodeArray &na, vector<Transition> &t )
{
LabelMapping lmap;
// for all nodes in the current set
for( size_t i=0; i<na.size(); i++) {
Node *n = na[i]; // old node
// For each non-epsilon transition, add the target node
// to the respective node set.
for( ArcsIter p(n->arcs(),ArcsIter::non_eps); p; p++ ) {
Arc *arc=p;
lmap[arc->label()].add(arc->target_node());
}
}
t.reserve(lmap.size());
for( LabelMapping::iterator it=lmap.begin(); it!=lmap.end(); it++ )
t.push_back(Transition(it->first, new NodeArray( it->second )));
}
/*******************************************************************/
/* */
/* determinise_node */
/* */
/*******************************************************************/
static void determinise_node( NodeArray &na, Node *node, Transducer *a,
NodeMapping &map, long depth )
{
if (depth > 10000)
fprintf(stderr,"\r%ld",depth);
node->set_final(na.is_final());
vector<Transition> t;
compute_transitions( na, t );
for( size_t i=0; i<t.size(); i++ ) {
NodeMapping::iterator it=map.find(t[i].nodes);
if (it == map.end()) {
// new node set
Node *target_node = a->new_node();
map[t[i].nodes] = target_node;
node->add_arc( t[i].label, target_node, a );
determinise_node( *t[i].nodes, target_node, a, map, depth+1 );
}
else {
delete t[i].nodes;
node->add_arc( t[i].label, it->second, a );
}
}
}
/*******************************************************************/
/* */
/* Transducer::determinise */
/* */
/*******************************************************************/
Transducer &Transducer::determinise()
{
// initialisations
NodeMapping map;
Transducer *a = new Transducer();
a->alphabet.copy(alphabet);
// creation of the initial node set consisting of all nodes
// reachable from the start node via epsilon transitions.
NodeArray *na;
{
NodeSet ns;
ns.add(root_node());
na = new NodeArray(ns);
}
// map the node set to the new root node
map[na] = a->root_node();
// determinise the transducer recursively
determinise_node( *na, a->root_node(), a, map, 0);
a->deterministic = 1;
return *a;
}