-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslave.cpp
342 lines (322 loc) · 7.86 KB
/
slave.cpp
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <pvm3.h>
#include <string.h>
#include "consts.h"
using namespace std;
template<class T>
class Node {
public:
Node();
~Node();
vector<Node<T> *> children;
T value;
};
template<class T>
class Tree {
public:
Tree();
~Tree();
Node<T> *root;
void print();
void print(string pre, string post, string npre, string npost, Node<T> *node);
string serialize();
string serialize(Node<T> *node);
private:
// void clean_up(Node<T> *n);
};
class Matcher {
public:
Matcher();
~Matcher();
void read_syllables(string filename);
string match(string word);
private:
Tree<string> syllable_tree;
void get_prefixes(string word, vector<string> *matches, Node<string> *current = NULL);
Tree<string> *build_part_tree(string word);
void prune(Node<string> *n);
};
template<class T>
Node<T>::Node()
{
// children = new vector<Node<T> *>();
}
template<class T>
Node<T>::~Node()
{
// delete children;
}
template<class T>
Tree<T>::Tree()
{
root = NULL;
}
template<class T>
Tree<T>::~Tree()
{
delete(root);
}
template<class T>
void Tree<T>::print()
{
if (root != NULL)
{
print("", "", " ", "", root);
}
}
template<class T>
void Tree<T>::print(string pre, string post, string npre, string npost, Node<T> *node)
{
cout << pre << node->value << post << endl;
for (typename vector<Node<T> *>::iterator it = node->children.begin() ; it != node->children.end(); ++it)
{
print(pre + npre, post + npost, npre, npost, *it);
}
}
template<class T>
string Tree<T>::serialize()
{
if (root != NULL)
{
return serialize(root);
}
}
template<class T>
string Tree<T>::serialize(Node<T> *node)
{
string stree;
stree = node->value;
if (node->children.size() > 0)
{
for (typename vector<Node<T> *>::iterator it = node->children.begin() ; it < node->children.end(); ++it)
{
stree += " " + serialize(*it);
}
stree = "(" + stree + ")";
}
return stree;
}
Matcher::Matcher()
{
}
Matcher::~Matcher()
{
}
void Matcher::read_syllables(string filename)
{
ifstream in;
const char *fn = filename.c_str();
in.open(fn);
string syllable;
syllable_tree.root = new Node<string>();
syllable_tree.root->value = "";
Node<string> *current;
while(in >> syllable)
{
current = syllable_tree.root;
bool found = false;
for (int sylpos = 0; sylpos < syllable.length(); sylpos++)
{
found = false;
for (typename vector<Node<string> *>::iterator it = current->children.begin() ; it != current->children.end(); ++it)
{
if ((*it)->value == syllable.substr(sylpos,1))
{
found = true;
current = *it;
break;
}
}
if (!found)
{
Node<string> *n = new Node<string>();
n->value = syllable.substr(sylpos,1);
if (n != NULL)
current->children.push_back(n);
else
cerr << "FeuerFeuerFeuer 175" << endl;
current = n;
if (sylpos == syllable.length()-1)
{
// Add terminator
n = new Node<string>();
n->value = "$";
if (n != NULL)
current->children.push_back(n);
else
cerr << "FeuerFeuerFeuer 185" << endl;
current = n;
// Add whole syllable
n = new Node<string>();
n->value = syllable;
if (n != NULL)
current->children.push_back(n);
else
cerr << "FeuerFeuerFeuer 193" << endl;
current = n;
}
}
}
}
in.close();
// syllable_tree.print();
// cout << syllable_tree.serialize() << endl;
}
string Matcher::match(string word)
{
Tree<string> *part = build_part_tree(word);
// part->print();
prune(part->root);
// part->print();
return part->serialize();
}
void Matcher::get_prefixes(string word, vector<string> *matches, Node<string> *current)
{
if (current == NULL)
current = syllable_tree.root;
for (vector<Node<string> *>::iterator it = current->children.begin(); it != current->children.end(); it++)
{
if ((*it)->value == word.substr(0,1))
{
get_prefixes(word.substr(1),matches,*it);
}
else if ((*it)->value == "$")
{
matches->push_back((*it)->children.at(0)->value);
// cout << "Prefix: " << (*it)->children->at(0)->value << endl;
}
}
}
Tree<string> * Matcher::build_part_tree(string word)
{
// cout << "call " << word << endl;
Tree<string> *parts = new Tree<string>();
parts->root=new Node<string>();
parts->root->value="";
if (word == "")
{
// cout << "if" << endl;
Node<string> *n = new Node<string>();
n->value = "$";
if (n != NULL)
parts->root->children.push_back(n);
else
cerr << "FeuerFeuerFeuer 242" << endl;
// cout << parts->serialize() << endl;
}
else
{
// cout << "else" << endl;
vector<string> *prefixes = new vector<string>();
// cout << "Prefixes of " << word << endl;
get_prefixes(word,prefixes);
// cout << "size " <<prefixes->size() << endl;
for (vector<string>::iterator it = prefixes->begin(); it != prefixes->end() ; it++)
{
// cout << "for " << endl;
// cout << " " << *it << endl;
Node<string> *n = new Node<string>();
n->value = *it;
if (n != NULL)
parts->root->children.push_back(n);
else
cerr << "FeuerFeuerFeuer 261" << endl;
Tree<string> *sub = build_part_tree(word.substr(it->length()));
// delete(sub);
// n->children = new vector<Node<string> *>(*sub->root->children);
n->children = sub->root->children;
delete sub;
}
delete prefixes;
}
return parts;
}
void Matcher::prune(Node<string> *n)
{
for (typename vector<Node<string> *>::iterator it = n->children.begin(); it != n->children.end(); it++)
{
prune(*it);
}
bool erased = true;
while(erased)
{
erased = false;
for (typename vector<Node<string> *>::iterator it = n->children.begin(); it != n->children.end(); it++)
{
if ((*it)->children.size()==0 && (*it)->value != "$"){
// cerr << "Erase " << (*it)->value << endl;
n->children.erase(it);
erased = true;
break;
}
}
}
}
int main(int argc, char ** argv)
{
int master=pvm_parent();
int tid=pvm_mytid();
ofstream log;
const char *logname=string("/tmp/silben-slave" + std::to_string(tid) + ".log").c_str();
// const char *logname="/tmp/silben-slave.log";
log.open(logname);
Matcher m;
char *word = (char*) malloc(48);
string tree;
log << "reading syllable list" << endl;
m.read_syllables(SYLLABLE_LIST);
log << "finished reading" << endl;
while(!pvm_probe(master,DIE))
{
pvm_initsend(PvmDataDefault);
int sbuf = pvm_send(master, NEXT);
log << "send NEXT with " << sbuf << endl;
// Get words
int recbuf=pvm_recv(-1, DATA);
// Stop on die-message
if (pvm_probe(master,DIE))
break;
log << "received DATA with " << recbuf << endl;
pvm_initsend(PvmDataDefault);
int block_size;
pvm_upkint(&block_size,1,1);
log << "Recieving data block of size " << block_size << endl;
pvm_pkint(&block_size,1,1);
for (int ct=0; ct < block_size; ct++)
{
// log << "Trying to unpack" << endl;
// Unpack word
pvm_upkstr(word);
// log << "Unpacked " << word << endl;
// Split word
// log << "Trying to match" << endl;
tree = m.match(string(word));
// log << "Matched " << word << " to " << tree << endl;
// log << word << "\t" << tree << endl;
// Pack tree
pvm_pkstr(word);
pvm_pkstr((char *)tree.c_str());
}
// Send tree
if (block_size < BLOCK_SIZE)
{
sbuf=pvm_send(master,FINAL);
break;
}
else
sbuf=pvm_send(master,DATA);
log << "send DATA with " << sbuf << endl;
}
pvm_recv(master, DIE);
log << "received DIE" << endl;
pvm_exit();
// word = "studentenkorporation";
// tree = m.match(string(word));
// cout << "Matched " << word << " to " << tree << endl;
log.close();
// free(word);
return 0;
}